1 // qsynthPaletteForm.h
2 //
3 /****************************************************************************
4    Copyright (C) 2005-2020, rncbc aka Rui Nuno Capela. All rights reserved.
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    as published by the Free Software Foundation; either version 2
9    of the License, or (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20 *****************************************************************************/
21 
22 #ifndef __qsynthPaletteForm_h
23 #define __qsynthPaletteForm_h
24 
25 #include <QDialog>
26 #include <QPushButton>
27 #include <QAbstractTableModel>
28 #include <QItemDelegate>
29 #include <QSettings>
30 #include <QMap>
31 
32 
33 // Forward decls.
34 class QListView;
35 class QLabel;
36 class QToolButton;
37 
38 
39 //-------------------------------------------------------------------------
40 // qsynthPaletteForm
41 
42 namespace Ui { class qsynthPaletteForm; }
43 
44 
45 class qsynthPaletteForm: public QDialog
46 {
47 	Q_OBJECT
48 
49 public:
50 
51 	qsynthPaletteForm(QWidget *parent = nullptr,
52 		const QPalette& pal = QPalette());
53 
54 	virtual ~qsynthPaletteForm();
55 
56 	void setPalette(const QPalette& pal);
57 	const QPalette& palette() const;
58 
59 	void setSettings(QSettings *settings, bool owner = false);
60 	QSettings *settings() const;
61 
62 	void setPaletteName(const QString& name);
63 	QString paletteName() const;
64 
65 	bool isDirty() const;
66 
67 	static QStringList namedPaletteList(QSettings *settings);
68 	static bool namedPalette(QSettings *settings,
69 		const QString& name, QPalette& pal, bool fixup = false);
70 
71 	static QPalette::ColorRole colorRole(const QString& name);
72 
73 	class PaletteModel;
74 	class ColorDelegate;
75 	class ColorButton;
76 	class ColorEditor;
77 	class RoleEditor;
78 
79 protected slots:
80 
81 	void nameComboChanged(const QString& name);
82 	void saveButtonClicked();
83 	void deleteButtonClicked();
84 
85 	void generateButtonChanged();
86 	void resetButtonClicked();
87 	void detailsCheckClicked();
88 	void importButtonClicked();
89 	void exportButtonClicked();
90 
91 	void paletteChanged(const QPalette& pal);
92 
93 	void accept();
94 	void reject();
95 
96 protected:
97 
98 	void setPalette(const QPalette& pal, const QPalette& parentPal);
99 
100 	bool namedPalette(const QString& name, QPalette& pal);
101 	void saveNamedPalette(const QString& name, const QPalette& pal);
102 	void deleteNamedPalette(const QString& name);
103 	QStringList namedPaletteList();
104 
105 	void updateNamedPaletteList();
106 	void updateGenerateButton();
107 	void updateDialogButtons();
108 
109 	void setDefaultDir(const QString& dir);
110 	QString defaultDir() const;
111 
112 	void setShowDetails(bool on);
113 	bool isShowDetails() const;
114 
115 	void showEvent(QShowEvent *event);
116 	void resizeEvent(QResizeEvent *event);
117 
118 private:
119 
120 	Ui::qsynthPaletteForm *p_ui;
121 	Ui::qsynthPaletteForm& m_ui;
122 
123 	QSettings *m_settings;
124 	bool m_owner;
125 
126 	QPalette m_palette;
127 	QPalette m_parentPalette;
128 	PaletteModel *m_paletteModel;
129 	bool m_modelUpdated;
130 	bool m_paletteUpdated;
131 	int m_dirtyCount;
132 	int m_dirtyTotal;
133 };
134 
135 
136 //-------------------------------------------------------------------------
137 // qsynthPaletteForm::PaletteModel
138 
139 class qsynthPaletteForm::PaletteModel : public QAbstractTableModel
140 {
141 	Q_OBJECT
142 	Q_PROPERTY(QPalette::ColorRole colorRole READ colorRole)
143 
144 public:
145 
146 	PaletteModel(QObject *parent = nullptr);
147 
148 	int rowCount(const QModelIndex &parent = QModelIndex()) const;
149 	int columnCount(const QModelIndex &parent = QModelIndex()) const;
150 	QVariant data(const QModelIndex &index, int role) const;
151 	bool setData(const QModelIndex &index, const QVariant &value, int role);
152 	Qt::ItemFlags flags(const QModelIndex &index) const;
153 	QVariant headerData(int section, Qt::Orientation orientation,
154 		int role = Qt::DisplayRole) const;
155 
156 	void setPalette(const QPalette &palette, const QPalette &parentPalette);
157 	const QPalette& palette() const;
158 
setGenerate(bool on)159 	void setGenerate(bool on) { m_generate = on; }
160 
colorRole()161 	QPalette::ColorRole colorRole() const { return QPalette::NoRole; }
162 
163 signals:
164 
165 	void paletteChanged(const QPalette &palette);
166 
167 protected:
168 
169 	QPalette::ColorGroup columnToGroup(int index) const;
170 	int groupToColumn(QPalette::ColorGroup group) const;
171 
172 private:
173 
174 	QPalette m_palette;
175 	QPalette m_parentPalette;
176 	QMap<QPalette::ColorRole, QString> m_roleNames;
177 	int m_nrows;
178 	bool m_generate;
179 };
180 
181 
182 //-------------------------------------------------------------------------
183 // qsynthPaletteForm::ColorDelegate
184 
185 class qsynthPaletteForm::ColorDelegate : public QItemDelegate
186 {
187 public:
188 
189 	ColorDelegate(QObject *parent = nullptr)
QItemDelegate(parent)190 		: QItemDelegate(parent) {}
191 
192 	QWidget *createEditor(QWidget *parent,
193 		const QStyleOptionViewItem& option,
194 		const QModelIndex& index) const;
195 
196 	void setEditorData(QWidget *editor,
197 		const QModelIndex& index) const;
198 	void setModelData(QWidget *editor,
199 		QAbstractItemModel *model,
200 		const QModelIndex& index) const;
201 
202 	void updateEditorGeometry(QWidget *editor,
203 		const QStyleOptionViewItem& option,
204 		const QModelIndex &index) const;
205 
206 	virtual void paint(QPainter *painter,
207 		const QStyleOptionViewItem& option,
208 		const QModelIndex& index) const;
209 
210 	virtual QSize sizeHint(const QStyleOptionViewItem& option,
211 		const QModelIndex& index) const;
212 };
213 
214 
215 //-------------------------------------------------------------------------
216 // qsynthPaletteForm::ColorButton
217 
218 class qsynthPaletteForm::ColorButton : public QPushButton
219 {
220 	Q_OBJECT
221 	Q_PROPERTY(QBrush brush READ brush WRITE setBrush)
222 
223 public:
224 
225 	ColorButton (QWidget *parent = nullptr);
226 
227 	const QBrush& brush() const;
228 	void setBrush(const QBrush& b);
229 
230 signals:
231 
232 	void changed();
233 
234 protected slots:
235 
236 	void chooseColor();
237 
238 protected:
239 
240 	void paintEvent(QPaintEvent *event);
241 
242 private:
243 
244 	QBrush m_brush;
245 };
246 
247 
248 //-------------------------------------------------------------------------
249 // PaleteEditor::ColorEditor
250 
251 class qsynthPaletteForm::ColorEditor : public QWidget
252 {
253 	Q_OBJECT
254 
255 public:
256 
257 	ColorEditor(QWidget *parent = nullptr);
258 
259 	void setColor(const QColor &color);
260 	QColor color() const;
261 	bool changed() const;
262 
263 signals:
264 
265 	void changed(QWidget *widget);
266 
267 protected slots:
268 
269 	void colorChanged();
270 
271 private:
272 
273 	qsynthPaletteForm::ColorButton *m_button;
274 	bool  m_changed;
275 };
276 
277 
278 //-------------------------------------------------------------------------
279 // PaleteEditor::RoleEditor
280 
281 class qsynthPaletteForm::RoleEditor : public QWidget
282 {
283 	Q_OBJECT
284 
285 public:
286 
287 	RoleEditor(QWidget *parent = nullptr);
288 
289 	void setLabel(const QString &label);
290 	void setEdited(bool on);
291 	bool edited() const;
292 
293 signals:
294 
295 	void changed(QWidget *widget);
296 
297 protected slots:
298 
299 	void resetProperty();
300 
301 private:
302 
303 	QLabel *m_label;
304 	QToolButton *m_button;
305 	bool m_edited;
306 };
307 
308 
309 #endif // __qsynthPaletteForm_h
310 
311 // end of qsynthPaletteForm.h
312