widgets/colorbutton.cpp

Sat, 08 Apr 2023 15:08:19 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 15:08:19 +0300
changeset 346
3e3784c4cd3e
parent 345
81cd5dee6d77
child 347
5c655cc006de
permissions
-rw-r--r--

Change color edit coloring to use only stylesheets to colorise the line edit only

39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QColorDialog>
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
2 #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
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) :
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
12 QWidget{parent},
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
13 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
14 button{new QPushButton{"…", this}}
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 {
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
16 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
17 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
18 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
19 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
20 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
21 this->setLayout(layout);
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
22 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
23 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
24 if (color.isValid()) {
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
25 this->setColor(color);
39
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 });
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
28 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
29 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
30 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
31 const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black;
346
3e3784c4cd3e Change color edit coloring to use only stylesheets to colorise the line edit only
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 345
diff changeset
32 this->setStyleSheet(
3e3784c4cd3e Change color edit coloring to use only stylesheets to colorise the line edit only
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 345
diff changeset
33 QStringLiteral("QLineEdit{background-color: %1; color: %2;}")
3e3784c4cd3e Change color edit coloring to use only stylesheets to colorise the line edit only
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 345
diff changeset
34 .arg(color.name())
3e3784c4cd3e Change color edit coloring to use only stylesheets to colorise the line edit only
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 345
diff changeset
35 .arg(foreground.name()));
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
36 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
37 });
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
38 this->setColor(Qt::black);
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 }
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
41 QColor ColorButton::color() const
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
43 return QColor{this->lineEdit->text()};
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
46 void ColorButton::setColor(const QColor& color)
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 {
344
a24da8de2a3b Changed color button to a line edit+button combination
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
48 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
49 this->lineEdit->setText(color.name());
256
c6f5de03dc0f Move color button into widgets lib
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 39
diff changeset
50 }
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 }

mercurial