6#include <qdesigner_utils_p.h>
7#include <iconloader_p.h>
8#include <qtcolorbutton_p.h>
10#include <private/formbuilderextra_p.h>
11#include <private/ui4_p.h>
13#include <QtDesigner/abstractformeditor.h>
14#include <QtDesigner/abstractformwindowmanager.h>
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>
25#include <QtGui/qaction.h>
26#if QT_CONFIG(clipboard)
27# include <QtGui/qclipboard.h>
29#include <QtGui/qguiapplication.h>
30#include <QtGui/qpainter.h>
31#include <QtGui/qscreen.h>
33#include <QtCore/qfile.h>
34#include <QtCore/qmetaobject.h>
35#include <QtCore/qsavefile.h>
36#include <QtCore/qxmlstream.h>
40using namespace Qt::StringLiterals;
46PaletteEditor::PaletteEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
48 m_paletteModel(
new PaletteModel(
this)),
52 auto saveButton = ui.buttonBox->addButton(tr(
"Save..."), QDialogButtonBox::ActionRole);
53 connect(saveButton, &QPushButton::clicked,
this, &PaletteEditor::save);
54 auto loadButton = ui.buttonBox->addButton(tr(
"Load..."), QDialogButtonBox::ActionRole);
55 connect(loadButton, &QPushButton::clicked,
this, &PaletteEditor::load);
57 connect(ui.buildButton, &QtColorButton::colorChanged,
58 this, &PaletteEditor::buildButtonColorChanged);
59 connect(ui.activeRadio, &QAbstractButton::clicked,
60 this, &PaletteEditor::activeRadioClicked);
61 connect(ui.inactiveRadio, &QAbstractButton::clicked,
62 this, &PaletteEditor::inactiveRadioClicked);
63 connect(ui.disabledRadio, &QAbstractButton::clicked,
64 this, &PaletteEditor::disabledRadioClicked);
65 connect(ui.computeRadio, &QAbstractButton::clicked,
66 this, &PaletteEditor::computeRadioClicked);
67 connect(ui.detailsRadio, &QAbstractButton::clicked,
68 this, &PaletteEditor::detailsRadioClicked);
70 ui.paletteView->setModel(m_paletteModel);
71 ui.previewGroupBox->setTitle(tr(
"Preview (%1)").arg(style()->objectName()));
72 updatePreviewPalette();
74 ui.paletteView->setModel(m_paletteModel);
76 ui.paletteView->setItemDelegate(delegate);
77 ui.paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
78 connect(m_paletteModel, &PaletteModel::paletteChanged,
79 this, &PaletteEditor::paletteChanged);
80 ui.paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
81 ui.paletteView->setDragEnabled(
true);
82 ui.paletteView->setDropIndicatorShown(
true);
83 ui.paletteView->setRootIsDecorated(
false);
84 ui.paletteView->setColumnHidden(2,
true);
85 ui.paletteView->setColumnHidden(3,
true);
86 ui.paletteView->setContextMenuPolicy(Qt::CustomContextMenu);
87 connect(ui.paletteView, &QWidget::customContextMenuRequested,
88 this, &PaletteEditor::viewContextMenuRequested);
90 const auto itemRect = ui.paletteView->visualRect(m_paletteModel->index(0, 0));
91 const int minHeight = qMin(itemRect.height() * QPalette::NColorRoles,
92 (screen()->geometry().height() * 2) / 3);
93 ui.paletteView->setMinimumSize({itemRect.width() * 4, minHeight});
100 return m_editPalette;
105 m_editPalette = palette;
106 for (
int r = 0; r <
static_cast<
int>(QPalette::NColorRoles); ++r) {
107 for (
int g = 0; g <
static_cast<
int>(QPalette::NColorGroups); ++g) {
108 const auto role =
static_cast<QPalette::ColorRole>(r);
109 const auto group =
static_cast<QPalette::ColorGroup>(g);
110 if (!palette.isBrushSet(group, role))
111 m_editPalette.setBrush(group, role, m_parentPalette.brush(group, role));
114 m_editPalette.setResolveMask(palette.resolveMask());
115 updatePreviewPalette();
116 updateStyledButton();
117 m_paletteUpdated =
true;
119 m_paletteModel->setPalette(m_editPalette, m_parentPalette);
120 m_paletteUpdated =
false;
125 m_parentPalette = parentPalette;
136 m_currentColorGroup = QPalette::Active;
137 updatePreviewPalette();
142 m_currentColorGroup = QPalette::Inactive;
143 updatePreviewPalette();
148 m_currentColorGroup = QPalette::Disabled;
149 updatePreviewPalette();
156 ui.paletteView->setColumnHidden(2,
true);
157 ui.paletteView->setColumnHidden(3,
true);
166 const int w = ui.paletteView->columnWidth(1);
167 ui.paletteView->setColumnHidden(2,
false);
168 ui.paletteView->setColumnHidden(3,
false);
169 QHeaderView *header = ui.paletteView->header();
170 header->resizeSection(1, w / 3);
171 header->resizeSection(2, w / 3);
172 header->resizeSection(3, w / 3);
179 m_modelUpdated =
true;
180 if (!m_paletteUpdated)
182 m_modelUpdated =
false;
187 const QColor btn = ui.buildButton->color();
188 const QPalette temp = QPalette(btn);
194 const QPalette::ColorGroup g = currentColorGroup();
196 const QPalette currentPalette = palette();
197 QPalette previewPalette;
198 for (
int i = QPalette::WindowText; i < QPalette::NColorRoles; i++) {
199 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole>(i);
200 const QBrush &br = currentPalette.brush(g, r);
201 previewPalette.setBrush(QPalette::Active, r, br);
202 previewPalette.setBrush(QPalette::Inactive, r, br);
203 previewPalette.setBrush(QPalette::Disabled, r, br);
205 ui.previewFrame->setPreviewPalette(previewPalette);
207 const bool enabled = g != QPalette::Disabled;
208 ui.previewFrame->setEnabled(enabled);
209 ui.previewFrame->setSubWindowActive(g != QPalette::Inactive);
214 ui.buildButton->setColor(palette().color(QPalette::Active, QPalette::Button));
217QPalette
PaletteEditor::getPalette(QDesignerFormEditorInterface *core, QWidget* parent,
const QPalette &init,
218 const QPalette &parentPal,
int *ok)
221 QPalette parentPalette(parentPal);
222 for (
int r = 0; r <
static_cast<
int>(QPalette::NColorRoles); ++r) {
223 for (
int g = 0; g <
static_cast<
int>(QPalette::NColorGroups); ++g) {
224 const auto role =
static_cast<QPalette::ColorRole>(r);
225 const auto group =
static_cast<QPalette::ColorGroup>(g);
226 if (!init.isBrushSet(group, role))
227 parentPalette.setBrush(group, role, init.brush(group, role));
230 dlg.setPalette(init, parentPalette);
232 const int result = dlg.exec();
233 if (ok) *ok = result;
235 return result == QDialog::Accepted ? dlg.palette() : init;
240 const auto index = ui.paletteView->indexAt(pos);
241 if (!index.isValid())
243 auto brush = m_paletteModel->brushAt(index);
244 const auto color = brush.color();
245 if (!m_contextMenu) {
246 m_contextMenu =
new QMenu(
this);
247 m_lighterAction = m_contextMenu->addAction(tr(
"Lighter"));
248 m_darkerAction = m_contextMenu->addAction(tr(
"Darker"));
249 m_copyColorAction = m_contextMenu->addAction(QString());
251 const auto rgb = color.rgb() & 0xffffffu;
252 const bool isBlack = rgb == 0u;
253 m_lighterAction->setEnabled(rgb != 0xffffffu);
254 m_darkerAction->setDisabled(isBlack);
255 m_copyColorAction->setText(tr(
"Copy color %1").arg(color.name()));
256 auto action = m_contextMenu->exec(ui.paletteView->viewport()->mapToGlobal(pos));
259 if (action == m_copyColorAction) {
260#if QT_CONFIG(clipboard)
261 QGuiApplication::clipboard()->setText(color.name());
267 enum :
int { factor = 120 };
268 const QColor newColor = action == m_darkerAction
269 ? color.darker(factor)
270 : (isBlack ? QColor(0x404040u) : color.lighter(factor));
271 brush.setColor(newColor);
272 m_paletteModel->setData(index, QVariant(brush),
BrushRole);
277 return PaletteEditor::tr(
"QPalette UI file (*.xml)");
280static bool savePalette(
const QString &fileName,
const QPalette &pal, QString *errorMessage)
283 file.setFileName(fileName);
284 if (!file.open(QIODevice::WriteOnly)) {
285 *errorMessage = PaletteEditor::tr(
"Cannot open %1 for writing: %2")
286 .arg(QDir::toNativeSeparators(fileName), file.errorString());
290 QScopedPointer<DomPalette> domPalette(QFormBuilderExtra::savePalette(pal));
291 QXmlStreamWriter writer(&file);
292 writer.setAutoFormatting(
true);
293 writer.setAutoFormattingIndent(1);
294 writer.writeStartDocument();
295 domPalette->write(writer);
296 writer.writeEndDocument();
298 const bool result = file.commit();
300 *errorMessage = PaletteEditor::tr(
"Cannot write %1: %2")
301 .arg(QDir::toNativeSeparators(fileName), file.errorString());
309 return PaletteEditor::tr(
"Cannot read palette from %1:%2:%3")
310 .arg(QDir::toNativeSeparators(fileName)).arg(reader.lineNumber()).arg(why);
315 return msgCannotReadPalette(fileName, reader, reader.errorString());
318static bool loadPalette(
const QString &fileName, QPalette *pal, QString *errorMessage)
320 QFile file(fileName);
321 if (!file.open(QIODevice::ReadOnly)) {
322 *errorMessage = PaletteEditor::tr(
"Cannot open %1 for reading: %2")
323 .arg(QDir::toNativeSeparators(fileName), file.errorString());
326 QXmlStreamReader reader(&file);
327 if (!reader.readNextStartElement()) {
328 *errorMessage = msgCannotReadPalette(fileName, reader);
331 if (reader.name() !=
"palette"_L1) {
332 const auto why = PaletteEditor::tr(
"Invalid element \"%1\", expected \"palette\".")
333 .arg(reader.name().toString());
334 *errorMessage = msgCannotReadPalette(fileName, reader, why);
337 QScopedPointer<DomPalette> domPalette(
new DomPalette);
338 domPalette->read(reader);
339 if (reader.hasError()) {
340 *errorMessage = msgCannotReadPalette(fileName, reader);
343 *pal = QFormBuilderExtra::loadPalette(domPalette.data());
349 QFileDialog dialog(
this, tr(
"Save Palette"), QString(), paletteFilter());
350 dialog.setAcceptMode(QFileDialog::AcceptSave);
351 dialog.setDefaultSuffix(u"xml"_s);
352 while (dialog.exec() == QDialog::Accepted) {
353 QString errorMessage;
354 if (savePalette(dialog.selectedFiles().constFirst(), palette(), &errorMessage))
356 QMessageBox::warning(
this, tr(
"Error Writing Palette"), errorMessage);
362 QFileDialog dialog(
this, tr(
"Load Palette"), QString(), paletteFilter());
363 dialog.setAcceptMode(QFileDialog::AcceptOpen);
364 while (dialog.exec() == QDialog::Accepted) {
366 QString errorMessage;
367 if (loadPalette(dialog.selectedFiles().constFirst(), &pal, &errorMessage)) {
371 QMessageBox::warning(
this, tr(
"Error Reading Palette"), errorMessage);
383 QAbstractTableModel(parent)
385 const QMetaObject *meta = metaObject();
386 const int index = meta->indexOfProperty(
"colorRole");
387 const QMetaProperty p = meta->property(index);
388 const QMetaEnum e = p.enumerator();
389 m_roleEntries.reserve(QPalette::NColorRoles);
390 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; r++) {
391 const auto role =
static_cast<QPalette::ColorRole>(r);
392 if (role != QPalette::NoRole)
393 m_roleEntries.append({QLatin1StringView(e.key(r)), role});
399 return m_roleEntries.size();
409 return m_palette.brush(columnToGroup(index.column()), roleAt(index.row()));
415 return paletteResolveMask(roleAt(index.row()));
420 if (!index.isValid())
422 if (index.row() < 0 || index.row() >= m_roleEntries.size())
424 if (index.column() < 0 || index.column() >= 4)
427 if (index.column() == 0) {
428 if (role == Qt::DisplayRole)
429 return m_roleEntries.at(index.row()).name;
430 if (role == Qt::EditRole)
431 return (rowMask(index) & m_palette.resolveMask()) != 0;
434 if (role == Qt::ToolTipRole)
435 return brushAt(index).color().name();
436 if (role == BrushRole)
437 return brushAt(index);
443 if (!index.isValid())
446 const int row = index.row();
447 const auto colorRole = roleAt(row);
449 if (index.column() != 0 && role ==
BrushRole) {
450 const QBrush br = qvariant_cast<QBrush>(value);
451 const QPalette::ColorGroup g = columnToGroup(index.column());
452 m_palette.setBrush(g, colorRole, br);
454 QModelIndex idxBegin = PaletteModel::index(row, 0);
455 QModelIndex idxEnd = PaletteModel::index(row, 3);
457 m_palette.setBrush(QPalette::Inactive, colorRole, br);
459 case QPalette::WindowText:
461 case QPalette::ButtonText:
465 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
466 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
467 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
468 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
469 idxBegin = PaletteModel::index(0, 0);
470 idxEnd = PaletteModel::index(m_roleEntries.size() - 1, 3);
472 case QPalette::Window:
473 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
474 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
475 idxBegin = PaletteModel::index(rowOf(QPalette::Base), 0);
477 case QPalette::Highlight:
481 m_palette.setBrush(QPalette::Disabled, colorRole, br);
485 emit paletteChanged(m_palette);
486 emit dataChanged(idxBegin, idxEnd);
489 if (index.column() == 0 && role == Qt::EditRole) {
490 auto mask = m_palette.resolveMask();
491 const bool isMask = qvariant_cast<
bool>(value);
492 const auto bitMask = rowMask(index);
496 m_palette.setBrush(QPalette::Active, colorRole,
497 m_parentPalette.brush(QPalette::Active, colorRole));
498 m_palette.setBrush(QPalette::Inactive, colorRole,
499 m_parentPalette.brush(QPalette::Inactive, colorRole));
500 m_palette.setBrush(QPalette::Disabled, colorRole,
501 m_parentPalette.brush(QPalette::Disabled, colorRole));
505 m_palette.setResolveMask(mask);
506 emit paletteChanged(m_palette);
507 const QModelIndex idxEnd = PaletteModel::index(row, 3);
508 emit dataChanged(index, idxEnd);
516 if (!index.isValid())
517 return Qt::ItemIsEnabled;
518 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
524 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
526 return tr(
"Color Role");
527 if (section == groupToColumn(QPalette::Active))
529 if (section == groupToColumn(QPalette::Inactive))
530 return tr(
"Inactive");
531 if (section == groupToColumn(QPalette::Disabled))
532 return tr(
"Disabled");
544 m_parentPalette = parentPalette;
546 const QModelIndex idxBegin = index(0, 0);
547 const QModelIndex idxEnd = index(m_roleEntries.size() - 1, 3);
548 emit dataChanged(idxBegin, idxEnd);
551QPalette::ColorGroup
PaletteModel::columnToGroup(
int index)
const
554 return QPalette::Active;
556 return QPalette::Inactive;
557 return QPalette::Disabled;
560int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
562 if (group == QPalette::Active)
564 if (group == QPalette::Inactive)
571 for (qsizetype row = 0, size = m_roleEntries.size(); row < size; ++row) {
572 if (m_roleEntries.at(row).role == role)
580BrushEditor::BrushEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
582 m_button(
new QtColorButton(
this)),
585 QLayout *layout =
new QHBoxLayout(
this);
586 layout->setContentsMargins(QMargins());
587 layout->addWidget(m_button);
588 connect(m_button, &QtColorButton::colorChanged,
this, &BrushEditor::brushChanged);
589 setFocusProxy(m_button);
594 m_button->setColor(brush.color());
600 return QBrush(m_button->color());
618 m_label(
new QLabel(
this))
620 QHBoxLayout *layout =
new QHBoxLayout(
this);
621 layout->setContentsMargins(QMargins());
622 layout->setSpacing(0);
624 layout->addWidget(m_label);
625 m_label->setAutoFillBackground(
true);
626 m_label->setIndent(3);
627 setFocusProxy(m_label);
629 QToolButton *button =
new QToolButton(
this);
630 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
631 button->setIcon(createIconSet(
"resetproperty.png"_L1));
632 button->setIconSize(QSize(8,8));
633 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
634 layout->addWidget(button);
635 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
640 m_label->setText(label);
648 m_label->setFont(font);
664ColorDelegate::ColorDelegate(QDesignerFormEditorInterface *core, QObject *parent) :
665 QItemDelegate(parent),
670QWidget *
ColorDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
671 const QModelIndex &index)
const
673 QWidget *ed =
nullptr;
674 if (index.column() == 0) {
676 connect(editor, &RoleEditor::changed,
this, &ColorDelegate::commitData);
682 connect(editor, QOverload<QWidget *>::of(&BrushEditor::changed),
683 this, &ColorDelegate::commitData);
684 editor->setFocusPolicy(Qt::NoFocus);
685 editor->installEventFilter(
const_cast<
ColorDelegate *>(
this));
691void ColorDelegate::setEditorData(QWidget *ed,
const QModelIndex &index)
const
693 if (index.column() == 0) {
694 const bool mask = qvariant_cast<
bool>(index.model()->data(index, Qt::EditRole));
697 const QString colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
698 editor->setLabel(colorName);
700 const QBrush br = qvariant_cast<QBrush>(index.model()->data(index, BrushRole));
702 editor->setBrush(br);
707 const QModelIndex &index)
const
709 if (index.column() == 0) {
712 model->setData(index, mask, Qt::EditRole);
716 QBrush br = editor->brush();
723 const QStyleOptionViewItem &option,
const QModelIndex &index)
const
725 QItemDelegate::updateEditorGeometry(ed, option, index);
726 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
730 const QModelIndex &index)
const
732 QStyleOptionViewItem option = opt;
733 const bool mask = qvariant_cast<
bool>(index.model()->data(index, Qt::EditRole));
734 if (index.column() == 0 && mask) {
735 option.font.setBold(
true);
737 QBrush br = qvariant_cast<QBrush>(index.model()->data(index, BrushRole));
738 if (br.style() == Qt::LinearGradientPattern ||
739 br.style() == Qt::RadialGradientPattern ||
740 br.style() == Qt::ConicalGradientPattern) {
742 painter->translate(option.rect.x(), option.rect.y());
743 painter->scale(option.rect.width(), option.rect.height());
744 QGradient gr = *(br.gradient());
745 gr.setCoordinateMode(QGradient::LogicalMode);
747 painter->fillRect(0, 0, 1, 1, br);
751 painter->setBrushOrigin(option.rect.x(), option.rect.y());
752 painter->fillRect(option.rect, br);
755 QItemDelegate::paint(painter, option, index);
758 const QColor color =
static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
759 const QPen oldPen = painter->pen();
760 painter->setPen(QPen(color));
762 painter->drawLine(option.rect.right(), option.rect.y(),
763 option.rect.right(), option.rect.bottom());
764 painter->drawLine(option.rect.x(), option.rect.bottom(),
765 option.rect.right(), option.rect.bottom());
766 painter->setPen(oldPen);
771 return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);
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.
QPalette getPalette() const
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)
Combined button and popup list for selecting options.
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)