Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
paletteeditor.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <qdesigner_utils_p.h>
7#include <iconloader_p.h>
8#include <qtcolorbutton_p.h>
9
10#include <private/formbuilderextra_p.h>
11#include <private/ui4_p.h>
12
13#include <QtDesigner/abstractformeditor.h>
14#include <QtDesigner/abstractformwindowmanager.h>
15
16#include <QtWidgets/qfiledialog.h>
17#include <QtWidgets/qmessagebox.h>
18#include <QtWidgets/qpushbutton.h>
19#include <QtWidgets/qtoolbutton.h>
20#include <QtWidgets/qlabel.h>
21#include <QtWidgets/qmenu.h>
22#include <QtWidgets/qheaderview.h>
23#include <QtWidgets/qstyle.h>
24
25#include <QtGui/qaction.h>
26#if QT_CONFIG(clipboard)
27# include <QtGui/qclipboard.h>
28#endif
29#include <QtGui/qguiapplication.h>
30#include <QtGui/qpainter.h>
31#include <QtGui/qscreen.h>
32
33#include <QtCore/qfile.h>
34#include <QtCore/qmetaobject.h>
35#include <QtCore/qsavefile.h>
36#include <QtCore/qxmlstream.h>
37
38#include <memory>
39
40QT_BEGIN_NAMESPACE
41
42using namespace Qt::StringLiterals;
43
44namespace qdesigner_internal {
45
46enum { BrushRole = 33 };
47
48PaletteEditor::PaletteEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
49 QDialog(parent),
50 m_paletteModel(new PaletteModel(this)),
51 m_core(core)
52{
53 ui.setupUi(this);
54 auto saveButton = ui.buttonBox->addButton(tr("Save..."), QDialogButtonBox::ActionRole);
55 connect(saveButton, &QPushButton::clicked, this, &PaletteEditor::save);
56 auto loadButton = ui.buttonBox->addButton(tr("Load..."), QDialogButtonBox::ActionRole);
57 connect(loadButton, &QPushButton::clicked, this, &PaletteEditor::load);
58
59 connect(ui.buildButton, &QtColorButton::colorChanged,
60 this, &PaletteEditor::buildButtonColorChanged);
61 connect(ui.activeRadio, &QAbstractButton::clicked,
62 this, &PaletteEditor::activeRadioClicked);
63 connect(ui.inactiveRadio, &QAbstractButton::clicked,
64 this, &PaletteEditor::inactiveRadioClicked);
65 connect(ui.disabledRadio, &QAbstractButton::clicked,
66 this, &PaletteEditor::disabledRadioClicked);
67 connect(ui.computeRadio, &QAbstractButton::clicked,
68 this, &PaletteEditor::computeRadioClicked);
69 connect(ui.detailsRadio, &QAbstractButton::clicked,
70 this, &PaletteEditor::detailsRadioClicked);
71
72 ui.paletteView->setModel(m_paletteModel);
73 ui.previewGroupBox->setTitle(tr("Preview (%1)").arg(style()->objectName()));
74 updatePreviewPalette();
75 updateStyledButton();
76 ui.paletteView->setModel(m_paletteModel);
77 ColorDelegate *delegate = new ColorDelegate(core, this);
78 ui.paletteView->setItemDelegate(delegate);
79 ui.paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
80 connect(m_paletteModel, &PaletteModel::paletteChanged,
81 this, &PaletteEditor::paletteChanged);
82 ui.paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
83 ui.paletteView->setDragEnabled(true);
84 ui.paletteView->setDropIndicatorShown(true);
85 ui.paletteView->setRootIsDecorated(false);
86 ui.paletteView->setColumnHidden(2, true);
87 ui.paletteView->setColumnHidden(3, true);
88 ui.paletteView->setContextMenuPolicy(Qt::CustomContextMenu);
89 connect(ui.paletteView, &QWidget::customContextMenuRequested,
90 this, &PaletteEditor::viewContextMenuRequested);
91
92 const auto itemRect = ui.paletteView->visualRect(m_paletteModel->index(0, 0));
93 const int minHeight = qMin(itemRect.height() * QPalette::NColorRoles,
94 (screen()->geometry().height() * 2) / 3);
95 ui.paletteView->setMinimumSize({itemRect.width() * 4, minHeight});
96}
97
98PaletteEditor::~PaletteEditor() = default;
99
101{
102 return m_editPalette;
103}
104
105void PaletteEditor::setPalette(const QPalette &palette)
106{
107 m_editPalette = palette;
108 for (int r = 0; r < static_cast<int>(QPalette::NColorRoles); ++r) {
109 for (int g = 0; g < static_cast<int>(QPalette::NColorGroups); ++g) {
110 const auto role = static_cast<QPalette::ColorRole>(r);
111 const auto group = static_cast<QPalette::ColorGroup>(g);
112 if (!palette.isBrushSet(group, role))
113 m_editPalette.setBrush(group, role, m_parentPalette.brush(group, role));
114 }
115 }
116 m_editPalette.setResolveMask(palette.resolveMask());
117 updatePreviewPalette();
118 updateStyledButton();
119 m_paletteUpdated = true;
120 if (!m_modelUpdated)
121 m_paletteModel->setPalette(m_editPalette, m_parentPalette);
122 m_paletteUpdated = false;
123}
124
125void PaletteEditor::setPalette(const QPalette &palette, const QPalette &parentPalette)
126{
127 m_parentPalette = parentPalette;
128 setPalette(palette);
129}
130
131void PaletteEditor::buildButtonColorChanged()
132{
133 buildPalette();
134}
135
136void PaletteEditor::activeRadioClicked()
137{
138 m_currentColorGroup = QPalette::Active;
139 updatePreviewPalette();
140}
141
142void PaletteEditor::inactiveRadioClicked()
143{
144 m_currentColorGroup = QPalette::Inactive;
145 updatePreviewPalette();
146}
147
148void PaletteEditor::disabledRadioClicked()
149{
150 m_currentColorGroup = QPalette::Disabled;
151 updatePreviewPalette();
152}
153
154void PaletteEditor::computeRadioClicked()
155{
156 if (m_compute)
157 return;
158 ui.paletteView->setColumnHidden(2, true);
159 ui.paletteView->setColumnHidden(3, true);
160 m_compute = true;
161 m_paletteModel->setCompute(true);
162}
163
164void PaletteEditor::detailsRadioClicked()
165{
166 if (!m_compute)
167 return;
168 const int w = ui.paletteView->columnWidth(1);
169 ui.paletteView->setColumnHidden(2, false);
170 ui.paletteView->setColumnHidden(3, false);
171 QHeaderView *header = ui.paletteView->header();
172 header->resizeSection(1, w / 3);
173 header->resizeSection(2, w / 3);
174 header->resizeSection(3, w / 3);
175 m_compute = false;
176 m_paletteModel->setCompute(false);
177}
178
179void PaletteEditor::paletteChanged(const QPalette &palette)
180{
181 m_modelUpdated = true;
182 if (!m_paletteUpdated)
183 setPalette(palette);
184 m_modelUpdated = false;
185}
186
187void PaletteEditor::buildPalette()
188{
189 const QColor btn = ui.buildButton->color();
190 const QPalette temp = QPalette(btn);
191 setPalette(temp);
192}
193
194void PaletteEditor::updatePreviewPalette()
195{
196 const QPalette::ColorGroup g = currentColorGroup();
197 // build the preview palette
198 const QPalette currentPalette = palette();
199 QPalette previewPalette;
200 for (int i = QPalette::WindowText; i < QPalette::NColorRoles; i++) {
201 const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(i);
202 const QBrush &br = currentPalette.brush(g, r);
203 previewPalette.setBrush(QPalette::Active, r, br);
204 previewPalette.setBrush(QPalette::Inactive, r, br);
205 previewPalette.setBrush(QPalette::Disabled, r, br);
206 }
207 ui.previewFrame->setPreviewPalette(previewPalette);
208
209 const bool enabled = g != QPalette::Disabled;
210 ui.previewFrame->setEnabled(enabled);
211 ui.previewFrame->setSubWindowActive(g != QPalette::Inactive);
212}
213
214void PaletteEditor::updateStyledButton()
215{
216 ui.buildButton->setColor(palette().color(QPalette::Active, QPalette::Button));
217}
218
219QPalette PaletteEditor::getPalette(QDesignerFormEditorInterface *core, QWidget* parent, const QPalette &init,
220 const QPalette &parentPal, int *ok)
221{
222 PaletteEditor dlg(core, parent);
223 QPalette parentPalette(parentPal);
224 for (int r = 0; r < static_cast<int>(QPalette::NColorRoles); ++r) {
225 for (int g = 0; g < static_cast<int>(QPalette::NColorGroups); ++g) {
226 const auto role = static_cast<QPalette::ColorRole>(r);
227 const auto group = static_cast<QPalette::ColorGroup>(g);
228 if (!init.isBrushSet(group, role))
229 parentPalette.setBrush(group, role, init.brush(group, role));
230 }
231 }
232 dlg.setPalette(init, parentPalette);
233
234 const int result = dlg.exec();
235 if (ok) *ok = result;
236
237 return result == QDialog::Accepted ? dlg.palette() : init;
238}
239
240void PaletteEditor::viewContextMenuRequested(QPoint pos)
241{
242 const auto index = ui.paletteView->indexAt(pos);
243 if (!index.isValid())
244 return;
245 auto brush = m_paletteModel->brushAt(index);
246 const auto color = brush.color();
247 if (!m_contextMenu) {
248 m_contextMenu = new QMenu(this);
249 m_lighterAction = m_contextMenu->addAction(tr("Lighter"));
250 m_darkerAction = m_contextMenu->addAction(tr("Darker"));
251 m_copyColorAction = m_contextMenu->addAction(QString());
252 }
253 const auto rgb = color.rgb() & 0xffffffu;
254 const bool isBlack = rgb == 0u;
255 m_lighterAction->setEnabled(rgb != 0xffffffu);
256 m_darkerAction->setDisabled(isBlack);
257 m_copyColorAction->setText(tr("Copy color %1").arg(color.name()));
258 auto action = m_contextMenu->exec(ui.paletteView->viewport()->mapToGlobal(pos));
259 if (!action)
260 return;
261 if (action == m_copyColorAction) {
262#if QT_CONFIG(clipboard)
263 QGuiApplication::clipboard()->setText(color.name());
264#endif
265 return;
266 }
267 // Fall through to darker/lighter. Note: black cannot be made lighter due
268 // to QTBUG-9343.
269 enum : int { factor = 120 };
270 const QColor newColor = action == m_darkerAction
271 ? color.darker(factor)
272 : (isBlack ? QColor(0x404040u) : color.lighter(factor));
273 brush.setColor(newColor);
274 m_paletteModel->setData(index, QVariant(brush), BrushRole);
275}
276
277static inline QString paletteFilter()
278{
279 return PaletteEditor::tr("QPalette UI file (*.xml)");
280}
281
282static bool savePalette(const QString &fileName, const QPalette &pal, QString *errorMessage)
283{
284 QSaveFile file;
285 file.setFileName(fileName);
286 if (!file.open(QIODevice::WriteOnly)) {
287 *errorMessage = PaletteEditor::tr("Cannot open %1 for writing: %2")
288 .arg(QDir::toNativeSeparators(fileName), file.errorString());
289 return false;
290 }
291 {
292 std::unique_ptr<DomPalette> domPalette(QFormBuilderExtra::savePalette(pal));
293 QXmlStreamWriter writer(&file);
294 writer.setAutoFormatting(true);
295 writer.setAutoFormattingIndent(1);
296 writer.writeStartDocument();
297 domPalette->write(writer);
298 writer.writeEndDocument();
299 }
300 const bool result = file.commit();
301 if (!result) {
302 *errorMessage = PaletteEditor::tr("Cannot write %1: %2")
303 .arg(QDir::toNativeSeparators(fileName), file.errorString());
304 }
305 return result;
306}
307
308static QString msgCannotReadPalette(const QString &fileName, const QXmlStreamReader &reader,
309 const QString &why)
310{
311 return PaletteEditor::tr("Cannot read palette from %1:%2:%3")
312 .arg(QDir::toNativeSeparators(fileName)).arg(reader.lineNumber()).arg(why);
313}
314
315static inline QString msgCannotReadPalette(const QString &fileName, const QXmlStreamReader &reader)
316{
317 return msgCannotReadPalette(fileName, reader, reader.errorString());
318}
319
320static bool loadPalette(const QString &fileName, QPalette *pal, QString *errorMessage)
321{
322 QFile file(fileName);
323 if (!file.open(QIODevice::ReadOnly)) {
324 *errorMessage = PaletteEditor::tr("Cannot open %1 for reading: %2")
325 .arg(QDir::toNativeSeparators(fileName), file.errorString());
326 return false;
327 }
328 QXmlStreamReader reader(&file);
329 if (!reader.readNextStartElement()) {
330 *errorMessage = msgCannotReadPalette(fileName, reader);
331 return false;
332 }
333 if (reader.name() != "palette"_L1) {
334 const auto why = PaletteEditor::tr("Invalid element \"%1\", expected \"palette\".")
335 .arg(reader.name().toString());
336 *errorMessage = msgCannotReadPalette(fileName, reader, why);
337 return false;
338 }
339 auto domPalette = std::make_unique<DomPalette>();
340 domPalette->read(reader);
341 if (reader.hasError()) {
342 *errorMessage = msgCannotReadPalette(fileName, reader);
343 return false;
344 }
345 *pal = QFormBuilderExtra::loadPalette(domPalette.get());
346 return true;
347}
348
349void PaletteEditor::save()
350{
351 QFileDialog dialog(this, tr("Save Palette"), QString(), paletteFilter());
352 dialog.setAcceptMode(QFileDialog::AcceptSave);
353 dialog.setDefaultSuffix(u"xml"_s);
354 while (dialog.exec() == QDialog::Accepted) {
355 QString errorMessage;
356 if (savePalette(dialog.selectedFiles().constFirst(), palette(), &errorMessage))
357 break;
358 QMessageBox::warning(this, tr("Error Writing Palette"), errorMessage);
359 }
360}
361
362void PaletteEditor::load()
363{
364 QFileDialog dialog(this, tr("Load Palette"), QString(), paletteFilter());
365 dialog.setAcceptMode(QFileDialog::AcceptOpen);
366 while (dialog.exec() == QDialog::Accepted) {
367 QPalette pal;
368 QString errorMessage;
369 if (loadPalette(dialog.selectedFiles().constFirst(), &pal, &errorMessage)) {
370 setPalette(pal);
371 break;
372 }
373 QMessageBox::warning(this, tr("Error Reading Palette"), errorMessage);
374 }
375}
376
377//////////////////////
378// Column 0: Role name and reset button. Uses a boolean value indicating
379// whether the role is modified for the edit role.
380// Column 1: Color group Active
381// Column 2: Color group Inactive (visibility depending on m_compute/detail radio group)
382// Column 3: Color group Disabled
383
384PaletteModel::PaletteModel(QObject *parent) :
385 QAbstractTableModel(parent)
386{
387 const QMetaObject *meta = metaObject();
388 const int index = meta->indexOfProperty("colorRole");
389 const QMetaProperty p = meta->property(index);
390 const QMetaEnum e = p.enumerator();
391 m_roleEntries.reserve(QPalette::NColorRoles);
392 for (int r = QPalette::WindowText; r < QPalette::NColorRoles; r++) {
393 const auto role = static_cast<QPalette::ColorRole>(r);
394 if (role != QPalette::NoRole)
395 m_roleEntries.append({QLatin1StringView(e.key(r)), role});
396 }
397}
398
399int PaletteModel::rowCount(const QModelIndex &) const
400{
401 return m_roleEntries.size();
402}
403
404int PaletteModel::columnCount(const QModelIndex &) const
405{
406 return 4;
407}
408
409QBrush PaletteModel::brushAt(const QModelIndex &index) const
410{
411 return m_palette.brush(columnToGroup(index.column()), roleAt(index.row()));
412}
413
414// Palette resolve mask with all group bits for a row/role
415quint64 PaletteModel::rowMask(const QModelIndex &index) const
416{
417 return paletteResolveMask(roleAt(index.row()));
418}
419
420QVariant PaletteModel::data(const QModelIndex &index, int role) const
421{
422 if (!index.isValid())
423 return QVariant();
424 if (index.row() < 0 || index.row() >= m_roleEntries.size())
425 return QVariant();
426 if (index.column() < 0 || index.column() >= 4)
427 return QVariant();
428
429 if (index.column() == 0) { // Role name/bold print if changed
430 if (role == Qt::DisplayRole)
431 return m_roleEntries.at(index.row()).name;
432 if (role == Qt::EditRole)
433 return (rowMask(index) & m_palette.resolveMask()) != 0;
434 return QVariant();
435 }
436 if (role == Qt::ToolTipRole)
437 return brushAt(index).color().name();
438 if (role == BrushRole)
439 return brushAt(index);
440 return QVariant();
441}
442
443bool PaletteModel::setData(const QModelIndex &index, const QVariant &value, int role)
444{
445 if (!index.isValid())
446 return false;
447
448 const int row = index.row();
449 const auto colorRole = roleAt(row);
450
451 if (index.column() != 0 && role == BrushRole) {
452 const QBrush br = qvariant_cast<QBrush>(value);
453 const QPalette::ColorGroup g = columnToGroup(index.column());
454 m_palette.setBrush(g, colorRole, br);
455
456 QModelIndex idxBegin = PaletteModel::index(row, 0);
457 QModelIndex idxEnd = PaletteModel::index(row, 3);
458 if (m_compute) {
459 m_palette.setBrush(QPalette::Inactive, colorRole, br);
460 switch (colorRole) {
461 case QPalette::WindowText:
462 case QPalette::Text:
463 case QPalette::ButtonText:
464 case QPalette::Base:
465 break;
466 case QPalette::Dark:
467 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
468 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
469 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
470 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
471 idxBegin = PaletteModel::index(0, 0);
472 idxEnd = PaletteModel::index(m_roleEntries.size() - 1, 3);
473 break;
474 case QPalette::Window:
475 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
476 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
477 idxBegin = PaletteModel::index(rowOf(QPalette::Base), 0);
478 break;
479 case QPalette::Highlight:
480 //m_palette.setBrush(QPalette::Disabled, QPalette::Highlight, c.dark(120));
481 break;
482 default:
483 m_palette.setBrush(QPalette::Disabled, colorRole, br);
484 break;
485 }
486 }
487 emit paletteChanged(m_palette);
488 emit dataChanged(idxBegin, idxEnd);
489 return true;
490 }
491 if (index.column() == 0 && role == Qt::EditRole) {
492 auto mask = m_palette.resolveMask();
493 const bool isMask = qvariant_cast<bool>(value);
494 const auto bitMask = rowMask(index);
495 if (isMask) {
496 mask |= bitMask;
497 } else {
498 m_palette.setBrush(QPalette::Active, colorRole,
499 m_parentPalette.brush(QPalette::Active, colorRole));
500 m_palette.setBrush(QPalette::Inactive, colorRole,
501 m_parentPalette.brush(QPalette::Inactive, colorRole));
502 m_palette.setBrush(QPalette::Disabled, colorRole,
503 m_parentPalette.brush(QPalette::Disabled, colorRole));
504
505 mask &= ~bitMask;
506 }
507 m_palette.setResolveMask(mask);
508 emit paletteChanged(m_palette);
509 const QModelIndex idxEnd = PaletteModel::index(row, 3);
510 emit dataChanged(index, idxEnd);
511 return true;
512 }
513 return false;
514}
515
516Qt::ItemFlags PaletteModel::flags(const QModelIndex &index) const
517{
518 if (!index.isValid())
519 return Qt::ItemIsEnabled;
520 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
521}
522
523QVariant PaletteModel::headerData(int section, Qt::Orientation orientation,
524 int role) const
525{
526 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
527 if (section == 0)
528 return tr("Color Role");
529 if (section == groupToColumn(QPalette::Active))
530 return tr("Active");
531 if (section == groupToColumn(QPalette::Inactive))
532 return tr("Inactive");
533 if (section == groupToColumn(QPalette::Disabled))
534 return tr("Disabled");
535 }
536 return QVariant();
537}
538
540{
541 return m_palette;
542}
543
544void PaletteModel::setPalette(const QPalette &palette, const QPalette &parentPalette)
545{
546 m_parentPalette = parentPalette;
547 m_palette = palette;
548 const QModelIndex idxBegin = index(0, 0);
549 const QModelIndex idxEnd = index(m_roleEntries.size() - 1, 3);
550 emit dataChanged(idxBegin, idxEnd);
551}
552
553QPalette::ColorGroup PaletteModel::columnToGroup(int index) const
554{
555 if (index == 1)
556 return QPalette::Active;
557 if (index == 2)
558 return QPalette::Inactive;
559 return QPalette::Disabled;
560}
561
562int PaletteModel::groupToColumn(QPalette::ColorGroup group) const
563{
564 if (group == QPalette::Active)
565 return 1;
566 if (group == QPalette::Inactive)
567 return 2;
568 return 3;
569}
570
571int PaletteModel::rowOf(QPalette::ColorRole role) const
572{
573 for (qsizetype row = 0, size = m_roleEntries.size(); row < size; ++row) {
574 if (m_roleEntries.at(row).role == role)
575 return row;
576 }
577 return -1;
578}
579
580//////////////////////////
581
582BrushEditor::BrushEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
583 QWidget(parent),
584 m_button(new QtColorButton(this)),
585 m_core(core)
586{
587 QLayout *layout = new QHBoxLayout(this);
588 layout->setContentsMargins(QMargins());
589 layout->addWidget(m_button);
590 connect(m_button, &QtColorButton::colorChanged, this, &BrushEditor::brushChanged);
591 setFocusProxy(m_button);
592}
593
594void BrushEditor::setBrush(const QBrush &brush)
595{
596 m_button->setColor(brush.color());
597 m_changed = false;
598}
599
601{
602 return QBrush(m_button->color());
603}
604
605void BrushEditor::brushChanged()
606{
607 m_changed = true;
608 emit changed(this);
609}
610
611bool BrushEditor::changed() const
612{
613 return m_changed;
614}
615
616//////////////////////////
617
618RoleEditor::RoleEditor(QWidget *parent) :
619 QWidget(parent),
620 m_label(new QLabel(this))
621{
622 QHBoxLayout *layout = new QHBoxLayout(this);
623 layout->setContentsMargins(QMargins());
624 layout->setSpacing(0);
625
626 layout->addWidget(m_label);
627 m_label->setAutoFillBackground(true);
628 m_label->setIndent(3); // ### hardcode it should have the same value of textMargin in QItemDelegate
629 setFocusProxy(m_label);
630
631 QToolButton *button = new QToolButton(this);
632 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
633 button->setIcon(createIconSet("resetproperty.png"_L1));
634 button->setIconSize(QSize(8,8));
635 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
636 layout->addWidget(button);
637 connect(button, &QAbstractButton::clicked, this, &RoleEditor::emitResetProperty);
638}
639
640void RoleEditor::setLabel(const QString &label)
641{
642 m_label->setText(label);
643}
644
645void RoleEditor::setEdited(bool on)
646{
647 QFont font;
648 if (on)
649 font.setBold(on);
650 m_label->setFont(font);
651 m_edited = on;
652}
653
654bool RoleEditor::edited() const
655{
656 return m_edited;
657}
658
659void RoleEditor::emitResetProperty()
660{
661 setEdited(false);
662 emit changed(this);
663}
664
665//////////////////////////
666ColorDelegate::ColorDelegate(QDesignerFormEditorInterface *core, QObject *parent) :
667 QStyledItemDelegate(parent),
668 m_core(core)
669{
670}
671
672QWidget *ColorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &,
673 const QModelIndex &index) const
674{
675 QWidget *ed = nullptr;
676 if (index.column() == 0) {
677 RoleEditor *editor = new RoleEditor(parent);
678 connect(editor, &RoleEditor::changed, this, &ColorDelegate::commitData);
679 //editor->setFocusPolicy(Qt::NoFocus);
680 //editor->installEventFilter(const_cast<ColorDelegate *>(this));
681 ed = editor;
682 } else {
683 BrushEditor *editor = new BrushEditor(m_core, parent);
684 connect(editor, QOverload<QWidget *>::of(&BrushEditor::changed),
685 this, &ColorDelegate::commitData);
686 editor->setFocusPolicy(Qt::NoFocus);
687 editor->installEventFilter(const_cast<ColorDelegate *>(this));
688 ed = editor;
689 }
690 return ed;
691}
692
693void ColorDelegate::setEditorData(QWidget *ed, const QModelIndex &index) const
694{
695 if (index.column() == 0) {
696 const bool mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
697 RoleEditor *editor = static_cast<RoleEditor *>(ed);
698 editor->setEdited(mask);
699 const QString colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
700 editor->setLabel(colorName);
701 } else {
702 const QBrush br = qvariant_cast<QBrush>(index.model()->data(index, BrushRole));
703 BrushEditor *editor = static_cast<BrushEditor *>(ed);
704 editor->setBrush(br);
705 }
706}
707
708void ColorDelegate::setModelData(QWidget *ed, QAbstractItemModel *model,
709 const QModelIndex &index) const
710{
711 if (index.column() == 0) {
712 RoleEditor *editor = static_cast<RoleEditor *>(ed);
713 const bool mask = editor->edited();
714 model->setData(index, mask, Qt::EditRole);
715 } else {
716 BrushEditor *editor = static_cast<BrushEditor *>(ed);
717 if (editor->changed()) {
718 QBrush br = editor->brush();
719 model->setData(index, br, BrushRole);
720 }
721 }
722}
723
724void ColorDelegate::updateEditorGeometry(QWidget *ed,
725 const QStyleOptionViewItem &option, const QModelIndex &index) const
726{
727 QStyledItemDelegate::updateEditorGeometry(ed, option, index);
728 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
729}
730
731void ColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt,
732 const QModelIndex &index) const
733{
734 QStyleOptionViewItem option = opt;
735 const bool mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
736 if (index.column() == 0 && mask) {
737 option.font.setBold(true);
738 }
739 QBrush br = qvariant_cast<QBrush>(index.model()->data(index, BrushRole));
740 if (br.style() == Qt::LinearGradientPattern ||
741 br.style() == Qt::RadialGradientPattern ||
742 br.style() == Qt::ConicalGradientPattern) {
743 painter->save();
744 painter->translate(option.rect.x(), option.rect.y());
745 painter->scale(option.rect.width(), option.rect.height());
746 QGradient gr = *(br.gradient());
747 gr.setCoordinateMode(QGradient::LogicalMode);
748 br = QBrush(gr);
749 painter->fillRect(0, 0, 1, 1, br);
750 painter->restore();
751 } else {
752 painter->save();
753 painter->setBrushOrigin(option.rect.x(), option.rect.y());
754 painter->fillRect(option.rect, br);
755 painter->restore();
756 }
757 QStyledItemDelegate::paint(painter, option, index);
758
759
760 const QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
761 const QPen oldPen = painter->pen();
762 painter->setPen(QPen(color));
763
764 painter->drawLine(option.rect.right(), option.rect.y(),
765 option.rect.right(), option.rect.bottom());
766 painter->drawLine(option.rect.x(), option.rect.bottom(),
767 option.rect.right(), option.rect.bottom());
768 painter->setPen(oldPen);
769}
770
771QSize ColorDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const
772{
773 return QStyledItemDelegate::sizeHint(opt, index) + QSize(4, 4);
774}
775}
776
777QT_END_NAMESPACE
void setBrush(const QBrush &brush)
QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const override
This pure abstract function must be reimplemented if you want to provide custom rendering.
void setPalette(const QPalette &palette)
void setPalette(const QPalette &palette, const QPalette &parentPalette)
QBrush brushAt(const QModelIndex &index) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Returns the data for the given role and section in the header with the specified orientation.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns for the children of the given parent.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of rows under the given parent.
quint64 rowMask(const QModelIndex &index) const
QVariant data(const QModelIndex &index, int role) const override
Returns the data stored under the given role for the item referred to by the index.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Sets the role data for the item at index to value.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags for the given index.
void setPalette(const QPalette &palette, const QPalette &parentPalette)
Auxiliary methods to store/retrieve settings.
static QString paletteFilter()
static bool savePalette(const QString &fileName, const QPalette &pal, QString *errorMessage)
static QString msgCannotReadPalette(const QString &fileName, const QXmlStreamReader &reader)
static QString msgCannotReadPalette(const QString &fileName, const QXmlStreamReader &reader, const QString &why)
static bool loadPalette(const QString &fileName, QPalette *pal, QString *errorMessage)