4#include "ui_paletteeditor.h"
10#include <QMetaProperty>
26 , m_currentColorGroup(QPalette::Active)
28 , m_modelUpdated(false)
29 , m_paletteUpdated(false)
33 m_ui->paletteView->setModel(m_paletteModel);
34 updatePreviewPalette();
36 m_ui->paletteView->setModel(m_paletteModel);
38 m_ui->paletteView->setItemDelegate(delegate);
39 m_ui->paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
40 m_ui->paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
41 m_ui->paletteView->setDragEnabled(
true);
42 m_ui->paletteView->setDropIndicatorShown(
true);
43 m_ui->paletteView->setRootIsDecorated(
false);
44 m_ui->paletteView->setColumnHidden(2,
true);
45 m_ui->paletteView->setColumnHidden(3,
true);
47 auto saveButton = m_ui->buttonBox->addButton(tr(
"Save…"), QDialogButtonBox::ActionRole);
48 connect(saveButton, &QPushButton::clicked,
this, &PaletteEditor::save);
49 auto loadButton = m_ui->buttonBox->addButton(tr(
"Load…"), QDialogButtonBox::ActionRole);
50 connect(loadButton, &QPushButton::clicked,
this, &PaletteEditor::load);
54 connect(m_ui->computeRadio, &QRadioButton::clicked,
this, &PaletteEditor::handleComputeRadioClicked);
55 connect(m_ui->detailsRadio, &QRadioButton::clicked,
this, &PaletteEditor::handleDetailsRadioClicked);
71#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
77 using MaskType = std::remove_cv_t<
decltype(mask)>;
78 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
79 if (mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(i))) {
82 m_editPalette.setBrush(
83 QPalette::Active,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
84 m_editPalette.setBrush(
85 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
86 m_editPalette.setBrush(
87 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
90#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
92 m_editPalette = m_editPalette.resolve(m_editPalette)
97 updatePreviewPalette();
99 m_paletteUpdated =
true;
100 if (!m_modelUpdated) {
101 m_paletteModel->
setPalette(m_editPalette, m_parentPalette);
103 m_paletteUpdated =
false;
108 m_parentPalette = parentPalette;
112void PaletteEditor::handleComputeRadioClicked()
117 m_ui->paletteView->setColumnHidden(2,
true);
118 m_ui->paletteView->setColumnHidden(3,
true);
123void PaletteEditor::handleDetailsRadioClicked()
128 const int w = m_ui->paletteView->columnWidth(1);
129 m_ui->paletteView->setColumnHidden(2,
false);
130 m_ui->paletteView->setColumnHidden(3,
false);
131 auto *
const header = m_ui->paletteView->header();
132 header->resizeSection(1, w / 3);
133 header->resizeSection(2, w / 3);
134 header->resizeSection(3, w / 3);
139static inline QString paletteSuffix()
141 return QStringLiteral(
"ini");
144static inline QString paletteFilter()
146 return PaletteEditor::tr(
"Color palette configuration (*.ini)");
149static bool loadPalette(
const QString &fileName, QPalette *pal, QString *errorMessage)
151 const auto settings = QSettings(fileName, QSettings::IniFormat);
152 if (settings.status() != QSettings::NoError) {
153 *errorMessage = PaletteEditor::tr(
"Unable to load \"%1\".").arg(fileName);
156 const auto value = settings.value(QStringLiteral(
"palette"));
157 if (!value.isValid() || !value.canConvert<QPalette>()) {
158 *errorMessage = PaletteEditor::tr(
"\"%1\" does not contain a valid palette.").arg(fileName);
161 *pal = settings.value(QStringLiteral(
"palette")).value<QPalette>();
165static bool savePalette(
const QString &fileName,
const QPalette &pal, QString *errorMessage)
167 auto settings = QSettings(fileName, QSettings::IniFormat);
168 settings.setValue(QStringLiteral(
"palette"), QVariant(pal));
170 if (settings.status() != QSettings::NoError) {
171 *errorMessage = PaletteEditor::tr(
"Unable to write \"%1\".").arg(fileName);
177void PaletteEditor::load()
179 auto dialog = QFileDialog(
this, tr(
"Load palette"), QString(), paletteFilter());
180 dialog.setAcceptMode(QFileDialog::AcceptOpen);
181 if (dialog.exec() != QDialog::Accepted) {
184 auto pal = QPalette();
185 auto errorMessage = QString();
186 if (loadPalette(dialog.selectedFiles().constFirst(), &pal, &errorMessage)) {
189 QMessageBox::warning(
this, tr(
"Error reading palette"), errorMessage);
193void PaletteEditor::save()
195 auto dialog = QFileDialog(
this, tr(
"Save palette"), QString(), paletteFilter());
196 dialog.setAcceptMode(QFileDialog::AcceptSave);
197 dialog.setDefaultSuffix(paletteSuffix());
198 if (dialog.exec() != QDialog::Accepted) {
201 auto errorMessage = QString();
202 if (!savePalette(dialog.selectedFiles().constFirst(),
palette(), &errorMessage)) {
203 QMessageBox::warning(
this, tr(
"Error writing palette"), errorMessage);
207void PaletteEditor::paletteChanged(
const QPalette &palette)
209 m_modelUpdated =
true;
210 if (!m_paletteUpdated) {
213 m_modelUpdated =
false;
216void PaletteEditor::buildPalette()
218 const QColor btn(m_ui->buildButton->color());
219 const QPalette temp(btn);
223void PaletteEditor::updatePreviewPalette()
225 const QPalette::ColorGroup g = currentColorGroup();
227 const QPalette currentPalette =
palette();
228 QPalette previewPalette;
229 for (
int i = QPalette::WindowText; i < QPalette::NColorRoles; ++i) {
230 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(i);
231 const QBrush br = currentPalette.brush(g, r);
232 previewPalette.setBrush(QPalette::Active, r, br);
233 previewPalette.setBrush(QPalette::Inactive, r, br);
234 previewPalette.setBrush(QPalette::Disabled, r, br);
238void PaletteEditor::updateStyledButton()
240 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
246 auto parentPalette(parentPal);
247 const auto mask = init.
248#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
254 using MaskType = std::remove_cv_t<
decltype(mask)>;
255 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
256 if (mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(i))) {
259 parentPalette.setBrush(
260 QPalette::Active,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
261 parentPalette.setBrush(
262 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
263 parentPalette.setBrush(
264 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
268 const int result = dlg.exec();
272 return result == QDialog::Accepted ? dlg.
palette() : init;
276 : QAbstractTableModel(parent)
279 const QMetaObject *meta = metaObject();
280 const QMetaProperty
property = meta->property(meta->indexOfProperty(
"colorRole"));
281 const QMetaEnum enumerator =
property.enumerator();
282 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) {
283 m_roleNames[
static_cast<QPalette::ColorRole
>(r)] = QLatin1String(enumerator.key(r));
289 return static_cast<int>(m_roleNames.count());
299 if (!index.isValid() || index.row() < 0 || index.row() >= QPalette::NColorRoles || index.column() < 0 || index.column() >= 4) {
303 if (index.column() == 0) {
304 if (role == Qt::DisplayRole) {
305 return m_roleNames[
static_cast<QPalette::ColorRole
>(index.row())];
307 if (role == Qt::EditRole) {
308 const auto mask = m_palette.
309#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
315 using MaskType = std::remove_cv_t<
decltype(mask)>;
316 return mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(index.row()));
321 return m_palette.brush(columnToGroup(index.column()),
static_cast<QPalette::ColorRole
>(index.row()));
328 if (!index.isValid()) {
332 if (index.column() != 0 && role ==
BrushRole) {
333 const QBrush br = qvariant_cast<QBrush>(value);
334 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(index.row());
335 const QPalette::ColorGroup g = columnToGroup(index.column());
336 m_palette.setBrush(g, r, br);
338 QModelIndex idxBegin = PaletteModel::index(r, 0);
339 QModelIndex idxEnd = PaletteModel::index(r, 3);
341 m_palette.setBrush(QPalette::Inactive, r, br);
343 case QPalette::WindowText:
345 case QPalette::ButtonText:
349 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
350 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
351 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
352 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
353 idxBegin = PaletteModel::index(0, 0);
354 idxEnd = PaletteModel::index(
static_cast<int>(m_roleNames.count()) - 1, 3);
356 case QPalette::Window:
357 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
358 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
359 idxBegin = PaletteModel::index(QPalette::Base, 0);
361 case QPalette::Highlight:
364 m_palette.setBrush(QPalette::Disabled, r, br);
369 emit dataChanged(idxBegin, idxEnd);
372 if (index.column() == 0 && role == Qt::EditRole) {
373 auto mask = m_palette.
374#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
380 const bool isMask = qvariant_cast<bool>(value);
381 const int r = index.row();
383 mask |= (
static_cast<decltype(mask)
>(1) <<
static_cast<decltype(mask)
>(r));
386 QPalette::Active,
static_cast<QPalette::ColorRole
>(r), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(r)));
387 m_palette.setBrush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r),
388 m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r)));
389 m_palette.setBrush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r),
390 m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r)));
391 mask &= ~static_cast<decltype(mask)>((
static_cast<decltype(mask)
>(1) <<
static_cast<decltype(mask)
>(index.row())));
394#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
395 setResolveMask(mask);
396 m_palette = m_palette.resolve(m_palette)
402 const QModelIndex idxEnd = PaletteModel::index(r, 3);
403 emit dataChanged(index, idxEnd);
411 if (!index.isValid())
412 return Qt::ItemIsEnabled;
413 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
418 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
420 return tr(
"Color Role");
421 if (section == groupToColumn(QPalette::Active))
423 if (section == groupToColumn(QPalette::Inactive))
424 return tr(
"Inactive");
425 if (section == groupToColumn(QPalette::Disabled))
426 return tr(
"Disabled");
438 m_parentPalette = parentPalette;
440 const QModelIndex idxBegin = index(0, 0);
441 const QModelIndex idxEnd = index(
static_cast<int>(m_roleNames.count()) - 1, 3);
442 emit dataChanged(idxBegin, idxEnd);
445QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const
448 return QPalette::Active;
450 return QPalette::Inactive;
451 return QPalette::Disabled;
454int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
456 if (group == QPalette::Active)
458 if (group == QPalette::Inactive)
468 auto *
const layout =
new QHBoxLayout(
this);
469 layout->setContentsMargins(0, 0, 0, 0);
470 layout->addWidget(m_button);
472 setFocusProxy(m_button);
483 return QBrush(m_button->
color());
486void BrushEditor::brushChanged()
499 , m_label(new QLabel(this))
502 QHBoxLayout *layout =
new QHBoxLayout(
this);
503 layout->setContentsMargins(0, 0, 0, 0);
504 layout->setSpacing(0);
506 layout->addWidget(m_label);
507 m_label->setAutoFillBackground(
true);
508 m_label->setIndent(3);
509 setFocusProxy(m_label);
511 auto *
const button =
new QToolButton(
this);
512 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
513 button->setIcon(QIcon::fromTheme(QStringLiteral(
"edit-clear")));
514 button->setIconSize(QSize(8, 8));
515 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
516 layout->addWidget(button);
517 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
522 m_label->setText(label);
531 m_label->setFont(font);
540void RoleEditor::emitResetProperty()
547 : QItemDelegate(parent)
553 if (index.column() == 0) {
559 using BrushEditorWidgetSignal = void (
BrushEditor::*)(QWidget *);
562 connect(editor,
static_cast<BrushEditorWidgetSignal
>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
563 editor->setFocusPolicy(Qt::NoFocus);
564 editor->installEventFilter(
const_cast<ColorDelegate *
>(
this));
570 if (index.column() == 0) {
571 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
572 auto *
const editor =
static_cast<RoleEditor *
>(ed);
574 const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
575 editor->setLabel(colorName);
577 const auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
578 auto *
const editor =
static_cast<BrushEditor *
>(ed);
585 if (index.column() == 0) {
586 const auto *
const editor =
static_cast<RoleEditor *
>(ed);
587 const auto mask = editor->
edited();
588 model->setData(index, mask, Qt::EditRole);
590 const auto *
const editor =
static_cast<BrushEditor *
>(ed);
591 if (editor->changed()) {
592 QBrush br = editor->
brush();
600 QItemDelegate::updateEditorGeometry(ed, option, index);
601 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
606 QStyleOptionViewItem option = opt;
607 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
608 if (index.column() == 0 && mask) {
609 option.font.setBold(
true);
611 auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
612 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
614 painter->translate(option.rect.x(), option.rect.y());
615 painter->scale(option.rect.width(), option.rect.height());
616 QGradient gr = *(br.gradient());
617 gr.setCoordinateMode(QGradient::LogicalMode);
619 painter->fillRect(0, 0, 1, 1, br);
623 painter->setBrushOrigin(option.rect.x(), option.rect.y());
624 painter->fillRect(option.rect, br);
627 QItemDelegate::paint(painter, option, index);
629 const QColor color =
static_cast<QRgb
>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
630 const QPen oldPen = painter->pen();
631 painter->setPen(QPen(color));
633 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
634 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
635 painter->setPen(oldPen);
640 return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);
The BrushEditor class is used by PaletteEditor.
void setBrush(const QBrush &brush)
BrushEditor(QWidget *parent=nullptr)
The ColorDelegate class is used by PaletteEditor.
void setModelData(QWidget *ed, QAbstractItemModel *model, const QModelIndex &index) const override
void updateEditorGeometry(QWidget *ed, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setEditorData(QWidget *ed, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const override
ColorDelegate(QObject *parent=nullptr)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
The PaletteEditor class provides a dialog to customize a QPalette.
~PaletteEditor() override
static QPalette getPalette(QWidget *parent, const QPalette &init=QPalette(), const QPalette &parentPal=QPalette(), int *result=nullptr)
PaletteEditor(QWidget *parent)
void setPalette(const QPalette &palette)
The PaletteModel class is used by PaletteEditor.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QPalette getPalette() const
QVariant data(const QModelIndex &index, int role) const override
PaletteModel(QObject *parent=nullptr)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
void setPalette(const QPalette &palette, const QPalette &parentPalette)
void paletteChanged(const QPalette &palette)
The RoleEditor class is used by PaletteEditor.
void changed(QWidget *widget)
RoleEditor(QWidget *parent=nullptr)
void setLabel(const QString &label)