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>
42using namespace Qt::StringLiterals;
48PaletteEditor::PaletteEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
50 m_paletteModel(
new PaletteModel(
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);
59 connect(ui.buildButton, &QtColorButton::colorChanged,
60 this, &PaletteEditor::buildButtonColorChanged);
61 connect(ui.activeRadio, &QAbstractButton::clicked,
63 connect(ui.inactiveRadio, &QAbstractButton::clicked,
65 connect(ui.disabledRadio, &QAbstractButton::clicked,
67 connect(ui.computeRadio, &QAbstractButton::clicked,
69 connect(ui.detailsRadio, &QAbstractButton::clicked,
72 ui.paletteView->setModel(m_paletteModel);
73 ui.previewGroupBox->setTitle(tr(
"Preview (%1)").arg(style()->objectName()));
74 updatePreviewPalette();
76 ui.paletteView->setModel(m_paletteModel);
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);
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});
102 return m_editPalette;
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));
116 m_editPalette.setResolveMask(palette.resolveMask());
117 updatePreviewPalette();
118 updateStyledButton();
119 m_paletteUpdated =
true;
121 m_paletteModel->setPalette(m_editPalette, m_parentPalette);
122 m_paletteUpdated =
false;
127 m_parentPalette = parentPalette;
138 m_currentColorGroup = QPalette::Active;
139 updatePreviewPalette();
144 m_currentColorGroup = QPalette::Inactive;
145 updatePreviewPalette();
150 m_currentColorGroup = QPalette::Disabled;
151 updatePreviewPalette();
158 ui.paletteView->setColumnHidden(2,
true);
159 ui.paletteView->setColumnHidden(3,
true);
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);
181 m_modelUpdated =
true;
182 if (!m_paletteUpdated)
184 m_modelUpdated =
false;
189 const QColor btn = ui.buildButton->color();
190 const QPalette temp = QPalette(btn);
196 const QPalette::ColorGroup g = currentColorGroup();
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);
207 ui.previewFrame->setPreviewPalette(previewPalette);
209 const bool enabled = g != QPalette::Disabled;
210 ui.previewFrame->setEnabled(enabled);
211 ui.previewFrame->setSubWindowActive(g != QPalette::Inactive);
216 ui.buildButton->setColor(palette().color(QPalette::Active, QPalette::Button));
219QPalette
PaletteEditor::getPalette(QDesignerFormEditorInterface *core, QWidget* parent,
const QPalette &init,
220 const QPalette &parentPal,
int *ok)
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));
232 dlg.setPalette(init, parentPalette);
234 const int result = dlg.exec();
235 if (ok) *ok = result;
237 return result == QDialog::Accepted ? dlg.palette() : init;
242 const auto index = ui.paletteView->indexAt(pos);
243 if (!index.isValid())
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());
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));
261 if (action == m_copyColorAction) {
262#if QT_CONFIG(clipboard)
263 QGuiApplication::clipboard()->setText(color.name());
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);
279 return PaletteEditor::tr(
"QPalette UI file (*.xml)");
282static bool savePalette(
const QString &fileName,
const QPalette &pal, QString *errorMessage)
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());
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();
300 const bool result = file.commit();
302 *errorMessage = PaletteEditor::tr(
"Cannot write %1: %2")
303 .arg(QDir::toNativeSeparators(fileName), file.errorString());
311 return PaletteEditor::tr(
"Cannot read palette from %1:%2:%3")
312 .arg(QDir::toNativeSeparators(fileName)).arg(reader.lineNumber()).arg(why);
317 return msgCannotReadPalette(fileName, reader, reader.errorString());
320static bool loadPalette(
const QString &fileName, QPalette *pal, QString *errorMessage)
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());
328 QXmlStreamReader reader(&file);
329 if (!reader.readNextStartElement()) {
330 *errorMessage = msgCannotReadPalette(fileName, reader);
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);
339 auto domPalette = std::make_unique<DomPalette>();
340 domPalette->read(reader);
341 if (reader.hasError()) {
342 *errorMessage = msgCannotReadPalette(fileName, reader);
345 *pal = QFormBuilderExtra::loadPalette(domPalette.get());
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))
358 QMessageBox::warning(
this, tr(
"Error Writing Palette"), errorMessage);
364 QFileDialog dialog(
this, tr(
"Load Palette"), QString(), paletteFilter());
365 dialog.setAcceptMode(QFileDialog::AcceptOpen);
366 while (dialog.exec() == QDialog::Accepted) {
368 QString errorMessage;
369 if (loadPalette(dialog.selectedFiles().constFirst(), &pal, &errorMessage)) {
373 QMessageBox::warning(
this, tr(
"Error Reading Palette"), errorMessage);
385 QAbstractTableModel(parent)
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});
401 return m_roleEntries.size();
411 return m_palette.brush(columnToGroup(index.column()), roleAt(index.row()));
417 return paletteResolveMask(roleAt(index.row()));
422 if (!index.isValid())
424 if (index.row() < 0 || index.row() >= m_roleEntries.size())
426 if (index.column() < 0 || index.column() >= 4)
429 if (index.column() == 0) {
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;
436 if (role == Qt::ToolTipRole)
437 return brushAt(index).color().name();
438 if (role == BrushRole)
439 return brushAt(index);
445 if (!index.isValid())
448 const int row = index.row();
449 const auto colorRole = roleAt(row);
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);
456 QModelIndex idxBegin = PaletteModel::index(row, 0);
457 QModelIndex idxEnd = PaletteModel::index(row, 3);
459 m_palette.setBrush(QPalette::Inactive, colorRole, br);
461 case QPalette::WindowText:
463 case QPalette::ButtonText:
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);
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);
479 case QPalette::Highlight:
483 m_palette.setBrush(QPalette::Disabled, colorRole, br);
487 emit paletteChanged(m_palette);
488 emit dataChanged(idxBegin, idxEnd);
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);
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));
507 m_palette.setResolveMask(mask);
508 emit paletteChanged(m_palette);
509 const QModelIndex idxEnd = PaletteModel::index(row, 3);
510 emit dataChanged(index, idxEnd);
518 if (!index.isValid())
519 return Qt::ItemIsEnabled;
520 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
526 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
528 return tr(
"Color Role");
529 if (section == groupToColumn(QPalette::Active))
531 if (section == groupToColumn(QPalette::Inactive))
532 return tr(
"Inactive");
533 if (section == groupToColumn(QPalette::Disabled))
534 return tr(
"Disabled");
546 m_parentPalette = parentPalette;
548 const QModelIndex idxBegin = index(0, 0);
549 const QModelIndex idxEnd = index(m_roleEntries.size() - 1, 3);
550 emit dataChanged(idxBegin, idxEnd);
553QPalette::ColorGroup
PaletteModel::columnToGroup(
int index)
const
556 return QPalette::Active;
558 return QPalette::Inactive;
559 return QPalette::Disabled;
562int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
564 if (group == QPalette::Active)
566 if (group == QPalette::Inactive)
573 for (qsizetype row = 0, size = m_roleEntries.size(); row < size; ++row) {
574 if (m_roleEntries.at(row).role == role)
582BrushEditor::BrushEditor(QDesignerFormEditorInterface *core, QWidget *parent) :
584 m_button(
new QtColorButton(
this)),
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);
596 m_button->setColor(brush.color());
602 return QBrush(m_button->color());
620 m_label(
new QLabel(
this))
622 QHBoxLayout *layout =
new QHBoxLayout(
this);
623 layout->setContentsMargins(QMargins());
624 layout->setSpacing(0);
626 layout->addWidget(m_label);
627 m_label->setAutoFillBackground(
true);
628 m_label->setIndent(3);
629 setFocusProxy(m_label);
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);
642 m_label->setText(label);
650 m_label->setFont(font);
666ColorDelegate::ColorDelegate(QDesignerFormEditorInterface *core, QObject *parent) :
667 QStyledItemDelegate(parent),
672QWidget *
ColorDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
673 const QModelIndex &index)
const
675 QWidget *ed =
nullptr;
676 if (index.column() == 0) {
678 connect(editor, &RoleEditor::changed,
this, &ColorDelegate::commitData);
684 connect(editor, QOverload<QWidget *>::of(&BrushEditor::changed),
685 this, &ColorDelegate::commitData);
686 editor->setFocusPolicy(Qt::NoFocus);
687 editor->installEventFilter(
const_cast<
ColorDelegate *>(
this));
693void ColorDelegate::setEditorData(QWidget *ed,
const QModelIndex &index)
const
695 if (index.column() == 0) {
696 const bool mask = qvariant_cast<
bool>(index.model()->data(index, Qt::EditRole));
699 const QString colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
700 editor->setLabel(colorName);
702 const QBrush br = qvariant_cast<QBrush>(index.model()->data(index, BrushRole));
704 editor->setBrush(br);
709 const QModelIndex &index)
const
711 if (index.column() == 0) {
714 model->setData(index, mask, Qt::EditRole);
718 QBrush br = editor->brush();
725 const QStyleOptionViewItem &option,
const QModelIndex &index)
const
727 QStyledItemDelegate::updateEditorGeometry(ed, option, index);
728 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
732 const QModelIndex &index)
const
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);
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) {
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);
749 painter->fillRect(0, 0, 1, 1, br);
753 painter->setBrushOrigin(option.rect.x(), option.rect.y());
754 painter->fillRect(option.rect, br);
757 QStyledItemDelegate::paint(painter, option, index);
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));
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);
773 return QStyledItemDelegate::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)
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)