widgets/colorbutton.cpp

changeset 344
a24da8de2a3b
parent 264
76a025db4948
child 345
81cd5dee6d77
equal deleted inserted replaced
343:4a82990affd5 344:a24da8de2a3b
1 #include <QColorDialog> 1 #include <QColorDialog>
2 #include <QStyleFactory> 2 #include <QStyleFactory>
3 #include <QHBoxLayout>
3 #include "widgets/colorbutton.h" 4 #include "widgets/colorbutton.h"
4 5
5 ColorButton::ColorButton(const QColor& color, QWidget* parent) : 6 ColorButton::ColorButton(const QColor& color, QWidget* parent) :
6 ColorButton{parent} 7 ColorButton{parent}
7 { 8 {
8 this->setColor(color); 9 this->setColor(color);
9 } 10 }
10 11
11 ColorButton::ColorButton(QWidget* parent) : 12 ColorButton::ColorButton(QWidget* parent) :
12 QPushButton{parent} 13 QWidget{parent},
14 lineEdit{new QLineEdit{this}},
15 button{new QPushButton{"…", this}}
13 { 16 {
14 connect(this, &QPushButton::clicked, [&](){ 17 auto*const layout = new QHBoxLayout{};
15 const QColor color = QColorDialog::getColor(this->storedSelectedColor, this->parentWidget()); 18 layout->addWidget(this->lineEdit);
19 layout->addWidget(this->button);
20 layout->setStretch(0, 1);
21 layout->setStretch(1, 0);
22 this->setLayout(layout);
23 connect(this->button, &QPushButton::clicked, [&](){
24 const QColor color = QColorDialog::getColor(this->color(), this->parentWidget());
16 if (color.isValid()) { 25 if (color.isValid()) {
17 this->setColor(color); 26 this->setColor(color);
18 } 27 }
19 }); 28 });
29 connect(this->lineEdit, &QLineEdit::textChanged, [&]{
30 const QColor color = this->color();
31 QPalette palette{color};
32 const qreal lightness = color.lightnessF();
33 const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black;
34 const auto style = QStyleFactory::create("Fusion");
35 this->setStyle(style);
36 this->setStyleSheet(QStringLiteral(
37 "*{background-color: %1; color: %2;}"
38 ).arg(color.name()).arg(foreground.name()));
39 /*
40 palette.setColor(QPalette::ButtonText, foreground);
41 for (QWidget* widget : {(QWidget*)this->lineEdit, (QWidget*)this->button}) {
42 widget->setPalette(palette);
43 widget->setStyle(style);
44 }
45 */
46 Q_EMIT this->colorChanged(this->color());
47 });
20 this->setColor(Qt::black); 48 this->setColor(Qt::black);
21 } 49 }
22 50
23 const QColor& ColorButton::color() const 51 QColor ColorButton::color() const
24 { 52 {
25 return this->storedSelectedColor; 53 return QColor{this->lineEdit->text()};
26 } 54 }
27 55
28 void ColorButton::setColor(const QColor& color) 56 void ColorButton::setColor(const QColor& color)
29 { 57 {
30 if (this->storedSelectedColor != color) { 58 if (color.isValid() and this->color() != color) {
31 this->storedSelectedColor = color; 59 this->lineEdit->setText(color.name());
32 this->setText(color.name());
33 QPalette palette{color};
34 const qreal lightness = color.lightnessF();
35 const QColor foreground = lightness < 0.4 ? Qt::white : Qt::black;
36 palette.setColor(QPalette::ButtonText, foreground);
37 this->setPalette(palette);
38 this->setStyle(QStyleFactory::create("Fusion"));
39 Q_EMIT this->colorChanged(this->storedSelectedColor);
40 } 60 }
41 } 61 }

mercurial