4 #include "ui_paletteeditor.h" 8 #include <QMetaProperty> 10 #include <QToolButton> 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 m_ui->paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);
37 m_ui->paletteView->setDragEnabled(
true);
38 m_ui->paletteView->setDropIndicatorShown(
true);
39 m_ui->paletteView->setRootIsDecorated(
false);
40 m_ui->paletteView->setColumnHidden(2,
true);
41 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(
60 QPalette::Active, static_cast<QPalette::ColorRole>(i), 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));
169 QPalette parentPalette(parentPal);
170 uint mask =
init.resolve();
171 for (
int i = 0; i < (int)QPalette::NColorRoles; ++i) {
172 if (!(mask & (1 << i))) {
173 parentPalette.setBrush(
174 QPalette::Active, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));
175 parentPalette.setBrush(
176 QPalette::Inactive, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));
177 parentPalette.setBrush(
178 QPalette::Disabled, static_cast<QPalette::ColorRole>(i),
init.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));
183 const int result = dlg.exec();
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()), static_cast<QPalette::ColorRole>(index.row()));
242 if (!index.isValid())
245 if (index.column() != 0 && role ==
BrushRole) {
246 const QBrush br = qvariant_cast<QBrush>(value);
247 const QPalette::ColorRole r =
static_cast<QPalette::ColorRole
>(index.row());
248 const QPalette::ColorGroup g = columnToGroup(index.column());
249 m_palette.setBrush(g, r, br);
251 QModelIndex idxBegin = PaletteModel::index(r, 0);
252 QModelIndex idxEnd = PaletteModel::index(r, 3);
254 m_palette.setBrush(QPalette::Inactive, r, br);
256 case QPalette::WindowText:
258 case QPalette::ButtonText:
262 m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);
263 m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);
264 m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);
265 m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);
266 idxBegin = PaletteModel::index(0, 0);
267 idxEnd = PaletteModel::index(m_roleNames.count() - 1, 3);
269 case QPalette::Window:
270 m_palette.setBrush(QPalette::Disabled, QPalette::Base, br);
271 m_palette.setBrush(QPalette::Disabled, QPalette::Window, br);
272 idxBegin = PaletteModel::index(QPalette::Base, 0);
274 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();
295 QPalette::Active, static_cast<QPalette::ColorRole>(r), 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;
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)
373 QLayout *layout =
new QHBoxLayout(
this);
374 layout->setMargin(0);
375 layout->addWidget(m_button);
376 connect(m_button, &ColorButton::colorChanged,
this, &BrushEditor::brushChanged);
377 setFocusProxy(m_button);
388 return QBrush(m_button->
color());
391 void BrushEditor::brushChanged()
406 , m_label(new QLabel(this))
409 QHBoxLayout *layout =
new QHBoxLayout(
this);
410 layout->setMargin(0);
411 layout->setSpacing(0);
413 layout->addWidget(m_label);
414 m_label->setAutoFillBackground(
true);
415 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 *);
473 connect(editor, static_cast<BrushEditorWidgetSignal>(&
BrushEditor::changed),
this, &ColorDelegate::commitData);
474 editor->setFocusPolicy(Qt::NoFocus);
475 editor->installEventFilter(const_cast<ColorDelegate *>(
this));
483 if (index.column() == 0) {
484 const bool mask = qvariant_cast<
bool>(index.model()->data(index, Qt::EditRole));
487 const QString colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole));
488 editor->setLabel(colorName);
490 const QBrush br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
498 if (index.column() == 0) {
500 const bool mask = editor->
edited();
501 model->setData(index, mask, Qt::EditRole);
505 QBrush br = editor->
brush();
513 QItemDelegate::updateEditorGeometry(ed, option, index);
514 ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));
519 QStyleOptionViewItem option = opt;
520 const bool mask = qvariant_cast<
bool>(index.model()->data(index, Qt::EditRole));
521 if (index.column() == 0 && mask) {
522 option.font.setBold(
true);
524 QBrush br = qvariant_cast<QBrush>(index.model()->data(index,
BrushRole));
525 if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) {
527 painter->translate(option.rect.x(), option.rect.y());
528 painter->scale(option.rect.width(), option.rect.height());
529 QGradient gr = *(br.gradient());
530 gr.setCoordinateMode(QGradient::LogicalMode);
532 painter->fillRect(0, 0, 1, 1, br);
536 painter->setBrushOrigin(option.rect.x(), option.rect.y());
537 painter->fillRect(option.rect, br);
540 QItemDelegate::paint(painter, option, index);
542 const QColor color =
static_cast<QRgb
>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));
543 const QPen oldPen = painter->pen();
544 painter->setPen(QPen(color));
546 painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom());
547 painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());
548 painter->setPen(oldPen);
553 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.