diff --git a/components/slider.cpp b/components/slider.cpp index f87eaa4..4b1e92b 100644 --- a/components/slider.cpp +++ b/components/slider.cpp @@ -30,8 +30,8 @@ int Slider::thumbOffset() const maximum(), sliderPosition(), Qt::Horizontal == orientation() - ? rect().width() - SLIDER_MARGIN*2 - : rect().height() - SLIDER_MARGIN*2, + ? width() - SLIDER_MARGIN*2 + : height() - SLIDER_MARGIN*2, invertedAppearance()); } @@ -100,6 +100,8 @@ void Slider::paintEvent(QPaintEvent *event) Q_UNUSED(event) #ifdef DEBUG_LAYOUT + QPainter painter(this); + if (hasFocus()) painter.drawRect(rect().adjusted(0, 0, -1, -1)); diff --git a/components/slider_p.h b/components/slider_p.h index 2a667fe..1b4b8a0 100644 --- a/components/slider_p.h +++ b/components/slider_p.h @@ -21,14 +21,13 @@ public: QRectF trackBoundingRect() const; QRectF thumbBoundingRect() const; - //void paintTrack(QPainter *painter); int valueFromPosition(const QPoint &pos) const; void setHovered(bool status); Slider *const q_ptr; - SliderTrack *const track; SliderThumb *const thumb; + SliderTrack *const track; SliderStateMachine *const machine; bool hoverTrack; bool hoverThumb; @@ -42,9 +41,9 @@ public: SliderPrivate::SliderPrivate(Slider *parent) : q_ptr(parent), - track(new SliderTrack(parent)), thumb(new SliderThumb(parent)), - machine(new SliderStateMachine(parent, thumb)), + track(new SliderTrack(parent)), + machine(new SliderStateMachine(parent, thumb, track)), hoverTrack(false), hoverThumb(false), hover(false), @@ -94,57 +93,6 @@ QRectF SliderPrivate::thumbBoundingRect() const SLIDER_MARGIN*2, SLIDER_MARGIN*2); } -/* -void SliderPrivate::paintTrack(QPainter *painter) -{ - Q_Q(const Slider); - - Style &style = Style::instance(); - - painter->save(); - - QBrush fg; - fg.setStyle(Qt::SolidPattern); - fg.setColor(q->isEnabled() ? style.themeColor("primary1") - : style.themeColor("disabled")); - QBrush bg; - bg.setStyle(Qt::SolidPattern); - bg.setColor(hover ? QColor(0, 0, 0, 150) // @TODO: set theme color - : style.themeColor("accent3")); - - qreal offset = q->thumbOffset() + SLIDER_MARGIN; - - QSizeF box(q->isEnabled() ? offset : offset - 7, - qMax(q->width(), q->height())); - - if (Qt::Vertical == q->orientation()) - box.transpose(); - - QRectF rect = Qt::Vertical == q->orientation() - ? QRectF(0, q->isEnabled() ? offset : offset + 7, - box.width(), box.width()) - : QRectF(q->isEnabled() ? offset : offset + 7, 0, - box.height(), box.height()); - - bool inverted = q->invertedAppearance(); - - painter->fillRect(QRectF(QPointF(0, 0), box).intersected(trackBoundingRect()), - inverted ? bg : fg); - painter->fillRect(rect.intersected(trackBoundingRect()), inverted ? fg : bg); - - painter->restore(); - -#ifdef DEBUG_LAYOUT - if (hoverTrack) { - painter->save(); - painter->setPen(Qt::red); - painter->drawRect(trackBoundingRect()); - painter->restore(); - } -#endif -} -*/ - int SliderPrivate::valueFromPosition(const QPoint &pos) const { Q_Q(const Slider); @@ -152,8 +100,8 @@ int SliderPrivate::valueFromPosition(const QPoint &pos) const int position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y(); int span = Qt::Horizontal == q->orientation() - ? q->rect().width() - SLIDER_MARGIN*2 - : q->rect().height() - SLIDER_MARGIN*2; + ? q->width() - SLIDER_MARGIN*2 + : q->height() - SLIDER_MARGIN*2; return Style::sliderValueFromPosition( q->minimum(), diff --git a/components/sliderstatemachine.cpp b/components/sliderstatemachine.cpp index 9749cdf..26f9bb0 100644 --- a/components/sliderstatemachine.cpp +++ b/components/sliderstatemachine.cpp @@ -5,8 +5,11 @@ #include "lib/style.h" #include "slider.h" #include "sliderthumb.h" +#include "slidertrack.h" -SliderStateMachine::SliderStateMachine(Slider *parent, SliderThumb *thumb) +SliderStateMachine::SliderStateMachine(Slider *parent, + SliderThumb *thumb, + SliderTrack *track) : QStateMachine(parent) { Style &style = Style::instance(); @@ -44,6 +47,11 @@ SliderStateMachine::SliderStateMachine(Slider *parent, SliderThumb *thumb) focusState->assignProperty(thumb, "fillColor", fillColor); slidingState->assignProperty(thumb, "fillColor", fillColor); + inactiveState->assignProperty(track, "fillColor", style.themeColor("accent2")); + slidingState->assignProperty(track, "fillColor", style.themeColor("accent3")); + focusState->assignProperty(track, "fillColor", style.themeColor("accent3")); + disabledState->assignProperty(track, "fillColor", style.themeColor("disabled")); + addState(topState); fstState->setInitialState(inactiveState); @@ -79,6 +87,7 @@ SliderStateMachine::SliderStateMachine(Slider *parent, SliderThumb *thumb) animation = new QPropertyAnimation(thumb, "haloSize"); animation->setEasingCurve(QEasingCurve::InOutSine); transition->addAnimation(animation); + transition->addAnimation(new QPropertyAnimation(track, "fillColor")); inactiveState->addTransition(transition); // Show halo on focus in @@ -89,6 +98,7 @@ SliderStateMachine::SliderStateMachine(Slider *parent, SliderThumb *thumb) animation = new QPropertyAnimation(thumb, "haloSize"); animation->setEasingCurve(QEasingCurve::InOutSine); transition->addAnimation(animation); + transition->addAnimation(new QPropertyAnimation(track, "fillColor")); inactiveState->addTransition(transition); // Hide halo on focus out @@ -99,6 +109,7 @@ SliderStateMachine::SliderStateMachine(Slider *parent, SliderThumb *thumb) animation = new QPropertyAnimation(thumb, "haloSize"); animation->setEasingCurve(QEasingCurve::InOutSine); transition->addAnimation(animation); + transition->addAnimation(new QPropertyAnimation(track, "fillColor")); focusState->addTransition(transition); // Hide halo on mouse leave, except if widget has focus @@ -109,6 +120,7 @@ SliderStateMachine::SliderStateMachine(Slider *parent, SliderThumb *thumb) animation = new QPropertyAnimation(thumb, "haloSize"); animation->setEasingCurve(QEasingCurve::InOutSine); transition->addAnimation(animation); + transition->addAnimation(new QPropertyAnimation(track, "fillColor")); focusState->addTransition(transition); // Pulse in diff --git a/components/sliderstatemachine.h b/components/sliderstatemachine.h index 6780856..8b5face 100644 --- a/components/sliderstatemachine.h +++ b/components/sliderstatemachine.h @@ -5,13 +5,14 @@ class Slider; class SliderThumb; +class SliderTrack; class SliderStateMachine : public QStateMachine { Q_OBJECT public: - SliderStateMachine(Slider *parent, SliderThumb *thumb); + SliderStateMachine(Slider *parent, SliderThumb *thumb, SliderTrack *track); ~SliderStateMachine(); signals: diff --git a/components/slidertrack.cpp b/components/slidertrack.cpp index df30b37..9166235 100644 --- a/components/slidertrack.cpp +++ b/components/slidertrack.cpp @@ -36,10 +36,9 @@ void SliderTrack::paintEvent(QPaintEvent *event) : 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")); + bg.setColor(slider->isEnabled() ? _fillColor : style.themeColor("disabled")); - qreal offset = slider->thumbOffset() + SLIDER_MARGIN; + qreal offset = slider->thumbOffset(); QSizeF box(slider->isEnabled() ? offset : offset - 7, qMax(slider->width(), slider->height())); @@ -50,25 +49,34 @@ void SliderTrack::paintEvent(QPaintEvent *event) QRectF rect = Qt::Vertical == slider->orientation() ? QRectF(0, slider->isEnabled() ? offset : offset + 7, box.width(), box.width()) - : QRectF(slider->isEnabled() ? offset : offset + 7, 0, + : QRectF(slider->isEnabled() ? slider->x() + 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, + ? QRectF(slider->x() + SLIDER_MARGIN, + slider->y() + slider->height()/2 - hw, + slider->width() - SLIDER_MARGIN*2, + hw*2) + : QRectF(slider->x() + slider->width()/2 - hw, + slider->y() + 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); + QPointF pos = Qt::Horizontal == slider->orientation() + ? QPointF(slider->x() + SLIDER_MARGIN, 0) + : QPointF(0, slider->y() + SLIDER_MARGIN); + + //painter.fillRect(QRectF(QPointF(0, 0), box).intersected(geometry), + +// painter.fillRect(QRectF(pos, box), inverted ? bg : fg); + painter.fillRect(rect, inverted ? fg : bg); #ifdef DEBUG_LAYOUT - if (hoverTrack) { + if (slider->hovered()) { painter.save(); painter.setPen(Qt::red); painter.drawRect(geometry); diff --git a/qt-material-widgets.pro b/qt-material-widgets.pro index ba213fb..e91d5a5 100644 --- a/qt-material-widgets.pro +++ b/qt-material-widgets.pro @@ -5,7 +5,7 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = qt-material-widgets TEMPLATE = app -#DEFINES += DEBUG_LAYOUT=1 +DEFINES += DEBUG_LAYOUT=1 SOURCES += main.cpp\ mainwindow.cpp \