/[svn]/qsampler/trunk/src/qsamplerPaletteForm.cpp
ViewVC logotype

Contents of /qsampler/trunk/src/qsamplerPaletteForm.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC