4#include "ui_paletteeditor.h"
10#include <QMetaProperty>
24 , m_currentColorGroup(QPalette::Active)
26 , m_modelUpdated(false)
27 , m_paletteUpdated(false)
31 m_ui->paletteView->setModel(m_paletteModel);
32 updatePreviewPalette();
34 m_ui->paletteView->setModel(m_paletteModel);
36 m_ui->paletteView->setItemDelegate(delegate);
37 m_ui->paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
38 m_ui->paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
39 m_ui->paletteView->setDragEnabled(
true);
40 m_ui->paletteView->setDropIndicatorShown(
true);
41 m_ui->paletteView->setRootIsDecorated(
false);
42 m_ui->paletteView->setColumnHidden(2,
true);
43 m_ui->paletteView->setColumnHidden(3,
true);
45 auto saveButton = m_ui->buttonBox->addButton(tr(
"Save…"), QDialogButtonBox::ActionRole);
46 connect(saveButton, &QPushButton::clicked,
this, &PaletteEditor::save);
47 auto loadButton = m_ui->buttonBox->addButton(tr(
"Load…"), QDialogButtonBox::ActionRole);
48 connect(loadButton, &QPushButton::clicked,
this, &PaletteEditor::load);
52 connect(m_ui->computeRadio, &QRadioButton::clicked,
this, &PaletteEditor::handleComputeRadioClicked);
53 connect(m_ui->detailsRadio, &QRadioButton::clicked,
this, &PaletteEditor::handleDetailsRadioClicked);
69#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
75 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
76 if (mask & (1 << i)) {
79 m_editPalette.setBrush(
80 QPalette::Active,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
81 m_editPalette.setBrush(
82 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
83 m_editPalette.setBrush(
84 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
87#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
89 m_editPalette = m_editPalette.resolve(m_editPalette)
94 updatePreviewPalette();
96 m_paletteUpdated =
true;
97 if (!m_modelUpdated) {
98 m_paletteModel->
setPalette(m_editPalette, m_parentPalette);
100 m_paletteUpdated =
false;
105 m_parentPalette = parentPalette;
109void PaletteEditor::handleComputeRadioClicked()
114 m_ui->paletteView->setColumnHidden(2,
true);
115 m_ui->paletteView->setColumnHidden(3,
true);
120void PaletteEditor::handleDetailsRadioClicked()
125 const int w = m_ui->paletteView->columnWidth(1);
126 m_ui->paletteView->setColumnHidden(2,
false);
127 m_ui->paletteView->setColumnHidden(3,
false);
128 auto *
const header = m_ui->paletteView->header();
129 header->resizeSection(1, w / 3);
130 header->resizeSection(2, w / 3);
131 header->resizeSection(3, w / 3);
136static inline QString paletteSuffix()
138 return QStringLiteral(
"ini");
141static inline QString paletteFilter()
143 return PaletteEditor::tr(
"Color palette configuration (*.ini)");
146static bool loadPalette(
const QString &fileName, QPalette *pal, QString *errorMessage)
148 const auto settings = QSettings(fileName, QSettings::IniFormat);
149 if (settings.status() != QSettings::NoError) {
150 *errorMessage = PaletteEditor::tr(
"Unable to load \"%1\".").arg(fileName);
153 const auto value = settings.value(QStringLiteral(
"palette"));
154 if (!value.isValid() || !value.canConvert<QPalette>()) {
155 *errorMessage = PaletteEditor::tr(
"\"%1\" does not contain a valid palette.").arg(fileName);
158 *pal = settings.value(QStringLiteral(
"palette")).value<QPalette>();
162static bool savePalette(
const QString &fileName,
const QPalette &pal, QString *errorMessage)
164 auto settings = QSettings(fileName, QSettings::IniFormat);
165 settings.setValue(QStringLiteral(
"palette"), QVariant(pal));
167 if (settings.status() != QSettings::NoError) {
168 *errorMessage = PaletteEditor::tr(
"Unable to write \"%1\".").arg(fileName);
174void PaletteEditor::load()
176 auto dialog = QFileDialog(
this, tr(
"Load palette"), QString(), paletteFilter());
177 dialog.setAcceptMode(QFileDialog::AcceptOpen);
178 if (dialog.exec() != QDialog::Accepted) {
181 auto pal = QPalette();
182 auto errorMessage = QString();
183 if (loadPalette(dialog.selectedFiles().constFirst(), &pal, &errorMessage)) {
186 QMessageBox::warning(
this, tr(
"Error reading palette"), errorMessage);
190void PaletteEditor::save()
192 auto dialog = QFileDialog(
this, tr(
"Save palette"), QString(), paletteFilter());
193 dialog.setAcceptMode(QFileDialog::AcceptSave);
194 dialog.setDefaultSuffix(paletteSuffix());
195 if (dialog.exec() != QDialog::Accepted) {
198 auto errorMessage = QString();
199 if (!savePalette(dialog.selectedFiles().constFirst(),
palette(), &errorMessage)) {
200 QMessageBox::warning(
this, tr(
"Error writing palette"), errorMessage);
204void PaletteEditor::paletteChanged(
const QPalette &palette)
206 m_modelUpdated =
true;
207 if (!m_paletteUpdated) {
210 m_modelUpdated =
false;
213void PaletteEditor::buildPalette()
215 const QColor btn(m_ui->buildButton->color());
216 const QPalette temp(btn);
220void PaletteEditor::updatePreviewPalette()
222 const QPalette::ColorGroup g = currentColorGroup();
224 const QPalette currentPalette =
palette();
225 QPalette previewPalette;
226 for (
int i = QPalette::WindowText; i < QPalette::NColorRoles; ++i) {
227 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(i);
228 const QBrush br = currentPalette.brush(g, r);
229 previewPalette.setBrush(QPalette::Active, r, br);
230 previewPalette.setBrush(QPalette::Inactive, r, br);
231 previewPalette.setBrush(QPalette::Disabled, r, br);
235void PaletteEditor::updateStyledButton()
237 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
243 auto parentPalette(parentPal);
244 const auto mask = init.
245#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
251 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
252 if (mask & (1 << 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 if (mask & (1 << index.row()))
318 return m_palette.brush(columnToGroup(index.column()),
static_cast<QPalette::ColorRole
>(index.row()));
325 if (!index.isValid()) {
329 if (index.column() != 0 && role ==
BrushRole) {
330 const QBrush br = qvariant_cast<QBrush>(value);
331 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(index.row());
332 const QPalette::ColorGroup g = columnToGroup(index.column());
333 m_palette.setBrush(g, r, br);
335 QModelIndex idxBegin = PaletteModel::index(r, 0);
336 QModelIndex idxEnd = PaletteModel::index(r, 3);
338 m_palette.setBrush(QPalette::Inactive, r, br);
340 case QPalette::WindowText:
342 case QPalette::ButtonText:
346 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
347 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
348 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
349 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
350 idxBegin = PaletteModel::index(0, 0);
351 idxEnd = PaletteModel::index(
static_cast<int>(m_roleNames.count()) - 1, 3);
353 case QPalette::Window:
354 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
355 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
356 idxBegin = PaletteModel::index(QPalette::Base, 0);
358 case QPalette::Highlight:
361 m_palette.setBrush(QPalette::Disabled, r, br);
366 emit dataChanged(idxBegin, idxEnd);
369 if (index.column() == 0 && role == Qt::EditRole) {
370 auto mask = m_palette.
371#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
377 const bool isMask = qvariant_cast<bool>(value);
378 const int r = index.row();
383 QPalette::Active,
static_cast<QPalette::ColorRole
>(r), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(r)));
384 m_palette.setBrush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r),
385 m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r)));
386 m_palette.setBrush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r),
387 m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r)));
388 mask &= ~static_cast<decltype(mask)>(1 << index.row());
391#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
392 setResolveMask(mask);
393 m_palette = m_palette.resolve(m_palette)
399 const QModelIndex idxEnd = PaletteModel::index(r, 3);
400 emit dataChanged(index, idxEnd);
408 if (!index.isValid())
409 return Qt::ItemIsEnabled;
410 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
415 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
417 return tr(
"Color Role");
418 if (section == groupToColumn(QPalette::Active))
420 if (section == groupToColumn(QPalette::Inactive))
421 return tr(
"Inactive");
422 if (section == groupToColumn(QPalette::Disabled))
423 return tr(
"Disabled");
435 m_parentPalette = parentPalette;
437 const QModelIndex idxBegin = index(0, 0);
438 const QModelIndex idxEnd = index(
static_cast<int>(m_roleNames.count()) - 1, 3);
439 emit dataChanged(idxBegin, idxEnd);
442QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const
445 return QPalette::Active;
447 return QPalette::Inactive;
448 return QPalette::Disabled;
451int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
453 if (group == QPalette::Active)
455 if (group == QPalette::Inactive)
465 auto *
const layout =
new QHBoxLayout(
this);
466 layout->setContentsMargins(0, 0, 0, 0);
467 layout->addWidget(m_button);
469 setFocusProxy(m_button);
480 return QBrush(m_button->
color());
483void BrushEditor::brushChanged()
496 , m_label(new QLabel(this))
499 QHBoxLayout *layout =
new QHBoxLayout(
this);
500 layout->setContentsMargins(0, 0, 0, 0);
501 layout->setSpacing(0);
503 layout->addWidget(m_label);
504 m_label->setAutoFillBackground(
true);
505 m_label->setIndent(3);
506 setFocusProxy(m_label);
508 auto *
const button =
new QToolButton(
this);
509 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
510 button->setIcon(QIcon::fromTheme(QStringLiteral(
"edit-clear")));
511 button->setIconSize(QSize(8, 8));
512 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
513 layout->addWidget(button);
514 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
519 m_label->setText(label);
528 m_label->setFont(font);
537void RoleEditor::emitResetProperty()
544 : QItemDelegate(parent)
550 if (index.column() == 0) {
556 using BrushEditorWidgetSignal = void (
BrushEditor::*)(QWidget *);
559 connect(editor,
static_cast<BrushEditorWidgetSignal
>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
560 editor->setFocusPolicy(Qt::NoFocus);
561 editor->installEventFilter(
const_cast<ColorDelegate *
>(
this));
567 if (index.column() == 0) {
568 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
569 auto *
const editor =
static_cast<RoleEditor *
>(ed);
571 const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
572 editor->setLabel(colorName);
574 const auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
575 auto *
const editor =
static_cast<BrushEditor *
>(ed);
582 if (index.column() == 0) {
583 const auto *
const editor =
static_cast<RoleEditor *
>(ed);
584 const auto mask = editor->
edited();
585 model->setData(index, mask, Qt::EditRole);
587 const auto *
const editor =
static_cast<BrushEditor *
>(ed);
588 if (editor->changed()) {
589 QBrush br = editor->
brush();
597 QItemDelegate::updateEditorGeometry(ed, option, index);
598 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
603 QStyleOptionViewItem option = opt;
604 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
605 if (index.column() == 0 && mask) {
606 option.font.setBold(
true);
608 auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
609 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
611 painter->translate(option.rect.x(), option.rect.y());
612 painter->scale(option.rect.width(), option.rect.height());
613 QGradient gr = *(br.gradient());
614 gr.setCoordinateMode(QGradient::LogicalMode);
616 painter->fillRect(0, 0, 1, 1, br);
620 painter->setBrushOrigin(option.rect.x(), option.rect.y());
621 painter->fillRect(option.rect, br);
624 QItemDelegate::paint(painter, option, index);
626 const QColor color =
static_cast<QRgb
>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
627 const QPen oldPen = painter->pen();
628 painter->setPen(QPen(color));
630 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
631 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
632 painter->setPen(oldPen);
637 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)