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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC