1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
//=========================================================
// MusE
// Linux Music Editor
// $Id: sliderbase.cpp,v 1.4.2.4 2007/01/27 14:52:43 spamatica Exp $
// Copyright (C) 1997 Josef Wilgen
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2,
// as published by the Free Software Foundation.
//
// (C) Copyright 1999 Werner Schweer (ws@seh.de)
//=========================================================
#include <cmath>
#include "sliderbase.h"
#include "mmath.h"
#include <QWheelEvent>
#include <QMouseEvent>
#include <QTimerEvent>
// DESCRIPTION
// SliderBase is a base class for
// slider widgets. QwtSliderBase handles the mouse events
// and updates the slider's value accordingly. Derived classes
// only have to implement the @QwtSliderBase::getValue@ and
// @QwtSliderBase::getScrollMode@ members, and should react to a
// @QwtSliderbase::valueChange@, which normally requires repainting.
//------------------------------------------------------------
//.F SliderBase::SliderBase
// Constructor
//
//.u Syntax
//.f SliderBase::SliderBase(QWidget *parent, const char *name)
//
//.u Parameters
//.p QWidget *parent, const char *name
//
//------------------------------------------------------------
SliderBase::SliderBase(QWidget *parent, const char *name)
: QWidget(parent, name)
{
_id = -1;
_cursorHoming = false;
_ignoreMouseMove = false;
d_tmrID = 0;
d_updTime = 150;
d_mass = 0.0;
d_tracking = true;
d_mouseOffset = 0.0;
d_scrollMode = ScrNone;
setRange(0.0, 1.0, 0.1);
}
//------------------------------------------------------------
//.F SliderBase::~SliderBase
// Destructor
//
//.u Syntax
//.f SliderBase::~SliderBase()
//------------------------------------------------------------
SliderBase::~SliderBase()
{
if (d_tmrID)
killTimer(d_tmrID);
}
//------------------------------------------------------------
//.F void SliderBase::wheelEvent(QWheelEvent *e)
// Add wheel event handling
//
//.u Syntax
//.f void SliderBase::wheelEvent(QWheelEvent *e)
//------------------------------------------------------------
void SliderBase::wheelEvent(QWheelEvent *e)
{
// Avoid unwanted wheel events from outside the control.
// Just in case it grabs focus somehow.
// Tested: No go, can't seem to determine where event came from.
/*
const QPoint gp = mapToGlobal(e->pos());
const QRect gr = QRect(mapToGlobal(rect().topLeft()), mapToGlobal(rect().bottomRight()));
if(!gr.contains(gp))
{
e->ignore();
return;
} */
e->accept();
float inc = (maxValue() - minValue()) / 40;
if (e->state() == Qt::ShiftModifier)
inc = inc / 10;
if(inc < step())
inc = step();
if(e->delta() > 0)
setValue(value()+inc);
else
setValue(value()-inc);
emit sliderMoved(value(), _id);
}
//------------------------------------------------------------
//.F SliderBase::stopMoving
// Stop updating if automatic scrolling is active
//
//.u Syntax
//.f void SliderBase::stopMoving()
//------------------------------------------------------------
void SliderBase::stopMoving()
{
if(d_tmrID) {
killTimer(d_tmrID);
d_tmrID = 0;
}
}
//------------------------------------------------------------
//.F SliderBase::setUpdateTime
// Specify the update interval for automatic scrolling
//
//.u Syntax
//.f void SliderBase::setUpdateTime(int t)
//
//.u Parameters
//.p int t -- update interval in milliseconds
//
//.u See also
// @SliderBase::getScrollMode@
//------------------------------------------------------------
void SliderBase::setUpdateTime(int t)
{
if (t < 50)
t = 50;
d_updTime = t;
}
//------------------------------------------------------------
//.F SliderBase::mousePressEvent
// Mouse press event handler
//
//.u Syntax
//.f void SliderBase::mousePressEvent(QMouseEvent *e)
//
//.u Parameters
//.p QMouseEvent *e -- Qt Mouse press event
//------------------------------------------------------------
void SliderBase::mousePressEvent(QMouseEvent *e)
{
QPoint p = e->pos();
const Qt::ButtonState button = e->button();
d_timerTick = 0;
getScrollMode(p, button, d_scrollMode, d_direction);
stopMoving();
switch(d_scrollMode) {
case ScrPage:
case ScrTimer:
d_mouseOffset = 0;
DoubleRange::incPages(d_direction);
emit sliderMoved(value(), _id);
d_tmrID = startTimer(qwtMax(250, 2 * d_updTime));
break;
case ScrMouse:
d_speed = 0;
if(button == Qt::RightButton)
{
emit sliderRightClicked(e->globalPos(), _id);
break;
}
d_time.start();
if(_cursorHoming && button == Qt::LeftButton)
{
_ignoreMouseMove = true;
d_mouseOffset = 0.0;
}
else
d_mouseOffset = getValue(p) - value();
emit sliderPressed(_id);
break;
default:
d_mouseOffset = 0;
d_direction = 0;
break;
}
}
//------------------------------------------------------------
//.-
//.F SliderBase::buttonRelease
// Emit a valueChanged() signal if necessary
//
//.u Syntax
//.f void SliderBase::buttonReleased()
//
//------------------------------------------------------------
void SliderBase::buttonReleased()
{
if ((!d_tracking) || (value() != prevValue()))
emit valueChanged(value(), _id);
}
//------------------------------------------------------------
//
//.F SliderBase::mouseReleaseEvent
// Mouse Release Event handler
//
//.u Syntax
//.f void SliderBase::mouseReleaseEvent(QMouseEvent *e)
//
//.u Parameters
//.p QMouseEvent *e -- Qt Mouse Event
//
//------------------------------------------------------------
void SliderBase::mouseReleaseEvent(QMouseEvent *e)
{
int ms = 0;
/*double inc = step(); */ // prevent compiler warning: unused variable
_ignoreMouseMove = false;
const Qt::ButtonState button = e->button();
switch(d_scrollMode)
{
case ScrMouse:
if(button == Qt::RightButton)
{
d_scrollMode = ScrNone;
break;
}
if(_cursorHoming && button == Qt::LeftButton)
d_scrollMode = ScrNone;
else
{
setPosition(e->pos());
d_direction = 0;
d_mouseOffset = 0;
if (d_mass > 0.0)
{
ms = d_time.elapsed();
if ((fabs(d_speed) > 0.0) && (ms < 50))
d_tmrID = startTimer(d_updTime);
}
else
{
d_scrollMode = ScrNone;
buttonReleased();
}
}
emit sliderReleased(_id);
break;
case ScrDirect:
setPosition(e->pos());
d_direction = 0;
d_mouseOffset = 0;
d_scrollMode = ScrNone;
buttonReleased();
break;
case ScrPage:
stopMoving();
d_timerTick = 0;
buttonReleased();
d_scrollMode = ScrNone;
break;
case ScrTimer:
stopMoving();
d_timerTick = 0;
buttonReleased();
d_scrollMode = ScrNone;
break;
default:
d_scrollMode = ScrNone;
buttonReleased();
}
}
//------------------------------------------------------------
//
//.F SliderBase::setPosition
// Move the slider to a specified point, adjust the value
// and emit signals if necessary
//
//.u Syntax
//.f void SliderBase::setPosition(const QPoint &p)
//
//.u Parameters
//.p const QPoint &p
//
//------------------------------------------------------------
void SliderBase::setPosition(const QPoint &p)
{
DoubleRange::fitValue(getValue(p) - d_mouseOffset);
}
//------------------------------------------------------------
//
//.F SliderBase::setTracking
//
// Enables or disables tracking.
//
//.u Syntax
//.f void SliderBase::setTracking(bool enable)
//
//.u Parameters
//.p bool enable -- enable (TRUE) or disable (FALSE) tracking
//
//.u Description
//
// If tracking is enabled, the slider emits a
// valueChanged() signal whenever its value
// changes (the default behaviour). If tracking
// is disabled, the value changed() signal will only
// be emitted if
//.i -- the user releases the mouse
// button and the value has changed or
// -- at the end of automatic scrolling.
//.P
// Tracking is enabled by default.
//------------------------------------------------------------
void SliderBase::setTracking(bool enable)
{
d_tracking = enable;
}
//------------------------------------------------------------
//.-
//.F SliderBase::mouseMoveEvent
// Mouse Move Event handler
//
//.u Syntax
//.f void SliderBase::mouseMoveEvent(QMouseEvent *e)
//
//.u Parameters
//.p QMouseEvent *e -- Qt Mouse Move Event
//
//------------------------------------------------------------
void SliderBase::mouseMoveEvent(QMouseEvent *e)
{
if(_ignoreMouseMove)
{
_ignoreMouseMove = false;
return;
}
double ms = 0.0;
if (d_scrollMode == ScrMouse )
{
setPosition(e->pos());
if (d_mass > 0.0)
{
ms = double(d_time.elapsed());
if (ms < 1.0) ms = 1.0;
d_speed = (exactValue() - exactPrevValue()) / ms;
d_time.start();
}
if (value() != prevValue())
emit sliderMoved(value(), _id);
}
}
//------------------------------------------------------------
//
//.F SliderBase::timerEvent
// Timer event handler
//
//.u Syntax
//.f void SliderBase::timerEvent(QTimerEvent *e)
//
//.u Parameters
//.p QTimerEvent *e -- Qt timer event
//
//------------------------------------------------------------
void SliderBase::timerEvent(QTimerEvent*)
{
double newval;
double inc = step();
switch (d_scrollMode)
{
case ScrMouse:
if (d_mass > 0.0)
{
d_speed *= exp( - double(d_updTime) * 0.001 / d_mass );
newval = exactValue() + d_speed * double(d_updTime);
DoubleRange::fitValue(newval);
// stop if d_speed < one step per second
if (fabs(d_speed) < 0.001 * fabs(step()))
{
d_speed = 0;
stopMoving();
buttonReleased();
}
}
else
stopMoving();
break;
case ScrPage:
DoubleRange::incPages(d_direction);
if (value() != prevValue())
emit sliderMoved(value(), _id);
if (!d_timerTick)
{
killTimer(d_tmrID);
d_tmrID = startTimer(d_updTime);
}
break;
case ScrTimer:
DoubleRange::fitValue(value() + double(d_direction) * inc);
if (value() != prevValue())
emit sliderMoved(value(), _id);
if (!d_timerTick)
{
killTimer(d_tmrID);
d_tmrID = startTimer(d_updTime);
}
break;
default:
stopMoving();
break;
}
d_timerTick = 1;
}
//------------------------------------------------------------
//
//.F SliderBase::valueChange
// Notify change of value
//
//.u Syntax
//.f void SliderBase::valueChange()
//
//.u Parameters
//.p double x -- new value
//
//.u Description
// This function can be reimplemented by derived classes
// in order to keep track of changes, i.e. repaint the widget.
// The default implementation emits a valueChanged() signal
// if tracking is enabled.
//
//------------------------------------------------------------
void SliderBase::valueChange()
{
if (d_tracking)
emit valueChanged(value(), _id);
}
//------------------------------------------------------------
//
//.F SliderBase::setMass
// Set the slider's mass for flywheel effect.
//
//.u Syntax
//.f void SliderBase::setMass(double val)
//
//.u Parameters
//.p double val -- new mass in kg
//
//.u Description
//
// If the slider's mass is greater then 0, it will continue
// to move after the mouse button has been released. Its speed
// decreases with time at a rate depending on the slider's mass.
// A large mass means that it will continue to move for a
// long time.
//
// Limits: If the mass is smaller than 1g, it is set to zero.
// The maximal mass is limited to 100kg.
//
// Derived widgets may overload this function to make it public.
//
//------------------------------------------------------------
void SliderBase::setMass(double val)
{
if (val < 0.001)
d_mass = 0.0;
else if (val > 100.0)
d_mass = 100.0;
else
d_mass = val;
}
//------------------------------------------------------------
//
//.F SliderBase::setValue
// Move the slider to a specified value
//
//.u Syntax
//.f void SliderBase::setValue(double val)
//
//.u Parameters
//.p double val -- new value
//
//.u Description
// This function can be used to move the slider to a value
// which is not an integer multiple of the step size.
//
//.u See also
// @SliderBase::fitValue@
//------------------------------------------------------------
void SliderBase::setValue(double val)
{
if (d_scrollMode == ScrMouse)
stopMoving();
DoubleRange::setValue(val);
}
//------------------------------------------------------------
//
//.F QSlider::fitValue
// Set the slider's value to the nearest integer multiple
// of the step size.
//
//.u Syntax
//.f void SliderBase::fitValue(double val)
//
//.u See also:
// @SliderBase::setValue@
//------------------------------------------------------------
void SliderBase::fitValue(double val)
{
if (d_scrollMode == ScrMouse) stopMoving();
DoubleRange::fitValue(val);
}
//------------------------------------------------------------
//
//.F SliderBase::incValue
// Increment the value by a specified number of steps
//
//.u Syntax
//.f void SliderBase::incValue(int steps)
//
//.u Parameters
//.p int steps -- number of steps
//
//------------------------------------------------------------
void SliderBase::incValue(int steps)
{
if (d_scrollMode == ScrMouse) stopMoving();
DoubleRange::incValue(steps);
}
//------------------------------------------------------------
//
//.F SliderBase::stepPage
// Increment the value by a specified number of steps
//
//.u Syntax
//.f void SliderBase::stepPages(int pages)
//
//.u Parameters
//.p int pages -- +/- number of pages
//
//.u Description
// Steps the control as if pager was clicked.
// Designed to be called from outside (like from a buddy label), rather than from
// the control itself. Calls DoubleRange::incPages, which normally causes valueChange()
// (and emits valueChanged), but also emits sliderMoved.
//
//------------------------------------------------------------
void SliderBase::stepPages(int pages)
{
DoubleRange::incPages(pages);
emit sliderMoved(value(), _id);
}
//------------------------------------------------------------
//
//.F SliderBase::getValue
// Determine the value corresponding to a specified poind
//
//.u Syntax
//.f void SliderBase::getValue(const QPoint &p)
//
//.u Parameters
//.p const QPoint &p -- point
//
//.u Description
// This is an abstract virtual function which is called when
// the user presses or releases a mouse button or moves the
// mouse. It has to be implemented by the derived class.
//
//------------------------------------------------------------
//------------------------------------------------------------
//
//.F SliderBase::getScrollMode
// Determine what to do when the user presses a mouse button.
//
//.u Syntax
//.f void SliderBase::getScrollMode(const QPoint &p, int &scrollMode, int &direction)
//
//.u Input Parameters
//.p const QPoint &p -- point where the mouse was pressed
//
//.u Output parameters
// int &scrollMode -- The scrolling mode
// int &direction -- direction: 1, 0, or -1.
//
//.u Description
// This function is abstract and has to be implemented by derived classes.
// It is called on a mousePress event. The derived class can determine
// what should happen next in dependence of the position where the mouse
// was pressed by returning scrolling mode and direction. SliderBase
// knows the following modes:
//.t
// SliderBase::ScrNone -- Scrolling switched off. Don't change the value.
// SliderBase::ScrMouse -- Change the value while the user keeps the
// button pressed and moves the mouse.
// SliderBase::ScrTimer -- Automatic scrolling. Increment the value
// in the specified direction as long as
// the user keeps the button pressed.
// SliderBase::ScrPage -- Automatic scrolling. Same as ScrTimer, but
// increment by page size.
//
//
//------------------------------------------------------------
//------------------------------------------------------------
//
//.F SliderBase::valueChanged
// Notify a change of value.
//
//.u Syntax
//.f void SliderBase::valueChanged(double value, int id)
//
//.u Parameters
//.p double value -- new value
//
//.u Description
// In the default setting
// (tracking enabled), this signal will be emitted every
// time the value changes ( see setTracking() ).
//------------------------------------------------------------
//------------------------------------------------------------
//
//.F SliderBase::sliderPressed
// This signal is emitted when the user presses the
// movable part of the slider (start ScrMouse Mode).
//
//.u Syntax
//.f void SliderBase::sliderPressed()
//
//------------------------------------------------------------
//------------------------------------------------------------
//
//.F SliderBase::SliderReleased
// This signal is emitted when the user releases the
// movable part of the slider.
//
//.u Syntax
//.f void QwtSliderbase::SliderReleased()
//
//------------------------------------------------------------
//------------------------------------------------------------
//
//.F SliderBase::sliderMoved
// This signal is emitted when the user moves the
// slider with the mouse.
//
//.u Syntax
//.f void SliderBase::sliderMoved(double value, int _id)
//
//.u Parameters
//.p double value -- new value
//
//------------------------------------------------------------
|