4 #include "ui_paletteeditor.h" 6 #include <QMetaProperty> 10 #include <QHeaderView> 18 PaletteEditor::PaletteEditor(QWidget *parent) :
21 m_currentColorGroup(QPalette::Active),
23 m_modelUpdated(false),
24 m_paletteUpdated(false),
28 m_ui->paletteView->setModel(m_paletteModel);
29 updatePreviewPalette();
31 m_ui->paletteView->setModel(m_paletteModel);
33 m_ui->paletteView->setItemDelegate(delegate);
34 m_ui->paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);
36 this, &PaletteEditor::paletteChanged);
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);
56 const uint mask = palette.resolve();
57 for (
int i = 0; i < (int)QPalette::NColorRoles; i++) {
58 if (!(mask & (1 << i))) {
59 m_editPalette.setBrush(QPalette::Active, static_cast<QPalette::ColorRole>(i),
60 m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));
61 m_editPalette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i),
62 m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));
63 m_editPalette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i),
64 m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));
67 m_editPalette.resolve(mask);
68 updatePreviewPalette();
70 m_paletteUpdated =
true;
72 m_paletteModel->
setPalette(m_editPalette, m_parentPalette);
73 m_paletteUpdated =
false;
78 m_parentPalette = parentPalette;
82 void PaletteEditor::on_buildButton_colorChanged(
const QColor &)
87 void PaletteEditor::on_activeRadio_clicked()
89 m_currentColorGroup = QPalette::Active;
90 updatePreviewPalette();
93 void PaletteEditor::on_inactiveRadio_clicked()
95 m_currentColorGroup = QPalette::Inactive;
96 updatePreviewPalette();
99 void PaletteEditor::on_disabledRadio_clicked()
101 m_currentColorGroup = QPalette::Disabled;
102 updatePreviewPalette();
105 void PaletteEditor::on_computeRadio_clicked()
109 m_ui->paletteView->setColumnHidden(2,
true);
110 m_ui->paletteView->setColumnHidden(3,
true);
115 void PaletteEditor::on_detailsRadio_clicked()
119 const int w = m_ui->paletteView->columnWidth(1);
120 m_ui->paletteView->setColumnHidden(2,
false);
121 m_ui->paletteView->setColumnHidden(3,
false);
122 QHeaderView *header = m_ui->paletteView->header();
123 header->resizeSection(1, w / 3);
124 header->resizeSection(2, w / 3);
125 header->resizeSection(3, w / 3);
130 void PaletteEditor::paletteChanged(
const QPalette &
palette)
132 m_modelUpdated =
true;
133 if (!m_paletteUpdated) {
136 m_modelUpdated =
false;
139 void PaletteEditor::buildPalette()
141 const QColor btn(m_ui->buildButton->color());
142 const QPalette temp(btn);
146 void PaletteEditor::updatePreviewPalette()
148 const QPalette::ColorGroup g = currentColorGroup();
150 const QPalette currentPalette =
palette();
151 QPalette previewPalette;
152 for(
int i = QPalette::WindowText; i < QPalette::NColorRoles; ++i) {
153 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(i);
154 const QBrush br = currentPalette.brush(g, r);
155 previewPalette.setBrush(QPalette::Active, r, br);
156 previewPalette.setBrush(QPalette::Inactive, r, br);
157 previewPalette.setBrush(QPalette::Disabled, r, br);
161 void PaletteEditor::updateStyledButton()
163 m_ui->buildButton->setColor(
palette().color(QPalette::Active, QPalette::Button));
167 const QPalette &parentPal,
int *ok)
170 QPalette parentPalette(parentPal);
171 uint mask = init.resolve();
172 for (
int i = 0; i < (int)QPalette::NColorRoles; ++i) {
173 if (!(mask & (1 << i))) {
174 parentPalette.setBrush(QPalette::Active, static_cast<QPalette::ColorRole>(i),
175 init.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));
176 parentPalette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i),
177 init.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));
178 parentPalette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i),
179 init.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));
184 const int result = dlg.exec();
185 if (ok) *ok = result;
187 return result == QDialog::Accepted ? dlg.
palette() :
init;
193 QAbstractTableModel(parent),
196 const QMetaObject *meta = metaObject();
197 const int index = meta->indexOfProperty(
"colorRole");
198 const QMetaProperty p = meta->property(index);
199 const QMetaEnum e = p.enumerator();
200 for (
int r = QPalette::WindowText; r < QPalette::NColorRoles; r++) {
201 m_roleNames[
static_cast<QPalette::ColorRole
>(r)] = QLatin1String(e.key(r));
207 return m_roleNames.count();
217 if (!index.isValid())
219 if (index.row() < 0 || index.row() >= QPalette::NColorRoles)
221 if (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())];
227 if (role == Qt::EditRole) {
228 const uint mask = m_palette.resolve();
229 if (mask & (1 << index.row()))
236 return m_palette.brush(columnToGroup(index.column()),
237 static_cast<QPalette::ColorRole>(index.row()));
243 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:
279 m_palette.setBrush(QPalette::Disabled, r, br);
284 emit dataChanged(idxBegin, idxEnd);
287 if (index.column() == 0 && role == Qt::EditRole) {
288 uint mask = m_palette.resolve();
289 const bool isMask = qvariant_cast<
bool>(value);
290 const int r = index.row();
294 m_palette.setBrush(QPalette::Active, static_cast<QPalette::ColorRole>(r),
295 m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(r)));
296 m_palette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r),
297 m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r)));
298 m_palette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(r),
299 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;
322 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
324 return tr(
"Color Role");
325 if (section == groupToColumn(QPalette::Active))
327 if (section == groupToColumn(QPalette::Inactive))
328 return tr(
"Inactive");
329 if (section == groupToColumn(QPalette::Disabled))
330 return tr(
"Disabled");
342 m_parentPalette = parentPalette;
344 const QModelIndex idxBegin = index(0, 0);
345 const QModelIndex idxEnd = index(m_roleNames.count() - 1, 3);
346 emit dataChanged(idxBegin, idxEnd);
349 QPalette::ColorGroup PaletteModel::columnToGroup(
int index)
const 352 return QPalette::Active;
354 return QPalette::Inactive;
355 return QPalette::Disabled;
358 int PaletteModel::groupToColumn(QPalette::ColorGroup group)
const 360 if (group == QPalette::Active)
362 if (group == QPalette::Inactive)
374 QLayout *layout =
new QHBoxLayout(
this);
375 layout->setMargin(0);
376 layout->addWidget(m_button);
377 connect(m_button, &ColorButton::colorChanged,
this, &BrushEditor::brushChanged);
378 setFocusProxy(m_button);
389 return QBrush(m_button->
color());
392 void BrushEditor::brushChanged()
407 m_label(new QLabel(this)),
410 QHBoxLayout *layout =
new QHBoxLayout(
this);
411 layout->setMargin(0);
412 layout->setSpacing(0);
414 layout->addWidget(m_label);
415 m_label->setAutoFillBackground(
true);
416 m_label->setIndent(3);
417 setFocusProxy(m_label);
419 QToolButton *button =
new QToolButton(
this);
420 button->setToolButtonStyle(Qt::ToolButtonIconOnly);
421 button->setIcon(QIcon(QStringLiteral(
":/qtutilities/icons/hicolor/48x48/actions/edit-clear.png")));
422 button->setIconSize(QSize(8,8));
423 button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
424 layout->addWidget(button);
425 connect(button, &QAbstractButton::clicked,
this, &RoleEditor::emitResetProperty);
430 m_label->setText(label);
439 m_label->setFont(font);
448 void RoleEditor::emitResetProperty()
456 QItemDelegate(parent)
462 QWidget *ed =
nullptr;
463 if (index.column() == 0) {
470 typedef void (
BrushEditor::*BrushEditorWidgetSignal)(QWidget *);
474 this, &ColorDelegate::commitData);
475 editor->setFocusPolicy(Qt::NoFocus);
476 editor->installEventFilter(const_cast<ColorDelegate *>(
this));
484 if (index.column() == 0) {
485 const bool mask = qvariant_cast<
bool>(index.model()->data(index, Qt::EditRole));
488 const QString colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
489 editor->setLabel(colorName);
491 const QBrush br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
498 const QModelIndex &index)
const 500 if (index.column() == 0) {
502 const bool mask = editor->
edited();
503 model->setData(index, mask, Qt::EditRole);
507 QBrush br = editor->
brush();
514 const QStyleOptionViewItem &option,
const QModelIndex &index)
const 516 QItemDelegate::updateEditorGeometry(ed, option, index);
517 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
522 QStyleOptionViewItem option = opt;
523 const bool mask = qvariant_cast<
bool>(index.model()->data(index, Qt::EditRole));
524 if (index.column() == 0 && mask) {
525 option.font.setBold(
true);
527 QBrush br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
528 if (br.style() == Qt::LinearGradientPattern ||
529 br.style() == Qt::RadialGradientPattern ||
530 br.style() == Qt::ConicalGradientPattern) {
532 painter->translate(option.rect.x(), option.rect.y());
533 painter->scale(option.rect.width(), option.rect.height());
534 QGradient gr = *(br.gradient());
535 gr.setCoordinateMode(QGradient::LogicalMode);
537 painter->fillRect(0, 0, 1, 1, br);
541 painter->setBrushOrigin(option.rect.x(), option.rect.y());
542 painter->fillRect(option.rect, br);
545 QItemDelegate::paint(painter, option, index);
548 const QColor color =
static_cast<QRgb
>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
549 const QPen oldPen = painter->pen();
550 painter->setPen(QPen(color));
552 painter->drawLine(option.rect.right(), option.rect.y(),
553 option.rect.right(), option.rect.bottom());
554 painter->drawLine(option.rect.x(), option.rect.bottom(),
555 option.rect.right(), option.rect.bottom());
556 painter->setPen(oldPen);
561 return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);
QPalette getPalette() const
void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const
The BrushEditor class is used by PaletteEditor.
Qt::ItemFlags flags(const QModelIndex &index) const
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
PaletteModel(QObject *parent=nullptr)
void setModelData(QWidget *ed, QAbstractItemModel *model, const QModelIndex &index) const
The ColorDelegate class is used by PaletteEditor.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
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)
bool setData(const QModelIndex &index, const QVariant &value, int role)
BrushEditor(QWidget *parent=nullptr)
void paletteChanged(const QPalette &palette)
int columnCount(const QModelIndex &parent=QModelIndex()) const
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
void setEditorData(QWidget *ed, const QModelIndex &index) const
void updateEditorGeometry(QWidget *ed, const QStyleOptionViewItem &option, const QModelIndex &index) const
void changed(QWidget *widget)
void setLabel(const QString &label)
The RoleEditor class is used by PaletteEditor.
RoleEditor(QWidget *parent=nullptr)
int rowCount(const QModelIndex &parent=QModelIndex()) const
QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const
void setPalette(const QPalette &palette)
void setBrush(const QBrush &brush)
QVariant data(const QModelIndex &index, int role) const
void setPalette(const QPalette &palette, const QPalette &parentPalette)
The PaletteModel class is used by PaletteEditor.