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);
55 const uint mask =
palette.resolve();
56 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
57 if (mask & (1 << i)) {
60 m_editPalette.setBrush(
61 QPalette::Active, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));
62 m_editPalette.setBrush(
63 QPalette::Inactive, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));
64 m_editPalette.setBrush(
65 QPalette::Disabled, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));
67 m_editPalette.resolve(mask);
68 updatePreviewPalette();
70 m_paletteUpdated =
true;
71 if (!m_modelUpdated) {
72 m_paletteModel->
setPalette(m_editPalette, m_parentPalette);
74 m_paletteUpdated =
false;
79 m_parentPalette = parentPalette;
83 void PaletteEditor::handleBuildButtonColorChanged(
const QColor &)
88 void PaletteEditor::handleActiveRadioClicked()
90 m_currentColorGroup = QPalette::Active;
91 updatePreviewPalette();
94 void PaletteEditor::handleInactiveRadioClicked()
96 m_currentColorGroup = QPalette::Inactive;
97 updatePreviewPalette();
100 void PaletteEditor::handleDisabledRadioClicked()
102 m_currentColorGroup = QPalette::Disabled;
103 updatePreviewPalette();
106 void PaletteEditor::handleComputeRadioClicked()
111 m_ui->paletteView->setColumnHidden(2,
true);
112 m_ui->paletteView->setColumnHidden(3,
true);
117 void PaletteEditor::handleDetailsRadioClicked()
122 const int w = m_ui->paletteView->columnWidth(1);
123 m_ui->paletteView->setColumnHidden(2,
false);
124 m_ui->paletteView->setColumnHidden(3,
false);
125 auto *
const header = m_ui->paletteView->header();
126 header->resizeSection(1, w / 3);
127 header->resizeSection(2, w / 3);
128 header->resizeSection(3, w / 3);
133 void PaletteEditor::paletteChanged(
const QPalette &palette)
135 m_modelUpdated =
true;
136 if (!m_paletteUpdated) {
139 m_modelUpdated =
false;
142 void PaletteEditor::buildPalette()
144 const QColor btn(m_ui->buildButton->color());
145 const QPalette temp(btn);
149 void PaletteEditor::updatePreviewPalette()
151 const QPalette::ColorGroup g = currentColorGroup();
153 const QPalette currentPalette =
palette();
154 QPalette previewPalette;
155 for (
int i = QPalette::WindowText; i < QPalette::NColorRoles; ++i) {
156 const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(i);
157 const QBrush br = currentPalette.brush(g, r);
158 previewPalette.setBrush(QPalette::Active, r, br);
159 previewPalette.setBrush(QPalette::Inactive, r, br);
160 previewPalette.setBrush(QPalette::Disabled, r, br);
164 void PaletteEditor::updateStyledButton()
166 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
172 auto parentPalette(parentPal);
173 const uint mask =
init.resolve();
174 for (
int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) {
175 if (mask & (1 << i)) {
178 parentPalette.setBrush(
179 QPalette::Active, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));
180 parentPalette.setBrush(
181 QPalette::Inactive, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));
182 parentPalette.setBrush(
183 QPalette::Disabled, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));
187 const int result = dlg.exec();
191 return result == QDialog::Accepted ? dlg.
palette() :
init;
195 : QAbstractTableModel(parent)
198 const QMetaObject *meta = metaObject();
199 const QMetaProperty
property = meta->property(meta->indexOfProperty(
"colorRole"));
200 const QMetaEnum enumerator =
property.enumerator();
201 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) {
202 m_roleNames[static_cast<QPalette::ColorRole>(r)] = QLatin1String(enumerator.key(r));
208 return m_roleNames.count();
218 if (!index.isValid() || index.row() < 0 || index.row() >= QPalette::NColorRoles || index.column() < 0 || index.column() >= 4) {
222 if (index.column() == 0) {
223 if (role == Qt::DisplayRole) {
224 return m_roleNames[static_cast<QPalette::ColorRole>(index.row())];
226 if (role == Qt::EditRole) {
227 const uint mask = m_palette.resolve();
228 if (mask & (1 << index.row()))
235 return m_palette.brush(columnToGroup(index.column()), static_cast<QPalette::ColorRole>(index.row()));
242 if (!index.isValid()) {
246 if (index.column() != 0 && role ==
BrushRole) {
247 const QBrush br = qvariant_cast<QBrush>(value);
248 const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(index.row());
249 const QPalette::ColorGroup g = columnToGroup(index.column());
250 m_palette.setBrush(g, r, br);
252 QModelIndex idxBegin = PaletteModel::index(r, 0);
253 QModelIndex idxEnd = PaletteModel::index(r, 3);
255 m_palette.setBrush(QPalette::Inactive, r, br);
257 case QPalette::WindowText:
259 case QPalette::ButtonText:
263 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
264 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
265 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
266 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
267 idxBegin = PaletteModel::index(0, 0);
268 idxEnd = PaletteModel::index(m_roleNames.count() - 1, 3);
270 case QPalette::Window:
271 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
272 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
273 idxBegin = PaletteModel::index(QPalette::Base, 0);
275 case QPalette::Highlight:
278 m_palette.setBrush(QPalette::Disabled, r, br);
283 emit dataChanged(idxBegin, idxEnd);
286 if (index.column() == 0 && role == Qt::EditRole) {
287 uint mask = m_palette.resolve();
288 const bool isMask = qvariant_cast<bool>(value);
289 const int r = index.row();
294 QPalette::Active, static_cast<QPalette::ColorRole>(r), m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(r)));
295 m_palette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r),
296 m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r)));
297 m_palette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(r),
298 m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(r)));
299 mask &= ~(1 << index.row());
301 m_palette.resolve(mask);
303 const QModelIndex idxEnd = PaletteModel::index(r, 3);
304 emit dataChanged(index, idxEnd);
312 if (!index.isValid())
313 return Qt::ItemIsEnabled;
314 return Qt::ItemIsEditable | Qt::ItemIsEnabled;
319 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
321 return tr(
"Color Role");
322 if (section == groupToColumn(QPalette::Active))
324 if (section == groupToColumn(QPalette::Inactive))
325 return tr(
"Inactive");
326 if (section == groupToColumn(QPalette::Disabled))
327 return tr(
"Disabled");
339 m_parentPalette = parentPalette;
341 const QModelIndex idxBegin = index(0, 0);
342 const QModelIndex idxEnd = index(m_roleNames.count() - 1, 3);
343 emit dataChanged(idxBegin, idxEnd);
346 QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const
349 return QPalette::Active;
351 return QPalette::Inactive;
352 return QPalette::Disabled;
355 int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const
357 if (group == QPalette::Active)
359 if (group == QPalette::Inactive)
369 auto *
const layout =
new QHBoxLayout(
this);
370 layout->setContentsMargins(0, 0, 0, 0);
371 layout->addWidget(m_button);
373 setFocusProxy(m_button);
384 return QBrush(m_button->
color());
387 void BrushEditor::brushChanged()
400 , m_label(new QLabel(this))
403 QHBoxLayout *layout =
new QHBoxLayout(
this);
404 layout->setContentsMargins(0, 0, 0, 0);
405 layout->setSpacing(0);
407 layout->addWidget(m_label);
408 m_label->setAutoFillBackground(
true);
409 m_label->setIndent(3);
410 setFocusProxy(m_label);
412 auto *
const button =
new QToolButton(
this);
413 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
414 button->setIcon(QIcon::fromTheme(QStringLiteral(
"edit-clear")));
415 button->setIconSize(QSize(8, 8));
416 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
417 layout->addWidget(button);
418 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
423 m_label->setText(label);
432 m_label->setFont(font);
441 void RoleEditor::emitResetProperty()
448 : QItemDelegate(parent)
454 if (index.column() == 0) {
460 using BrushEditorWidgetSignal = void (
BrushEditor::*)(QWidget *);
463 connect(editor, static_cast<BrushEditorWidgetSignal>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
464 editor->setFocusPolicy(Qt::NoFocus);
465 editor->installEventFilter(const_cast<ColorDelegate *>(
this));
471 if (index.column() == 0) {
472 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
473 auto *
const editor = static_cast<RoleEditor *>(ed);
474 editor->setEdited(mask);
475 const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
476 editor->setLabel(colorName);
478 const auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
479 auto *
const editor = static_cast<BrushEditor *>(ed);
480 editor->setBrush(br);
486 if (index.column() == 0) {
487 const auto *
const editor = static_cast<RoleEditor *>(ed);
488 const auto mask = editor->edited();
489 model->setData(index, mask, Qt::EditRole);
491 const auto *
const editor = static_cast<BrushEditor *>(ed);
492 if (editor->changed()) {
493 QBrush br = editor->brush();
501 QItemDelegate::updateEditorGeometry(ed, option, index);
502 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
507 QStyleOptionViewItem option = opt;
508 const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole));
509 if (index.column() == 0 && mask) {
510 option.font.setBold(
true);
512 auto br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
513 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
515 painter->translate(option.rect.x(), option.rect.y());
516 painter->scale(option.rect.width(), option.rect.height());
517 QGradient gr = *(br.gradient());
518 gr.setCoordinateMode(QGradient::LogicalMode);
520 painter->fillRect(0, 0, 1, 1, br);
524 painter->setBrushOrigin(option.rect.x(), option.rect.y());
525 painter->fillRect(option.rect, br);
528 QItemDelegate::paint(painter, option, index);
530 const QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
531 const QPen oldPen = painter->pen();
532 painter->setPen(QPen(color));
534 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
535 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
536 painter->setPen(oldPen);
541 return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);