1 #include <QApplication>
2 #include <QMouseEvent>
3 #include <QPainter>
4 #include <QColorDialog>
5 #include <QGridLayout>
6 #include <QLabel>
7 #include <QPushButton>
8 #include "paletteeditor.h"
9 
10 
11 
12 typedef QList<QPair<QPalette::ColorRole,QString>> RoleLabels;
13 Q_GLOBAL_STATIC_WITH_ARGS(RoleLabels, roleText, ({
14     { QPalette::WindowText, QObject::tr("Window text") },
15     { QPalette::Button, QObject::tr("Button") },
16     { QPalette::Light, QObject::tr("Light") },
17     { QPalette::Midlight, QObject::tr("Midlight") },
18     { QPalette::Dark, QObject::tr("Dark") },
19     { QPalette::Mid, QObject::tr("Mid") },
20     { QPalette::Text, QObject::tr("Text") },
21     { QPalette::BrightText, QObject::tr("Bright text") },
22     { QPalette::ButtonText, QObject::tr("Button text") },
23     { QPalette::Base, QObject::tr("Base") },
24     { QPalette::Window, QObject::tr("Window") },
25     { QPalette::Shadow, QObject::tr("Shadow") },
26     { QPalette::Highlight, QObject::tr("Highlight") },
27     { QPalette::HighlightedText, QObject::tr("Highlighted text") },
28     { QPalette::Link, QObject::tr("Link") },
29     { QPalette::LinkVisited, QObject::tr("Link (visited)") },
30     { QPalette::AlternateBase, QObject::tr("Base (alternate)") },
31     { QPalette::NoRole, QObject::tr("No role") },
32     { QPalette::ToolTipBase, QObject::tr("Tooltip base") },
33     { QPalette::ToolTipText, QObject::tr("Tooltip text") }
34 }));
35 
36 typedef QList<QPair<QPalette::ColorGroup,QString>> GroupLabels;
37 Q_GLOBAL_STATIC_WITH_ARGS(GroupLabels, groupText, ({
38     { QPalette::Active, QObject::tr("Active") },
39     { QPalette::Disabled, QObject::tr("Disabled") },
40     { QPalette::Inactive, QObject::tr("Inactive") }
41 }));
42 
43 
44 
PaletteBox(QWidget * parent)45 PaletteBox::PaletteBox(QWidget *parent) : QWidget(parent), color(0,0,0)
46 {
47 
48 }
49 
value()50 QColor PaletteBox::value()
51 {
52     return color;
53 }
54 
setValue(const QColor & c)55 void PaletteBox::setValue(const QColor &c)
56 {
57     color = c;
58     emit valueChanged(color);
59     update();
60 }
61 
paintEvent(QPaintEvent * event)62 void PaletteBox::paintEvent(QPaintEvent *event)
63 {
64     Q_UNUSED(event)
65     QPainter p(this);
66     p.fillRect(QRect(0,0,width(),height()), QBrush(color));
67 }
68 
mousePressEvent(QMouseEvent * event)69 void PaletteBox::mousePressEvent(QMouseEvent *event)
70 {
71     if (event->button() == Qt::LeftButton) {
72         event->accept();
73         QColor ret = QColorDialog::getColor(color, this);
74         if (ret.isValid()) {
75             setValue(ret);
76             emit valueSelected(color);
77         }
78         return;
79     }
80     QWidget::mousePressEvent(event);
81 }
82 
83 
84 
PaletteEditor(QWidget * parent)85 PaletteEditor::PaletteEditor(QWidget *parent) : QWidget(parent)
86 {
87     system = qApp->palette();
88     auto makeBox = [this](ColorPair data) {
89         PaletteBox *box = new PaletteBox;
90         PaletteEditor *owner = this;
91         connect(box, &PaletteBox::valueSelected,
92                 owner, [owner,data](QColor c) {
93             owner->colorField_valueSelected(data,c);
94         });
95         boxes.insert(data, box);
96         return box;
97     };
98 
99     int col = 0;
100     int row = 0;
101     QGridLayout *layout = new QGridLayout();
102 
103     // First row - labels
104     for (auto &gtt : *groupText)
105         layout->addWidget(new QLabel(gtt.second), row, 1 + col++);
106     row++;
107 
108     // Middle rows - colorboxes
109     for (auto &role : *roleText) {
110         col = 0;
111         layout->addWidget(new QLabel(role.second), row, col++);
112         for (auto &group : *groupText)
113             layout->addWidget(makeBox({group.first,role.first}), row, col++);
114         row++;
115     }
116 
117     // Final row - generate buttons
118     col = 0;
119     layout->addWidget(new QLabel(tr("Generate")), row, col++);
120 
121     QPushButton *button;
122     button = new QPushButton(tr("Button"), this);
123     connect(button, &QPushButton::clicked,
124             this, &PaletteEditor::generateButton_clicked);
125     layout->addWidget(button, row, col++);
126 
127     button = new QPushButton(tr("Button && Window"), this);
128     connect(button, &QPushButton::clicked,
129             this, &PaletteEditor::generateButton_clicked);
130     layout->addWidget(button, row, col++);
131 
132     button = new QPushButton(tr("Reset to System"), this);
133     connect(button, &QPushButton::clicked,
134             this, &PaletteEditor::resetPalette);
135     layout->addWidget(button, row, col++);
136 
137     setLayout(layout);
138 
139     connect(this, &PaletteEditor::paletteChanged,
140             this, &PaletteEditor::valueChanged);
141 }
142 
palette()143 QPalette PaletteEditor::palette()
144 {
145     return selected;
146 }
147 
systemPalette()148 QPalette PaletteEditor::systemPalette()
149 {
150     return system;
151 }
152 
variant()153 QVariant PaletteEditor::variant()
154 {
155     QVariant v = paletteToVariant(selected);
156     return v;
157 }
158 
variantToPalette(const QVariant & v)159 QPalette PaletteEditor::variantToPalette(const QVariant &v)
160 {
161     QPalette p = system;
162     QVariantList array = v.toList();
163     RoleLabels::ConstIterator it;
164     QVariant defaultValue("#000000");
165     int index = 0;
166     for (it = roleText->constBegin(); it != roleText->constEnd(); it++) {
167         QVariantList items = array.value(index++).toList();
168         p.setColor(QPalette::Active, it->first, items.value(0, defaultValue).toString());
169         p.setColor(QPalette::Disabled, it->first, items.value(1, defaultValue).toString());
170         p.setColor(QPalette::Inactive, it->first, items.value(2, defaultValue).toString());
171     }
172     return p;
173 }
174 
paletteToVariant(const QPalette & p)175 QVariant PaletteEditor::paletteToVariant(const QPalette &p)
176 {
177     QVariantList array;
178     RoleLabels::ConstIterator it;
179     for (it = roleText->constBegin(); it != roleText->constEnd(); it++) {
180         QVariantList items;
181         items.append(p.color(QPalette::Active, it->first).name());
182         items.append(p.color(QPalette::Disabled, it->first).name());
183         items.append(p.color(QPalette::Inactive, it->first).name());
184         array.append(QVariant(items));
185     }
186     return array;
187 }
188 
setPalette(const QPalette & pal)189 void PaletteEditor::setPalette(const QPalette &pal)
190 {
191     selected = pal;
192     for (BoxMap::Iterator it = boxes.begin(); it != boxes.end(); it++)
193         it.value()->setValue(selected.color(it.key().first, it.key().second));
194     emit paletteChanged(selected);
195 }
196 
setVariant(const QVariant & value)197 void PaletteEditor::setVariant(const QVariant &value)
198 {
199     setPalette(variantToPalette(value));
200 }
201 
resetPalette()202 void PaletteEditor::resetPalette()
203 {
204     setPalette(system);
205 }
206 
colorField_valueSelected(PaletteEditor::ColorPair entry,QColor color)207 void PaletteEditor::colorField_valueSelected(PaletteEditor::ColorPair entry, QColor color)
208 {
209     selected.setColor(entry.first, entry.second, color);
210     emit paletteChanged(selected);
211 }
212 
generateButton_clicked()213 void PaletteEditor::generateButton_clicked()
214 {
215     setPalette(QPalette(selected.color(QPalette::Button)));
216 }
217 
generateButtonWindow_clicked()218 void PaletteEditor::generateButtonWindow_clicked()
219 {
220     setPalette(QPalette(selected.color(QPalette::Button),
221                         selected.color(QPalette::Window)));
222 }
223