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);
35 m_ui->paletteView->setModel(m_paletteModel);
37 m_ui->paletteView->setItemDelegate(delegate);
38 m_ui->paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
39 m_ui->paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
40 m_ui->paletteView->setDragEnabled(
true);
41 m_ui->paletteView->setDropIndicatorShown(
true);
42 m_ui->paletteView->setRootIsDecorated(
false);
43 m_ui->paletteView->setColumnHidden(2,
true);
44 m_ui->paletteView->setColumnHidden(3,
true);
46 auto saveButton = m_ui->buttonBox->addButton(tr(
"Save…"), QDialogButtonBox::ActionRole);
47 connect(saveButton, &QPushButton::clicked,
this, &PaletteEditor::save);
48 auto loadButton = m_ui->buttonBox->addButton(tr(
"Load…"), QDialogButtonBox::ActionRole);
49 connect(loadButton, &QPushButton::clicked,
this, &PaletteEditor::load);
53 connect(m_ui->computeRadio, &QRadioButton::clicked,
this, &PaletteEditor::handleComputeRadioClicked);
54 connect(m_ui->detailsRadio, &QRadioButton::clicked,
this, &PaletteEditor::handleDetailsRadioClicked);
70#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
76 using MaskType = std::remove_cv_t<
decltype(mask)>;
77 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
78 if (mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(i))) {
81 m_editPalette.setBrush(
82 QPalette::Active,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
83 m_editPalette.setBrush(
84 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
85 m_editPalette.setBrush(
86 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
89#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
91 m_editPalette = m_editPalette.resolve(m_editPalette)
97 m_paletteUpdated =
true;
98 if (!m_modelUpdated) {
99 m_paletteModel->
setPalette(m_editPalette, m_parentPalette);
101 m_paletteUpdated =
false;
106 m_parentPalette = parentPalette;
112 switch (
event->type()) {
113 case QEvent::LanguageChange:
114 m_ui->retranslateUi(
this);
118 return QDialog::event(
event);
121void PaletteEditor::handleComputeRadioClicked()
126 m_ui->paletteView->setColumnHidden(2,
true);
127 m_ui->paletteView->setColumnHidden(3,
true);
132void PaletteEditor::handleDetailsRadioClicked()
137 const int w = m_ui->paletteView->columnWidth(1);
138 m_ui->paletteView->setColumnHidden(2,
false);
139 m_ui->paletteView->setColumnHidden(3,
false);
140 auto *
const header = m_ui->paletteView->header();
141 header->resizeSection(1, w / 3);
142 header->resizeSection(2, w / 3);
143 header->resizeSection(3, w / 3);
148static inline QString paletteSuffix()
150 return QStringLiteral(
"ini");
153static inline QString paletteFilter()
155 return PaletteEditor::tr(
"Color palette configuration (*.ini)");
158static bool loadPalette(
const QString &fileName, QPalette *pal, QString *errorMessage)
160 const auto settings = QSettings(fileName, QSettings::IniFormat);
161 if (settings.status() != QSettings::NoError) {
162 *errorMessage = PaletteEditor::tr(
"Unable to load \"%1\".").arg(fileName);
165 const auto value = settings.value(QStringLiteral(
"palette"));
166 if (!value.isValid() || !value.canConvert<QPalette>()) {
167 *errorMessage = PaletteEditor::tr(
"\"%1\" does not contain a valid palette.").arg(fileName);
170 *pal = settings.value(QStringLiteral(
"palette")).value<QPalette>();
174static bool savePalette(
const QString &fileName,
const QPalette &pal, QString *errorMessage)
176 auto settings = QSettings(fileName, QSettings::IniFormat);
177 settings.setValue(QStringLiteral(
"palette"), QVariant(pal));
179 if (settings.status() != QSettings::NoError) {
180 *errorMessage = PaletteEditor::tr(
"Unable to write \"%1\".").arg(fileName);
186void PaletteEditor::load()
188 auto dialog = QFileDialog(
this, tr(
"Load palette"), QString(), paletteFilter());
189 dialog.setAcceptMode(QFileDialog::AcceptOpen);
190 if (dialog.exec() != QDialog::Accepted) {
193 auto pal = QPalette();
194 auto errorMessage = QString();
195 if (loadPalette(dialog.selectedFiles().constFirst(), &pal, &errorMessage)) {
200 QMessageBox::warning(
this, tr(
"Error reading palette"), errorMessage);
204void PaletteEditor::save()
206 auto dialog = QFileDialog(
this, tr(
"Save palette"), QString(), paletteFilter());
207 dialog.setAcceptMode(QFileDialog::AcceptSave);
208 dialog.setDefaultSuffix(paletteSuffix());
209 if (dialog.exec() != QDialog::Accepted) {
212 auto errorMessage = QString();
213 if (!savePalette(dialog.selectedFiles().constFirst(),
palette(), &errorMessage)) {
214 QMessageBox::warning(
this, tr(
"Error writing palette"), errorMessage);
218void PaletteEditor::paletteChanged(
const QPalette &palette)
220 m_modelUpdated =
true;
221 if (!m_paletteUpdated) {
224 m_modelUpdated =
false;
227void PaletteEditor::buildPalette()
229 const QColor btn(m_ui->buildButton->color());
230 const QPalette temp(btn);
234void PaletteEditor::updateStyledButton()
236 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
242 auto parentPalette(parentPal);
243 const auto mask = init.
244#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
250 using MaskType = std::remove_cv_t<
decltype(mask)>;
251 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
252 if (mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(i))) {
255 parentPalette.setBrush(
256 QPalette::Active,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
257 parentPalette.setBrush(
258 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
259 parentPalette.setBrush(
260 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
264 const int result = dlg.exec();
268 return result == QDialog::Accepted ? dlg.
palette() : init;
272 : QAbstractTableModel(parent)
275 const QMetaObject *meta = metaObject();
276 const QMetaProperty
property = meta->property(meta->indexOfProperty(
"colorRole"));
277 const QMetaEnum enumerator =
property.enumerator();
278 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) {
279 m_roleNames[
static_cast<QPalette::ColorRole
>(r)] = QLatin1String(enumerator.key(r));
285 return static_cast<int>(m_roleNames.count());
295 if (!index.isValid() || index.row() < 0 || index.row() >= QPalette::NColorRoles || index.column() < 0 || index.column() >= 4) {
299 if (index.column() == 0) {
300 if (role == Qt::DisplayRole) {
301 return m_roleNames[
static_cast<QPalette::ColorRole
>(index.row())];
303 if (role == Qt::EditRole) {
304 const auto mask = m_palette.
305#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
311 using MaskType = std::remove_cv_t<
decltype(mask)>;
312 return mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(index.row()));
317 return m_palette.brush(columnToGroup(index.column()),
static_cast<QPalette::ColorRole
>(index.row()));
324 if (!index.isValid()) {
328 if (index.column() != 0 && role ==
BrushRole) {
329 const QBrush br = qvariant_cast<QBrush>(value);
330 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(index.row());
331 const QPalette::ColorGroup g = columnToGroup(index.column());
332 m_palette.setBrush(g, r, br);
334 QModelIndex idxBegin = PaletteModel::index(r, 0);
335 QModelIndex idxEnd = PaletteModel::index(r, 3);
337 m_palette.setBrush(QPalette::Inactive, r, br);
339 case QPalette::WindowText:
341 case QPalette::ButtonText:
345 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
346 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
347 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
348 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
349 idxBegin = PaletteModel::index(0, 0);
350 idxEnd = PaletteModel::index(
static_cast<int>(m_roleNames.count()) - 1, 3);
352 case QPalette::Window:
353 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
354 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
355 idxBegin = PaletteModel::index(QPalette::Base, 0);
357 case QPalette::Highlight:
360 m_palette.setBrush(QPalette::Disabled, r, br);
365 emit dataChanged(idxBegin, idxEnd);
368 if (index.column() == 0 && role == Qt::EditRole) {
369 auto mask = m_palette.
370#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
376 const bool isMask = qvariant_cast<bool>(value);
377 const int r = index.row();
379 mask |= (
static_cast<decltype(mask)
>(1) <<
static_cast<decltype(mask)
>(r));
382 QPalette::Active,
static_cast<QPalette::ColorRole
>(r), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(r)));
383 m_palette.setBrush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r),
384 m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r)));
385 m_palette.setBrush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r),
386 m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r)));
387 mask &= ~static_cast<decltype(mask)>((
static_cast<decltype(mask)
>(1) <<
static_cast<decltype(mask)
>(index.row())));
390#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
391 setResolveMask(mask);
392 m_palette = m_palette.resolve(m_palette)
398 const QModelIndex idxEnd = PaletteModel::index(r, 3);
399 emit dataChanged(index, idxEnd);
407 if (!index.isValid())
408 return Qt::ItemIsEnabled;
409 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
414 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
416 return tr(
"Color Role");
417 if (section == groupToColumn(QPalette::Active))
419 if (section == groupToColumn(QPalette::Inactive))
420 return tr(
"Inactive");
421 if (section == groupToColumn(QPalette::Disabled))
422 return tr(
"Disabled");
434 m_parentPalette = parentPalette;
436 const QModelIndex idxBegin = index(0, 0);
437 const QModelIndex idxEnd = index(
static_cast<int>(m_roleNames.count()) - 1, 3);
438 emit dataChanged(idxBegin, idxEnd);
441QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const
444 return QPalette::Active;
446 return QPalette::Inactive;
447 return QPalette::Disabled;
450int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
452 if (group == QPalette::Active)
454 if (group == QPalette::Inactive)
464 auto *
const layout =
new QHBoxLayout(
this);
465 layout->setContentsMargins(0, 0, 0, 0);
466 layout->addWidget(m_button);
468 setFocusProxy(m_button);
479 return QBrush(m_button->
color());
482void BrushEditor::brushChanged()
495 , m_label(new QLabel(this))
498 QHBoxLayout *layout =
new QHBoxLayout(
this);
499 layout->setContentsMargins(0, 0, 0, 0);
500 layout->setSpacing(0);
502 layout->addWidget(m_label);
503 m_label->setAutoFillBackground(
true);
504 m_label->setIndent(3);
505 setFocusProxy(m_label);
507 auto *
const button =
new QToolButton(
this);
508 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
509 button->setIcon(QIcon::fromTheme(QStringLiteral(
"edit-clear")));
510 button->setIconSize(QSize(8, 8));
511 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
512 layout->addWidget(button);
513 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
518 m_label->setText(label);
527 m_label->setFont(font);
536void RoleEditor::emitResetProperty()
543 : QItemDelegate(parent)
549 if (index.column() == 0) {
555 using BrushEditorWidgetSignal = void (
BrushEditor::*)(QWidget *);
558 connect(editor,
static_cast<BrushEditorWidgetSignal
>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
559 editor->setFocusPolicy(Qt::NoFocus);
560 editor->installEventFilter(
const_cast<ColorDelegate *
>(
this));
566 if (index.column() == 0) {
567 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
568 auto *
const editor =
static_cast<RoleEditor *
>(ed);
570 const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
571 editor->setLabel(colorName);
573 const auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
574 auto *
const editor =
static_cast<BrushEditor *
>(ed);
581 if (index.column() == 0) {
582 const auto *
const editor =
static_cast<RoleEditor *
>(ed);
583 const auto mask = editor->
edited();
584 model->setData(index, mask, Qt::EditRole);
586 const auto *
const editor =
static_cast<BrushEditor *
>(ed);
587 if (editor->changed()) {
588 QBrush br = editor->brush();
596 QItemDelegate::updateEditorGeometry(ed, option, index);
597 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
602 QStyleOptionViewItem option = opt;
603 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
604 if (index.column() == 0 && mask) {
605 option.font.setBold(
true);
607 auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
608 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
610 painter->translate(option.rect.x(), option.rect.y());
611 painter->scale(option.rect.width(), option.rect.height());
612 QGradient gr = *(br.gradient());
613 gr.setCoordinateMode(QGradient::LogicalMode);
615 painter->fillRect(0, 0, 1, 1, br);
619 painter->setBrushOrigin(option.rect.x(), option.rect.y());
620 painter->fillRect(option.rect, br);
623 QItemDelegate::paint(painter, option, index);
625 const QColor color =
static_cast<QRgb
>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
626 const QPen oldPen = painter->pen();
627 painter->setPen(QPen(color));
629 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
630 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
631 painter->setPen(oldPen);
636 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)
bool event(QEvent *event) override
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)