1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#ifndef PALETTEEDITOR_H
30#define PALETTEEDITOR_H
31
32#include "ui_paletteeditor.h"
33#include <QtWidgets/qitemdelegate.h>
34
35QT_BEGIN_NAMESPACE
36
37class QAction;
38class QListView;
39class QMenu;
40class QLabel;
41class QtColorButton;
42class QDesignerFormEditorInterface;
43
44namespace qdesigner_internal {
45
46class PaletteEditor: public QDialog
47{
48 Q_OBJECT
49public:
50 ~PaletteEditor() override;
51
52 static QPalette getPalette(QDesignerFormEditorInterface *core,
53 QWidget* parent, const QPalette &init = QPalette(),
54 const QPalette &parentPal = QPalette(), int *result = nullptr);
55
56 QPalette palette() const;
57 void setPalette(const QPalette &palette);
58 void setPalette(const QPalette &palette, const QPalette &parentPalette);
59
60private slots:
61
62 void on_buildButton_colorChanged(const QColor &);
63 void on_activeRadio_clicked();
64 void on_inactiveRadio_clicked();
65 void on_disabledRadio_clicked();
66 void on_computeRadio_clicked();
67 void on_detailsRadio_clicked();
68
69 void paletteChanged(const QPalette &palette);
70 void viewContextMenuRequested(const QPoint &pos);
71 void save();
72 void load();
73
74protected:
75
76private:
77 PaletteEditor(QDesignerFormEditorInterface *core, QWidget *parent);
78 void buildPalette();
79
80 void updatePreviewPalette();
81 void updateStyledButton();
82
83 QPalette::ColorGroup currentColorGroup() const
84 { return m_currentColorGroup; }
85
86 Ui::PaletteEditor ui;
87 QPalette m_editPalette;
88 QPalette m_parentPalette;
89 class PaletteModel *m_paletteModel;
90 QDesignerFormEditorInterface *m_core;
91 QAction *m_lighterAction = nullptr;
92 QAction *m_darkerAction = nullptr;
93 QAction *m_copyColorAction = nullptr;
94 QMenu *m_contextMenu = nullptr;
95 QPalette::ColorGroup m_currentColorGroup = QPalette::Active;
96 bool m_modelUpdated = false;
97 bool m_paletteUpdated = false;
98 bool m_compute = true;
99};
100
101
102class PaletteModel : public QAbstractTableModel
103{
104 Q_OBJECT
105 Q_PROPERTY(QPalette::ColorRole colorRole READ colorRole)
106public:
107 explicit PaletteModel(QObject *parent = nullptr);
108
109 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
110 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
111 QVariant data(const QModelIndex &index, int role) const override;
112 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
113 Qt::ItemFlags flags(const QModelIndex &index) const override;
114 QVariant headerData(int section, Qt::Orientation orientation,
115 int role = Qt::DisplayRole) const override;
116
117 QPalette getPalette() const;
118 void setPalette(const QPalette &palette, const QPalette &parentPalette);
119
120 QBrush brushAt(const QModelIndex &index) const;
121
122 QPalette::ColorRole colorRole() const { return QPalette::NoRole; }
123 void setCompute(bool on) { m_compute = on; }
124signals:
125 void paletteChanged(const QPalette &palette);
126private:
127 struct RoleEntry
128 {
129 QString name;
130 QPalette::ColorRole role;
131 };
132
133 QPalette::ColorGroup columnToGroup(int index) const;
134 int groupToColumn(QPalette::ColorGroup group) const;
135 QPalette::ColorRole roleAt(int row) const { return m_roleEntries.at(i: row).role; }
136 int rowOf(QPalette::ColorRole role) const;
137
138 QPalette m_palette;
139 QPalette m_parentPalette;
140 QVector<RoleEntry> m_roleEntries;
141 bool m_compute = true;
142};
143
144class BrushEditor : public QWidget
145{
146 Q_OBJECT
147public:
148 explicit BrushEditor(QDesignerFormEditorInterface *core, QWidget *parent = nullptr);
149
150 void setBrush(const QBrush &brush);
151 QBrush brush() const;
152 bool changed() const;
153signals:
154 void changed(QWidget *widget);
155private slots:
156 void brushChanged();
157private:
158 QtColorButton *m_button;
159 bool m_changed = false;
160 QDesignerFormEditorInterface *m_core;
161};
162
163class RoleEditor : public QWidget
164{
165 Q_OBJECT
166public:
167 explicit RoleEditor(QWidget *parent = nullptr);
168
169 void setLabel(const QString &label);
170 void setEdited(bool on);
171 bool edited() const;
172signals:
173 void changed(QWidget *widget);
174private slots:
175 void emitResetProperty();
176private:
177 QLabel *m_label;
178 bool m_edited = false;
179};
180
181class ColorDelegate : public QItemDelegate
182{
183 Q_OBJECT
184
185public:
186 explicit ColorDelegate(QDesignerFormEditorInterface *core, QObject *parent = nullptr);
187
188 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
189 const QModelIndex &index) const override;
190
191 void setEditorData(QWidget *ed, const QModelIndex &index) const override;
192 void setModelData(QWidget *ed, QAbstractItemModel *model,
193 const QModelIndex &index) const override;
194
195 void updateEditorGeometry(QWidget *ed, const QStyleOptionViewItem &option,
196 const QModelIndex &index) const override;
197
198 void paint(QPainter *painter, const QStyleOptionViewItem &opt,
199 const QModelIndex &index) const override;
200 QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const override;
201private:
202 QDesignerFormEditorInterface *m_core;
203};
204
205} // namespace qdesigner_internal
206
207QT_END_NAMESPACE
208
209#endif // PALETTEEDITOR_H
210

source code of qttools/src/designer/src/components/propertyeditor/paletteeditor.h