From 227e269820bcf94aef681d8965e2da3813b4e023 Mon Sep 17 00:00:00 2001 From: laserpants Date: Mon, 2 May 2016 02:22:43 +0300 Subject: [PATCH] implement hover event handlers --- components/slider.cpp | 26 +++++++++++++++++++++ components/slider.h | 1 + components/slider_p.h | 54 +++++++++++++++++++++++++++++++++---------- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/components/slider.cpp b/components/slider.cpp index d512425..2743bee 100644 --- a/components/slider.cpp +++ b/components/slider.cpp @@ -1,5 +1,7 @@ #include "slider.h" #include +#include +#include #include "slider_p.h" @@ -47,3 +49,27 @@ void Slider::paintEvent(QPaintEvent *event) painter.drawRect(rect().adjusted(0, 0, -1, -1)); #endif } + +void Slider::mouseMoveEvent(QMouseEvent *event) +{ + Q_D(Slider); + + QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2)); + const bool onTrack = track.contains(event->pos()); + + if (onTrack != d->hoverTrack) { + d->hoverTrack = onTrack; + update(); + } + + QRectF thumb(0, 0, 16, 16); + thumb.moveCenter(d->thumbGeometry().center()); + const bool onThumb = thumb.contains(event->pos()); + + if (onThumb != d->hoverThumb) { + d->hoverThumb = onThumb; + update(); + } + + QAbstractSlider::mouseMoveEvent(event); +} diff --git a/components/slider.h b/components/slider.h index 7f0bd06..6eb3ff0 100644 --- a/components/slider.h +++ b/components/slider.h @@ -19,6 +19,7 @@ public: protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; const QScopedPointer d_ptr; diff --git a/components/slider_p.h b/components/slider_p.h index bd62ebc..028c597 100644 --- a/components/slider_p.h +++ b/components/slider_p.h @@ -4,6 +4,8 @@ #include "slider.h" #include +#define THUMB_OUTER_SIZE 30 + class SliderPrivate { Q_DISABLE_COPY(SliderPrivate) @@ -12,8 +14,11 @@ class SliderPrivate public: SliderPrivate(Slider *parent) : q_ptr(parent), - orientation(Qt::Horizontal) + orientation(Qt::Horizontal), + hoverTrack(false), + hoverThumb(false) { + parent->setMouseTracking(true); } QRect trackGeometry() const @@ -21,26 +26,33 @@ public: Q_Q(const Slider); return Qt::Horizontal == orientation - ? QRect(0, q->rect().height()/2 - 1, q->rect().width(), 2) - : QRect(q->rect().width()/2 - 1, 0, 2, q->rect().height()); + ? 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, + q->rect().height() - THUMB_OUTER_SIZE); } void paintTrack(QPainter *painter) { Q_Q(Slider); + painter->save(); + QBrush brush; brush.setStyle(Qt::SolidPattern); brush.setColor(QColor(0, 0, 0, 255)); - painter->save(); - painter->fillRect(trackGeometry(), brush); painter->restore(); #ifdef DEBUG_LAYOUT - painter->drawRect(q->rect().adjusted(1, 1, -2, -2)); + if (hoverTrack) { + painter->save(); + painter->setPen(Qt::red); + painter->drawRect(trackGeometry()); + painter->restore(); + } #endif } @@ -49,31 +61,49 @@ public: Q_Q(const Slider); return Qt::Horizontal == orientation - ? QRectF(0, q->rect().height()/2 - 5, 10, 10) - : QRectF(q->rect().width()/2 - 5, 0, 10, 10); + ? QRectF(0, q->rect().height()/2 - THUMB_OUTER_SIZE/2, + THUMB_OUTER_SIZE, THUMB_OUTER_SIZE) + : QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, 0, + THUMB_OUTER_SIZE, THUMB_OUTER_SIZE); } void paintThumb(QPainter *painter) { Q_Q(Slider); + painter->save(); + QBrush brush; brush.setStyle(Qt::SolidPattern); brush.setColor(QColor(0, 0, 0, 255)); - - painter->save(); + painter->setBrush(brush); painter->setRenderHint(QPainter::Antialiasing); - painter->setBrush(brush); - painter->drawEllipse(thumbGeometry()); + QRectF thumb(0, 0, 12, 12); + thumb.moveCenter(thumbGeometry().center()); + + painter->drawEllipse(thumb); painter->restore(); + +#ifdef DEBUG_LAYOUT + painter->drawRect(thumbGeometry()); + + if (hoverThumb) { + painter->save(); + painter->setPen(Qt::red); + painter->drawRect(thumbGeometry()); + painter->restore(); + } +#endif } Slider *const q_ptr; Qt::Orientation orientation; + bool hoverTrack; + bool hoverThumb; }; #endif // SLIDER_P_H