From 40f311bdb73f04ba2fde4bf662998db81ad41b8e Mon Sep 17 00:00:00 2001 From: FarmRadio Hangar Date: Thu, 12 May 2016 14:19:59 +0300 Subject: [PATCH] move Slider track to separate class --- components/slider.cpp | 18 +-------- components/slider.h | 5 +-- components/slider_p.h | 19 +++++---- components/slidertrack.cpp | 80 ++++++++++++++++++++++++++++++++++++++ components/slidertrack.h | 53 +++++++++++++++++++++++++ qt-material-widgets.pro | 6 ++- 6 files changed, 152 insertions(+), 29 deletions(-) create mode 100644 components/slidertrack.cpp create mode 100644 components/slidertrack.h diff --git a/components/slider.cpp b/components/slider.cpp index f870aa3..f87eaa4 100644 --- a/components/slider.cpp +++ b/components/slider.cpp @@ -49,19 +49,11 @@ bool Slider::pageStepMode() const return d->pageStepMode; } -void Slider::setTrackWidth(int width) -{ - Q_D(Slider); - - d->trackWidth = width; - update(); -} - -int Slider::trackWidth() const +bool Slider::hovered() const { Q_D(const Slider); - return d->trackWidth; + return d->hover; } void Slider::sliderChange(SliderChange change) @@ -107,12 +99,6 @@ void Slider::paintEvent(QPaintEvent *event) { Q_UNUSED(event) - Q_D(Slider); - - QPainter painter(this); - - d->paintTrack(&painter); - #ifdef DEBUG_LAYOUT if (hasFocus()) painter.drawRect(rect().adjusted(0, 0, -1, -1)); diff --git a/components/slider.h b/components/slider.h index b95d718..c5d88f1 100644 --- a/components/slider.h +++ b/components/slider.h @@ -12,8 +12,6 @@ class Slider : public QAbstractSlider { Q_OBJECT - Q_PROPERTY(int trackWidth WRITE setTrackWidth READ trackWidth) - public: explicit Slider(QWidget *parent = 0); ~Slider(); @@ -25,8 +23,7 @@ public: void setPageStepMode(bool pageStep); bool pageStepMode() const; - void setTrackWidth(int width); - int trackWidth() const; + bool hovered() const; protected: void sliderChange(SliderChange change) Q_DECL_OVERRIDE; diff --git a/components/slider_p.h b/components/slider_p.h index 3d04e10..2a667fe 100644 --- a/components/slider_p.h +++ b/components/slider_p.h @@ -6,6 +6,7 @@ #include #include "lib/style.h" #include "slider.h" +#include "slidertrack.h" #include "sliderthumb.h" #include "sliderstatemachine.h" @@ -20,12 +21,13 @@ public: QRectF trackBoundingRect() const; QRectF thumbBoundingRect() const; - void paintTrack(QPainter *painter); + //void paintTrack(QPainter *painter); int valueFromPosition(const QPoint &pos) const; void setHovered(bool status); Slider *const q_ptr; + SliderTrack *const track; SliderThumb *const thumb; SliderStateMachine *const machine; bool hoverTrack; @@ -40,6 +42,7 @@ public: SliderPrivate::SliderPrivate(Slider *parent) : q_ptr(parent), + track(new SliderTrack(parent)), thumb(new SliderThumb(parent)), machine(new SliderStateMachine(parent, thumb)), hoverTrack(false), @@ -74,10 +77,10 @@ QRectF SliderPrivate::trackBoundingRect() const qreal hw = static_cast(trackWidth)/2; return Qt::Horizontal == q->orientation() - ? QRectF(SLIDER_MARGIN, q->rect().height()/2 - hw, - q->rect().width() - SLIDER_MARGIN*2, hw*2) - : QRectF(q->rect().width()/2 - hw, SLIDER_MARGIN, hw*2, - q->rect().height() - SLIDER_MARGIN*2); + ? QRectF(SLIDER_MARGIN, q->height()/2 - hw, + q->width() - SLIDER_MARGIN*2, hw*2) + : QRectF(q->width()/2 - hw, SLIDER_MARGIN, hw*2, + q->height() - SLIDER_MARGIN*2); } QRectF SliderPrivate::thumbBoundingRect() const @@ -85,12 +88,13 @@ QRectF SliderPrivate::thumbBoundingRect() const Q_Q(const Slider); return Qt::Horizontal == q->orientation() - ? QRectF(q->thumbOffset(), q->rect().height()/2 - SLIDER_MARGIN, + ? QRectF(q->thumbOffset(), q->height()/2 - SLIDER_MARGIN, SLIDER_MARGIN*2, SLIDER_MARGIN*2) - : QRectF(q->rect().width()/2 - SLIDER_MARGIN, q->thumbOffset(), + : QRectF(q->width()/2 - SLIDER_MARGIN, q->thumbOffset(), SLIDER_MARGIN*2, SLIDER_MARGIN*2); } +/* void SliderPrivate::paintTrack(QPainter *painter) { Q_Q(const Slider); @@ -139,6 +143,7 @@ void SliderPrivate::paintTrack(QPainter *painter) } #endif } +*/ int SliderPrivate::valueFromPosition(const QPoint &pos) const { diff --git a/components/slidertrack.cpp b/components/slidertrack.cpp new file mode 100644 index 0000000..df30b37 --- /dev/null +++ b/components/slidertrack.cpp @@ -0,0 +1,80 @@ +#include "slidertrack.h" +#include +#include +#include "lib/style.h" +#include "slider.h" + +SliderTrack::SliderTrack(Slider *slider) + : QWidget(slider->parentWidget()), + slider(slider), + _width(2) +{ + slider->installEventFilter(this); + setAttribute(Qt::WA_TransparentForMouseEvents, true); +} + +SliderTrack::~SliderTrack() +{ +} + +bool SliderTrack::eventFilter(QObject *obj, QEvent *event) +{ + if (QEvent::ParentChange == event->type()) { + setParent(slider->parentWidget()); + } + return QWidget::eventFilter(obj, event); +} + +void SliderTrack::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + Style &style = Style::instance(); + + QBrush fg; + fg.setStyle(Qt::SolidPattern); + fg.setColor(slider->isEnabled() ? style.themeColor("primary1") + : style.themeColor("disabled")); + QBrush bg; + bg.setStyle(Qt::SolidPattern); + bg.setColor(slider->hovered() ? QColor(0, 0, 0, 150) // @TODO: set theme color + : style.themeColor("accent3")); + + qreal offset = slider->thumbOffset() + SLIDER_MARGIN; + + QSizeF box(slider->isEnabled() ? offset : offset - 7, + qMax(slider->width(), slider->height())); + + if (Qt::Vertical == slider->orientation()) + box.transpose(); + + QRectF rect = Qt::Vertical == slider->orientation() + ? QRectF(0, slider->isEnabled() ? offset : offset + 7, + box.width(), box.width()) + : QRectF(slider->isEnabled() ? offset : offset + 7, 0, + box.height(), box.height()); + + qreal hw = static_cast(_width)/2; + + QRectF geometry = Qt::Horizontal == slider->orientation() + ? QRectF(SLIDER_MARGIN, slider->rect().height()/2 - hw, + slider->width() - SLIDER_MARGIN*2, hw*2) + : QRectF(slider->width()/2 - hw, SLIDER_MARGIN, hw*2, + slider->height() - SLIDER_MARGIN*2); + + bool inverted = slider->invertedAppearance(); + + painter.fillRect(QRectF(QPointF(0, 0), box).intersected(geometry), + inverted ? bg : fg); + painter.fillRect(rect.intersected(geometry), inverted ? fg : bg); + +#ifdef DEBUG_LAYOUT + if (hoverTrack) { + painter.save(); + painter.setPen(Qt::red); + painter.drawRect(geometry); + painter.restore(); + } +#endif + + QWidget::paintEvent(event); +} diff --git a/components/slidertrack.h b/components/slidertrack.h new file mode 100644 index 0000000..4abbd01 --- /dev/null +++ b/components/slidertrack.h @@ -0,0 +1,53 @@ +#ifndef SLIDERTRACK_H +#define SLIDERTRACK_H + +#include + +class Slider; + +class SliderTrack : public QWidget +{ + Q_OBJECT + + Q_PROPERTY(QColor fillColor WRITE setFillColor READ fillColor) + Q_PROPERTY(int trackWidth WRITE setTrackWidth READ trackWidth) + +public: + explicit SliderTrack(Slider *slider); + ~SliderTrack(); + + inline void setFillColor(const QColor &color) + { + _fillColor = color; + update(); + } + + inline QColor fillColor() const + { + return _fillColor; + } + + void setTrackWidth(int width) + { + _width = width; + update(); + } + + int trackWidth() const + { + return _width; + } + +protected: + bool eventFilter(QObject *obj, QEvent *event); + void paintEvent(QPaintEvent *event); + +private: + Q_DISABLE_COPY(SliderTrack) + + const Slider *const slider; + QColor _fillColor; + int _width; +}; + +#endif // SLIDERTRACK_H diff --git a/qt-material-widgets.pro b/qt-material-widgets.pro index f572049..ba213fb 100644 --- a/qt-material-widgets.pro +++ b/qt-material-widgets.pro @@ -54,7 +54,8 @@ SOURCES += main.cpp\ components/sliderthumb.cpp \ components/searchfield.cpp \ lib/theme.cpp \ - components/sliderstatemachine.cpp + components/sliderstatemachine.cpp \ + components/slidertrack.cpp HEADERS += mainwindow.h \ components/appbar.h \ @@ -104,7 +105,8 @@ HEADERS += mainwindow.h \ components/searchfield.h \ lib/theme.h \ lib/theme_p.h \ - components/sliderstatemachine.h + components/sliderstatemachine.h \ + components/slidertrack.h RESOURCES += \ resources.qrc