10#include "qwt_slider.h"
11#include "qwt_painter.h"
12#include "qwt_scale_draw.h"
13#include "qwt_scale_map.h"
21#include <qstyleoption.h>
24static QSize qwtHandleSize(
const QSize& size,
25 Qt::Orientation orientation,
bool hasTrough )
27 QSize handleSize = size;
29 if ( handleSize.isEmpty() )
31 const int handleThickness = 16;
32 handleSize.setWidth( 2 * handleThickness );
33 handleSize.setHeight( handleThickness );
36 handleSize.transpose();
38 if ( orientation == Qt::Vertical )
39 handleSize.transpose();
50 if ( orientation == Qt::Vertical )
70class QwtSlider::PrivateData
75 , updateInterval( 150 )
77 , pendingValueChange( false )
91 bool pendingValueChange;
99 Qt::Orientation orientation;
107 mutable QSize sizeHintCache;
122 initSlider( Qt::Vertical );
148void QwtSlider::initSlider( Qt::Orientation orientation )
151 setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
153 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
155 setAttribute( Qt::WA_WState_OwnSizePolicy,
false );
157 m_data =
new QwtSlider::PrivateData;
162 qwtScaleDrawAlignment(
orientation, m_data->scalePosition ) );
183 qwtScaleDrawAlignment(
orientation, m_data->scalePosition ) );
185 if ( !testAttribute( Qt::WA_WState_OwnSizePolicy ) )
187 QSizePolicy sp = sizePolicy();
191 setAttribute( Qt::WA_WState_OwnSizePolicy,
false );
194 if ( testAttribute( Qt::WA_WState_Polished ) )
195 layoutSlider(
true );
204 return m_data->orientation;
220 qwtScaleDrawAlignment( m_data->orientation,
scalePosition ) );
222 if ( testAttribute( Qt::WA_WState_Polished ) )
223 layoutSlider(
true );
232 return m_data->scalePosition;
249 if ( width != m_data->borderWidth )
251 m_data->borderWidth = width;
253 if ( testAttribute( Qt::WA_WState_Polished ) )
254 layoutSlider(
true );
264 return m_data->borderWidth;
283 if (
spacing != m_data->spacing )
287 if ( testAttribute( Qt::WA_WState_Polished ) )
288 layoutSlider(
true );
298 return m_data->spacing;
313 if ( size != m_data->handleSize )
315 m_data->handleSize = size;
317 if ( testAttribute( Qt::WA_WState_Polished ) )
318 layoutSlider(
true );
328 return m_data->handleSize;
347 if ( scaleDraw == NULL ||
scaleDraw == previousScaleDraw )
350 if ( previousScaleDraw )
355 if ( testAttribute( Qt::WA_WState_Polished ) )
356 layoutSlider(
true );
382 if ( testAttribute( Qt::WA_WState_Polished ) )
383 layoutSlider(
true );
397 m_data->updateInterval = qMax( interval, 50 );
406 return m_data->updateInterval;
416 QPainter* painter,
const QRect& sliderRect )
const
420 if ( m_data->hasTrough )
422 const int bw = m_data->borderWidth;
423 innerRect =
sliderRect.adjusted( bw, bw, -bw, -bw );
425 painter->fillRect( innerRect, palette().brush( QPalette::Mid ) );
426 qDrawShadePanel( painter,
sliderRect, palette(),
true, bw, NULL );
429 if ( m_data->hasGroove )
431 const QSize
handleSize = qwtHandleSize( m_data->handleSize,
432 m_data->orientation, m_data->hasTrough );
434 const int slotExtent = 4;
435 const int slotMargin = 4;
440 int slotOffset = qMax( 1,
handleSize.width() / 2 - slotMargin );
441 int slotHeight = slotExtent + ( innerRect.height() % 2 );
443 slotRect.setWidth( innerRect.width() - 2 * slotOffset );
444 slotRect.setHeight( slotHeight );
448 int slotOffset = qMax( 1,
handleSize.height() / 2 - slotMargin );
449 int slotWidth = slotExtent + ( innerRect.width() % 2 );
451 slotRect.setWidth( slotWidth );
452 slotRect.setHeight( innerRect.height() - 2 * slotOffset );
456 slotRect.moveCenter( innerRect.center() );
458 QBrush brush = palette().brush( QPalette::Dark );
459 qDrawShadePanel( painter, slotRect, palette(),
true, 1, &brush );
474 const QRect& handleRect,
int pos )
const
476 const int bw = m_data->borderWidth;
478 qDrawShadePanel( painter,
480 &palette().brush( QPalette::Button ) );
485 qDrawShadeLine( painter, pos,
handleRect.top() + bw,
486 pos,
handleRect.bottom() - bw, palette(),
true, 1 );
490 qDrawShadeLine( painter,
handleRect.left() + bw, pos,
491 handleRect.right() - bw, pos, palette(),
true, 1 );
507 const double v = (
orientation() == Qt::Horizontal )
531 p -= m_data->mouseOffset;
538 p = qBound( min, p, max );
555 const QPoint pos =
event->pos();
557 if (
isValid() && m_data->sliderRect.contains( pos ) )
565 if ( m_data->orientation == Qt::Horizontal )
567 if ( pos.x() < markerPos )
568 m_data->stepsIncrement = -m_data->stepsIncrement;
572 if ( pos.y() < markerPos )
573 m_data->stepsIncrement = -m_data->stepsIncrement;
577 m_data->stepsIncrement = -m_data->stepsIncrement;
579 const double v =
value();
587 m_data->pendingValueChange =
true;
592 m_data->timerTick =
false;
593 m_data->repeatTimerId = startTimer( qMax( 250, 2 *
updateInterval() ) );
608 if ( m_data->repeatTimerId > 0 )
610 killTimer( m_data->repeatTimerId );
611 m_data->repeatTimerId = 0;
612 m_data->timerTick =
false;
613 m_data->stepsIncrement = 0;
616 if ( m_data->pendingValueChange )
618 m_data->pendingValueChange =
false;
635 if (
event->timerId() != m_data->repeatTimerId )
637 QwtAbstractSlider::timerEvent(
event );
643 killTimer( m_data->repeatTimerId );
644 m_data->repeatTimerId = 0;
648 const double v =
value();
656 m_data->pendingValueChange =
true;
661 if ( !m_data->timerTick )
664 killTimer( m_data->repeatTimerId );
667 m_data->timerTick =
true;
677 QPainter painter(
this );
678 painter.setClipRegion(
event->region() );
682 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter,
this);
686 if ( !m_data->sliderRect.contains(
event->rect() ) )
702 layoutSlider(
false );
703 QwtAbstractSlider::resizeEvent(
event );
714 if (
event->type() == QEvent::PolishRequest )
715 layoutSlider(
false );
717 return QwtAbstractSlider::event(
event );
726 if (
event->type() == QEvent::StyleChange ||
727 event->type() == QEvent::FontChange )
729 if ( testAttribute( Qt::WA_WState_Polished ) )
730 layoutSlider(
true );
743void QwtSlider::layoutSlider(
bool update_geometry )
746 if ( m_data->hasTrough )
747 bw = m_data->borderWidth;
749 const QSize
handleSize = qwtHandleSize( m_data->handleSize,
750 m_data->orientation, m_data->hasTrough );
773 scaleMargin = qMax( d1, d2 ) - bw;
776 int scaleX, scaleY, scaleLength;
778 if ( m_data->orientation == Qt::Horizontal )
780 const int handleMargin =
handleSize.width() / 2 - 1;
781 if ( scaleMargin > handleMargin )
783 int off = scaleMargin - handleMargin;
792 int handleMargin =
handleSize.height() / 2 - 1;
793 if ( scaleMargin > handleMargin )
795 int off = scaleMargin - handleMargin;
803 scaleLength -= 2 * bw;
807 if ( m_data->orientation == Qt::Horizontal )
819 scaleY =
sliderRect.bottom() + 1 + m_data->spacing;
829 scaleX =
sliderRect.right() + 1 + m_data->spacing;
843 if ( update_geometry )
845 m_data->sizeHintCache = QSize();
862 if ( m_data->hasTrough != on )
864 m_data->hasTrough = on;
866 if ( testAttribute( Qt::WA_WState_Polished ) )
867 layoutSlider(
true );
877 return m_data->hasTrough;
891 if ( m_data->hasGroove != on )
893 m_data->hasGroove = on;
895 if ( testAttribute( Qt::WA_WState_Polished ) )
896 layoutSlider(
true );
906 return m_data->hasGroove;
915 return qwtExpandedToGlobalStrut( hint );
924 if ( !m_data->sizeHintCache.isEmpty() )
925 return m_data->sizeHintCache;
927 const QSize
handleSize = qwtHandleSize( m_data->handleSize,
928 m_data->orientation, m_data->hasTrough );
931 if ( m_data->hasTrough )
932 bw = m_data->borderWidth;
934 int sliderLength = 0;
942 const int scaleBorderDist = 2 * ( qMax( d1, d2 ) - bw );
944 int handleBorderDist;
945 if ( m_data->orientation == Qt::Horizontal )
951 if ( handleBorderDist > scaleBorderDist )
954 sliderLength += handleBorderDist - scaleBorderDist;
957 scaleExtent += m_data->spacing;
958 scaleExtent += qwtCeil(
scaleDraw()->extent( font() ) );
961 sliderLength = qMax( sliderLength, 84 );
966 if ( m_data->orientation == Qt::Horizontal )
969 h =
handleSize.height() + 2 * bw + scaleExtent;
973 w =
handleSize.width() + 2 * bw + scaleExtent;
978 const QMargins m = contentsMargins();
980 w += m.left() + m.right();
981 h += m.top() + m.bottom();
983 m_data->sizeHintCache = QSize( w, h );
984 return m_data->sizeHintCache;
997 QPoint center = m_data->sliderRect.center();
998 if ( m_data->orientation == Qt::Horizontal )
999 center.setX( markerPos );
1001 center.setY( markerPos );
1004 rect.setSize( qwtHandleSize( m_data->handleSize,
1005 m_data->orientation, m_data->hasTrough ) );
1006 rect.moveCenter( center );
1016 return m_data->sliderRect;
1019#include "moc_qwt_slider.cpp"
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
const QwtScaleMap & scaleMap() const
double lowerBound() const
const QwtAbstractScaleDraw * abstractScaleDraw() const
int transform(double) const
void setAbstractScaleDraw(QwtAbstractScaleDraw *)
Set a scale draw.
double upperBound() const
void setScale(double lowerBound, double upperBound)
Specify a scale.
virtual void changeEvent(QEvent *) override
An abstract base class for slider widgets with a scale.
virtual void scaleChange() override
virtual void mouseReleaseEvent(QMouseEvent *) override
virtual void mousePressEvent(QMouseEvent *) override
void valueChanged(double value)
Notify a change of value.
void setValue(double value)
void incrementValue(int stepCount)
double value() const
Returns the current value.
void sliderMoved(double value)
static void drawFocusRect(QPainter *, const QWidget *)
Draw a focus rectangle on a widget using its style.
A class for drawing scales.
void setLength(double length)
void move(double x, double y)
void getBorderDistHint(const QFont &, int &start, int &end) const
Determine the minimum border distance.
int minLength(const QFont &) const
void setAlignment(Alignment)
Alignment alignment() const
@ BottomScale
The scale is below.
@ TopScale
The scale is above.
@ RightScale
The scale is right.
@ LeftScale
The scale is left.
double invTransform(double p) const
virtual void mouseReleaseEvent(QMouseEvent *) override
@ LeadingScale
The scale is right of a vertical or below a horizontal slider.
@ TrailingScale
The scale is left of a vertical or above a horizontal slider.
@ NoScale
The slider has no scale.
virtual void resizeEvent(QResizeEvent *) override
virtual ~QwtSlider()
Destructor.
virtual bool isScrollPosition(const QPoint &) const override
Determine what to do when the user presses a mouse button.
void setOrientation(Qt::Orientation)
Set the orientation.
const QwtScaleDraw * scaleDraw() const
void setScalePosition(ScalePosition)
Change the position of the scale.
virtual void paintEvent(QPaintEvent *) override
QwtSlider(QWidget *parent=NULL)
virtual bool event(QEvent *) override
virtual QSize minimumSizeHint() const override
virtual void drawHandle(QPainter *, const QRect &, int pos) const
void setHandleSize(const QSize &)
Set the slider's handle size.
virtual void timerEvent(QTimerEvent *) override
int updateInterval() const
void setScaleDraw(QwtScaleDraw *)
Set a scale draw.
void setSpacing(int)
Change the spacing between trough and scale.
virtual double scrolledTo(const QPoint &) const override
Determine the value for a new position of the slider handle.
virtual void changeEvent(QEvent *) override
ScalePosition scalePosition() const
void setBorderWidth(int)
Change the slider's border width.
virtual void scaleChange() override
Notify changed scale.
virtual QSize sizeHint() const override
virtual void mousePressEvent(QMouseEvent *) override
void setUpdateInterval(int)
Specify the update interval for automatic scrolling.
Qt::Orientation orientation() const
virtual void drawSlider(QPainter *, const QRect &) const