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