7
//-------------------------------------------------------------
8
//.H Slider | 3 | 02/04/98 | Qwt Widget Library | Qwt Programmer's Manual
11
// Slider - The Slider Widget
14
// #include <qwt_slider.h>
17
// Slider is a slider widget which operates on an interval
18
// of type double. Slider supports different layouts as
22
//.U INHERITED CLASSES
23
// @^SliderBase@, @^QwtScaleIf@
27
// Slider::Slider -- Constructor
28
// Slider::~Slider -- Destructor
29
// Slider::setThumbLength -- Set the length of the slider's thumb
30
// Slider::setThumbWidth -- Set the length of the slider's thumb
31
// Slider::setBorderWidth -- Set the border width
32
// Slider::setMargins -- Set the internal border distance
35
// @^SliderBase@ and @^QwtScaleIf@ for the descriptions
36
// of the inherited members.
37
//------------------------------------------------------------
39
//------------------------------------------------------------
40
//.C MEMBER FUNCTION DESCRIPTION
41
//------------------------------------------------------------
44
//------------------------------------------------------------
51
//.f Slider::Slider(QWidget *parent, const char *name, OrientationX orient = Horizontal, ScalePos scalePos = None, int bgStyle = BgTrough)
55
// QWidget *parent -- parent widget
56
// const char *name -- The Widget's name. Default = 0.
57
// Orientation Orient -- Orientation of the slider. Can be Slider::Horizontal
58
// or Slider::Vertical.
59
// Defaults to Horizontal.
60
// ScalePos scalePos -- Position of the scale. Can be Slider::None,
61
// Slider::Left, Slider::Right, Slider::Top,
62
// or Slider::Bottom. Defaults to Slider::None.
63
// int bgStyle -- Background style. Slider::BgTrough draws the
64
// slider button in a trough, Slider::BgSlot draws
65
// a slot underneath the button. An or-combination of both
66
// may also be used. The default is Slider::BgTrough.
69
//------------------------------------------------------------
70
Slider::Slider(QWidget *parent, const char *name,
71
OrientationX orient, ScalePos scalePos, int bgStyle)
72
: SliderBase(parent,name)
74
ScaleDraw::OrientationX so;
77
if (bgStyle == BgSlot) {
92
d_scalePos = scalePos;
97
if (bgStyle & BgTrough)
98
d_bwTrough = d_borderWidth;
102
d_sliderRect.setRect(0,0,8,8);
107
if (d_scalePos == Right)
108
so = ScaleDraw::Right;
110
so = ScaleDraw::Left;
113
if (d_scalePos == Bottom)
114
so = ScaleDraw::Bottom;
120
d_scale.setGeometry(0, 0, 40, so);
122
if (d_orient == Vertical)
123
setMinimumSize(10,20);
125
setMinimumSize(20,10);
129
//------------------------------------------------------------
133
//.f Slider::~Slider()
134
//------------------------------------------------------------
140
//------------------------------------------------------------
142
//.F Slider::setBorderWidth
143
// Change the slider's border width
146
//.f void Slider::setBorderWidth(int bd)
149
//.p int bd -- border width
151
//------------------------------------------------------------
152
void Slider::setBorderWidth(int bd)
154
d_borderWidth = qwtMin(qwtMax(bd,0),10);
155
if (d_bgStyle & BgTrough)
156
d_bwTrough = d_borderWidth;
161
//----------------------------------------------------
163
//.F Slider::setThumbLength
165
// Set the slider's thumb length
168
// void Slider::setThumbLength(int l)
171
//.p int l -- new length
173
//-----------------------------------------------------
174
void Slider::setThumbLength(int l)
176
d_thumbLength = qwtMax(l,8);
177
d_thumbHalf = d_thumbLength / 2;
181
//------------------------------------------------------------
183
//.F Slider::setThumbWidth
184
// Change the width of the thumb
187
//.p void Slider::setThumbWidth(int w)
190
//.p int w -- new width
192
//------------------------------------------------------------
193
void Slider::setThumbWidth(int w)
195
d_thumbWidth = qwtMax(w,4);
200
//------------------------------------------------------------
202
//.F Slider::scaleChange
203
// Notify changed scale
206
//.f void Slider::scaleChange()
209
// Called by QwtScaledWidget
211
//------------------------------------------------------------
212
void Slider::scaleChange()
215
d_scale.setScale(minValue(), maxValue(), d_maxMajor, d_maxMinor);
220
//------------------------------------------------------------
222
//.F Slider::fontChange
223
// Notify change in font
226
//.f Slider::fontChange(const QFont &oldFont)
228
//------------------------------------------------------------
229
void Slider::fontChange(const QFont & /*oldFont*/)
234
//------------------------------------------------------------
236
//.F Slider::drawSlider
237
// Draw the slider into the specified rectangle.
240
//.f void Slider::drawSlider(QPaiter *p, const QRect &r)
243
//.p QPainter *p, const QRect &r
245
//------------------------------------------------------------
246
void Slider::drawSlider (QPainter *p, const QRect &r)
249
QColorGroup g = colorGroup();
250
QBrush brBack(g.background());
252
QBrush brDark(g.dark());
262
qDrawShadePanel(p, r.x(), r.y(),
263
r.width(), r.height(),
264
g, TRUE, d_bwTrough,0);
265
cr.setRect(r.x() + d_bwTrough,
267
r.width() - 2*d_bwTrough,
268
r.height() - 2*d_bwTrough);
269
brMid = QBrush(g.mid());
278
rpos = (value() - minValue()) / (maxValue() - minValue());
280
lineDist = d_borderWidth - 1;
281
if (lineDist < 1) lineDist = 1;
283
if (d_orient == Horizontal)
286
dist1 = int(double(cr.width() - d_thumbLength) * rpos);
287
ipos = cr.x() + dist1;
288
markerPos = ipos + d_thumbHalf;
293
if (d_bgStyle & BgSlot)
295
drawHsBgSlot(p, cr, QRect(ipos, cr.y(), d_thumbLength, cr.height()), brMid);
299
p->fillRect(cr.x(),cr.y(),dist1,cr.height(),brMid);
300
p->fillRect(ipos + d_thumbLength, cr.y(),
301
cr.width() - d_thumbLength - dist1, cr.height(),brMid);
307
qDrawShadePanel(p,ipos, cr.y(), d_thumbLength, cr.height(),
308
colorGroup(), FALSE, d_borderWidth, &brBack);
311
qDrawShadeLine(p,markerPos, cr.y() + lineDist , markerPos,
312
cr.y() + cr.height() - lineDist,
313
colorGroup(), TRUE, 1);
317
p->drawLine(markerPos -1 , cr.y() + lineDist, markerPos -1,
318
cr.y() + cr.height() - lineDist - 1);
319
p->setPen(g.light());
320
p->drawLine(markerPos, cr.y() + lineDist, markerPos,
321
cr.y() + cr.height() - lineDist - 1);
328
dist1 = int(double(cr.height() - d_thumbLength) * (1.0 - rpos));
329
ipos = cr.y() + dist1;
330
markerPos = ipos + d_thumbHalf;
332
if ( d_bgStyle & BgSlot)
334
drawVsBgSlot(p, cr, QRect(cr.left(), ipos, cr.width(),
335
d_thumbLength), brMid);
339
p->fillRect(cr.x(),cr.y(),cr.width(),ipos,brMid);
340
p->fillRect(cr.x(), ipos + d_thumbLength, cr.width(),
341
cr.height() - d_thumbLength - dist1, brMid);
344
qDrawShadePanel(p,cr.x(),ipos , cr.width(), d_thumbLength,
345
colorGroup(),FALSE,d_borderWidth, &brBack);
347
qDrawShadeLine(p, cr.x() + lineDist , markerPos,
348
cr.x() + cr.width() - lineDist, markerPos,
349
colorGroup(), TRUE, 1);
353
p->drawLine(cr.x() + lineDist, markerPos - 1 ,
354
cr.x() + cr.width() - lineDist - 1, markerPos - 1);
355
p->setPen(g.light());
356
p->drawLine(cr.x() + lineDist, markerPos,
357
cr.x() + cr.width() - lineDist - 1 , markerPos);
363
//------------------------------------------------------------
365
//.F Slider::drawSlotBg
369
//.f void Slider::drawSlotBg(QPainter *p, const QRect &rBound, const QRect &rThumb, const QRect &rSlot, const QBrush &brBack)
372
//.p QPainter *p, const QRect &rBound, const QRect &rThumb, const QRect &rSlot, const QBrush &brBack
374
//------------------------------------------------------------
375
void Slider::drawHsBgSlot(QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack)
380
const QColorGroup &g = colorGroup();
382
ws = rBound.height();
383
if ((ws / 2) * 2 != ws)
388
ds = qwtMax(1, d_thumbLength/2 - 4);
389
dLeft = rThumb.left() - rBound.left();
391
rSlot = QRect(rBound.x() + ds, rBound.y() + (rBound.height() - ws) / 2,
392
rBound.width() - 2 * ds, ws);
394
rPos = qwtMin(rSlot.x(), rThumb.left());
396
if (rThumb.left() > rBound.x())
398
p->fillRect(rBound.x(),rBound.y(),dLeft, rSlot.top() - rBound.top(), brBack);
399
p->fillRect(rBound.x(),rSlot.bottom() + 1,dLeft,
400
rBound.bottom() - rSlot.bottom(),brBack);
401
if (rPos > rBound.left())
402
p->fillRect(rBound.x(),rSlot.y(),
403
rPos - rBound.left(),ws,brBack);
406
if (rSlot.x() < rThumb.left())
407
p->drawLine(rSlot.x(), rSlot.bottom(), rSlot.x(), rSlot.top());
408
if (rSlot.x() < rThumb.left() - 1)
410
p->drawLine(rSlot.x(), rSlot.top(), rThumb.left() - 1, rSlot.top());
411
p->setPen(g.light());
412
p->drawLine(rSlot.x() + 1, rSlot.bottom(),
413
rThumb.left() - 1, rSlot.bottom());
415
p->fillRect(rSlot.x() + 1, rSlot.y() + 1, dLeft - ds -1,
416
rSlot.height() -2, QBrush(black));
421
lPos = qwtMax(rSlot.right(), rThumb.right()) + 1;
422
if (rThumb.right() < rBound.right())
424
p->fillRect(rThumb.right() + 1,rBound.y(),rBound.right() - rThumb.right(),
425
rSlot.top() - rBound.top(), brBack);
426
p->fillRect(rThumb.right() + 1,rSlot.bottom() + 1,
427
rBound.right() - rThumb.right(),
428
rBound.bottom() - rSlot.bottom(),brBack);
429
if (lPos <= rBound.right())
430
p->fillRect(lPos, rSlot.y() , rBound.right() - lPos + 1, ws ,brBack);
433
if (rSlot.right() > rThumb.right())
435
p->drawLine(rThumb.right() + 1, rSlot.top(), rSlot.right(), rSlot.top());
436
p->setPen(g.light());
437
p->drawLine(rSlot.right(), rSlot.bottom(), rSlot.right(), rSlot.top() + 1);
440
if (rSlot.right() > rThumb.right() + 1)
442
p->setPen(g.light());
443
p->drawLine(rThumb.right() + 1, rSlot.bottom(),
444
rSlot.right() -1, rSlot.bottom());
445
p->fillRect(rThumb.right() + 1, rSlot.y() + 1,
446
rSlot.right() - rThumb.right() - 1,
447
rSlot.height() -2, QBrush(black));
453
//------------------------------------------------------------
455
//.F Slider::drawVsBgSlot
459
//.f void Slider::drawVsBgSlot(QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack)
462
//.p QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack
468
//------------------------------------------------------------
469
void Slider::drawVsBgSlot(QPainter *p, const QRect &rBound, const QRect &rThumb, const QBrush &brBack)
475
const QColorGroup &g = colorGroup();
478
if ((ws / 2) * 2 != ws)
483
ds = qwtMax(1, d_thumbLength/2 - 4);
484
dTop = rThumb.top() - rBound.top();
486
rSlot = QRect(rBound.x() + (rBound.width() - ws) / 2, rBound.y() + ds,
487
ws, rBound.height() - 2 * ds);
489
hPos = qwtMin(rSlot.y(), rThumb.top());
491
if (rThumb.top() > rBound.top())
493
p->fillRect(rBound.x(),rBound.y(), rSlot.left() - rBound.left(),dTop, brBack);
494
p->fillRect(rSlot.right() + 1, rBound.y(),
495
rBound.right() - rSlot.right(), dTop,brBack);
496
if (hPos > rBound.top())
497
p->fillRect(rSlot.x(),rBound.y(), ws,
498
hPos - rBound.top(),brBack);
501
if (rSlot.top() < rThumb.top())
502
p->drawLine(rSlot.left(), rSlot.top(), rSlot.right(), rSlot.top());
505
if (rSlot.top() < rThumb.top() - 1)
507
p->drawLine(rSlot.left(), rThumb.top() - 1, rSlot.left(), rSlot.top());
508
p->setPen(g.light());
509
p->drawLine(rSlot.right(), rSlot.top() + 1, rSlot.right(),
512
p->fillRect(rSlot.x() + 1, rSlot.y() + 1, rSlot.width() - 2,
513
dTop - ds -1, QBrush(black));
518
lPos = qwtMax(rSlot.bottom(), rThumb.bottom()) + 1;
519
if (rThumb.bottom() < rBound.bottom())
521
p->fillRect(rBound.left(), rThumb.bottom() + 1,
522
rSlot.left() - rBound.left(),
523
rBound.bottom() - rThumb.bottom(), brBack);
524
p->fillRect(rSlot.right() + 1, rThumb.bottom() + 1,
525
rBound.right() - rSlot.right(),
526
rBound.bottom() - rThumb.bottom(), brBack);
527
if (lPos <= rBound.bottom())
528
p->fillRect(rSlot.left(), lPos, ws, rBound.bottom() - lPos + 1, brBack);
531
if (rSlot.bottom() > rThumb.bottom())
533
p->drawLine(rSlot.left(), rThumb.bottom() + 1, rSlot.left(), rSlot.bottom());
534
p->setPen(g.light());
535
p->drawLine(rSlot.left() * 1, rSlot.bottom(), rSlot.right(), rSlot.bottom());
538
if (rSlot.bottom() > rThumb.bottom() + 1)
540
p->setPen(g.light());
541
p->drawLine(rSlot.right(), rThumb.bottom() + 1, rSlot.right(),
543
p->fillRect(rSlot.left() + 1, rThumb.bottom() + 1,
544
rSlot.width() - 2, rSlot.bottom() - rThumb.bottom() - 1,
551
//------------------------------------------------------------
553
//.F Slider::getValue
554
// Determine the value corresponding to a specified
558
//.f double Slider::getValue(const QPoint &p)
561
//.p const QPoint &p --
564
// Called by SliderBase
565
//------------------------------------------------------------
566
double Slider::getValue( const QPoint &p)
570
QRect r = d_sliderRect;
572
r.setLeft(r.left() + d_bwTrough);
573
r.setRight(r.right() - d_bwTrough);
574
r.setTop(r.top() - d_bwTrough);
575
r.setBottom(r.bottom() - d_bwTrough);
577
if (d_orient == Horizontal)
580
if (r.width() <= d_thumbLength)
582
rv = 0.5 * (minValue() + maxValue());
586
pos = p.x() - r.x() - d_thumbHalf;
588
rint( (maxValue() - minValue()) * double(pos)
589
/ double(r.width() - d_thumbLength)
596
if (r.height() <= d_thumbLength)
598
rv = 0.5 * (minValue() + maxValue());
602
pos = p.y() - r.y() - d_thumbHalf;
604
rint( (maxValue() - minValue()) *
606
/ double(r.height() - d_thumbLength))
616
//------------------------------------------------------------
618
//.F Slider::getScrollMode
619
// Determine scrolling mode and direction
622
//.f void Slider::getScrollMode( const QPoint &p, int &scrollMode, int &direction )
625
//.p const QPoint &p -- point
628
// Called by SliderBase
630
//------------------------------------------------------------
631
void Slider::getScrollMode( const QPoint &p, int &scrollMode, int &direction )
635
if (d_orient == Horizontal)
640
if (d_sliderRect.contains(p))
642
if ((currentPos > markerPos - d_thumbHalf)
643
&& (currentPos < markerPos + d_thumbHalf))
645
scrollMode = ScrMouse;
650
scrollMode = ScrPage;
651
if (((currentPos > markerPos) && (d_orient == Horizontal))
652
|| ((currentPos <= markerPos) && (d_orient != Horizontal)))
660
scrollMode = ScrNone;
666
//------------------------------------------------------------
668
//.F Slider::paintEvent
675
//.f void Slider::paintEvent(QPaintEvent *e)
677
//------------------------------------------------------------
678
void Slider::paintEvent(QPaintEvent * /*e*/)
684
if (d_scalePos != None)
686
p.fillRect(this->rect(), this->backgroundColor());
690
drawSlider(&p, d_sliderRect);
696
//------------------------------------------------------------
698
//.F Slider::resizeEvent
705
//.f void Slider::resizeEvent(QResizeEvent *e)
707
//------------------------------------------------------------
708
void Slider::resizeEvent(QResizeEvent *e)
713
int sliderWidth = d_thumbWidth + 2 * d_bwTrough;
716
if(d_orient == Horizontal)
722
d_sliderRect.setRect(this->rect().x() + d_xMargin,
723
this->rect().y() + s.height() - 1
724
- d_yMargin - sliderWidth,
725
s.width() - 2 * d_xMargin,
727
d_scale.setGeometry(d_sliderRect.x() + d_bwTrough + d_thumbHalf,
728
d_sliderRect.y() - d_scaleDist,
729
d_sliderRect.width() - d_thumbLength - 2*d_bwTrough,
736
d_sliderRect.setRect(this->rect().x() + d_xMargin,
737
this->rect().y() + d_yMargin,
738
s.width() - 2*d_xMargin,
740
d_scale.setGeometry(d_sliderRect.x() + d_bwTrough + d_thumbHalf,
741
d_sliderRect.y() + d_sliderRect.height() + d_scaleDist,
742
d_sliderRect.width() - d_thumbLength - 2*d_bwTrough,
748
d_sliderRect.setRect(this->rect().x(), this->rect().x(),
749
s.width(), s.height());
758
d_sliderRect.setRect(this->rect().x() + s.width()
759
- sliderWidth - 1 - d_xMargin,
760
this->rect().y() + d_yMargin,
762
s.height() - 2 * d_yMargin);
763
d_scale.setGeometry(d_sliderRect.x() - d_scaleDist,
764
d_sliderRect.y() + d_thumbHalf + d_bwTrough,
765
s.height() - d_thumbLength - 2*d_bwTrough,
770
d_sliderRect.setRect(this->rect().x() + d_xMargin,
771
this->rect().y() + d_yMargin,
773
s.height() - 2* d_yMargin);
774
d_scale.setGeometry(this->rect().x() + d_sliderRect.width()
776
d_sliderRect.y() + d_thumbHalf + d_bwTrough,
777
s.height() - d_thumbLength - 2*d_bwTrough,
781
d_sliderRect.setRect(this->rect().x(), this->rect().x(),
782
s.width(), s.height());
789
//------------------------------------------------------------
791
//.F Slider::valueChange
792
// Notify change of value
795
//.f void Slider::valueChange()
797
//------------------------------------------------------------
798
void Slider::valueChange()
803
drawSlider(&p,d_sliderRect);
806
SliderBase::valueChange();
811
//------------------------------------------------------------
813
//.F Slider::rangeChange
814
// Notify change of range
819
//.f void Slider::rangeChange()
821
//------------------------------------------------------------
822
void Slider::rangeChange()
825
d_scale.setScale(minValue(), maxValue(), d_maxMajor, d_maxMinor);
826
SliderBase::rangeChange();
830
//------------------------------------------------------------
832
//.F Slider::setMargins
833
// Set distances between the widget's border and
837
//.f void Slider::setMargins(int hor, int vert)
840
//.p int hor, int vert -- Margins
842
//------------------------------------------------------------
843
void Slider::setMargins(int hor, int vert)
845
d_xMargin = qwtMax(0, hor);
846
d_yMargin = qwtMin(0, vert);
847
resize(this->size());
850
//------------------------------------------------------------
852
//.F Slider::sizeHint
853
// Return a recommended size
856
//.f QSize Slider::sizeHint() const
859
// The return value of sizeHint() depends on the font and the
861
//------------------------------------------------------------
863
QSize Slider::sizeHint() const
866
int msWidth = 0, msHeight = 0;
870
if (d_scalePos != None) {
872
msWidth = d_scale.maxWidth(&p, FALSE);
873
msHeight = d_scale.maxHeight(&p);
879
w = 2*d_xMargin + d_thumbWidth + 2*d_bwTrough + msWidth + d_scaleDist + 2;
882
h = 2*d_yMargin + d_thumbWidth + 2*d_bwTrough + msHeight + d_scaleDist;
889
w = 16 + 2 * d_bwTrough;
892
h = 16 + 2 * d_bwTrough;