1 // samplv1widget_param.h
2 //
3 /****************************************************************************
4    Copyright (C) 2012-2021, 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 __samplv1widget_param_h
23 #define __samplv1widget_param_h
24 
25 #include <QWidget>
26 #include <QDial>
27 #include <QDoubleSpinBox>
28 #include <QButtonGroup>
29 #include <QGroupBox>
30 
31 
32 // Forward declarations.
33 class QLabel;
34 class QComboBox;
35 class QCheckBox;
36 
37 
38 //-------------------------------------------------------------------------
39 // samplv1widget_param - Custom composite widget.
40 
41 class samplv1widget_param : public QWidget
42 {
43 	Q_OBJECT
44 
45 public:
46 
47 	// Constructor.
48 	samplv1widget_param(QWidget *pParent = 0);
49 
50 	// Accessors.
51 	virtual void setText(const QString& sText);
52 	virtual QString text() const;
53 
54 	virtual void setMaximum(float fMaximum);
55 	float maximum() const;
56 
57 	virtual void setMinimum(float fMinimum);
58 	float minimum() const;
59 
60 	void resetDefaultValue();
61 	bool isDefaultValue() const;
62 	void setDefaultValue(float fDefaultValue);
63 	float defaultValue() const;
64 
65 	virtual QString valueText() const;
66 	float value() const;
67 
68 	// Scale multiplier accessors.
69 	void setScale(float fScale);
70 	float scale() const;
71 
72 public slots:
73 
74 	// Virtual accessor.
75 	virtual void setValue(float fValue);
76 
77 signals:
78 
79 	// Change signal.
80 	void valueChanged(float);
81 
82 protected:
83 
84 	// Mouse behavior event handler.
85 	void mousePressEvent(QMouseEvent *pMouseEvent);
86 
87 	// Scale/value converters.
88 	float scaleFromValue(float fValue) const;
89 	float valueFromScale(float fScale) const;
90 
91 private:
92 
93 	// Current value.
94 	float m_fValue;
95 
96 	// Current value range.
97 	float m_fMinimum;
98 	float m_fMaximum;
99 
100 	// Default value.
101 	float m_fDefaultValue;
102 	int   m_iDefaultValue;
103 
104 	// Scale multiplier (default=100).
105 	float m_fScale;
106 };
107 
108 
109 //-------------------------------------------------------------------------
110 // samplv1widget_dial - A better QDial widget.
111 
112 class samplv1widget_dial : public QDial
113 {
114 	Q_OBJECT
115 
116 public:
117 
118 	// Constructor.
119 	samplv1widget_dial(QWidget *pParent = 0);
120 
121 	// Dial mode behavior:
122 	// DefaultMode - default (old) QDial behavior.
123 	// LinearMode  - proportionally to distance in one ortogonal axis.
124 	// AngularMode - angularly relative to widget center.
125 	enum DialMode { DefaultMode = 0, LinearMode, AngularMode };
126 
127 	// Set knob dial mode behavior.
128 	static void setDialMode(DialMode dialMode);
129 	static DialMode dialMode();
130 
131 protected:
132 
133 	// Mouse angle determination.
134 	float mouseAngle(const QPoint& pos);
135 
136 	// Alternate mouse behavior event handlers.
137 	void mousePressEvent(QMouseEvent *pMouseEvent);
138 	void mouseMoveEvent(QMouseEvent *pMouseEvent);
139 	void mouseReleaseEvent(QMouseEvent *pMouseEvent);
140 
141 private:
142 
143 	// Alternate mouse behavior tracking.
144 	bool   m_bMousePressed;
145 	QPoint m_posMouse;
146 
147 	// Just for more precission on the movement
148 	float m_fLastDragValue;
149 
150 	// Knob dial mode behavior.
151 	static DialMode g_dialMode;
152 };
153 
154 
155 //-------------------------------------------------------------------------
156 // samplv1widget_knob - Custom knob/dial widget.
157 
158 class samplv1widget_knob : public samplv1widget_param
159 {
160 	Q_OBJECT
161 
162 public:
163 
164 	// Constructor.
165 	samplv1widget_knob(QWidget *pParent = 0);
166 
167 	// Accessors.
168 	void setText(const QString& sText);
169 	QString text() const;
170 
171 	void setMaximum(float fMaximum);
172 	void setMinimum(float fMinimum);
173 
174 public slots:
175 
176 	// Virtual accessor.
177 	void setValue(float fValue);
178 
179 protected slots:
180 
181 	// Dial change slot.
182 	void dialValueChanged(int);
183 
184 protected:
185 
186 	// Scale-step accessors.
187 	void setSingleStep(float fSingleStep);
188 	float singleStep() const;
189 
190 private:
191 
192 	// Widget members.
193 	QLabel *m_pLabel;
194 
195 	samplv1widget_dial *m_pDial;
196 };
197 
198 
199 //-------------------------------------------------------------------------
200 // samplv1widget_edit - A better QDoubleSpinBox widget.
201 
202 class samplv1widget_edit : public QDoubleSpinBox
203 {
204 	Q_OBJECT
205 
206 public:
207 
208 	// Constructor.
209 	samplv1widget_edit(QWidget *pParent = 0);
210 
211 	// Edit mode behavior:
212 	// DefaultMode - default (immediate value changes) behavior.
213 	// DeferredMode - deferred value changes (to when editing is finished).
214 	enum EditMode { DefaultMode = 0, DeferredMode };
215 
216 	// Set spin-box edit mode behavior.
217 	static void setEditMode(EditMode editMode);
218 	static EditMode editMode();
219 
220 protected slots:
221 
222 	// Alternate value change behavior handlers.
223 	void lineEditTextChanged(const QString&);
224 	void spinBoxEditingFinished();
225 	void spinBoxValueChanged(double);
226 
227 signals:
228 
229 	// Alternate value change signal.
230 	void valueChangedEx(double);
231 
232 protected:
233 
234 	// Inherited/override methods.
235 	QValidator::State validate(QString& sText, int& iPos) const;
236 
237 private:
238 
239 	// Alternate edit behavior tracking.
240 	int m_iTextChanged;
241 
242 	// Spin-box edit mode behavior.
243 	static EditMode g_editMode;
244 };
245 
246 
247 //-------------------------------------------------------------------------
248 // samplv1widget_spin - Custom knob/spin-box widget.
249 
250 class samplv1widget_spin : public samplv1widget_knob
251 {
252 	Q_OBJECT
253 
254 public:
255 
256 	// Constructor.
257 	samplv1widget_spin(QWidget *pParent = 0);
258 
259 	// Virtual accessors.
260 	void setMaximum(float fMaximum);
261 	void setMinimum(float fMinimum);
262 
263 	QString valueText() const;
264 
265 	// Specialized accessors.
266 	void setSpecialValueText(const QString& sText);
267 	QString specialValueText() const;
268 
269 	bool isSpecialValue() const;
270 
271 	void setDecimals(int iDecimals);
272 	int decimals() const;
273 
274 public slots:
275 
276 	// Virtual accessor.
277 	void setValue(float fValue);
278 
279 protected slots:
280 
281 	// Change slot.
282 	void spinBoxValueChanged(double);
283 
284 private:
285 
286 	// Widget members.
287 	samplv1widget_edit *m_pSpinBox;
288 };
289 
290 
291 //-------------------------------------------------------------------------
292 // samplv1widget_combo - Custom knob/combo-box widget.
293 
294 class samplv1widget_combo : public samplv1widget_knob
295 {
296 	Q_OBJECT
297 
298 public:
299 
300 	// Constructor.
301 	samplv1widget_combo(QWidget *pParent = 0);
302 
303 	// Virtual accessors.
304 	QString valueText() const;
305 
306 	// Specialized accessors.
307 	void insertItems(int iIndex, const QStringList& items);
308 	void clear();
309 
310 public slots:
311 
312 	// Virtual accessor.
313 	void setValue(float fValue);
314 
315 protected slots:
316 
317 	// Change slot.
318 	void comboBoxValueChanged(int);
319 
320 protected:
321 
322 	// Reimplemented mouse-wheel stepping.
323 	void wheelEvent(QWheelEvent *pWheelEvent);
324 
325 private:
326 
327 	// Widget members.
328 	QComboBox *m_pComboBox;
329 };
330 
331 
332 //-------------------------------------------------------------------------
333 // samplv1widget_radio - Custom radio-button widget.
334 
335 class samplv1widget_radio : public samplv1widget_param
336 {
337 	Q_OBJECT
338 
339 public:
340 
341 	// Constructor.
342 	samplv1widget_radio(QWidget *pParent = 0);
343 
344 	// Desstructor.
345 	~samplv1widget_radio();
346 
347 	// Virtual accessors.
348 	QString valueText() const;
349 
350 	// Specialized accessors.
351 	void insertItems(int iIndex, const QStringList& items);
352 	void clear();
353 
354 public slots:
355 
356 	// Virtual accessor.
357 	void setValue(float fValue);
358 
359 protected slots:
360 
361 	// Change slot.
362 	void radioGroupValueChanged(int);
363 
364 private:
365 
366 	// Widget members.
367 	QButtonGroup m_group;
368 };
369 
370 
371 //-------------------------------------------------------------------------
372 // samplv1widget_check - Custom check-box widget.
373 
374 class samplv1widget_check : public samplv1widget_param
375 {
376 	Q_OBJECT
377 
378 public:
379 
380 	// Constructor.
381 	samplv1widget_check(QWidget *pParent = 0);
382 
383 	// Desstructor.
384 	~samplv1widget_check();
385 
386 	// Accessors.
387 	void setText(const QString& sText);
388 	QString text() const;
389 
390 	void setAlignment(Qt::Alignment alignment);
391 	Qt::Alignment alignment() const;
392 
393 public slots:
394 
395 	// Virtual accessor.
396 	void setValue(float fValue);
397 
398 protected slots:
399 
400 	// Change slot.
401 	void checkBoxValueChanged(bool);
402 
403 private:
404 
405 	// Widget members.
406 	QCheckBox *m_pCheckBox;
407 
408 	Qt::Alignment m_alignment;
409 };
410 
411 
412 //-------------------------------------------------------------------------
413 // samplv1widget_group - Custom checkable group-box widget.
414 
415 class samplv1widget_group : public QGroupBox
416 {
417 	Q_OBJECT
418 
419 public:
420 
421 	// Constructor.
422 	samplv1widget_group(QWidget *pParent = 0);
423 
424 	// Desstructor.
425 	~samplv1widget_group();
426 
427 	// Accessors.
428 	void setToolTip(const QString& sToolTip);
429 	QString toolTip() const;
430 
431 	samplv1widget_param *param() const;
432 
433 protected slots:
434 
435 	// Change slot.
436 	void paramValueChanged(float);
437 	void groupBoxValueChanged(bool);
438 
439 private:
440 
441 	// Widget members.
442 	samplv1widget_param *m_pParam;
443 };
444 
445 
446 #endif  // __samplv1widget_param_h
447 
448 // end of samplv1widget_param.h
449