widgets/colorbutton.cpp

Fri, 01 Jul 2022 23:48:27 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 23:48:27 +0300
changeset 313
c24d87f64bed
parent 264
76a025db4948
child 344
a24da8de2a3b
permissions
-rw-r--r--

Improve click handling

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>
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
3 #include "widgets/colorbutton.h"
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 ColorButton::ColorButton(const QColor& color, QWidget* parent) :
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 ColorButton{parent}
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 {
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
8 this->setColor(color);
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 }
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 ColorButton::ColorButton(QWidget* parent) :
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 QPushButton{parent}
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 {
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
14 connect(this, &QPushButton::clicked, [&](){
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 const QColor color = QColorDialog::getColor(this->storedSelectedColor, this->parentWidget());
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
16 if (color.isValid()) {
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
17 this->setColor(color);
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 }
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 });
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
20 this->setColor(Qt::black);
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 }
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
23 const QColor& ColorButton::color() const
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 return this->storedSelectedColor;
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 }
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
28 void ColorButton::setColor(const QColor& color)
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
30 if (this->storedSelectedColor != color) {
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
31 this->storedSelectedColor = color;
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
32 this->setText(color.name());
257
afdab56e8210 Fix appearance of ColorButton
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 256
diff changeset
33 QPalette palette{color};
afdab56e8210 Fix appearance of ColorButton
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 256
diff changeset
34 const qreal lightness = color.lightnessF();
afdab56e8210 Fix appearance of ColorButton
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 256
diff changeset
35 const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black;
afdab56e8210 Fix appearance of ColorButton
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 256
diff changeset
36 palette.setColor(QPalette::ButtonText, foreground);
afdab56e8210 Fix appearance of ColorButton
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 256
diff changeset
37 this->setPalette(palette);
afdab56e8210 Fix appearance of ColorButton
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 256
diff changeset
38 this->setStyle(QStyleFactory::create("Fusion"));
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
39 Q_EMIT this->colorChanged(this->storedSelectedColor);
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
40 }
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 }

mercurial