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;
110void PaletteEditor::handleComputeRadioClicked()
115 m_ui->paletteView->setColumnHidden(2,
true);
116 m_ui->paletteView->setColumnHidden(3,
true);
121void PaletteEditor::handleDetailsRadioClicked()
126 const int w = m_ui->paletteView->columnWidth(1);
127 m_ui->paletteView->setColumnHidden(2,
false);
128 m_ui->paletteView->setColumnHidden(3,
false);
129 auto *
const header = m_ui->paletteView->header();
130 header->resizeSection(1, w / 3);
131 header->resizeSection(2, w / 3);
132 header->resizeSection(3, w / 3);
137static inline QString paletteSuffix()
139 return QStringLiteral(
"ini");
142static inline QString paletteFilter()
144 return PaletteEditor::tr(
"Color palette configuration (*.ini)");
147static bool loadPalette(
const QString &fileName, QPalette *pal, QString *errorMessage)
149 const auto settings = QSettings(fileName, QSettings::IniFormat);
150 if (settings.status() != QSettings::NoError) {
151 *errorMessage = PaletteEditor::tr(
"Unable to load \"%1\".").arg(fileName);
154 const auto value = settings.value(QStringLiteral(
"palette"));
155 if (!value.isValid() || !value.canConvert<QPalette>()) {
156 *errorMessage = PaletteEditor::tr(
"\"%1\" does not contain a valid palette.").arg(fileName);
159 *pal = settings.value(QStringLiteral(
"palette")).value<QPalette>();
163static bool savePalette(
const QString &fileName,
const QPalette &pal, QString *errorMessage)
165 auto settings = QSettings(fileName, QSettings::IniFormat);
166 settings.setValue(QStringLiteral(
"palette"), QVariant(pal));
168 if (settings.status() != QSettings::NoError) {
169 *errorMessage = PaletteEditor::tr(
"Unable to write \"%1\".").arg(fileName);
175void PaletteEditor::load()
177 auto dialog = QFileDialog(
this, tr(
"Load palette"), QString(), paletteFilter());
178 dialog.setAcceptMode(QFileDialog::AcceptOpen);
179 if (dialog.exec() != QDialog::Accepted) {
182 auto pal = QPalette();
183 auto errorMessage = QString();
184 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::updateStyledButton()
225 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
231 auto parentPalette(parentPal);
232 const auto mask = init.
233#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
239 using MaskType = std::remove_cv_t<
decltype(mask)>;
240 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
241 if (mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(i))) {
244 parentPalette.setBrush(
245 QPalette::Active,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
246 parentPalette.setBrush(
247 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
248 parentPalette.setBrush(
249 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i), init.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
253 const int result = dlg.exec();
257 return result == QDialog::Accepted ? dlg.
palette() : init;
261 : QAbstractTableModel(parent)
264 const QMetaObject *meta = metaObject();
265 const QMetaProperty
property = meta->property(meta->indexOfProperty(
"colorRole"));
266 const QMetaEnum enumerator =
property.enumerator();
267 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) {
268 m_roleNames[
static_cast<QPalette::ColorRole
>(r)] = QLatin1String(enumerator.key(r));
274 return static_cast<int>(m_roleNames.count());
284 if (!index.isValid() || index.row() < 0 || index.row() >= QPalette::NColorRoles || index.column() < 0 || index.column() >= 4) {
288 if (index.column() == 0) {
289 if (role == Qt::DisplayRole) {
290 return m_roleNames[
static_cast<QPalette::ColorRole
>(index.row())];
292 if (role == Qt::EditRole) {
293 const auto mask = m_palette.
294#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
300 using MaskType = std::remove_cv_t<
decltype(mask)>;
301 return mask & (
static_cast<MaskType
>(1) <<
static_cast<MaskType
>(index.row()));
306 return m_palette.brush(columnToGroup(index.column()),
static_cast<QPalette::ColorRole
>(index.row()));
313 if (!index.isValid()) {
317 if (index.column() != 0 && role ==
BrushRole) {
318 const QBrush br = qvariant_cast<QBrush>(value);
319 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(index.row());
320 const QPalette::ColorGroup g = columnToGroup(index.column());
321 m_palette.setBrush(g, r, br);
323 QModelIndex idxBegin = PaletteModel::index(r, 0);
324 QModelIndex idxEnd = PaletteModel::index(r, 3);
326 m_palette.setBrush(QPalette::Inactive, r, br);
328 case QPalette::WindowText:
330 case QPalette::ButtonText:
334 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
335 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
336 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
337 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
338 idxBegin = PaletteModel::index(0, 0);
339 idxEnd = PaletteModel::index(
static_cast<int>(m_roleNames.count()) - 1, 3);
341 case QPalette::Window:
342 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
343 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
344 idxBegin = PaletteModel::index(QPalette::Base, 0);
346 case QPalette::Highlight:
349 m_palette.setBrush(QPalette::Disabled, r, br);
354 emit dataChanged(idxBegin, idxEnd);
357 if (index.column() == 0 && role == Qt::EditRole) {
358 auto mask = m_palette.
359#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
365 const bool isMask = qvariant_cast<bool>(value);
366 const int r = index.row();
368 mask |= (
static_cast<decltype(mask)
>(1) <<
static_cast<decltype(mask)
>(r));
371 QPalette::Active,
static_cast<QPalette::ColorRole
>(r), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(r)));
372 m_palette.setBrush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r),
373 m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r)));
374 m_palette.setBrush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r),
375 m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r)));
376 mask &= ~static_cast<decltype(mask)>((
static_cast<decltype(mask)
>(1) <<
static_cast<decltype(mask)
>(index.row())));
379#
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
380 setResolveMask(mask);
381 m_palette = m_palette.resolve(m_palette)
387 const QModelIndex idxEnd = PaletteModel::index(r, 3);
388 emit dataChanged(index, idxEnd);
396 if (!index.isValid())
397 return Qt::ItemIsEnabled;
398 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
403 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
405 return tr(
"Color Role");
406 if (section == groupToColumn(QPalette::Active))
408 if (section == groupToColumn(QPalette::Inactive))
409 return tr(
"Inactive");
410 if (section == groupToColumn(QPalette::Disabled))
411 return tr(
"Disabled");
423 m_parentPalette = parentPalette;
425 const QModelIndex idxBegin = index(0, 0);
426 const QModelIndex idxEnd = index(
static_cast<int>(m_roleNames.count()) - 1, 3);
427 emit dataChanged(idxBegin, idxEnd);
430QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const
433 return QPalette::Active;
435 return QPalette::Inactive;
436 return QPalette::Disabled;
439int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
441 if (group == QPalette::Active)
443 if (group == QPalette::Inactive)
453 auto *
const layout =
new QHBoxLayout(
this);
454 layout->setContentsMargins(0, 0, 0, 0);
455 layout->addWidget(m_button);
457 setFocusProxy(m_button);
468 return QBrush(m_button->
color());
471void BrushEditor::brushChanged()
484 , m_label(new QLabel(this))
487 QHBoxLayout *layout =
new QHBoxLayout(
this);
488 layout->setContentsMargins(0, 0, 0, 0);
489 layout->setSpacing(0);
491 layout->addWidget(m_label);
492 m_label->setAutoFillBackground(
true);
493 m_label->setIndent(3);
494 setFocusProxy(m_label);
496 auto *
const button =
new QToolButton(
this);
497 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
498 button->setIcon(QIcon::fromTheme(QStringLiteral(
"edit-clear")));
499 button->setIconSize(QSize(8, 8));
500 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
501 layout->addWidget(button);
502 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
507 m_label->setText(label);
516 m_label->setFont(font);
525void RoleEditor::emitResetProperty()
532 : QItemDelegate(parent)
538 if (index.column() == 0) {
544 using BrushEditorWidgetSignal = void (
BrushEditor::*)(QWidget *);
547 connect(editor,
static_cast<BrushEditorWidgetSignal
>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
548 editor->setFocusPolicy(Qt::NoFocus);
549 editor->installEventFilter(
const_cast<ColorDelegate *
>(
this));
555 if (index.column() == 0) {
556 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
557 auto *
const editor =
static_cast<RoleEditor *
>(ed);
559 const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
560 editor->setLabel(colorName);
562 const auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
563 auto *
const editor =
static_cast<BrushEditor *
>(ed);
570 if (index.column() == 0) {
571 const auto *
const editor =
static_cast<RoleEditor *
>(ed);
572 const auto mask = editor->
edited();
573 model->setData(index, mask, Qt::EditRole);
575 const auto *
const editor =
static_cast<BrushEditor *
>(ed);
576 if (editor->changed()) {
577 QBrush br = editor->
brush();
585 QItemDelegate::updateEditorGeometry(ed, option, index);
586 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
591 QStyleOptionViewItem option = opt;
592 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
593 if (index.column() == 0 && mask) {
594 option.font.setBold(
true);
596 auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
597 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
599 painter->translate(option.rect.x(), option.rect.y());
600 painter->scale(option.rect.width(), option.rect.height());
601 QGradient gr = *(br.gradient());
602 gr.setCoordinateMode(QGradient::LogicalMode);
604 painter->fillRect(0, 0, 1, 1, br);
608 painter->setBrushOrigin(option.rect.x(), option.rect.y());
609 painter->fillRect(option.rect, br);
612 QItemDelegate::paint(painter, option, index);
614 const QColor color =
static_cast<QRgb
>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
615 const QPen oldPen = painter->pen();
616 painter->setPen(QPen(color));
618 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
619 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
620 painter->setPen(oldPen);
625 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)