implement hover event handlers

This commit is contained in:
laserpants 2016-05-02 02:22:43 +03:00
parent 764b1a6a64
commit 227e269820
3 changed files with 69 additions and 12 deletions

View File

@ -1,5 +1,7 @@
#include "slider.h" #include "slider.h"
#include <QPainter> #include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include "slider_p.h" #include "slider_p.h"
@ -47,3 +49,27 @@ void Slider::paintEvent(QPaintEvent *event)
painter.drawRect(rect().adjusted(0, 0, -1, -1)); painter.drawRect(rect().adjusted(0, 0, -1, -1));
#endif #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);
}

View File

@ -19,6 +19,7 @@ public:
protected: protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
const QScopedPointer<SliderPrivate> d_ptr; const QScopedPointer<SliderPrivate> d_ptr;

View File

@ -4,6 +4,8 @@
#include "slider.h" #include "slider.h"
#include <QPainter> #include <QPainter>
#define THUMB_OUTER_SIZE 30
class SliderPrivate class SliderPrivate
{ {
Q_DISABLE_COPY(SliderPrivate) Q_DISABLE_COPY(SliderPrivate)
@ -12,8 +14,11 @@ class SliderPrivate
public: public:
SliderPrivate(Slider *parent) SliderPrivate(Slider *parent)
: q_ptr(parent), : q_ptr(parent),
orientation(Qt::Horizontal) orientation(Qt::Horizontal),
hoverTrack(false),
hoverThumb(false)
{ {
parent->setMouseTracking(true);
} }
QRect trackGeometry() const QRect trackGeometry() const
@ -21,26 +26,33 @@ public:
Q_Q(const Slider); Q_Q(const Slider);
return Qt::Horizontal == orientation return Qt::Horizontal == orientation
? QRect(0, q->rect().height()/2 - 1, q->rect().width(), 2) ? QRect(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1,
: QRect(q->rect().width()/2 - 1, 0, 2, q->rect().height()); 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) void paintTrack(QPainter *painter)
{ {
Q_Q(Slider); Q_Q(Slider);
painter->save();
QBrush brush; QBrush brush;
brush.setStyle(Qt::SolidPattern); brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(0, 0, 0, 255)); brush.setColor(QColor(0, 0, 0, 255));
painter->save();
painter->fillRect(trackGeometry(), brush); painter->fillRect(trackGeometry(), brush);
painter->restore(); painter->restore();
#ifdef DEBUG_LAYOUT #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 #endif
} }
@ -49,31 +61,49 @@ public:
Q_Q(const Slider); Q_Q(const Slider);
return Qt::Horizontal == orientation return Qt::Horizontal == orientation
? QRectF(0, q->rect().height()/2 - 5, 10, 10) ? QRectF(0, q->rect().height()/2 - THUMB_OUTER_SIZE/2,
: QRectF(q->rect().width()/2 - 5, 0, 10, 10); 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) void paintThumb(QPainter *painter)
{ {
Q_Q(Slider); Q_Q(Slider);
painter->save();
QBrush brush; QBrush brush;
brush.setStyle(Qt::SolidPattern); brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(0, 0, 0, 255)); brush.setColor(QColor(0, 0, 0, 255));
painter->setBrush(brush);
painter->save();
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(brush); QRectF thumb(0, 0, 12, 12);
painter->drawEllipse(thumbGeometry()); thumb.moveCenter(thumbGeometry().center());
painter->drawEllipse(thumb);
painter->restore(); 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; Slider *const q_ptr;
Qt::Orientation orientation; Qt::Orientation orientation;
bool hoverTrack;
bool hoverThumb;
}; };
#endif // SLIDER_P_H #endif // SLIDER_P_H