Qt Utilities  6.4.1
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
paletteeditor.h
Go to the documentation of this file.
1 #ifndef WIDGETS_PALETTEEDITOR_H
2 #define WIDGETS_PALETTEEDITOR_H
3 
4 #include "../global.h"
5 
6 #include <QDialog>
7 #include <QItemDelegate>
8 
9 #include <memory>
10 
11 QT_FORWARD_DECLARE_CLASS(QListView)
12 QT_FORWARD_DECLARE_CLASS(QLabel)
13 
14 namespace QtUtilities {
15 
16 class ColorButton;
17 
18 namespace Ui {
19 class PaletteEditor;
20 }
21 
29 class QT_UTILITIES_EXPORT PaletteEditor : public QDialog {
30  Q_OBJECT
31 public:
32  PaletteEditor(QWidget *parent);
33  ~PaletteEditor() override;
34 
35  static QPalette getPalette(QWidget *parent, const QPalette &init = QPalette(), const QPalette &parentPal = QPalette(), int *result = nullptr);
36 
37  QPalette palette() const;
38  void setPalette(const QPalette &palette);
39  void setPalette(const QPalette &palette, const QPalette &parentPalette);
40 
41 private Q_SLOTS:
42  void handleBuildButtonColorChanged(const QColor &);
43  void handleActiveRadioClicked();
44  void handleInactiveRadioClicked();
45  void handleDisabledRadioClicked();
46  void handleComputeRadioClicked();
47  void handleDetailsRadioClicked();
48 
49  void paletteChanged(const QPalette &palette);
50 
51 private:
52  void buildPalette();
53 
54  void updatePreviewPalette();
55  void updateStyledButton();
56 
57  QPalette::ColorGroup currentColorGroup() const
58  {
59  return m_currentColorGroup;
60  }
61 
62  std::unique_ptr<Ui::PaletteEditor> m_ui;
63  QPalette m_editPalette;
64  QPalette m_parentPalette;
65  QPalette::ColorGroup m_currentColorGroup;
66  class PaletteModel *m_paletteModel;
67  bool m_modelUpdated;
68  bool m_paletteUpdated;
69  bool m_compute;
70 };
71 
75 class QT_UTILITIES_EXPORT PaletteModel : public QAbstractTableModel {
76  Q_OBJECT
77  Q_PROPERTY(QPalette::ColorRole colorRole READ colorRole)
78 public:
79  explicit PaletteModel(QObject *parent = nullptr);
80 
81  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
82  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
83  QVariant data(const QModelIndex &index, int role) const override;
84  bool setData(const QModelIndex &index, const QVariant &value, int role) override;
85  Qt::ItemFlags flags(const QModelIndex &index) const override;
86  QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
87 
88  QPalette getPalette() const;
89  void setPalette(const QPalette &palette, const QPalette &parentPalette);
90 
91  QPalette::ColorRole colorRole() const
92  {
93  return QPalette::NoRole;
94  }
95  void setCompute(bool on)
96  {
97  m_compute = on;
98  }
99 
100 Q_SIGNALS:
101  void paletteChanged(const QPalette &palette);
102 
103 private:
104  QPalette::ColorGroup columnToGroup(int index) const;
105  int groupToColumn(QPalette::ColorGroup group) const;
106 
107  QPalette m_palette;
108  QPalette m_parentPalette;
109  QMap<QPalette::ColorRole, QString> m_roleNames;
110  bool m_compute;
111 };
112 
116 class QT_UTILITIES_EXPORT BrushEditor : public QWidget {
117  Q_OBJECT
118 
119 public:
120  explicit BrushEditor(QWidget *parent = nullptr);
121 
122  void setBrush(const QBrush &brush);
123  QBrush brush() const;
124  bool changed() const;
125 
126 Q_SIGNALS:
127  void changed(QWidget *widget);
128 
129 private Q_SLOTS:
130  void brushChanged();
131 
132 private:
133  ColorButton *m_button;
134  bool m_changed;
135 };
136 
140 class QT_UTILITIES_EXPORT RoleEditor : public QWidget {
141  Q_OBJECT
142 public:
143  explicit RoleEditor(QWidget *parent = nullptr);
144 
145  void setLabel(const QString &label);
146  void setEdited(bool on);
147  bool edited() const;
148 
149 Q_SIGNALS:
150  void changed(QWidget *widget);
151 
152 private Q_SLOTS:
153  void emitResetProperty();
154 
155 private:
156  QLabel *m_label;
157  bool m_edited;
158 };
159 
163 class QT_UTILITIES_EXPORT ColorDelegate : public QItemDelegate {
164  Q_OBJECT
165 
166 public:
167  explicit ColorDelegate(QObject *parent = nullptr);
168 
169  QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
170 
171  void setEditorData(QWidget *ed, const QModelIndex &index) const override;
172  void setModelData(QWidget *ed, QAbstractItemModel *model, const QModelIndex &index) const override;
173 
174  void updateEditorGeometry(QWidget *ed, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
175 
176  void paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const override;
177  QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const override;
178 };
179 } // namespace QtUtilities
180 
181 #endif // WIDGETS_PALETTEEDITOR_H
The BrushEditor class is used by PaletteEditor.
void changed(QWidget *widget)
The ColorButton class is used by PaletteEditor.
Definition: colorbutton.h:15
The ColorDelegate class is used by PaletteEditor.
The PaletteEditor class provides a dialog to customize a QPalette.
Definition: paletteeditor.h:29
The PaletteModel class is used by PaletteEditor.
Definition: paletteeditor.h:75
QPalette::ColorRole colorRole() const
Definition: paletteeditor.h:91
void paletteChanged(const QPalette &palette)
The RoleEditor class is used by PaletteEditor.
void changed(QWidget *widget)
#define QT_UTILITIES_EXPORT
Marks the symbol to be exported by the qtutilities library.
QT_UTILITIES_EXPORT void init()
Initiates the resources used and provided by this library.
Definition: resources.cpp:51