-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorselector.cpp
43 lines (37 loc) · 1.13 KB
/
colorselector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "colorselector.h"
#include <QMouseEvent>
#include <QColorDialog>
ColorSelector::ColorSelector (QWidget *parent) :
QLabel(parent)
{
setAutoFillBackground(true);
setSelectedColor(QColor(255, 255, 255));
}
QColor ColorSelector::selectedColor () const {
return palette().color(backgroundRole());
}
void ColorSelector::setSelectedColor (QColor color) {
QPalette p = palette();
p.setColor(backgroundRole(), color);
setPalette(p);
if (usecss_)
setStyleSheet(QString("#%4 { background:rgb(%1,%2,%3); }")
.arg(color.red())
.arg(color.green())
.arg(color.blue())
.arg(objectName()));
else
setStyleSheet(QString());
emit colorChanged(color);
}
void ColorSelector::mousePressEvent (QMouseEvent *ev) {
if (ev->button() == Qt::LeftButton) {
QColor color = QColorDialog::getColor(selectedColor(), this, "Choose Color");
if (color.isValid())
setSelectedColor(color);
}
}
void ColorSelector::setUseStyleSheet (bool use) {
usecss_ = use;
setSelectedColor(selectedColor());
}