widgets/colorbutton.cpp

Sat, 08 Apr 2023 15:03:54 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 15:03:54 +0300
changeset 344
a24da8de2a3b
parent 264
76a025db4948
child 345
81cd5dee6d77
permissions
-rw-r--r--

Changed color button to a line edit+button combination

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 /*
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
40 palette.setColor(QPalette::ButtonText, foreground);
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
41 for (QWidget* widget : {(QWidget*)this->lineEdit, (QWidget*)this->button}) {
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
42 widget->setPalette(palette);
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
43 widget->setStyle(style);
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
44 }
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
45 */
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
46 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
47 });
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
48 this->setColor(Qt::black);
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 }
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 QColor ColorButton::color() const
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 {
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
53 return QColor{this->lineEdit->text()};
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 }
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
56 void ColorButton::setColor(const QColor& color)
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 {
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
58 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
59 this->lineEdit->setText(color.name());
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
60 }
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 }

mercurial