widgets/coloredit.cpp

Sun, 09 Apr 2023 17:05:40 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 09 Apr 2023 17:05:40 +0300
changeset 368
9444de9762c3
parent 348
98776f54a8d0
permissions
-rw-r--r--

Simplify signature of openModelFromPath

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>
348
98776f54a8d0 Rename colorbutton.cpp -> coloredit.cpp
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 347
diff changeset
3 #include "widgets/coloredit.h"
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
347
5c655cc006de Rename ColorButton -> ColorEdit
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 346
diff changeset
5 ColorEdit::ColorEdit(const QColor& color, QWidget* parent) :
5c655cc006de Rename ColorButton -> ColorEdit
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 346
diff changeset
6 ColorEdit{parent}
39
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
347
5c655cc006de Rename ColorButton -> ColorEdit
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 346
diff changeset
11 ColorEdit::ColorEdit(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
347
5c655cc006de Rename ColorButton -> ColorEdit
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 346
diff changeset
41 QColor ColorEdit::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
347
5c655cc006de Rename ColorButton -> ColorEdit
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 346
diff changeset
46 void ColorEdit::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