1 //=============================================================================
2 // Awl
3 // Audio Widget Library
4 //
5 // Copyright (C) 2002-2006 by Werner Schweer and others
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19
20 #include "styledslider.h"
21
22 #include <QStyle>
23 #include <QStylePainter>
24 #include <QStyleOptionSlider>
25 #include <QMouseEvent>
26 #include <QDebug>
27
28 namespace Awl {
29
30 //---------------------------------------------------------
31 // StyledSlider
32 //---------------------------------------------------------
33
StyledSlider(QWidget * parent)34 StyledSlider::StyledSlider(QWidget *parent) : QWidget(parent)
35 {
36 setFocusPolicy(Qt::StrongFocus);
37 }
38
39
40
mouseDoubleClickEvent(QMouseEvent *)41 void StyledSlider::mouseDoubleClickEvent(QMouseEvent*)
42 {
43 setValue(_doubleClickValue);
44 }
45
46
47
48 //---------------------------------------------------------
49 // wheelEvent
50 //---------------------------------------------------------
51
wheelEvent(QWheelEvent * e)52 void StyledSlider::wheelEvent(QWheelEvent* e)
53 {
54 QPoint ad = e->angleDelta();
55 //120 degrees is one tick
56 auto value = _value + ad.y() / 120;
57 value = qBound(_minValue, value, _maxValue);
58 setValue(value);
59 update();
60 }
61
62 //---------------------------------------------------------
63 // keyPressEvent
64 //---------------------------------------------------------
65
keyPressEvent(QKeyEvent * e)66 void StyledSlider::keyPressEvent(QKeyEvent* e)
67 {
68 switch (e->key())
69 {
70 case Qt::Key_Up:
71 setValue(_value + 1);
72 break;
73 case Qt::Key_Down:
74 setValue(_value - 1);
75 break;
76 }
77 }
78
79 //---------------------------------------------------------
80 // mousePressEvent
81 //---------------------------------------------------------
82
mousePressEvent(QMouseEvent * e)83 void StyledSlider::mousePressEvent(QMouseEvent* e)
84 {
85 draggingMouse = true;
86 mouseDownPos = e->pos();
87 mouseDownVal = _value;
88 emit sliderPressed();
89 }
90
91 //---------------------------------------------------------
92 // mouseReleaseEvent
93 //---------------------------------------------------------
94
mouseReleaseEvent(QMouseEvent *)95 void StyledSlider::mouseReleaseEvent(QMouseEvent*)
96 {
97 draggingMouse = false;
98
99 }
100
101 //---------------------------------------------------------
102 // mouseMoveEvent
103 //---------------------------------------------------------
104
mouseMoveEvent(QMouseEvent * e)105 void StyledSlider::mouseMoveEvent(QMouseEvent* e)
106 {
107 if (draggingMouse) {
108 QPoint p = e->pos();
109 double dPixY = p.y() - mouseDownPos.y();
110 double barLength = height() - (_margin * 2);
111 double dy = dPixY * (_maxValue - _minValue) / barLength;
112
113 double val = qBound(_minValue, mouseDownVal - dy, _maxValue);
114
115 setValue(val);
116 update();
117 }
118 }
119
120 //---------------------------------------------------------
121 // paintEvent
122 //---------------------------------------------------------
123
paintEvent(QPaintEvent * ev)124 void StyledSlider::paintEvent(QPaintEvent *ev)
125 {
126 QWidget::paintEvent(ev);
127
128 QPainter p(this);
129 //QRect geo = this->geometry();
130
131 int w = width();
132 int h = height();
133
134 double barLength = h - _margin * 2;
135 double x0 = (w - _barThickness) / 2;
136 // double valueSpan = _maxValue - _minValue;
137 double span = (_value - _minValue) / (_maxValue - _minValue);
138 span = qMin(qMax(span, 0.0), 1.0);
139 double y1 = (1 - span) * barLength;
140
141 double midPtPix = w / 2.0;
142
143 //Ticks
144 p.setPen(QPen(_tickColor));
145
146 // for (qreal yVal = _minValue; yVal <= _maxValue + .00001; yVal += _numMinorTicks) {
147 // qreal yPix = (yVal - _minValue) / valueSpan;
148 // yPix = (1.0 - yPix) * barLength + _margin;
149 // QLineF line(midPtPix - _minorTickWidth, yPix, midPtPix + _minorTickWidth, yPix);
150 // p.drawLine(line);
151 // }
152
153 // for (qreal yVal = _minValue; yVal <= _maxValue + .00001; yVal += _numMajorTicks) {
154 // qreal yPix = (yVal - _minValue) / valueSpan;
155 // yPix = (1.0 - yPix) * barLength + _margin;
156 // QLineF line(midPtPix - _majorTickWidth, yPix, midPtPix + _majorTickWidth, yPix);
157 // p.drawLine(line);
158
159 // for (int i = 1; i < _numMinorTicks; ++i) {
160 // qreal yyPix = yPix + (i * _numMajorTicks) / _numMinorTicks;
161 // QLineF line(midPtPix - _minorTickWidth, yyPix, midPtPix + _minorTickWidth, yyPix);
162 // p.drawLine(line);
163 // }
164
165 // }
166
167 for (int i = 0; i <= _numMajorTicks; ++i) {
168 qreal yVal = i * barLength / _numMajorTicks + _margin;
169 QLineF line(midPtPix - _majorTickWidth, yVal, midPtPix + _majorTickWidth, yVal);
170 p.drawLine(line);
171
172 if (i < _numMajorTicks) {
173 qreal yValNext = (i + 1) * barLength / _numMajorTicks + _margin;
174 for (int j = 1; j < _numMinorTicks; ++j) {
175 qreal yyVal = yVal + j * (yValNext - yVal) / _numMinorTicks;
176
177 QLineF line1(midPtPix - _minorTickWidth, yyVal, midPtPix + _minorTickWidth, yyVal);
178 p.drawLine(line1);
179 }
180 }
181 }
182
183 //Marks
184 // p.setPen(QPen(_tickColor, 2));
185
186 // for (double mark: marks) {
187 // qreal yPix = (mark - _minValue) / valueSpan;
188 // yPix = (1.0 - yPix) * barLength + _margin;
189
190 // QLineF line(midPtPix - _majorTickWidth, yPix, midPtPix + _majorTickWidth, yPix);
191 // p.drawLine(line);
192 // }
193
194
195 //Bars
196 double yPix = _margin + y1;
197 QRectF bgRect(x0, _margin, _barThickness, barLength);
198 QRectF hiliteRect(x0, yPix, _barThickness, barLength - y1);
199
200 p.fillRect(bgRect, QBrush(_backgroundColor));
201
202 p.fillRect(hiliteRect, QBrush(_hilightColor));
203
204 //Slider head
205 if (!_sliderHeadIcon.isNull()) {
206 QRect r((int)midPtPix - 14, (int)yPix - 20, (int)28, (int)40);
207 _sliderHeadIcon.paint(&p, r);
208 }
209 }
210
211 //---------------------------------------------------------
212 // setValue
213 //---------------------------------------------------------
214
setValue(double v)215 void StyledSlider::setValue(double v)
216 {
217 if (v == _value)
218 return;
219
220 _value = v;
221 update();
222 emit valueChanged(v);
223 }
224
225 //---------------------------------------------------------
226 // setMinValue
227 //---------------------------------------------------------
228
setMinValue(double v)229 void StyledSlider::setMinValue(double v)
230 {
231 if (v == _minValue)
232 return;
233 _minValue = v;
234 update();
235 emit minValueChanged(v);
236 }
237
238 //---------------------------------------------------------
239 // setMaxValue
240 //---------------------------------------------------------
241
setMaxValue(double v)242 void StyledSlider::setMaxValue(double v)
243 {
244 if (v == _maxValue)
245 return;
246 _maxValue = v;
247 update();
248 emit maxValueChanged(v);
249 }
250
251 //---------------------------------------------------------
252 // setBackgroundColor
253 //---------------------------------------------------------
254
setBackgroundColor(QColor v)255 void StyledSlider::setBackgroundColor(QColor v)
256 {
257 _backgroundColor = v;
258 update();
259 }
260
261 //---------------------------------------------------------
262 // setHilightColor
263 //---------------------------------------------------------
264
setHilightColor(QColor v)265 void StyledSlider::setHilightColor(QColor v)
266 {
267 _hilightColor = v;
268 update();
269 }
270
271 //---------------------------------------------------------
272 // setTickColor
273 //---------------------------------------------------------
274
setTickColor(QColor v)275 void StyledSlider::setTickColor(QColor v)
276 {
277 _tickColor = v;
278 update();
279 }
280
281 //---------------------------------------------------------
282 // setBarThickness
283 //---------------------------------------------------------
284
setBarThickness(double v)285 void StyledSlider::setBarThickness(double v)
286 {
287 _barThickness = v;
288 update();
289 }
290
291 //---------------------------------------------------------
292 // setMargin
293 //---------------------------------------------------------
294
setMargin(double v)295 void StyledSlider::setMargin(double v)
296 {
297 _margin = v;
298 update();
299 }
300
301 //---------------------------------------------------------
302 // setMajorTickSpacing
303 //---------------------------------------------------------
304
setNumMajorTicks(int v)305 void StyledSlider::setNumMajorTicks(int v)
306 {
307 _numMajorTicks = v;
308 update();
309 }
310
311 //---------------------------------------------------------
312 // setMinorTickSpacing
313 //---------------------------------------------------------
314
setNumMinorTicks(int v)315 void StyledSlider::setNumMinorTicks(int v)
316 {
317 _numMinorTicks = v;
318 update();
319 }
320
321 //---------------------------------------------------------
322 // setMajorTickWidth
323 //---------------------------------------------------------
324
setMajorTickWidth(double v)325 void StyledSlider::setMajorTickWidth(double v)
326 {
327 _majorTickWidth = v;
328 update();
329 }
330
331 //---------------------------------------------------------
332 // setMinorTickWidth
333 //---------------------------------------------------------
334
setMinorTickWidth(double v)335 void StyledSlider::setMinorTickWidth(double v)
336 {
337 _minorTickWidth = v;
338 update();
339 }
340
341 //---------------------------------------------------------
342 // setSliderHeadIcon
343 //---------------------------------------------------------
344
setSliderHeadIcon(QIcon v)345 void StyledSlider::setSliderHeadIcon(QIcon v)
346 {
347 _sliderHeadIcon = v;
348 update();
349 }
350
351 }
352