diff --git a/components/slider.cpp b/components/slider.cpp index 6d065de..4612c71 100644 --- a/components/slider.cpp +++ b/components/slider.cpp @@ -10,23 +10,27 @@ Slider::Slider(QWidget *parent) : QAbstractSlider(parent), d_ptr(new SliderPrivate(this)) { + connect(this, SIGNAL(actionTriggered(int)), this, SLOT(handleAction(int))); } Slider::~Slider() { } -void Slider::setOrientation(Qt::Orientation orientation) +void Slider::handleAction(int action) { Q_D(Slider); - d->orientation = orientation; - update(); -} -Qt::Orientation Slider::orientation() const -{ - Q_D(const Slider); - return d->orientation; + if (!d->step) + return; + + if ((SliderPageStepAdd == action && sliderPosition() > d->stepTo) || + (SliderPageStepSub == action && sliderPosition() < d->stepTo)) + { + d->step = false; + setRepeatAction(SliderNoAction); + setSliderPosition(d->stepTo); + } } void Slider::paintEvent(QPaintEvent *event) @@ -59,9 +63,11 @@ void Slider::mouseMoveEvent(QMouseEvent *event) { Q_D(Slider); - if (d->slide) { + if (d->slide) + { setSliderPosition(d->valueFromPosition(event->pos())); - } else + } + else { QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2)); @@ -86,11 +92,28 @@ void Slider::mousePressEvent(QMouseEvent *event) { Q_D(Slider); + const QPoint pos = event->pos(); + QRectF thumb(0, 0, 16, 16); thumb.moveCenter(d->thumbGeometry().center()); - if (thumb.contains(event->pos())) { + if (thumb.contains(pos)) { d->slide = true; + return; + } + + QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2)); + + if (track.contains(pos)) { + d->step = true; + d->stepTo = d->valueFromPosition(pos); + + SliderAction action = d->stepTo > sliderPosition() + ? SliderPageStepAdd + : SliderPageStepSub; + + triggerAction(action); + setRepeatAction(action, 200); } } @@ -102,12 +125,9 @@ void Slider::mouseReleaseEvent(QMouseEvent *event) d->slide = false; setValue(sliderPosition()); return QAbstractSlider::mouseReleaseEvent(event); - } - - QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2)); - - if (track.contains(event->pos())) { - setValue(d->valueFromPosition(event->pos())); + } else if (d->step) { + d->step = false; + setRepeatAction(SliderNoAction); } QAbstractSlider::mouseReleaseEvent(event); diff --git a/components/slider.h b/components/slider.h index 39b5a6d..1863364 100644 --- a/components/slider.h +++ b/components/slider.h @@ -14,8 +14,8 @@ public: explicit Slider(QWidget *parent = 0); ~Slider(); - void setOrientation(Qt::Orientation orientation); - Qt::Orientation orientation() const; +protected slots: + void handleAction(int action); protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; diff --git a/components/slider_p.h b/components/slider_p.h index fd61ed0..61c8591 100644 --- a/components/slider_p.h +++ b/components/slider_p.h @@ -15,10 +15,11 @@ class SliderPrivate public: SliderPrivate(Slider *parent) : q_ptr(parent), - orientation(Qt::Horizontal), hoverTrack(false), hoverThumb(false), - slide(false) + slide(false), + step(false), + stepTo(0) { parent->setMouseTracking(true); } @@ -27,7 +28,7 @@ public: { Q_Q(const Slider); - return Qt::Horizontal == orientation + return Qt::Horizontal == q->orientation() ? QRect(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1, q->rect().width() - THUMB_OUTER_SIZE, 2) : QRect(q->rect().width()/2 - 1, THUMB_OUTER_SIZE/2, 2, @@ -62,7 +63,7 @@ public: { Q_Q(const Slider); - const int span = Qt::Horizontal == orientation + const int span = Qt::Horizontal == q->orientation() ? q->rect().width() - THUMB_OUTER_SIZE : q->rect().height() - THUMB_OUTER_SIZE; @@ -73,7 +74,7 @@ public: span, false); - return Qt::Horizontal == orientation + return Qt::Horizontal == q->orientation() ? QRectF(pos, q->rect().height()/2 - THUMB_OUTER_SIZE/2, THUMB_OUTER_SIZE, THUMB_OUTER_SIZE) : QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, pos, @@ -116,12 +117,12 @@ public: { Q_Q(const Slider); - const int span = Qt::Horizontal == orientation + int position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y(); + + int span = Qt::Horizontal == q->orientation() ? q->rect().width() - THUMB_OUTER_SIZE : q->rect().height() - THUMB_OUTER_SIZE; - const int position = Qt::Horizontal == orientation ? pos.x() : pos.y(); - return Style::sliderValueFromPosition( q->minimum(), q->maximum(), @@ -132,10 +133,11 @@ public: Slider *const q_ptr; - Qt::Orientation orientation; - bool hoverTrack; - bool hoverThumb; - bool slide; + bool hoverTrack; + bool hoverThumb; + bool slide; + bool step; + int stepTo; }; #endif // SLIDER_P_H diff --git a/examples/sliderexamples.cpp b/examples/sliderexamples.cpp index 5ae0613..7731b28 100644 --- a/examples/sliderexamples.cpp +++ b/examples/sliderexamples.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "sliderexamples.h" #include "components/slider.h" #include "exampleview.h" @@ -113,6 +114,8 @@ SliderExamples::SliderExamples(QWidget *parent) layout->addWidget(_slider2); layout->addLayout(hLayout); + layout->addWidget(new QSlider(Qt::Horizontal)); + ExampleView *view = new ExampleView; view->setWidget(widget); @@ -128,6 +131,7 @@ SliderExamples::SliderExamples(QWidget *parent) _slider2->setTracking(false); } + } SliderExamples::~SliderExamples()