widgets/colorbutton.cpp

changeset 257
afdab56e8210
parent 256
c6f5de03dc0f
child 264
76a025db4948
equal deleted inserted replaced
256:c6f5de03dc0f 257:afdab56e8210
1 #include <QColorDialog> 1 #include <QColorDialog>
2 #include <QStyleFactory>
2 #include "colorbutton.h" 3 #include "colorbutton.h"
3 4
4 ColorButton::ColorButton(const QColor& color, QWidget* parent) : 5 ColorButton::ColorButton(const QColor& color, QWidget* parent) :
5 ColorButton{parent} 6 ColorButton{parent}
6 { 7 {
26 27
27 void ColorButton::setColor(const QColor& color) 28 void ColorButton::setColor(const QColor& color)
28 { 29 {
29 if (this->storedSelectedColor != color) { 30 if (this->storedSelectedColor != color) {
30 this->storedSelectedColor = color; 31 this->storedSelectedColor = color;
31 this->setStyleSheet(QString{"background-color: %1"}.arg(color.name()));
32 this->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"));
33 Q_EMIT this->colorChanged(this->storedSelectedColor); 39 Q_EMIT this->colorChanged(this->storedSelectedColor);
34 } 40 }
35 } 41 }
36

mercurial