4 #include "ui_paletteeditor.h"
8 #include <QMetaProperty>
11 #include <QToolButton>
20 , m_currentColorGroup(QPalette::Active)
22 , m_modelUpdated(false)
23 , m_paletteUpdated(false)
27 m_ui->paletteView->setModel(m_paletteModel);
28 updatePreviewPalette();
30 m_ui->paletteView->setModel(m_paletteModel);
32 m_ui->paletteView->setItemDelegate(delegate);
33 m_ui->paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
35 m_ui->paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
36 m_ui->paletteView->setDragEnabled(
true);
37 m_ui->paletteView->setDropIndicatorShown(
true);
38 m_ui->paletteView->setRootIsDecorated(
false);
39 m_ui->paletteView->setColumnHidden(2,
true);
40 m_ui->paletteView->setColumnHidden(3,
true);
56 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
62 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
63 if (mask & (1 << i)) {
66 m_editPalette.setBrush(
67 QPalette::Active,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
68 m_editPalette.setBrush(
69 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
70 m_editPalette.setBrush(
71 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i), m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
74 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
76 m_editPalette = m_editPalette.resolve(m_editPalette)
81 updatePreviewPalette();
83 m_paletteUpdated =
true;
84 if (!m_modelUpdated) {
85 m_paletteModel->
setPalette(m_editPalette, m_parentPalette);
87 m_paletteUpdated =
false;
92 m_parentPalette = parentPalette;
96 void PaletteEditor::handleBuildButtonColorChanged(
const QColor &)
101 void PaletteEditor::handleActiveRadioClicked()
103 m_currentColorGroup = QPalette::Active;
104 updatePreviewPalette();
107 void PaletteEditor::handleInactiveRadioClicked()
109 m_currentColorGroup = QPalette::Inactive;
110 updatePreviewPalette();
113 void PaletteEditor::handleDisabledRadioClicked()
115 m_currentColorGroup = QPalette::Disabled;
116 updatePreviewPalette();
119 void PaletteEditor::handleComputeRadioClicked()
124 m_ui->paletteView->setColumnHidden(2,
true);
125 m_ui->paletteView->setColumnHidden(3,
true);
130 void PaletteEditor::handleDetailsRadioClicked()
135 const int w = m_ui->paletteView->columnWidth(1);
136 m_ui->paletteView->setColumnHidden(2,
false);
137 m_ui->paletteView->setColumnHidden(3,
false);
138 auto *
const header = m_ui->paletteView->header();
139 header->resizeSection(1, w / 3);
140 header->resizeSection(2, w / 3);
141 header->resizeSection(3, w / 3);
146 void PaletteEditor::paletteChanged(
const QPalette &palette)
148 m_modelUpdated =
true;
149 if (!m_paletteUpdated) {
152 m_modelUpdated =
false;
155 void PaletteEditor::buildPalette()
157 const QColor btn(m_ui->buildButton->color());
158 const QPalette temp(btn);
162 void PaletteEditor::updatePreviewPalette()
164 const QPalette::ColorGroup g = currentColorGroup();
166 const QPalette currentPalette =
palette();
167 QPalette previewPalette;
168 for (
int i = QPalette::WindowText; i < QPalette::NColorRoles; ++i) {
169 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(i);
170 const QBrush br = currentPalette.brush(g, r);
171 previewPalette.setBrush(QPalette::Active, r, br);
172 previewPalette.setBrush(QPalette::Inactive, r, br);
173 previewPalette.setBrush(QPalette::Disabled, r, br);
177 void PaletteEditor::updateStyledButton()
179 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
185 auto parentPalette(parentPal);
186 const auto mask =
init.
187 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
193 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
194 if (mask & (1 << i)) {
197 parentPalette.setBrush(
198 QPalette::Active,
static_cast<QPalette::ColorRole
>(i),
init.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(i)));
199 parentPalette.setBrush(
200 QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i),
init.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(i)));
201 parentPalette.setBrush(
202 QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i),
init.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(i)));
206 const int result = dlg.exec();
210 return result == QDialog::Accepted ? dlg.
palette() :
init;
214 : QAbstractTableModel(parent)
217 const QMetaObject *meta = metaObject();
218 const QMetaProperty
property = meta->property(meta->indexOfProperty(
"colorRole"));
219 const QMetaEnum enumerator =
property.enumerator();
220 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) {
221 m_roleNames[
static_cast<QPalette::ColorRole
>(r)] = QLatin1String(enumerator.key(r));
227 return m_roleNames.count();
237 if (!index.isValid() || index.row() < 0 || index.row() >= QPalette::NColorRoles || index.column() < 0 || index.column() >= 4) {
241 if (index.column() == 0) {
242 if (role == Qt::DisplayRole) {
243 return m_roleNames[
static_cast<QPalette::ColorRole
>(index.row())];
245 if (role == Qt::EditRole) {
246 const auto mask = m_palette.
247 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
253 if (mask & (1 << index.row()))
260 return m_palette.brush(columnToGroup(index.column()),
static_cast<QPalette::ColorRole
>(index.row()));
267 if (!index.isValid()) {
271 if (index.column() != 0 && role ==
BrushRole) {
272 const QBrush br = qvariant_cast<QBrush>(value);
273 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(index.row());
274 const QPalette::ColorGroup g = columnToGroup(index.column());
275 m_palette.setBrush(g, r, br);
277 QModelIndex idxBegin = PaletteModel::index(r, 0);
278 QModelIndex idxEnd = PaletteModel::index(r, 3);
280 m_palette.setBrush(QPalette::Inactive, r, br);
282 case QPalette::WindowText:
284 case QPalette::ButtonText:
288 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
289 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
290 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
291 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
292 idxBegin = PaletteModel::index(0, 0);
293 idxEnd = PaletteModel::index(m_roleNames.count() - 1, 3);
295 case QPalette::Window:
296 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
297 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
298 idxBegin = PaletteModel::index(QPalette::Base, 0);
300 case QPalette::Highlight:
303 m_palette.setBrush(QPalette::Disabled, r, br);
308 emit dataChanged(idxBegin, idxEnd);
311 if (index.column() == 0 && role == Qt::EditRole) {
312 auto mask = m_palette.
313 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
319 const bool isMask = qvariant_cast<bool>(value);
320 const int r = index.row();
325 QPalette::Active,
static_cast<QPalette::ColorRole
>(r), m_parentPalette.brush(QPalette::Active,
static_cast<QPalette::ColorRole
>(r)));
326 m_palette.setBrush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r),
327 m_parentPalette.brush(QPalette::Inactive,
static_cast<QPalette::ColorRole
>(r)));
328 m_palette.setBrush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r),
329 m_parentPalette.brush(QPalette::Disabled,
static_cast<QPalette::ColorRole
>(r)));
330 mask &= ~static_cast<decltype(mask)>(1 << index.row());
333 #
if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
334 setResolveMask(mask);
335 m_palette = m_palette.resolve(m_palette)
341 const QModelIndex idxEnd = PaletteModel::index(r, 3);
342 emit dataChanged(index, idxEnd);
350 if (!index.isValid())
351 return Qt::ItemIsEnabled;
352 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
357 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
359 return tr(
"Color Role");
360 if (section == groupToColumn(QPalette::Active))
362 if (section == groupToColumn(QPalette::Inactive))
363 return tr(
"Inactive");
364 if (section == groupToColumn(QPalette::Disabled))
365 return tr(
"Disabled");
377 m_parentPalette = parentPalette;
379 const QModelIndex idxBegin = index(0, 0);
380 const QModelIndex idxEnd = index(m_roleNames.count() - 1, 3);
381 emit dataChanged(idxBegin, idxEnd);
384 QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const
387 return QPalette::Active;
389 return QPalette::Inactive;
390 return QPalette::Disabled;
393 int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
395 if (group == QPalette::Active)
397 if (group == QPalette::Inactive)
407 auto *
const layout =
new QHBoxLayout(
this);
408 layout->setContentsMargins(0, 0, 0, 0);
409 layout->addWidget(m_button);
411 setFocusProxy(m_button);
422 return QBrush(m_button->
color());
425 void BrushEditor::brushChanged()
438 , m_label(new QLabel(this))
441 QHBoxLayout *layout =
new QHBoxLayout(
this);
442 layout->setContentsMargins(0, 0, 0, 0);
443 layout->setSpacing(0);
445 layout->addWidget(m_label);
446 m_label->setAutoFillBackground(
true);
447 m_label->setIndent(3);
448 setFocusProxy(m_label);
450 auto *
const button =
new QToolButton(
this);
451 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
452 button->setIcon(QIcon::fromTheme(QStringLiteral(
"edit-clear")));
453 button->setIconSize(QSize(8, 8));
454 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
455 layout->addWidget(button);
456 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
461 m_label->setText(label);
470 m_label->setFont(font);
479 void RoleEditor::emitResetProperty()
486 : QItemDelegate(parent)
492 if (index.column() == 0) {
498 using BrushEditorWidgetSignal = void (
BrushEditor::*)(QWidget *);
501 connect(editor,
static_cast<BrushEditorWidgetSignal
>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
502 editor->setFocusPolicy(Qt::NoFocus);
503 editor->installEventFilter(
const_cast<ColorDelegate *
>(
this));
509 if (index.column() == 0) {
510 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
511 auto *
const editor =
static_cast<RoleEditor *
>(ed);
513 const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
514 editor->setLabel(colorName);
516 const auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
517 auto *
const editor =
static_cast<BrushEditor *
>(ed);
524 if (index.column() == 0) {
525 const auto *
const editor =
static_cast<RoleEditor *
>(ed);
526 const auto mask = editor->
edited();
527 model->setData(index, mask, Qt::EditRole);
529 const auto *
const editor =
static_cast<BrushEditor *
>(ed);
530 if (editor->changed()) {
531 QBrush br = editor->
brush();
539 QItemDelegate::updateEditorGeometry(ed, option, index);
540 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
545 QStyleOptionViewItem option = opt;
546 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
547 if (index.column() == 0 && mask) {
548 option.font.setBold(
true);
550 auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
551 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
553 painter->translate(option.rect.x(), option.rect.y());
554 painter->scale(option.rect.width(), option.rect.height());
555 QGradient gr = *(br.gradient());
556 gr.setCoordinateMode(QGradient::LogicalMode);
558 painter->fillRect(0, 0, 1, 1, br);
562 painter->setBrushOrigin(option.rect.x(), option.rect.y());
563 painter->fillRect(option.rect, br);
566 QItemDelegate::paint(painter, option, index);
568 const QColor color =
static_cast<QRgb
>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
569 const QPen oldPen = painter->pen();
570 painter->setPen(QPen(color));
572 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
573 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
574 painter->setPen(oldPen);
579 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)
QT_UTILITIES_EXPORT void init()
Initiates the resources used and provided by this library.