1
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
3
* Copyright (C) 1997 Josef Wilgen
4
* Copyright (C) 2002 Uwe Rathmann
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the Qwt License, Version 1.0
8
*****************************************************************************/
14
#include <qdrawutil.h>
16
#include <qwt_painter.h>
17
#include "qwt_paint_buffer.h"
18
#include "qwt_scale_draw.h"
19
#include "qwt_scale_map.h"
20
#include "qwt_slider.h"
22
class QwtSlider::PrivateData
34
QwtSlider::ScalePos scalePos;
35
QwtSlider::BGSTYLE bgStyle;
38
Scale and values might have different maps. This is
39
confusing and I can't see strong arguments for such
42
QwtScaleMap map; // linear map
43
mutable QSize sizeHintCache;
48
\param parent parent widget
49
\param orientation Orientation of the slider. Can be Qt::Horizontal
50
or Qt::Vertical. Defaults to Qt::Horizontal.
51
\param scalePos Position of the scale.
52
Defaults to QwtSlider::NoScale.
53
\param bgStyle Background style. QwtSlider::BgTrough draws the
54
slider button in a trough, QwtSlider::BgSlot draws
55
a slot underneath the button. An or-combination of both
56
may also be used. The default is QwtSlider::BgTrough.
58
QwtSlider enforces valid combinations of its orientation and scale position.
59
If the combination is invalid, the scale position will be set to NoScale.
60
Valid combinations are:
61
- Qt::Horizonal with NoScale, TopScale, or BottomScale;
62
- Qt::Vertical with NoScale, LeftScale, or RightScale.
64
QwtSlider::QwtSlider(QWidget *parent,
65
Qt::Orientation orientation, ScalePos scalePos, BGSTYLE bgStyle):
66
QwtAbstractSlider(orientation, parent)
68
initSlider(orientation, scalePos, bgStyle);
71
#if QT_VERSION < 0x040000
75
Build a horizontal slider with no scale and BgTrough as
78
\param parent parent widget
79
\param name Object name
81
QwtSlider::QwtSlider(QWidget *parent, const char* name):
82
QwtAbstractSlider(Qt::Horizontal, parent)
85
initSlider(Qt::Horizontal, NoScale, BgTrough);
89
void QwtSlider::initSlider(Qt::Orientation orientation,
90
ScalePos scalePos, BGSTYLE bgStyle)
92
if (orientation == Qt::Vertical)
93
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
95
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
97
#if QT_VERSION >= 0x040000
98
setAttribute(Qt::WA_WState_OwnSizePolicy, false);
100
clearWState( WState_OwnSizePolicy );
104
#if QT_VERSION < 0x040000
105
setWFlags(Qt::WNoAutoErase);
108
d_data = new QwtSlider::PrivateData;
110
d_data->borderWidth = 2;
111
d_data->scaleDist = 4;
112
d_data->scalePos = scalePos;
115
d_data->bgStyle = bgStyle;
117
if (bgStyle == BgSlot)
119
d_data->thumbLength = 16;
120
d_data->thumbWidth = 30;
124
d_data->thumbLength = 31;
125
d_data->thumbWidth = 16;
128
d_data->sliderRect.setRect(0,0,8,8);
130
QwtScaleDraw::Alignment align;
131
if ( orientation == Qt::Vertical )
133
// enforce a valid combination of scale position and orientation
134
if ((d_data->scalePos == BottomScale) || (d_data->scalePos == TopScale))
135
d_data->scalePos = NoScale;
136
// adopt the policy of layoutSlider (NoScale lays out like Left)
137
if (d_data->scalePos == RightScale)
138
align = QwtScaleDraw::RightScale;
140
align = QwtScaleDraw::LeftScale;
144
// enforce a valid combination of scale position and orientation
145
if ((d_data->scalePos == LeftScale) || (d_data->scalePos == RightScale))
146
d_data->scalePos = NoScale;
147
// adopt the policy of layoutSlider (NoScale lays out like Bottom)
148
if (d_data->scalePos == TopScale)
149
align = QwtScaleDraw::TopScale;
151
align = QwtScaleDraw::BottomScale;
154
scaleDraw()->setAlignment(align);
155
scaleDraw()->setLength(100);
157
setRange(0.0, 100.0, 1.0);
161
QwtSlider::~QwtSlider()
167
\brief Set the orientation.
168
\param o Orientation. Allowed values are Qt::Horizontal and Qt::Vertical.
170
If the new orientation and the old scale position are an invalid combination,
171
the scale position will be set to QwtSlider::NoScale.
172
\sa QwtAbstractSlider::orientation()
174
void QwtSlider::setOrientation(Qt::Orientation o)
176
if ( o == orientation() )
179
if (o == Qt::Horizontal)
181
if ((d_data->scalePos == LeftScale) || (d_data->scalePos == RightScale))
182
d_data->scalePos = NoScale;
184
else // if (o == Qt::Vertical)
186
if ((d_data->scalePos == BottomScale) || (d_data->scalePos == TopScale))
187
d_data->scalePos = NoScale;
190
#if QT_VERSION >= 0x040000
191
if ( !testAttribute(Qt::WA_WState_OwnSizePolicy) )
193
if ( !testWState( WState_OwnSizePolicy ) )
196
QSizePolicy sp = sizePolicy();
200
#if QT_VERSION >= 0x040000
201
setAttribute(Qt::WA_WState_OwnSizePolicy, false);
203
clearWState( WState_OwnSizePolicy );
207
QwtAbstractSlider::setOrientation(o);
212
\brief Change the scale position (and slider orientation).
214
\param s Position of the scale.
216
A valid combination of scale position and orientation is enforced:
217
- if the new scale position is Left or Right, the scale orientation will
219
- if the new scale position is Bottom or Top the scale orientation will
220
become Qt::Horizontal;
221
- if the new scale position is QwtSlider::NoScale, the scale
222
orientation will not change.
224
void QwtSlider::setScalePosition(ScalePos s)
226
if ( d_data->scalePos == s )
229
d_data->scalePos = s;
231
switch(d_data->scalePos)
235
setOrientation(Qt::Horizontal);
236
scaleDraw()->setAlignment(QwtScaleDraw::BottomScale);
241
setOrientation(Qt::Horizontal);
242
scaleDraw()->setAlignment(QwtScaleDraw::TopScale);
247
setOrientation(Qt::Vertical);
248
scaleDraw()->setAlignment(QwtScaleDraw::LeftScale);
253
setOrientation(Qt::Vertical);
254
scaleDraw()->setAlignment(QwtScaleDraw::RightScale);
266
//! Return the scale position.
267
QwtSlider::ScalePos QwtSlider::scalePosition() const
269
return d_data->scalePos;
273
\brief Change the slider's border width
274
\param bd border width
276
void QwtSlider::setBorderWidth(int bd)
281
if ( bd != d_data->borderWidth )
283
d_data->borderWidth = bd;
289
\brief Set the slider's thumb length
290
\param thumbLength new length
292
void QwtSlider::setThumbLength(int thumbLength)
294
if ( thumbLength < 8 )
297
if ( thumbLength != d_data->thumbLength )
299
d_data->thumbLength = thumbLength;
305
\brief Change the width of the thumb
308
void QwtSlider::setThumbWidth(int w)
313
if ( d_data->thumbWidth != w )
315
d_data->thumbWidth = w;
320
void QwtSlider::setScaleDraw(QwtScaleDraw *scaleDraw)
322
setAbstractScaleDraw(scaleDraw);
325
const QwtScaleDraw *QwtSlider::scaleDraw() const
327
return (QwtScaleDraw *)abstractScaleDraw();
330
QwtScaleDraw *QwtSlider::scaleDraw()
332
return (QwtScaleDraw *)abstractScaleDraw();
335
//! Notify changed scale
336
void QwtSlider::scaleChange()
342
//! Notify change in font
343
void QwtSlider::fontChange(const QFont &f)
345
QwtAbstractSlider::fontChange( f );
349
//! Draw the slider into the specified rectangle.
350
void QwtSlider::drawSlider(QPainter *p, const QRect &r)
354
if (d_data->bgStyle & BgTrough)
356
qDrawShadePanel(p, r.x(), r.y(),
357
r.width(), r.height(),
358
#if QT_VERSION < 0x040000
363
true, d_data->borderWidth,0);
365
cr.setRect(r.x() + d_data->borderWidth,
366
r.y() + d_data->borderWidth,
367
r.width() - 2 * d_data->borderWidth,
368
r.height() - 2 * d_data->borderWidth);
370
p->fillRect(cr.x(), cr.y(), cr.width(), cr.height(),
371
#if QT_VERSION < 0x040000
372
colorGroup().brush(QColorGroup::Mid)
374
palette().brush(QPalette::Mid)
379
if ( d_data->bgStyle & BgSlot)
382
int ds = d_data->thumbLength / 2 - 4;
387
if (orientation() == Qt::Horizontal)
389
if ( cr.height() & 1 )
391
rSlot = QRect(cr.x() + ds,
392
cr.y() + (cr.height() - ws) / 2,
393
cr.width() - 2 * ds, ws);
397
if ( cr.width() & 1 )
399
rSlot = QRect(cr.x() + (cr.width() - ws) / 2,
401
ws, cr.height() - 2 * ds);
403
p->fillRect(rSlot.x(), rSlot.y(), rSlot.width(), rSlot.height(),
404
#if QT_VERSION < 0x040000
405
colorGroup().brush(QColorGroup::Dark)
407
palette().brush(QPalette::Dark)
410
qDrawShadePanel(p, rSlot.x(), rSlot.y(),
411
rSlot.width(), rSlot.height(),
412
#if QT_VERSION < 0x040000
422
drawThumb(p, cr, xyPosition(value()));
425
//! Draw the thumb at a position
426
void QwtSlider::drawThumb(QPainter *p, const QRect &sliderRect, int pos)
428
pos++; // shade line points one pixel below
429
if (orientation() == Qt::Horizontal)
431
qDrawShadePanel(p, pos - d_data->thumbLength / 2,
432
sliderRect.y(), d_data->thumbLength, sliderRect.height(),
433
#if QT_VERSION < 0x040000
438
false, d_data->borderWidth,
439
#if QT_VERSION < 0x040000
440
&colorGroup().brush(QColorGroup::Button)
442
&palette().brush(QPalette::Button)
446
qDrawShadeLine(p, pos, sliderRect.y(),
447
pos, sliderRect.y() + sliderRect.height() - 2,
448
#if QT_VERSION < 0x040000
457
qDrawShadePanel(p,sliderRect.x(), pos - d_data->thumbLength / 2,
458
sliderRect.width(), d_data->thumbLength,
459
#if QT_VERSION < 0x040000
464
false, d_data->borderWidth,
465
#if QT_VERSION < 0x040000
466
&colorGroup().brush(QColorGroup::Button)
468
&palette().brush(QPalette::Button)
472
qDrawShadeLine(p, sliderRect.x(), pos,
473
sliderRect.x() + sliderRect.width() - 2, pos,
474
#if QT_VERSION < 0x040000
483
//! Find the x/y position for a given value v
484
int QwtSlider::xyPosition(double v) const
486
return d_data->map.transform(v);
489
//! Determine the value corresponding to a specified mouse location.
490
double QwtSlider::getValue(const QPoint &p)
492
return d_data->map.invTransform(
493
orientation() == Qt::Horizontal ? p.x() : p.y());
498
\brief Determine scrolling mode and direction
500
\param scrollMode Scrolling mode
501
\param direction Direction
503
void QwtSlider::getScrollMode(const QPoint &p,
504
int &scrollMode, int &direction )
506
if (!d_data->sliderRect.contains(p))
508
scrollMode = ScrNone;
513
const int pos = ( orientation() == Qt::Horizontal ) ? p.x() : p.y();
514
const int markerPos = xyPosition(value());
516
if ((pos > markerPos - d_data->thumbLength / 2)
517
&& (pos < markerPos + d_data->thumbLength / 2))
519
scrollMode = ScrMouse;
524
scrollMode = ScrPage;
525
direction = (pos > markerPos) ? 1 : -1;
527
if ( scaleDraw()->map().p1() > scaleDraw()->map().p2() )
528
direction = -direction;
532
void QwtSlider::paintEvent(QPaintEvent *e)
534
const QRect &ur = e->rect();
537
#if QT_VERSION < 0x040000
538
QwtPaintBuffer paintBuffer(this, ur);
539
draw(paintBuffer.painter(), ur);
541
QPainter painter(this);
547
//! Draw the QwtSlider
548
void QwtSlider::draw(QPainter *painter, const QRect&)
550
if (d_data->scalePos != NoScale)
552
#if QT_VERSION < 0x040000
553
scaleDraw()->draw(painter, colorGroup());
555
scaleDraw()->draw(painter, palette());
559
drawSlider(painter, d_data->sliderRect);
562
QwtPainter::drawFocusRect(painter, this, d_data->sliderRect);
566
void QwtSlider::resizeEvent(QResizeEvent *)
568
layoutSlider( false );
572
Recalculate the slider's geometry and layout based on
573
the current rect and fonts.
574
\param update_geometry notify the layout system and call update
577
void QwtSlider::layoutSlider( bool update_geometry )
579
int sliderWidth = d_data->thumbWidth;
580
int sld1 = d_data->thumbLength / 2 - 1;
581
int sld2 = d_data->thumbLength / 2 + d_data->thumbLength % 2;
582
if ( d_data->bgStyle & BgTrough )
584
sliderWidth += 2 * d_data->borderWidth;
585
sld1 += d_data->borderWidth;
586
sld2 += d_data->borderWidth;
590
if ( d_data->scalePos != NoScale )
593
scaleDraw()->getBorderDistHint(font(), d1, d2);
594
scd = qwtMax(d1, d2);
597
int slo = scd - sld1;
603
const QRect r = rect();
604
if (orientation() == Qt::Horizontal)
606
switch (d_data->scalePos)
610
d_data->sliderRect.setRect(
611
r.x() + d_data->xMargin + slo,
613
d_data->yMargin - sliderWidth,
614
r.width() - 2 * d_data->xMargin - 2 * slo,
617
x = d_data->sliderRect.x() + sld1;
618
y = d_data->sliderRect.y() - d_data->scaleDist;
625
d_data->sliderRect.setRect(
626
r.x() + d_data->xMargin + slo,
627
r.y() + d_data->yMargin,
628
r.width() - 2 * d_data->xMargin - 2 * slo,
631
x = d_data->sliderRect.x() + sld1;
632
y = d_data->sliderRect.y() + d_data->sliderRect.height()
638
case NoScale: // like Bottom, but no scale. See QwtSlider().
639
default: // inconsistent orientation and scale position
641
d_data->sliderRect.setRect(
642
r.x() + d_data->xMargin + slo,
643
r.y() + d_data->yMargin,
644
r.width() - 2 * d_data->xMargin - 2 * slo,
647
x = d_data->sliderRect.x() + sld1;
653
length = d_data->sliderRect.width() - (sld1 + sld2);
655
else // if (orientation() == Qt::Vertical
657
switch (d_data->scalePos)
660
d_data->sliderRect.setRect(
661
r.x() + d_data->xMargin,
662
r.y() + d_data->yMargin + slo,
664
r.height() - 2 * d_data->yMargin - 2 * slo);
666
x = d_data->sliderRect.x() + d_data->sliderRect.width()
668
y = d_data->sliderRect.y() + sld1;
673
d_data->sliderRect.setRect(
674
r.x() + r.width() - sliderWidth - d_data->xMargin,
675
r.y() + d_data->yMargin + slo,
677
r.height() - 2 * d_data->yMargin - 2 * slo);
679
x = d_data->sliderRect.x() - d_data->scaleDist;
680
y = d_data->sliderRect.y() + sld1;
684
case NoScale: // like Left, but no scale. See QwtSlider().
685
default: // inconsistent orientation and scale position
686
d_data->sliderRect.setRect(
687
r.x() + r.width() - sliderWidth - d_data->xMargin,
688
r.y() + d_data->yMargin + slo,
690
r.height() - 2 * d_data->yMargin - 2 * slo);
693
y = d_data->sliderRect.y() + sld1;
697
length = d_data->sliderRect.height() - (sld1 + sld2);
700
scaleDraw()->move(x, y);
701
scaleDraw()->setLength(length);
703
d_data->map.setPaintXInterval(scaleDraw()->map().p1(),
704
scaleDraw()->map().p2());
706
if ( update_geometry )
708
d_data->sizeHintCache = QSize(); // invalidate
714
//! Notify change of value
715
void QwtSlider::valueChange()
717
QwtAbstractSlider::valueChange();
722
//! Notify change of range
723
void QwtSlider::rangeChange()
725
d_data->map.setScaleInterval(minValue(), maxValue());
728
rescale(minValue(), maxValue());
730
QwtAbstractSlider::rangeChange();
735
\brief Set distances between the widget's border and internals.
736
\param xMargin Horizontal margin
737
\param yMargin Vertical margin
739
void QwtSlider::setMargins(int xMargin, int yMargin)
746
if ( xMargin != d_data->xMargin || yMargin != d_data->yMargin )
748
d_data->xMargin = xMargin;
749
d_data->yMargin = yMargin;
755
Set the background style.
757
void QwtSlider::setBgStyle(BGSTYLE st)
759
d_data->bgStyle = st;
764
\return the background style.
766
QwtSlider::BGSTYLE QwtSlider::bgStyle() const
768
return d_data->bgStyle;
772
\return the thumb length.
774
int QwtSlider::thumbLength() const
776
return d_data->thumbLength;
780
\return the thumb width.
782
int QwtSlider::thumbWidth() const
784
return d_data->thumbWidth;
788
\return the border width.
790
int QwtSlider::borderWidth() const
792
return d_data->borderWidth;
796
\return QwtSlider::minimumSizeHint()
798
QSize QwtSlider::sizeHint() const
800
return minimumSizeHint();
804
\brief Return a minimum size hint
805
\warning The return value of QwtSlider::minimumSizeHint() depends on
806
the font and the scale.
808
QSize QwtSlider::minimumSizeHint() const
810
if (!d_data->sizeHintCache.isEmpty())
811
return d_data->sizeHintCache;
813
int sliderWidth = d_data->thumbWidth;
814
if (d_data->bgStyle & BgTrough)
815
sliderWidth += 2 * d_data->borderWidth;
818
if (d_data->scalePos != NoScale)
821
scaleDraw()->getBorderDistHint(font(), d1, d2);
822
int msMbd = qwtMax(d1, d2);
824
int mbd = d_data->thumbLength / 2;
825
if (d_data->bgStyle & BgTrough)
826
mbd += d_data->borderWidth;
831
const int sdExtent = scaleDraw()->extent( QPen(), font() );
832
const int sdLength = scaleDraw()->minLength( QPen(), font() );
834
h = sliderWidth + sdExtent + d_data->scaleDist;
835
w = sdLength - 2 * msMbd + 2 * mbd;
843
if ( orientation() == Qt::Vertical )
846
w += 2 * d_data->xMargin;
847
h += 2 * d_data->yMargin;
849
d_data->sizeHintCache = QSize(w, h);
850
return d_data->sizeHintCache;