1 |
// qsamplerPaletteForm.cpp |
2 |
// |
3 |
/**************************************************************************** |
4 |
Copyright (C) 2004-2020, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
Copyright (C) 2007, Christian Schoenebeck |
6 |
|
7 |
This program is free software; you can redistribute it and/or |
8 |
modify it under the terms of the GNU General Public License |
9 |
as published by the Free Software Foundation; either version 2 |
10 |
of the License, or (at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License along |
18 |
with this program; if not, write to the Free Software Foundation, Inc., |
19 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
21 |
*****************************************************************************/ |
22 |
|
23 |
#include "qsamplerAbout.h" |
24 |
#include "qsamplerPaletteForm.h" |
25 |
|
26 |
#include "ui_qsamplerPaletteForm.h" |
27 |
|
28 |
#include <QMetaProperty> |
29 |
#include <QToolButton> |
30 |
#include <QHeaderView> |
31 |
#include <QLabel> |
32 |
|
33 |
#include <QHash> |
34 |
|
35 |
#include <QPainter> |
36 |
#include <QStyle> |
37 |
#include <QStyleOption> |
38 |
|
39 |
#include <QColorDialog> |
40 |
#include <QFileDialog> |
41 |
#include <QMessageBox> |
42 |
|
43 |
|
44 |
namespace QSampler { |
45 |
|
46 |
// Local static consts. |
47 |
static const char *ColorThemesGroup = "/ColorThemes/"; |
48 |
|
49 |
static const char *PaletteEditorGroup = "/PaletteEditor/"; |
50 |
static const char *DefaultDirKey = "DefaultDir"; |
51 |
static const char *ShowDetailsKey = "ShowDetails"; |
52 |
static const char *DefaultSuffix = "conf"; |
53 |
|
54 |
|
55 |
static struct |
56 |
{ |
57 |
const char *key; |
58 |
QPalette::ColorRole value; |
59 |
|
60 |
} g_colorRoles[] = { |
61 |
|
62 |
{ "Window", QPalette::Window }, |
63 |
{ "WindowText", QPalette::WindowText }, |
64 |
{ "Button", QPalette::Button }, |
65 |
{ "ButtonText", QPalette::ButtonText }, |
66 |
{ "Light", QPalette::Light }, |
67 |
{ "Midlight", QPalette::Midlight }, |
68 |
{ "Dark", QPalette::Dark }, |
69 |
{ "Mid", QPalette::Mid }, |
70 |
{ "Text", QPalette::Text }, |
71 |
{ "BrightText", QPalette::BrightText }, |
72 |
{ "Base", QPalette::Base }, |
73 |
{ "AlternateBase", QPalette::AlternateBase }, |
74 |
{ "Shadow", QPalette::Shadow }, |
75 |
{ "Highlight", QPalette::Highlight }, |
76 |
{ "HighlightedText", QPalette::HighlightedText }, |
77 |
{ "Link", QPalette::Link }, |
78 |
{ "LinkVisited", QPalette::LinkVisited }, |
79 |
{ "ToolTipBase", QPalette::ToolTipBase }, |
80 |
{ "ToolTipText", QPalette::ToolTipText }, |
81 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) |
82 |
{ "PlaceholderText", QPalette::PlaceholderText }, |
83 |
#endif |
84 |
{ "NoRole", QPalette::NoRole }, |
85 |
|
86 |
{ nullptr, QPalette::NoRole } |
87 |
}; |
88 |
|
89 |
|
90 |
//------------------------------------------------------------------------- |
91 |
// Qsampler::PaletteForm |
92 |
|
93 |
PaletteForm::PaletteForm ( QWidget *parent, const QPalette& pal ) |
94 |
: QDialog(parent), p_ui(new Ui::qsamplerPaletteForm), m_ui(*p_ui) |
95 |
{ |
96 |
m_ui.setupUi(this); |
97 |
|
98 |
m_settings = nullptr; |
99 |
m_owner = false; |
100 |
|
101 |
m_modelUpdated = false; |
102 |
m_paletteUpdated = false; |
103 |
m_dirtyCount = 0; |
104 |
m_dirtyTotal = 0; |
105 |
|
106 |
updateGenerateButton(); |
107 |
|
108 |
m_paletteModel = new PaletteModel(this); |
109 |
m_ui.paletteView->setModel(m_paletteModel); |
110 |
ColorDelegate *delegate = new ColorDelegate(this); |
111 |
m_ui.paletteView->setItemDelegate(delegate); |
112 |
m_ui.paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers); |
113 |
// m_ui.paletteView->setAlternatingRowColors(true); |
114 |
m_ui.paletteView->setSelectionBehavior(QAbstractItemView::SelectRows); |
115 |
m_ui.paletteView->setDragEnabled(true); |
116 |
m_ui.paletteView->setDropIndicatorShown(true); |
117 |
m_ui.paletteView->setRootIsDecorated(false); |
118 |
m_ui.paletteView->setColumnHidden(2, true); |
119 |
m_ui.paletteView->setColumnHidden(3, true); |
120 |
|
121 |
QObject::connect(m_ui.nameCombo, |
122 |
SIGNAL(editTextChanged(const QString&)), |
123 |
SLOT(nameComboChanged(const QString&))); |
124 |
QObject::connect(m_ui.saveButton, |
125 |
SIGNAL(clicked()), |
126 |
SLOT(saveButtonClicked())); |
127 |
QObject::connect(m_ui.deleteButton, |
128 |
SIGNAL(clicked()), |
129 |
SLOT(deleteButtonClicked())); |
130 |
|
131 |
QObject::connect(m_ui.generateButton, |
132 |
SIGNAL(changed()), |
133 |
SLOT(generateButtonChanged())); |
134 |
QObject::connect(m_ui.resetButton, |
135 |
SIGNAL(clicked()), |
136 |
SLOT(resetButtonClicked())); |
137 |
QObject::connect(m_ui.detailsCheck, |
138 |
SIGNAL(clicked()), |
139 |
SLOT(detailsCheckClicked())); |
140 |
QObject::connect(m_ui.importButton, |
141 |
SIGNAL(clicked()), |
142 |
SLOT(importButtonClicked())); |
143 |
QObject::connect(m_ui.exportButton, |
144 |
SIGNAL(clicked()), |
145 |
SLOT(exportButtonClicked())); |
146 |
|
147 |
QObject::connect(m_paletteModel, |
148 |
SIGNAL(paletteChanged(const QPalette&)), |
149 |
SLOT(paletteChanged(const QPalette&))); |
150 |
|
151 |
QObject::connect(m_ui.dialogButtons, |
152 |
SIGNAL(accepted()), |
153 |
SLOT(accept())); |
154 |
QObject::connect(m_ui.dialogButtons, |
155 |
SIGNAL(rejected()), |
156 |
SLOT(reject())); |
157 |
|
158 |
setPalette(pal, pal); |
159 |
|
160 |
QDialog::adjustSize(); |
161 |
} |
162 |
|
163 |
|
164 |
PaletteForm::~PaletteForm (void) |
165 |
{ |
166 |
setSettings(nullptr); |
167 |
} |
168 |
|
169 |
|
170 |
void PaletteForm::setPalette ( const QPalette& pal ) |
171 |
{ |
172 |
m_palette = pal; |
173 |
|
174 |
const uint mask = pal.resolve(); |
175 |
for (int i = 0; g_colorRoles[i].key; ++i) { |
176 |
if ((mask & (1 << i)) == 0) { |
177 |
const QPalette::ColorRole cr = QPalette::ColorRole(i); |
178 |
m_palette.setBrush(QPalette::Active, cr, |
179 |
m_parentPalette.brush(QPalette::Active, cr)); |
180 |
m_palette.setBrush(QPalette::Inactive, cr, |
181 |
m_parentPalette.brush(QPalette::Inactive, cr)); |
182 |
m_palette.setBrush(QPalette::Disabled, cr, |
183 |
m_parentPalette.brush(QPalette::Disabled, cr)); |
184 |
} |
185 |
} |
186 |
m_palette.resolve(mask); |
187 |
|
188 |
updateGenerateButton(); |
189 |
|
190 |
m_paletteUpdated = true; |
191 |
if (!m_modelUpdated) |
192 |
m_paletteModel->setPalette(m_palette, m_parentPalette); |
193 |
m_paletteUpdated = false; |
194 |
} |
195 |
|
196 |
|
197 |
void PaletteForm::setPalette ( const QPalette& pal, const QPalette& parentPal ) |
198 |
{ |
199 |
m_parentPalette = parentPal; |
200 |
|
201 |
setPalette(pal); |
202 |
} |
203 |
|
204 |
|
205 |
const QPalette& PaletteForm::palette (void) const |
206 |
{ |
207 |
return m_palette; |
208 |
} |
209 |
|
210 |
|
211 |
void PaletteForm::setSettings ( QSettings *settings, bool owner ) |
212 |
{ |
213 |
if (m_settings && m_owner) |
214 |
delete m_settings; |
215 |
|
216 |
m_settings = settings; |
217 |
m_owner = owner; |
218 |
|
219 |
m_ui.detailsCheck->setChecked(isShowDetails()); |
220 |
|
221 |
updateNamedPaletteList(); |
222 |
updateDialogButtons(); |
223 |
} |
224 |
|
225 |
|
226 |
QSettings *PaletteForm::settings (void) const |
227 |
{ |
228 |
return m_settings; |
229 |
} |
230 |
|
231 |
|
232 |
void PaletteForm::nameComboChanged ( const QString& name ) |
233 |
{ |
234 |
if (m_dirtyCount > 0 || m_ui.nameCombo->findText(name) < 0) { |
235 |
updateDialogButtons(); |
236 |
} else { |
237 |
setPaletteName(name); |
238 |
++m_dirtyTotal; |
239 |
} |
240 |
} |
241 |
|
242 |
|
243 |
void PaletteForm::saveButtonClicked (void) |
244 |
{ |
245 |
const QString& name = m_ui.nameCombo->currentText(); |
246 |
if (!name.isEmpty()) { |
247 |
saveNamedPalette(name, m_palette); |
248 |
setPalette(m_palette, m_palette); |
249 |
updateNamedPaletteList(); |
250 |
resetButtonClicked(); |
251 |
} |
252 |
} |
253 |
|
254 |
|
255 |
void PaletteForm::deleteButtonClicked (void) |
256 |
{ |
257 |
const QString& name = m_ui.nameCombo->currentText(); |
258 |
if (m_ui.nameCombo->findText(name) >= 0) { |
259 |
deleteNamedPalette(name); |
260 |
updateNamedPaletteList(); |
261 |
updateDialogButtons(); |
262 |
} |
263 |
} |
264 |
|
265 |
|
266 |
void PaletteForm::generateButtonChanged (void) |
267 |
{ |
268 |
const QColor& color |
269 |
= m_ui.generateButton->brush().color(); |
270 |
const QPalette& pal = QPalette(color); |
271 |
setPalette(pal); |
272 |
|
273 |
++m_dirtyCount; |
274 |
updateDialogButtons(); |
275 |
} |
276 |
|
277 |
|
278 |
void PaletteForm::resetButtonClicked (void) |
279 |
{ |
280 |
const bool blocked = blockSignals(true); |
281 |
|
282 |
for (int i = 0; g_colorRoles[i].key; ++i) { |
283 |
const QPalette::ColorRole cr = g_colorRoles[i].value; |
284 |
const QModelIndex& index = m_paletteModel->index(cr, 0); |
285 |
m_paletteModel->setData(index, false, Qt::EditRole); |
286 |
} |
287 |
|
288 |
m_dirtyCount = 0; |
289 |
updateDialogButtons(); |
290 |
|
291 |
blockSignals(blocked); |
292 |
} |
293 |
|
294 |
|
295 |
void PaletteForm::detailsCheckClicked (void) |
296 |
{ |
297 |
const int cw = (m_ui.paletteView->viewport()->width() >> 2); |
298 |
QHeaderView *header = m_ui.paletteView->header(); |
299 |
header->resizeSection(0, cw); |
300 |
if (m_ui.detailsCheck->isChecked()) { |
301 |
m_ui.paletteView->setColumnHidden(2, false); |
302 |
m_ui.paletteView->setColumnHidden(3, false); |
303 |
header->resizeSection(1, cw); |
304 |
header->resizeSection(2, cw); |
305 |
header->resizeSection(3, cw); |
306 |
m_paletteModel->setGenerate(false); |
307 |
} else { |
308 |
m_ui.paletteView->setColumnHidden(2, true); |
309 |
m_ui.paletteView->setColumnHidden(3, true); |
310 |
header->resizeSection(1, cw * 3); |
311 |
m_paletteModel->setGenerate(true); |
312 |
} |
313 |
} |
314 |
|
315 |
|
316 |
void PaletteForm::importButtonClicked (void) |
317 |
{ |
318 |
const QString& title |
319 |
= tr("Import File - %1").arg(QDialog::windowTitle()); |
320 |
|
321 |
QStringList filters; |
322 |
filters.append(tr("Palette files (*.%1)").arg(DefaultSuffix)); |
323 |
filters.append(tr("All files (*.*)")); |
324 |
|
325 |
const QString& filename |
326 |
= QFileDialog::getOpenFileName(this, |
327 |
title, defaultDir(), filters.join(";;")); |
328 |
|
329 |
if (filename.isEmpty()) |
330 |
return; |
331 |
|
332 |
int imported = 0; |
333 |
QSettings settings(filename, QSettings::IniFormat); |
334 |
settings.beginGroup(ColorThemesGroup); |
335 |
QStringListIterator name_iter(settings.childGroups()); |
336 |
while (name_iter.hasNext()) { |
337 |
const QString& name = name_iter.next(); |
338 |
if (!name.isEmpty()) { |
339 |
QPalette pal; |
340 |
int result = 0; |
341 |
uint mask = pal.resolve(); |
342 |
settings.beginGroup(name + '/'); |
343 |
QStringListIterator iter(settings.childKeys()); |
344 |
while (iter.hasNext()) { |
345 |
const QString& key = iter.next(); |
346 |
const QPalette::ColorRole cr |
347 |
= PaletteForm::colorRole(key); |
348 |
const QStringList& clist |
349 |
= settings.value(key).toStringList(); |
350 |
if (clist.count() == 3) { |
351 |
pal.setColor(QPalette::Active, cr, QColor(clist.at(0))); |
352 |
pal.setColor(QPalette::Inactive, cr, QColor(clist.at(1))); |
353 |
pal.setColor(QPalette::Disabled, cr, QColor(clist.at(2))); |
354 |
mask &= ~(1 << int(cr)); |
355 |
++result; |
356 |
} |
357 |
} |
358 |
pal.resolve(mask); |
359 |
settings.endGroup(); |
360 |
if (result > 0) { |
361 |
saveNamedPalette(name, pal); |
362 |
setPaletteName(name); |
363 |
++imported; |
364 |
} |
365 |
} |
366 |
} |
367 |
settings.endGroup(); |
368 |
|
369 |
if (imported > 0) { |
370 |
updateNamedPaletteList(); |
371 |
resetButtonClicked(); |
372 |
setDefaultDir(QFileInfo(filename).absolutePath()); |
373 |
} else { |
374 |
QMessageBox::warning(this, |
375 |
tr("Warning - %1").arg(QDialog::windowTitle()), |
376 |
tr("Could not import from file:\n\n" |
377 |
"%1\n\nSorry.").arg(filename)); |
378 |
} |
379 |
} |
380 |
|
381 |
|
382 |
void PaletteForm::exportButtonClicked (void) |
383 |
{ |
384 |
const QString& title |
385 |
= tr("Export File - %1").arg(QDialog::windowTitle()); |
386 |
|
387 |
QStringList filters; |
388 |
filters.append(tr("Palette files (*.%1)").arg(DefaultSuffix)); |
389 |
filters.append(tr("All files (*.*)")); |
390 |
|
391 |
QString dirname = defaultDir(); |
392 |
if (!dirname.isEmpty()) |
393 |
dirname.append(QDir::separator()); |
394 |
dirname.append(paletteName() + '.' + DefaultSuffix); |
395 |
|
396 |
const QString& filename |
397 |
= QFileDialog::getSaveFileName(this, |
398 |
title, dirname, filters.join(";;")); |
399 |
|
400 |
if (filename.isEmpty()) |
401 |
return; |
402 |
|
403 |
const QPalette& pal = m_palette; |
404 |
|
405 |
QSettings settings(filename, QSettings::IniFormat); |
406 |
settings.beginGroup(ColorThemesGroup); |
407 |
settings.beginGroup(QFileInfo(filename).baseName() + '/'); |
408 |
for (int i = 0; g_colorRoles[i].key; ++i) { |
409 |
const QString& key |
410 |
= QString::fromLatin1(g_colorRoles[i].key); |
411 |
const QPalette::ColorRole cr |
412 |
= g_colorRoles[i].value; |
413 |
QStringList clist; |
414 |
clist.append(pal.color(QPalette::Active, cr).name()); |
415 |
clist.append(pal.color(QPalette::Inactive, cr).name()); |
416 |
clist.append(pal.color(QPalette::Disabled, cr).name()); |
417 |
settings.setValue(key, clist); |
418 |
} |
419 |
settings.endGroup(); |
420 |
settings.endGroup(); |
421 |
|
422 |
setDefaultDir(QFileInfo(filename).absolutePath()); |
423 |
} |
424 |
|
425 |
|
426 |
void PaletteForm::paletteChanged ( const QPalette& pal ) |
427 |
{ |
428 |
m_modelUpdated = true; |
429 |
if (!m_paletteUpdated) |
430 |
setPalette(pal); |
431 |
m_modelUpdated = false; |
432 |
|
433 |
++m_dirtyCount; |
434 |
updateDialogButtons(); |
435 |
} |
436 |
|
437 |
|
438 |
void PaletteForm::setPaletteName ( const QString& name ) |
439 |
{ |
440 |
const bool blocked = m_ui.nameCombo->blockSignals(true); |
441 |
|
442 |
m_ui.nameCombo->setEditText(name); |
443 |
|
444 |
QPalette pal; |
445 |
|
446 |
if (namedPalette(m_settings, name, pal, true)) |
447 |
setPalette(pal, pal); |
448 |
|
449 |
m_dirtyCount = 0; |
450 |
updateDialogButtons(); |
451 |
|
452 |
m_ui.nameCombo->blockSignals(blocked); |
453 |
} |
454 |
|
455 |
|
456 |
QString PaletteForm::paletteName (void) const |
457 |
{ |
458 |
return m_ui.nameCombo->currentText(); |
459 |
} |
460 |
|
461 |
|
462 |
void PaletteForm::updateNamedPaletteList (void) |
463 |
{ |
464 |
const bool blocked = m_ui.nameCombo->blockSignals(true); |
465 |
const QString old_name = m_ui.nameCombo->currentText(); |
466 |
|
467 |
m_ui.nameCombo->clear(); |
468 |
m_ui.nameCombo->insertItems(0, namedPaletteList()); |
469 |
// m_ui.nameCombo->model()->sort(0); |
470 |
|
471 |
const int i = m_ui.nameCombo->findText(old_name); |
472 |
if (i >= 0) |
473 |
m_ui.nameCombo->setCurrentIndex(i); |
474 |
else |
475 |
m_ui.nameCombo->setEditText(old_name); |
476 |
|
477 |
m_ui.nameCombo->blockSignals(blocked); |
478 |
} |
479 |
|
480 |
|
481 |
void PaletteForm::updateGenerateButton (void) |
482 |
{ |
483 |
m_ui.generateButton->setBrush( |
484 |
m_palette.brush(QPalette::Active, QPalette::Button)); |
485 |
} |
486 |
|
487 |
|
488 |
void PaletteForm::updateDialogButtons (void) |
489 |
{ |
490 |
const QString& name = m_ui.nameCombo->currentText(); |
491 |
const int i = m_ui.nameCombo->findText(name); |
492 |
m_ui.saveButton->setEnabled(!name.isEmpty() && (m_dirtyCount > 0 || i < 0)); |
493 |
m_ui.deleteButton->setEnabled(i >= 0); |
494 |
m_ui.resetButton->setEnabled(m_dirtyCount > 0); |
495 |
m_ui.exportButton->setEnabled(!name.isEmpty() || i >= 0); |
496 |
m_ui.dialogButtons->button(QDialogButtonBox::Ok)->setEnabled(i >= 0); |
497 |
if (name == "Wonton Soup" || name == "KXStudio") { |
498 |
m_ui.saveButton->setEnabled(false); |
499 |
m_ui.deleteButton->setEnabled(false); |
500 |
m_ui.exportButton->setEnabled(false); |
501 |
} |
502 |
} |
503 |
|
504 |
|
505 |
bool PaletteForm::namedPalette ( const QString& name, QPalette& pal ) |
506 |
{ |
507 |
return namedPalette(m_settings, name, pal); |
508 |
} |
509 |
|
510 |
|
511 |
bool PaletteForm::namedPalette ( |
512 |
QSettings *settings, const QString& name, QPalette& pal, bool fixup ) |
513 |
{ |
514 |
int result = 0; |
515 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
516 |
uint mask = pal.resolve(); |
517 |
#endif |
518 |
|
519 |
// Custom color themes... |
520 |
if (name == "Wonton Soup") { |
521 |
pal.setColor(QPalette::Active, QPalette::Window, QColor(73, 78, 88)); |
522 |
pal.setColor(QPalette::Inactive, QPalette::Window, QColor(73, 78, 88)); |
523 |
pal.setColor(QPalette::Disabled, QPalette::Window, QColor(64, 68, 77)); |
524 |
pal.setColor(QPalette::Active, QPalette::WindowText, QColor(182, 193, 208)); |
525 |
pal.setColor(QPalette::Inactive, QPalette::WindowText, QColor(182, 193, 208)); |
526 |
pal.setColor(QPalette::Disabled, QPalette::WindowText, QColor(97, 104, 114)); |
527 |
pal.setColor(QPalette::Active, QPalette::Base, QColor(60, 64, 72)); |
528 |
pal.setColor(QPalette::Inactive, QPalette::Base, QColor(60, 64, 72)); |
529 |
pal.setColor(QPalette::Disabled, QPalette::Base, QColor(52, 56, 63)); |
530 |
pal.setColor(QPalette::Active, QPalette::AlternateBase, QColor(67, 71, 80)); |
531 |
pal.setColor(QPalette::Inactive, QPalette::AlternateBase, QColor(67, 71, 80)); |
532 |
pal.setColor(QPalette::Disabled, QPalette::AlternateBase, QColor(59, 62, 70)); |
533 |
pal.setColor(QPalette::Active, QPalette::ToolTipBase, QColor(182, 193, 208)); |
534 |
pal.setColor(QPalette::Inactive, QPalette::ToolTipBase, QColor(182, 193, 208)); |
535 |
pal.setColor(QPalette::Disabled, QPalette::ToolTipBase, QColor(182, 193, 208)); |
536 |
pal.setColor(QPalette::Active, QPalette::ToolTipText, QColor(42, 44, 48)); |
537 |
pal.setColor(QPalette::Inactive, QPalette::ToolTipText, QColor(42, 44, 48)); |
538 |
pal.setColor(QPalette::Disabled, QPalette::ToolTipText, QColor(42, 44, 48)); |
539 |
pal.setColor(QPalette::Active, QPalette::Text, QColor(210, 222, 240)); |
540 |
pal.setColor(QPalette::Inactive, QPalette::Text, QColor(210, 222, 240)); |
541 |
pal.setColor(QPalette::Disabled, QPalette::Text, QColor(99, 105, 115)); |
542 |
pal.setColor(QPalette::Active, QPalette::Button, QColor(82, 88, 99)); |
543 |
pal.setColor(QPalette::Inactive, QPalette::Button, QColor(82, 88, 99)); |
544 |
pal.setColor(QPalette::Disabled, QPalette::Button, QColor(72, 77, 87)); |
545 |
pal.setColor(QPalette::Active, QPalette::ButtonText, QColor(210, 222, 240)); |
546 |
pal.setColor(QPalette::Inactive, QPalette::ButtonText, QColor(210, 222, 240)); |
547 |
pal.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(111, 118, 130)); |
548 |
pal.setColor(QPalette::Active, QPalette::BrightText, QColor(255, 255, 255)); |
549 |
pal.setColor(QPalette::Inactive, QPalette::BrightText, QColor(255, 255, 255)); |
550 |
pal.setColor(QPalette::Disabled, QPalette::BrightText, QColor(255, 255, 255)); |
551 |
pal.setColor(QPalette::Active, QPalette::Light, QColor(95, 101, 114)); |
552 |
pal.setColor(QPalette::Inactive, QPalette::Light, QColor(95, 101, 114)); |
553 |
pal.setColor(QPalette::Disabled, QPalette::Light, QColor(86, 92, 104)); |
554 |
pal.setColor(QPalette::Active, QPalette::Midlight, QColor(84, 90, 101)); |
555 |
pal.setColor(QPalette::Inactive, QPalette::Midlight, QColor(84, 90, 101)); |
556 |
pal.setColor(QPalette::Disabled, QPalette::Midlight, QColor(75, 81, 91)); |
557 |
pal.setColor(QPalette::Active, QPalette::Dark, QColor(40, 43, 49)); |
558 |
pal.setColor(QPalette::Inactive, QPalette::Dark, QColor(40, 43, 49)); |
559 |
pal.setColor(QPalette::Disabled, QPalette::Dark, QColor(35, 38, 43)); |
560 |
pal.setColor(QPalette::Active, QPalette::Mid, QColor(63, 68, 76)); |
561 |
pal.setColor(QPalette::Inactive, QPalette::Mid, QColor(63, 68, 76)); |
562 |
pal.setColor(QPalette::Disabled, QPalette::Mid, QColor(56, 59, 67)); |
563 |
pal.setColor(QPalette::Active, QPalette::Shadow, QColor(29, 31, 35)); |
564 |
pal.setColor(QPalette::Inactive, QPalette::Shadow, QColor(29, 31, 35)); |
565 |
pal.setColor(QPalette::Disabled, QPalette::Shadow, QColor(25, 27, 30)); |
566 |
pal.setColor(QPalette::Active, QPalette::Highlight, QColor(120, 136, 156)); |
567 |
pal.setColor(QPalette::Inactive, QPalette::Highlight, QColor(81, 90, 103)); |
568 |
pal.setColor(QPalette::Disabled, QPalette::Highlight, QColor(64, 68, 77)); |
569 |
pal.setColor(QPalette::Active, QPalette::HighlightedText, QColor(209, 225, 244)); |
570 |
pal.setColor(QPalette::Inactive, QPalette::HighlightedText, QColor(182, 193, 208)); |
571 |
pal.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(97, 104, 114)); |
572 |
pal.setColor(QPalette::Active, QPalette::Link, QColor(156, 212, 255)); |
573 |
pal.setColor(QPalette::Inactive, QPalette::Link, QColor(156, 212, 255)); |
574 |
pal.setColor(QPalette::Disabled, QPalette::Link, QColor(82, 102, 119)); |
575 |
pal.setColor(QPalette::Active, QPalette::LinkVisited, QColor(64, 128, 255)); |
576 |
pal.setColor(QPalette::Inactive, QPalette::LinkVisited, QColor(64, 128, 255)); |
577 |
pal.setColor(QPalette::Disabled, QPalette::LinkVisited, QColor(54, 76, 119)); |
578 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
579 |
mask = 0; |
580 |
#endif |
581 |
++result; |
582 |
} |
583 |
else |
584 |
if (name == "KXStudio") { |
585 |
pal.setColor(QPalette::Active, QPalette::Window, QColor(17, 17, 17)); |
586 |
pal.setColor(QPalette::Inactive, QPalette::Window, QColor(17, 17, 17)); |
587 |
pal.setColor(QPalette::Disabled, QPalette::Window, QColor(14, 14, 14)); |
588 |
pal.setColor(QPalette::Active, QPalette::WindowText, QColor(240, 240, 240)); |
589 |
pal.setColor(QPalette::Inactive, QPalette::WindowText, QColor(240, 240, 240)); |
590 |
pal.setColor(QPalette::Disabled, QPalette::WindowText, QColor(83, 83, 83)); |
591 |
pal.setColor(QPalette::Active, QPalette::Base, QColor(7, 7, 7)); |
592 |
pal.setColor(QPalette::Inactive, QPalette::Base, QColor(7, 7, 7)); |
593 |
pal.setColor(QPalette::Disabled, QPalette::Base, QColor(6, 6, 6)); |
594 |
pal.setColor(QPalette::Active, QPalette::AlternateBase, QColor(14, 14, 14)); |
595 |
pal.setColor(QPalette::Inactive, QPalette::AlternateBase, QColor(14, 14, 14)); |
596 |
pal.setColor(QPalette::Disabled, QPalette::AlternateBase, QColor(12, 12, 12)); |
597 |
pal.setColor(QPalette::Active, QPalette::ToolTipBase, QColor(4, 4, 4)); |
598 |
pal.setColor(QPalette::Inactive, QPalette::ToolTipBase, QColor(4, 4, 4)); |
599 |
pal.setColor(QPalette::Disabled, QPalette::ToolTipBase, QColor(4, 4, 4)); |
600 |
pal.setColor(QPalette::Active, QPalette::ToolTipText, QColor(230, 230, 230)); |
601 |
pal.setColor(QPalette::Inactive, QPalette::ToolTipText, QColor(230, 230, 230)); |
602 |
pal.setColor(QPalette::Disabled, QPalette::ToolTipText, QColor(230, 230, 230)); |
603 |
pal.setColor(QPalette::Active, QPalette::Text, QColor(230, 230, 230)); |
604 |
pal.setColor(QPalette::Inactive, QPalette::Text, QColor(230, 230, 230)); |
605 |
pal.setColor(QPalette::Disabled, QPalette::Text, QColor(74, 74, 74)); |
606 |
pal.setColor(QPalette::Active, QPalette::Button, QColor(28, 28, 28)); |
607 |
pal.setColor(QPalette::Inactive, QPalette::Button, QColor(28, 28, 28)); |
608 |
pal.setColor(QPalette::Disabled, QPalette::Button, QColor(24, 24, 24)); |
609 |
pal.setColor(QPalette::Active, QPalette::ButtonText, QColor(240, 240, 240)); |
610 |
pal.setColor(QPalette::Inactive, QPalette::ButtonText, QColor(240, 240, 240)); |
611 |
pal.setColor(QPalette::Disabled, QPalette::ButtonText, QColor(90, 90, 90)); |
612 |
pal.setColor(QPalette::Active, QPalette::BrightText, QColor(255, 255, 255)); |
613 |
pal.setColor(QPalette::Inactive, QPalette::BrightText, QColor(255, 255, 255)); |
614 |
pal.setColor(QPalette::Disabled, QPalette::BrightText, QColor(255, 255, 255)); |
615 |
pal.setColor(QPalette::Active, QPalette::Light, QColor(191, 191, 191)); |
616 |
pal.setColor(QPalette::Inactive, QPalette::Light, QColor(191, 191, 191)); |
617 |
pal.setColor(QPalette::Disabled, QPalette::Light, QColor(191, 191, 191)); |
618 |
pal.setColor(QPalette::Active, QPalette::Midlight, QColor(155, 155, 155)); |
619 |
pal.setColor(QPalette::Inactive, QPalette::Midlight, QColor(155, 155, 155)); |
620 |
pal.setColor(QPalette::Disabled, QPalette::Midlight, QColor(155, 155, 155)); |
621 |
pal.setColor(QPalette::Active, QPalette::Dark, QColor(129, 129, 129)); |
622 |
pal.setColor(QPalette::Inactive, QPalette::Dark, QColor(129, 129, 129)); |
623 |
pal.setColor(QPalette::Disabled, QPalette::Dark, QColor(129, 129, 129)); |
624 |
pal.setColor(QPalette::Active, QPalette::Mid, QColor(94, 94, 94)); |
625 |
pal.setColor(QPalette::Inactive, QPalette::Mid, QColor(94, 94, 94)); |
626 |
pal.setColor(QPalette::Disabled, QPalette::Mid, QColor(94, 94, 94)); |
627 |
pal.setColor(QPalette::Active, QPalette::Shadow, QColor(155, 155, 155)); |
628 |
pal.setColor(QPalette::Inactive, QPalette::Shadow, QColor(155, 155, 155)); |
629 |
pal.setColor(QPalette::Disabled, QPalette::Shadow, QColor(155, 155, 155)); |
630 |
pal.setColor(QPalette::Active, QPalette::Highlight, QColor(60, 60, 60)); |
631 |
pal.setColor(QPalette::Inactive, QPalette::Highlight, QColor(34, 34, 34)); |
632 |
pal.setColor(QPalette::Disabled, QPalette::Highlight, QColor(14, 14, 14)); |
633 |
pal.setColor(QPalette::Active, QPalette::HighlightedText, QColor(255, 255, 255)); |
634 |
pal.setColor(QPalette::Inactive, QPalette::HighlightedText, QColor(240, 240, 240)); |
635 |
pal.setColor(QPalette::Disabled, QPalette::HighlightedText, QColor(83, 83, 83)); |
636 |
pal.setColor(QPalette::Active, QPalette::Link, QColor(100, 100, 230)); |
637 |
pal.setColor(QPalette::Inactive, QPalette::Link, QColor(100, 100, 230)); |
638 |
pal.setColor(QPalette::Disabled, QPalette::Link, QColor(34, 34, 74)); |
639 |
pal.setColor(QPalette::Active, QPalette::LinkVisited, QColor(230, 100, 230)); |
640 |
pal.setColor(QPalette::Inactive, QPalette::LinkVisited, QColor(230, 100, 230)); |
641 |
pal.setColor(QPalette::Disabled, QPalette::LinkVisited, QColor(74, 34, 74)); |
642 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
643 |
mask = 0; |
644 |
#endif |
645 |
++result; |
646 |
} |
647 |
else |
648 |
if (settings) { |
649 |
settings->beginGroup(ColorThemesGroup); |
650 |
settings->beginGroup(name + '/'); |
651 |
QStringListIterator iter(settings->childKeys()); |
652 |
while (iter.hasNext()) { |
653 |
const QString& key = iter.next(); |
654 |
const QPalette::ColorRole cr |
655 |
= PaletteForm::colorRole(key); |
656 |
const QStringList& clist |
657 |
= settings->value(key).toStringList(); |
658 |
if (clist.count() == 3) { |
659 |
pal.setColor(QPalette::Active, cr, QColor(clist.at(0))); |
660 |
pal.setColor(QPalette::Inactive, cr, QColor(clist.at(1))); |
661 |
pal.setColor(QPalette::Disabled, cr, QColor(clist.at(2))); |
662 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
663 |
mask &= ~(1 << int(cr)); |
664 |
#endif |
665 |
++result; |
666 |
} |
667 |
} |
668 |
settings->endGroup(); |
669 |
settings->endGroup(); |
670 |
} |
671 |
|
672 |
// Dark themes grayed/disabled color group fix... |
673 |
if (!fixup && pal.base().color().value() < 0x7f) { |
674 |
const QColor& color = pal.window().color(); |
675 |
const int groups = int(QPalette::Active | QPalette::Inactive) + 1; |
676 |
for (int i = 0; i < groups; ++i) { |
677 |
const QPalette::ColorGroup cg = QPalette::ColorGroup(i); |
678 |
pal.setBrush(cg, QPalette::Light, color.lighter(140)); |
679 |
pal.setBrush(cg, QPalette::Midlight, color.lighter(100)); |
680 |
pal.setBrush(cg, QPalette::Mid, color.lighter(90)); |
681 |
pal.setBrush(cg, QPalette::Dark, color.darker(160)); |
682 |
pal.setBrush(cg, QPalette::Shadow, color.darker(180)); |
683 |
} |
684 |
pal.setColorGroup(QPalette::Disabled, |
685 |
pal.windowText().color().darker(), |
686 |
pal.button(), |
687 |
pal.light(), |
688 |
pal.dark(), |
689 |
pal.mid(), |
690 |
pal.text().color().darker(), |
691 |
pal.text().color().lighter(), |
692 |
pal.base(), |
693 |
pal.window()); |
694 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) |
695 |
pal.setColor(QPalette::Disabled, |
696 |
QPalette::Highlight, pal.mid().color()); |
697 |
pal.setColor(QPalette::Disabled, |
698 |
QPalette::ButtonText, pal.mid().color()); |
699 |
#endif |
700 |
++result; |
701 |
} |
702 |
|
703 |
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) |
704 |
pal.resolve(mask); |
705 |
#endif |
706 |
return (result > 0); |
707 |
} |
708 |
|
709 |
|
710 |
void PaletteForm::saveNamedPalette ( |
711 |
const QString& name, const QPalette& pal ) |
712 |
{ |
713 |
if (m_settings && name != "KXStudio" && name != "Wonton Soup") { |
714 |
m_settings->beginGroup(ColorThemesGroup); |
715 |
m_settings->beginGroup(name + '/'); |
716 |
for (int i = 0; g_colorRoles[i].key; ++i) { |
717 |
const QString& key |
718 |
= QString::fromLatin1(g_colorRoles[i].key); |
719 |
const QPalette::ColorRole cr |
720 |
= g_colorRoles[i].value; |
721 |
QStringList clist; |
722 |
clist.append(pal.color(QPalette::Active, cr).name()); |
723 |
clist.append(pal.color(QPalette::Inactive, cr).name()); |
724 |
clist.append(pal.color(QPalette::Disabled, cr).name()); |
725 |
m_settings->setValue(key, clist); |
726 |
} |
727 |
m_settings->endGroup(); |
728 |
m_settings->endGroup(); |
729 |
++m_dirtyTotal; |
730 |
} |
731 |
} |
732 |
|
733 |
|
734 |
void PaletteForm::deleteNamedPalette ( const QString& name ) |
735 |
{ |
736 |
if (m_settings) { |
737 |
m_settings->beginGroup(ColorThemesGroup); |
738 |
m_settings->remove(name); |
739 |
m_settings->endGroup(); |
740 |
++m_dirtyTotal; |
741 |
} |
742 |
} |
743 |
|
744 |
|
745 |
QStringList PaletteForm::namedPaletteList (void) |
746 |
{ |
747 |
return namedPaletteList(m_settings); |
748 |
} |
749 |
|
750 |
|
751 |
QStringList PaletteForm::namedPaletteList ( QSettings *settings ) |
752 |
{ |
753 |
QStringList list; |
754 |
list.append("Wonton Soup"); |
755 |
list.append("KXStudio"); |
756 |
|
757 |
if (settings) { |
758 |
settings->beginGroup(ColorThemesGroup); |
759 |
list.append(settings->childGroups()); |
760 |
settings->endGroup(); |
761 |
} |
762 |
|
763 |
return list; |
764 |
} |
765 |
|
766 |
|
767 |
QPalette::ColorRole PaletteForm::colorRole ( const QString& name ) |
768 |
{ |
769 |
static QHash<QString, QPalette::ColorRole> s_colorRoles; |
770 |
|
771 |
if (s_colorRoles.isEmpty()) { |
772 |
for (int i = 0; g_colorRoles[i].key; ++i) { |
773 |
const QString& key |
774 |
= QString::fromLatin1(g_colorRoles[i].key); |
775 |
const QPalette::ColorRole value |
776 |
= g_colorRoles[i].value; |
777 |
s_colorRoles.insert(key, value); |
778 |
} |
779 |
} |
780 |
|
781 |
return s_colorRoles.value(name, QPalette::NoRole); |
782 |
} |
783 |
|
784 |
|
785 |
bool PaletteForm::isDirty (void) const |
786 |
{ |
787 |
return (m_dirtyTotal > 0); |
788 |
} |
789 |
|
790 |
|
791 |
void PaletteForm::accept (void) |
792 |
{ |
793 |
setShowDetails(m_ui.detailsCheck->isChecked()); |
794 |
|
795 |
if (m_dirtyCount > 0) |
796 |
saveButtonClicked(); |
797 |
|
798 |
QDialog::accept(); |
799 |
} |
800 |
|
801 |
|
802 |
void PaletteForm::reject (void) |
803 |
{ |
804 |
if (m_dirtyCount > 0) { |
805 |
const QString& name = paletteName(); |
806 |
if (name.isEmpty()) { |
807 |
if (QMessageBox::warning(this, |
808 |
tr("Warning - %1").arg(QDialog::windowTitle()), |
809 |
tr("Some settings have been changed.\n\n" |
810 |
"Do you want to discard the changes?"), |
811 |
QMessageBox::Discard | |
812 |
QMessageBox::Cancel) == QMessageBox::Cancel) |
813 |
return; |
814 |
} else { |
815 |
switch (QMessageBox::warning(this, |
816 |
tr("Warning - %1").arg(QDialog::windowTitle()), |
817 |
tr("Some settings have been changed:\n\n" |
818 |
"\"%1\".\n\nDo you want to save the changes?") |
819 |
.arg(name), |
820 |
QMessageBox::Save | |
821 |
QMessageBox::Discard | |
822 |
QMessageBox::Cancel)) { |
823 |
case QMessageBox::Save: |
824 |
saveButtonClicked(); |
825 |
// Fall thru... |
826 |
case QMessageBox::Discard: |
827 |
break; |
828 |
default: // Cancel... |
829 |
return; |
830 |
} |
831 |
} |
832 |
} |
833 |
|
834 |
QDialog::reject(); |
835 |
} |
836 |
|
837 |
|
838 |
void PaletteForm::setDefaultDir ( const QString& dir ) |
839 |
{ |
840 |
if (m_settings) { |
841 |
m_settings->beginGroup(PaletteEditorGroup); |
842 |
m_settings->setValue(DefaultDirKey, dir); |
843 |
m_settings->endGroup(); |
844 |
} |
845 |
} |
846 |
|
847 |
|
848 |
QString PaletteForm::defaultDir (void) const |
849 |
{ |
850 |
QString dir; |
851 |
|
852 |
if (m_settings) { |
853 |
m_settings->beginGroup(PaletteEditorGroup); |
854 |
dir = m_settings->value(DefaultDirKey).toString(); |
855 |
m_settings->endGroup(); |
856 |
} |
857 |
|
858 |
return dir; |
859 |
} |
860 |
|
861 |
|
862 |
void PaletteForm::setShowDetails ( bool on ) |
863 |
{ |
864 |
if (m_settings) { |
865 |
m_settings->beginGroup(PaletteEditorGroup); |
866 |
m_settings->setValue(ShowDetailsKey, on); |
867 |
m_settings->endGroup(); |
868 |
} |
869 |
} |
870 |
|
871 |
|
872 |
bool PaletteForm::isShowDetails (void) const |
873 |
{ |
874 |
bool on = false; |
875 |
|
876 |
if (m_settings) { |
877 |
m_settings->beginGroup(PaletteEditorGroup); |
878 |
on = m_settings->value(ShowDetailsKey).toBool(); |
879 |
m_settings->endGroup(); |
880 |
} |
881 |
|
882 |
return on; |
883 |
} |
884 |
|
885 |
|
886 |
void PaletteForm::showEvent ( QShowEvent *event ) |
887 |
{ |
888 |
QDialog::showEvent(event); |
889 |
|
890 |
detailsCheckClicked(); |
891 |
} |
892 |
|
893 |
|
894 |
void PaletteForm::resizeEvent ( QResizeEvent *event ) |
895 |
{ |
896 |
QDialog::resizeEvent(event); |
897 |
|
898 |
detailsCheckClicked(); |
899 |
} |
900 |
|
901 |
|
902 |
//------------------------------------------------------------------------- |
903 |
// PaletteForm::PaletteModel |
904 |
|
905 |
PaletteForm::PaletteModel::PaletteModel ( QObject *parent ) |
906 |
: QAbstractTableModel(parent) |
907 |
{ |
908 |
for (m_nrows = 0; g_colorRoles[m_nrows].key; ++m_nrows) { |
909 |
const QPalette::ColorRole value |
910 |
= g_colorRoles[m_nrows].value; |
911 |
const QString& key |
912 |
= QString::fromLatin1(g_colorRoles[m_nrows].key); |
913 |
m_roleNames.insert(value, key); |
914 |
} |
915 |
|
916 |
m_generate = true; |
917 |
} |
918 |
|
919 |
|
920 |
int PaletteForm::PaletteModel::rowCount ( const QModelIndex& ) const |
921 |
{ |
922 |
return m_nrows; |
923 |
} |
924 |
|
925 |
|
926 |
int PaletteForm::PaletteModel::columnCount ( const QModelIndex& ) const |
927 |
{ |
928 |
return 4; |
929 |
} |
930 |
|
931 |
|
932 |
QVariant PaletteForm::PaletteModel::data ( const QModelIndex& index, int role ) const |
933 |
{ |
934 |
if (!index.isValid()) |
935 |
return QVariant(); |
936 |
if (index.row() < 0 || index.row() >= m_nrows) |
937 |
return QVariant(); |
938 |
if (index.column() < 0 || index.column() >= 4) |
939 |
return QVariant(); |
940 |
|
941 |
if (index.column() == 0) { |
942 |
if (role == Qt::DisplayRole) |
943 |
return m_roleNames.value(QPalette::ColorRole(index.row())); |
944 |
if (role == Qt::EditRole) { |
945 |
const uint mask = m_palette.resolve(); |
946 |
return bool(mask & (1 << index.row())); |
947 |
} |
948 |
} |
949 |
else |
950 |
if (role == Qt::BackgroundRole) { |
951 |
return m_palette.color( |
952 |
columnToGroup(index.column()), |
953 |
QPalette::ColorRole(index.row())); |
954 |
} |
955 |
|
956 |
return QVariant(); |
957 |
} |
958 |
|
959 |
|
960 |
bool PaletteForm::PaletteModel::setData ( |
961 |
const QModelIndex& index, const QVariant& value, int role ) |
962 |
{ |
963 |
if (!index.isValid()) |
964 |
return false; |
965 |
|
966 |
if (index.column() != 0 && role == Qt::BackgroundRole) { |
967 |
const QColor& color = value.value<QColor>(); |
968 |
const QPalette::ColorRole cr = QPalette::ColorRole(index.row()); |
969 |
const QPalette::ColorGroup cg = columnToGroup(index.column()); |
970 |
m_palette.setBrush(cg, cr, color); |
971 |
QModelIndex index_begin = PaletteModel::index(cr, 0); |
972 |
QModelIndex index_end = PaletteModel::index(cr, 3); |
973 |
if (m_generate) { |
974 |
m_palette.setBrush(QPalette::Inactive, cr, color); |
975 |
switch (cr) { |
976 |
case QPalette::Foreground: |
977 |
case QPalette::Text: |
978 |
case QPalette::ButtonText: |
979 |
case QPalette::Base: |
980 |
break; |
981 |
case QPalette::Dark: |
982 |
m_palette.setBrush(QPalette::Disabled, QPalette::Foreground, color); |
983 |
m_palette.setBrush(QPalette::Disabled, QPalette::Dark, color); |
984 |
m_palette.setBrush(QPalette::Disabled, QPalette::Text, color); |
985 |
m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, color); |
986 |
index_begin = PaletteModel::index(0, 0); |
987 |
index_end = PaletteModel::index(m_nrows - 1, 3); |
988 |
break; |
989 |
case QPalette::Background: |
990 |
m_palette.setBrush(QPalette::Disabled, QPalette::Base, color); |
991 |
m_palette.setBrush(QPalette::Disabled, QPalette::Background, color); |
992 |
index_begin = PaletteModel::index(QPalette::Base, 0); |
993 |
break; |
994 |
case QPalette::Highlight: |
995 |
m_palette.setBrush(QPalette::Disabled, QPalette::Highlight, color.darker(120)); |
996 |
break; |
997 |
default: |
998 |
m_palette.setBrush(QPalette::Disabled, cr, color); |
999 |
break; |
1000 |
} |
1001 |
} |
1002 |
emit paletteChanged(m_palette); |
1003 |
emit dataChanged(index_begin, index_end); |
1004 |
return true; |
1005 |
} |
1006 |
|
1007 |
if (index.column() == 0 && role == Qt::EditRole) { |
1008 |
uint mask = m_palette.resolve(); |
1009 |
const bool masked = value.value<bool>(); |
1010 |
const int i = index.row(); |
1011 |
if (masked) { |
1012 |
mask |= (1 << i); |
1013 |
} else { |
1014 |
const QPalette::ColorRole cr = QPalette::ColorRole(i); |
1015 |
m_palette.setBrush(QPalette::Active, cr, |
1016 |
m_parentPalette.brush(QPalette::Active, cr)); |
1017 |
m_palette.setBrush(QPalette::Inactive, cr, |
1018 |
m_parentPalette.brush(QPalette::Inactive, cr)); |
1019 |
m_palette.setBrush(QPalette::Disabled, cr, |
1020 |
m_parentPalette.brush(QPalette::Disabled, cr)); |
1021 |
mask &= ~(1 << i); |
1022 |
} |
1023 |
m_palette.resolve(mask); |
1024 |
emit paletteChanged(m_palette); |
1025 |
const QModelIndex& index_end = PaletteModel::index(i, 3); |
1026 |
emit dataChanged(index, index_end); |
1027 |
return true; |
1028 |
} |
1029 |
|
1030 |
return false; |
1031 |
} |
1032 |
|
1033 |
|
1034 |
Qt::ItemFlags PaletteForm::PaletteModel::flags ( const QModelIndex& index ) const |
1035 |
{ |
1036 |
if (!index.isValid()) |
1037 |
return Qt::ItemIsEnabled; |
1038 |
else |
1039 |
return Qt::ItemIsEditable | Qt::ItemIsEnabled; |
1040 |
} |
1041 |
|
1042 |
|
1043 |
QVariant PaletteForm::PaletteModel::headerData ( |
1044 |
int section, Qt::Orientation orientation, int role ) const |
1045 |
{ |
1046 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
1047 |
if (section == 0) |
1048 |
return tr("Color Role"); |
1049 |
else |
1050 |
if (section == groupToColumn(QPalette::Active)) |
1051 |
return tr("Active"); |
1052 |
else |
1053 |
if (section == groupToColumn(QPalette::Inactive)) |
1054 |
return tr("Inactive"); |
1055 |
else |
1056 |
if (section == groupToColumn(QPalette::Disabled)) |
1057 |
return tr("Disabled"); |
1058 |
} |
1059 |
|
1060 |
return QVariant(); |
1061 |
} |
1062 |
|
1063 |
|
1064 |
const QPalette& PaletteForm::PaletteModel::palette(void) const |
1065 |
{ |
1066 |
return m_palette; |
1067 |
} |
1068 |
|
1069 |
|
1070 |
void PaletteForm::PaletteModel::setPalette ( |
1071 |
const QPalette& palette, const QPalette& parentPalette ) |
1072 |
{ |
1073 |
m_palette = palette; |
1074 |
m_parentPalette = parentPalette; |
1075 |
|
1076 |
const QModelIndex& index_begin = index(0, 0); |
1077 |
const QModelIndex& index_end = index(m_nrows - 1, 3); |
1078 |
emit dataChanged(index_begin, index_end); |
1079 |
} |
1080 |
|
1081 |
|
1082 |
QPalette::ColorGroup PaletteForm::PaletteModel::columnToGroup ( int index ) const |
1083 |
{ |
1084 |
if (index == 1) |
1085 |
return QPalette::Active; |
1086 |
else |
1087 |
if (index == 2) |
1088 |
return QPalette::Inactive; |
1089 |
|
1090 |
return QPalette::Disabled; |
1091 |
} |
1092 |
|
1093 |
|
1094 |
int PaletteForm::PaletteModel::groupToColumn ( QPalette::ColorGroup group ) const |
1095 |
{ |
1096 |
if (group == QPalette::Active) |
1097 |
return 1; |
1098 |
else |
1099 |
if (group == QPalette::Inactive) |
1100 |
return 2; |
1101 |
|
1102 |
return 3; |
1103 |
} |
1104 |
|
1105 |
|
1106 |
//------------------------------------------------------------------------- |
1107 |
// QSampler::PaletteForm::ColorDelegate |
1108 |
|
1109 |
QWidget *PaletteForm::ColorDelegate::createEditor ( QWidget *parent, |
1110 |
const QStyleOptionViewItem&, const QModelIndex& index ) const |
1111 |
{ |
1112 |
QWidget *editor = nullptr; |
1113 |
|
1114 |
if (index.column() == 0) { |
1115 |
RoleEditor *ed = new RoleEditor(parent); |
1116 |
QObject::connect(ed, |
1117 |
SIGNAL(changed(QWidget *)), |
1118 |
SIGNAL(commitData(QWidget *))); |
1119 |
// ed->setFocusPolicy(Qt::NoFocus); |
1120 |
// ed->installEventFilter(const_cast<ColorDelegate *>(this)); |
1121 |
editor = ed; |
1122 |
} else { |
1123 |
ColorEditor *ed = new ColorEditor(parent); |
1124 |
QObject::connect(ed, |
1125 |
SIGNAL(changed(QWidget *)), |
1126 |
SIGNAL(commitData(QWidget *))); |
1127 |
ed->setFocusPolicy(Qt::NoFocus); |
1128 |
ed->installEventFilter(const_cast<ColorDelegate *>(this)); |
1129 |
editor = ed; |
1130 |
} |
1131 |
|
1132 |
return editor; |
1133 |
} |
1134 |
|
1135 |
|
1136 |
void PaletteForm::ColorDelegate::setEditorData ( |
1137 |
QWidget *editor, const QModelIndex& index ) const |
1138 |
{ |
1139 |
if (index.column() == 0) { |
1140 |
const bool masked |
1141 |
= index.model()->data(index, Qt::EditRole).value<bool>(); |
1142 |
RoleEditor *ed = static_cast<RoleEditor *>(editor); |
1143 |
ed->setEdited(masked); |
1144 |
const QString& colorName |
1145 |
= index.model()->data(index, Qt::DisplayRole).value<QString>(); |
1146 |
ed->setLabel(colorName); |
1147 |
} else { |
1148 |
const QColor& color |
1149 |
= index.model()->data(index, Qt::BackgroundRole).value<QColor>(); |
1150 |
ColorEditor *ed = static_cast<ColorEditor *>(editor); |
1151 |
ed->setColor(color); |
1152 |
} |
1153 |
} |
1154 |
|
1155 |
|
1156 |
void PaletteForm::ColorDelegate::setModelData ( QWidget *editor, |
1157 |
QAbstractItemModel *model, const QModelIndex& index ) const |
1158 |
{ |
1159 |
if (index.column() == 0) { |
1160 |
RoleEditor *ed = static_cast<RoleEditor *>(editor); |
1161 |
const bool masked = ed->edited(); |
1162 |
model->setData(index, masked, Qt::EditRole); |
1163 |
} else { |
1164 |
ColorEditor *ed = static_cast<ColorEditor *>(editor); |
1165 |
if (ed->changed()) { |
1166 |
const QColor& color = ed->color(); |
1167 |
model->setData(index, color, Qt::BackgroundRole); |
1168 |
} |
1169 |
} |
1170 |
} |
1171 |
|
1172 |
|
1173 |
void PaletteForm::ColorDelegate::updateEditorGeometry ( QWidget *editor, |
1174 |
const QStyleOptionViewItem& option, const QModelIndex& index ) const |
1175 |
{ |
1176 |
QItemDelegate::updateEditorGeometry(editor, option, index); |
1177 |
editor->setGeometry(editor->geometry().adjusted(0, 0, -1, -1)); |
1178 |
} |
1179 |
|
1180 |
|
1181 |
void PaletteForm::ColorDelegate::paint ( QPainter *painter, |
1182 |
const QStyleOptionViewItem& option, const QModelIndex& index ) const |
1183 |
{ |
1184 |
QStyleOptionViewItem opt = option; |
1185 |
|
1186 |
const bool masked |
1187 |
= index.model()->data(index, Qt::EditRole).value<bool>(); |
1188 |
if (index.column() == 0 && masked) |
1189 |
opt.font.setBold(true); |
1190 |
|
1191 |
QItemDelegate::paint(painter, opt, index); |
1192 |
|
1193 |
// painter->setPen(opt.palette.midlight().color()); |
1194 |
painter->setPen(Qt::darkGray); |
1195 |
painter->drawLine(opt.rect.right(), opt.rect.y(), |
1196 |
opt.rect.right(), opt.rect.bottom()); |
1197 |
painter->drawLine(opt.rect.x(), opt.rect.bottom(), |
1198 |
opt.rect.right(), opt.rect.bottom()); |
1199 |
} |
1200 |
|
1201 |
|
1202 |
QSize PaletteForm::ColorDelegate::sizeHint ( |
1203 |
const QStyleOptionViewItem& option, const QModelIndex &index) const |
1204 |
{ |
1205 |
return QItemDelegate::sizeHint(option, index) + QSize(4, 4); |
1206 |
} |
1207 |
|
1208 |
|
1209 |
//------------------------------------------------------------------------- |
1210 |
// QSampler::PaletteForm::ColorButton |
1211 |
|
1212 |
PaletteForm::ColorButton::ColorButton ( QWidget *parent ) |
1213 |
: QPushButton(parent), m_brush(Qt::darkGray) |
1214 |
{ |
1215 |
QPushButton::setMinimumWidth(48); |
1216 |
|
1217 |
QObject::connect(this, |
1218 |
SIGNAL(clicked()), |
1219 |
SLOT(chooseColor())); |
1220 |
} |
1221 |
|
1222 |
|
1223 |
const QBrush& PaletteForm::ColorButton::brush (void) const |
1224 |
{ |
1225 |
return m_brush; |
1226 |
} |
1227 |
|
1228 |
|
1229 |
void PaletteForm::ColorButton::setBrush ( const QBrush& brush ) |
1230 |
{ |
1231 |
m_brush = brush; |
1232 |
update(); |
1233 |
} |
1234 |
|
1235 |
|
1236 |
void PaletteForm::ColorButton::paintEvent ( QPaintEvent *event ) |
1237 |
{ |
1238 |
QPushButton::paintEvent(event); |
1239 |
|
1240 |
QStyleOptionButton opt; |
1241 |
opt.init(this); |
1242 |
|
1243 |
const QRect& rect |
1244 |
= style()->subElementRect(QStyle::SE_PushButtonContents, &opt, this); |
1245 |
|
1246 |
QPainter paint(this); |
1247 |
paint.setBrush(QBrush(m_brush.color())); |
1248 |
paint.drawRect(rect.adjusted(+1, +1, -2, -2)); |
1249 |
} |
1250 |
|
1251 |
|
1252 |
void PaletteForm::ColorButton::chooseColor (void) |
1253 |
{ |
1254 |
const QColor color |
1255 |
= QColorDialog::getColor(m_brush.color(), this); |
1256 |
if (color.isValid()) { |
1257 |
m_brush.setColor(color); |
1258 |
emit changed(); |
1259 |
} |
1260 |
} |
1261 |
|
1262 |
|
1263 |
//------------------------------------------------------------------------- |
1264 |
// QSampler::PaletteForm::ColorEditor |
1265 |
|
1266 |
PaletteForm::ColorEditor::ColorEditor ( QWidget *parent ) |
1267 |
: QWidget(parent) |
1268 |
{ |
1269 |
QLayout *layout = new QHBoxLayout(this); |
1270 |
layout->setMargin(0); |
1271 |
m_button = new PaletteForm::ColorButton(this); |
1272 |
layout->addWidget(m_button); |
1273 |
QObject::connect(m_button, |
1274 |
SIGNAL(changed()), |
1275 |
SLOT(colorChanged())); |
1276 |
setFocusProxy(m_button); |
1277 |
m_changed = false; |
1278 |
} |
1279 |
|
1280 |
|
1281 |
void PaletteForm::ColorEditor::setColor ( const QColor& color ) |
1282 |
{ |
1283 |
m_button->setBrush(color); |
1284 |
m_changed = false; |
1285 |
} |
1286 |
|
1287 |
|
1288 |
QColor PaletteForm::ColorEditor::color (void) const |
1289 |
{ |
1290 |
return m_button->brush().color(); |
1291 |
} |
1292 |
|
1293 |
|
1294 |
void PaletteForm::ColorEditor::colorChanged (void) |
1295 |
{ |
1296 |
m_changed = true; |
1297 |
emit changed(this); |
1298 |
} |
1299 |
|
1300 |
|
1301 |
bool PaletteForm::ColorEditor::changed (void) const |
1302 |
{ |
1303 |
return m_changed; |
1304 |
} |
1305 |
|
1306 |
|
1307 |
//------------------------------------------------------------------------- |
1308 |
// QSampler::PaletteForm::RoleEditor |
1309 |
|
1310 |
PaletteForm::RoleEditor::RoleEditor ( QWidget *parent ) |
1311 |
: QWidget(parent) |
1312 |
{ |
1313 |
m_edited = false; |
1314 |
|
1315 |
QHBoxLayout *layout = new QHBoxLayout(this); |
1316 |
layout->setMargin(0); |
1317 |
layout->setSpacing(0); |
1318 |
|
1319 |
m_label = new QLabel(this); |
1320 |
layout->addWidget(m_label); |
1321 |
m_label->setAutoFillBackground(true); |
1322 |
m_label->setIndent(3); // HACK: it should have the same value of textMargin in QItemDelegate |
1323 |
setFocusProxy(m_label); |
1324 |
|
1325 |
m_button = new QToolButton(this); |
1326 |
m_button->setToolButtonStyle(Qt::ToolButtonIconOnly); |
1327 |
m_button->setIcon(QPixmap(":/images/itemReset.png")); |
1328 |
m_button->setIconSize(QSize(8, 8)); |
1329 |
m_button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding)); |
1330 |
layout->addWidget(m_button); |
1331 |
|
1332 |
QObject::connect(m_button, |
1333 |
SIGNAL(clicked()), |
1334 |
SLOT(resetProperty())); |
1335 |
} |
1336 |
|
1337 |
|
1338 |
void PaletteForm::RoleEditor::setLabel ( const QString& label ) |
1339 |
{ |
1340 |
m_label->setText(label); |
1341 |
} |
1342 |
|
1343 |
|
1344 |
void PaletteForm::RoleEditor::setEdited ( bool on ) |
1345 |
{ |
1346 |
QFont font; |
1347 |
if (on) |
1348 |
font.setBold(on); |
1349 |
m_label->setFont(font); |
1350 |
m_button->setEnabled(on); |
1351 |
m_edited = on; |
1352 |
} |
1353 |
|
1354 |
|
1355 |
bool PaletteForm::RoleEditor::edited (void) const |
1356 |
{ |
1357 |
return m_edited; |
1358 |
} |
1359 |
|
1360 |
|
1361 |
void PaletteForm::RoleEditor::resetProperty (void) |
1362 |
{ |
1363 |
setEdited(false); |
1364 |
emit changed(this); |
1365 |
} |
1366 |
|
1367 |
|
1368 |
} // namespace QSampler |
1369 |
|
1370 |
// end of qsamplerPaletteForm.cpp |