Sat, 08 Apr 2023 15:05:57 +0300
delete commented out code
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | #include <QColorDialog> |
257
afdab56e8210
Fix appearance of ColorButton
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
256
diff
changeset
|
2 | #include <QStyleFactory> |
344
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
3 | #include <QHBoxLayout> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
257
diff
changeset
|
4 | #include "widgets/colorbutton.h" |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | ColorButton::ColorButton(const QColor& color, QWidget* parent) : |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | ColorButton{parent} |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
8 | { |
256
c6f5de03dc0f
Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
39
diff
changeset
|
9 | this->setColor(color); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
10 | } |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
11 | |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
12 | ColorButton::ColorButton(QWidget* parent) : |
344
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
13 | QWidget{parent}, |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
14 | lineEdit{new QLineEdit{this}}, |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
15 | button{new QPushButton{"…", this}} |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
16 | { |
344
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
17 | auto*const layout = new QHBoxLayout{}; |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
18 | layout->addWidget(this->lineEdit); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
19 | layout->addWidget(this->button); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
20 | layout->setStretch(0, 1); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
21 | layout->setStretch(1, 0); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
22 | this->setLayout(layout); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
23 | connect(this->button, &QPushButton::clicked, [&](){ |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
24 | const QColor color = QColorDialog::getColor(this->color(), this->parentWidget()); |
256
c6f5de03dc0f
Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
39
diff
changeset
|
25 | if (color.isValid()) { |
c6f5de03dc0f
Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
39
diff
changeset
|
26 | this->setColor(color); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | } |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | }); |
344
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
29 | connect(this->lineEdit, &QLineEdit::textChanged, [&]{ |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
30 | const QColor color = this->color(); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
31 | QPalette palette{color}; |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
32 | const qreal lightness = color.lightnessF(); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
33 | const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black; |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
34 | const auto style = QStyleFactory::create("Fusion"); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
35 | this->setStyle(style); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
36 | this->setStyleSheet(QStringLiteral( |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
37 | "*{background-color: %1; color: %2;}" |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
38 | ).arg(color.name()).arg(foreground.name())); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
39 | Q_EMIT this->colorChanged(this->color()); |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
40 | }); |
256
c6f5de03dc0f
Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
39
diff
changeset
|
41 | this->setColor(Qt::black); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | } |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | |
344
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
44 | QColor ColorButton::color() const |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | { |
344
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
46 | return QColor{this->lineEdit->text()}; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | } |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | |
256
c6f5de03dc0f
Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
39
diff
changeset
|
49 | void ColorButton::setColor(const QColor& color) |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | { |
344
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
51 | if (color.isValid() and this->color() != color) { |
a24da8de2a3b
Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
52 | this->lineEdit->setText(color.name()); |
256
c6f5de03dc0f
Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
39
diff
changeset
|
53 | } |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | } |