4 #include "ui_paletteeditor.h" 8 #include <QMetaProperty> 11 #include <QToolButton> 19 PaletteEditor::PaletteEditor(QWidget *parent)
22 , m_currentColorGroup(QPalette::Active)
24 , m_modelUpdated(false)
25 , m_paletteUpdated(false)
29 m_ui->paletteView->setModel(m_paletteModel);
30 updatePreviewPalette();
32 m_ui->paletteView->setModel(m_paletteModel);
34 m_ui->paletteView->setItemDelegate(delegate);
35 m_ui->paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
37 m_ui->paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
38 m_ui->paletteView->setDragEnabled(
true);
39 m_ui->paletteView->setDropIndicatorShown(
true);
40 m_ui->paletteView->setRootIsDecorated(
false);
41 m_ui->paletteView->setColumnHidden(2,
true);
42 m_ui->paletteView->setColumnHidden(3,
true);
57 const uint mask =
palette.resolve();
58 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
59 if (mask & (1 << i)) {
62 m_editPalette.setBrush(
63 QPalette::Active, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));
64 m_editPalette.setBrush(
65 QPalette::Inactive, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));
66 m_editPalette.setBrush(
67 QPalette::Disabled, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));
69 m_editPalette.resolve(mask);
70 updatePreviewPalette();
72 m_paletteUpdated =
true;
73 if (!m_modelUpdated) {
74 m_paletteModel->
setPalette(m_editPalette, m_parentPalette);
76 m_paletteUpdated =
false;
81 m_parentPalette = parentPalette;
85 void PaletteEditor::handleBuildButtonColorChanged(
const QColor &)
90 void PaletteEditor::handleActiveRadioClicked()
92 m_currentColorGroup = QPalette::Active;
93 updatePreviewPalette();
96 void PaletteEditor::handleInactiveRadioClicked()
98 m_currentColorGroup = QPalette::Inactive;
99 updatePreviewPalette();
102 void PaletteEditor::handleDisabledRadioClicked()
104 m_currentColorGroup = QPalette::Disabled;
105 updatePreviewPalette();
108 void PaletteEditor::handleComputeRadioClicked()
113 m_ui->paletteView->setColumnHidden(2,
true);
114 m_ui->paletteView->setColumnHidden(3,
true);
119 void PaletteEditor::handleDetailsRadioClicked()
124 const int w = m_ui->paletteView->columnWidth(1);
125 m_ui->paletteView->setColumnHidden(2,
false);
126 m_ui->paletteView->setColumnHidden(3,
false);
127 auto *
const header = m_ui->paletteView->header();
128 header->resizeSection(1, w / 3);
129 header->resizeSection(2, w / 3);
130 header->resizeSection(3, w / 3);
135 void PaletteEditor::paletteChanged(
const QPalette &palette)
137 m_modelUpdated =
true;
138 if (!m_paletteUpdated) {
141 m_modelUpdated =
false;
144 void PaletteEditor::buildPalette()
146 const QColor btn(m_ui->buildButton->color());
147 const QPalette temp(btn);
151 void PaletteEditor::updatePreviewPalette()
153 const QPalette::ColorGroup g = currentColorGroup();
155 const QPalette currentPalette =
palette();
156 QPalette previewPalette;
157 for (
int i = QPalette::WindowText; i < QPalette::NColorRoles; ++i) {
158 const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(i);
159 const QBrush br = currentPalette.brush(g, r);
160 previewPalette.setBrush(QPalette::Active, r, br);
161 previewPalette.setBrush(QPalette::Inactive, r, br);
162 previewPalette.setBrush(QPalette::Disabled, r, br);
166 void PaletteEditor::updateStyledButton()
168 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
174 auto parentPalette(parentPal);
175 const uint mask =
init.resolve();
176 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
177 if (mask & (1 << i)) {
180 parentPalette.setBrush(
181 QPalette::Active, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));
182 parentPalette.setBrush(
183 QPalette::Inactive, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));
184 parentPalette.setBrush(
185 QPalette::Disabled, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));
189 const int result = dlg.exec();
193 return result == QDialog::Accepted ? dlg.
palette() :
init;
197 : QAbstractTableModel(parent)
200 const QMetaObject *meta = metaObject();
201 const QMetaProperty
property = meta->property(meta->indexOfProperty(
"colorRole"));
202 const QMetaEnum enumerator =
property.enumerator();
203 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) {
204 m_roleNames[static_cast<QPalette::ColorRole>(r)] = QLatin1String(enumerator.key(r));
210 return m_roleNames.count();
220 if (!index.isValid() || index.row() < 0 || index.row() >= QPalette::NColorRoles || index.column() < 0 || index.column() >= 4) {
224 if (index.column() == 0) {
225 if (role == Qt::DisplayRole) {
226 return m_roleNames[static_cast<QPalette::ColorRole>(index.row())];
228 if (role == Qt::EditRole) {
229 const uint mask = m_palette.resolve();
230 if (mask & (1 << index.row()))
237 return m_palette.brush(columnToGroup(index.column()), static_cast<QPalette::ColorRole>(index.row()));
244 if (!index.isValid()) {
248 if (index.column() != 0 && role ==
BrushRole) {
249 const QBrush br = qvariant_cast<QBrush>(value);
250 const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(index.row());
251 const QPalette::ColorGroup g = columnToGroup(index.column());
252 m_palette.setBrush(g, r, br);
254 QModelIndex idxBegin = PaletteModel::index(r, 0);
255 QModelIndex idxEnd = PaletteModel::index(r, 3);
257 m_palette.setBrush(QPalette::Inactive, r, br);
259 case QPalette::WindowText:
261 case QPalette::ButtonText:
265 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
266 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
267 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
268 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
269 idxBegin = PaletteModel::index(0, 0);
270 idxEnd = PaletteModel::index(m_roleNames.count() - 1, 3);
272 case QPalette::Window:
273 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
274 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
275 idxBegin = PaletteModel::index(QPalette::Base, 0);
277 case QPalette::Highlight:
280 m_palette.setBrush(QPalette::Disabled, r, br);
285 emit dataChanged(idxBegin, idxEnd);
288 if (index.column() == 0 && role == Qt::EditRole) {
289 uint mask = m_palette.resolve();
290 const bool isMask = qvariant_cast<bool>(value);
291 const int r = index.row();
296 QPalette::Active, static_cast<QPalette::ColorRole>(r), m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(r)));
297 m_palette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r),
298 m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r)));
299 m_palette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(r),
300 m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(r)));
301 mask &= ~(1 << index.row());
303 m_palette.resolve(mask);
305 const QModelIndex idxEnd = PaletteModel::index(r, 3);
306 emit dataChanged(index, idxEnd);
314 if (!index.isValid())
315 return Qt::ItemIsEnabled;
316 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
321 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
323 return tr(
"Color Role");
324 if (section == groupToColumn(QPalette::Active))
326 if (section == groupToColumn(QPalette::Inactive))
327 return tr(
"Inactive");
328 if (section == groupToColumn(QPalette::Disabled))
329 return tr(
"Disabled");
341 m_parentPalette = parentPalette;
343 const QModelIndex idxBegin = index(0, 0);
344 const QModelIndex idxEnd = index(m_roleNames.count() - 1, 3);
345 emit dataChanged(idxBegin, idxEnd);
348 QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const 351 return QPalette::Active;
353 return QPalette::Inactive;
354 return QPalette::Disabled;
357 int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const 359 if (group == QPalette::Active)
361 if (group == QPalette::Inactive)
371 QLayout *layout =
new QHBoxLayout(
this);
372 layout->setMargin(0);
373 layout->addWidget(m_button);
374 connect(m_button, &ColorButton::colorChanged,
this, &BrushEditor::brushChanged);
375 setFocusProxy(m_button);
386 return QBrush(m_button->
color());
389 void BrushEditor::brushChanged()
402 , m_label(new QLabel(this))
405 QHBoxLayout *layout =
new QHBoxLayout(
this);
406 layout->setMargin(0);
407 layout->setSpacing(0);
409 layout->addWidget(m_label);
410 m_label->setAutoFillBackground(
true);
411 m_label->setIndent(3);
412 setFocusProxy(m_label);
414 auto *
const button =
new QToolButton(
this);
415 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
416 button->setIcon(QIcon(QStringLiteral(
":/qtutilities/icons/hicolor/48x48/actions/edit-clear.png")));
417 button->setIconSize(QSize(8, 8));
418 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
419 layout->addWidget(button);
420 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
425 m_label->setText(label);
434 m_label->setFont(font);
443 void RoleEditor::emitResetProperty()
450 : QItemDelegate(parent)
456 if (index.column() == 0) {
462 using BrushEditorWidgetSignal = void (
BrushEditor::*)(QWidget *);
465 connect(editor, static_cast<BrushEditorWidgetSignal>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
466 editor->setFocusPolicy(Qt::NoFocus);
467 editor->installEventFilter(const_cast<ColorDelegate *>(
this));
473 if (index.column() == 0) {
474 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
475 auto *
const editor = static_cast<RoleEditor *>(ed);
476 editor->setEdited(mask);
477 const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
478 editor->setLabel(colorName);
480 const auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
481 auto *
const editor = static_cast<BrushEditor *>(ed);
482 editor->setBrush(br);
488 if (index.column() == 0) {
489 const auto *
const editor = static_cast<RoleEditor *>(ed);
490 const auto mask = editor->edited();
491 model->setData(index, mask, Qt::EditRole);
493 const auto *
const editor = static_cast<BrushEditor *>(ed);
494 if (editor->changed()) {
495 QBrush br = editor->brush();
503 QItemDelegate::updateEditorGeometry(ed, option, index);
504 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
509 QStyleOptionViewItem option = opt;
510 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
511 if (index.column() == 0 && mask) {
512 option.font.setBold(
true);
514 auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
515 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
517 painter->translate(option.rect.x(), option.rect.y());
518 painter->scale(option.rect.width(), option.rect.height());
519 QGradient gr = *(br.gradient());
520 gr.setCoordinateMode(QGradient::LogicalMode);
522 painter->fillRect(0, 0, 1, 1, br);
526 painter->setBrushOrigin(option.rect.x(), option.rect.y());
527 painter->fillRect(option.rect, br);
530 QItemDelegate::paint(painter, option, index);
532 const QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
533 const QPen oldPen = painter->pen();
534 painter->setPen(QPen(color));
536 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
537 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
538 painter->setPen(oldPen);
543 return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);
QPalette getPalette() const
The BrushEditor class is used by PaletteEditor.
void updateEditorGeometry(QWidget *ed, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const override
PaletteModel(QObject *parent=nullptr)
The ColorDelegate class is used by PaletteEditor.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
The PaletteEditor class provides a dialog to customize a QPalette.
ColorDelegate(QObject *parent=nullptr)
QT_UTILITIES_EXPORT void init()
Initiates the resources used and provided by this library.
static QPalette getPalette(QWidget *parent, const QPalette &init=QPalette(), const QPalette &parentPal=QPalette(), int *result=nullptr)
BrushEditor(QWidget *parent=nullptr)
Qt::ItemFlags flags(const QModelIndex &index) const override
void paletteChanged(const QPalette &palette)
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
void changed(QWidget *widget)
QVariant data(const QModelIndex &index, int role) const override
void setEditorData(QWidget *ed, const QModelIndex &index) const override
void setLabel(const QString &label)
The RoleEditor class is used by PaletteEditor.
RoleEditor(QWidget *parent=nullptr)
void setPalette(const QPalette &palette)
~PaletteEditor() override
void setBrush(const QBrush &brush)
void setPalette(const QPalette &palette, const QPalette &parentPalette)
The PaletteModel class is used by PaletteEditor.
void setModelData(QWidget *ed, QAbstractItemModel *model, const QModelIndex &index) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override