From b0d2a569054ea2ef5ad88288dc9082ea1cee8dae Mon Sep 17 00:00:00 2001 From: laserpants Date: Sat, 14 May 2016 11:31:54 +0300 Subject: [PATCH] simplify slider state machine code --- components/slider.cpp | 15 ------------ components/slider.h | 2 -- components/sliderinternal.cpp | 44 +++++++---------------------------- components/sliderinternal.h | 15 ------------ 4 files changed, 8 insertions(+), 68 deletions(-) diff --git a/components/slider.cpp b/components/slider.cpp index 9a6b672..bbf3c36 100644 --- a/components/slider.cpp +++ b/components/slider.cpp @@ -80,21 +80,6 @@ void Slider::sliderChange(SliderChange change) QAbstractSlider::sliderChange(change); } -void Slider::changeEvent(QEvent *event) -{ - if (QEvent::EnabledChange == event->type()) - { - Q_D(Slider); - - if (isEnabled()) { - emit d->machine->sliderEnabled(); - } else { - emit d->machine->sliderDisabled(); - } - } - QAbstractSlider::changeEvent(event); -} - void Slider::paintEvent(QPaintEvent *event) { Q_UNUSED(event) diff --git a/components/slider.h b/components/slider.h index c5d88f1..9d5f7fb 100644 --- a/components/slider.h +++ b/components/slider.h @@ -27,8 +27,6 @@ public: protected: void sliderChange(SliderChange change) Q_DECL_OVERRIDE; - - void changeEvent(QEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; diff --git a/components/sliderinternal.cpp b/components/sliderinternal.cpp index 2615839..263f85b 100644 --- a/components/sliderinternal.cpp +++ b/components/sliderinternal.cpp @@ -22,7 +22,6 @@ SliderStateMachine::SliderStateMachine(Slider *parent, QState *inactiveState = new QState(fstState); QState *focusState = new QState(fstState); QState *slidingState = new QState(fstState); - QState *disabledState = new QState(fstState); QState *pulseOutState = new QState(focusState); QState *pulseInState = new QState(focusState); @@ -35,23 +34,15 @@ SliderStateMachine::SliderStateMachine(Slider *parent, pulseOutState->assignProperty(thumb, "haloSize", 35); pulseInState->assignProperty(thumb, "haloSize", 28); - disabledState->assignProperty(thumb, "diameter", 7); - disabledState->assignProperty(thumb, "fillColor", style.themeColor("disabled")); - inactiveState->assignProperty(thumb, "diameter", 11); focusState->assignProperty(thumb, "diameter", 11); slidingState->assignProperty(thumb, "diameter", 17); QColor fillColor = style.themeColor("primary1"); - inactiveState->assignProperty(thumb, "fillColor", fillColor); - 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); @@ -62,24 +53,6 @@ SliderStateMachine::SliderStateMachine(Slider *parent, QAbstractTransition *transition; QPropertyAnimation *animation; - // Add transitions - - transition = new QSignalTransition(this, SIGNAL(sliderDisabled())); - transition->setTargetState(disabledState); - inactiveState->addTransition(transition); - - transition = new QSignalTransition(this, SIGNAL(sliderDisabled())); - transition->setTargetState(disabledState); - focusState->addTransition(transition); - - transition = new QSignalTransition(this, SIGNAL(sliderDisabled())); - transition->setTargetState(disabledState); - slidingState->addTransition(transition); - - transition = new QSignalTransition(this, SIGNAL(sliderEnabled())); - transition->setTargetState(inactiveState); - disabledState->addTransition(transition); - // Show halo on mouse enter transition = new QSignalTransition(this, SIGNAL(noFocusMouseEnter())); @@ -187,12 +160,10 @@ SliderStateMachine::SliderStateMachine(Slider *parent, QColor canvasColor = style.themeColor("canvas"); - minState->assignProperty(thumb, "minFillColor", canvasColor); minState->assignProperty(thumb, "fillColor", canvasColor); minState->assignProperty(thumb, "haloColor", minHaloColor); minState->assignProperty(thumb, "borderWidth", 2); normalState->assignProperty(thumb, "fillColor", fillColor); - normalState->assignProperty(thumb, "minFillColor", fillColor); normalState->assignProperty(thumb, "haloColor", haloColor); normalState->assignProperty(thumb, "borderWidth", 0); @@ -218,7 +189,7 @@ SliderStateMachine::SliderStateMachine(Slider *parent, transition = new QSignalTransition(this, SIGNAL(changedToMinimum())); transition->setTargetState(minState); - animation = new QPropertyAnimation(thumb, "minFillColor"); + animation = new QPropertyAnimation(thumb, "fillColor"); animation->setDuration(200); transition->addAnimation(animation); @@ -295,13 +266,12 @@ void SliderThumb::paintEvent(QPaintEvent *event) // Knob - brush.setColor(slider->value() > slider->minimum() - ? (slider->isEnabled() - ? _fillColor : Style::instance().themeColor("disabled")) - : _minFillColor); + brush.setColor(slider->isEnabled() + ? _fillColor + : Style::instance().themeColor("disabled")); painter.setBrush(brush); - if (_borderWidth > 0) { + if (_borderWidth > 0 && slider->isEnabled()) { QPen pen; pen.setColor(Style::instance().themeColor("accent3")); pen.setWidthF(_borderWidth); @@ -316,7 +286,9 @@ void SliderThumb::paintEvent(QPaintEvent *event) : QRectF(slider->width()/2 - SLIDER_MARGIN, slider->thumbOffset(), SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos()); - QRectF thumb(0, 0, _diameter, _diameter); + qreal s = slider->isEnabled() ? _diameter : 7; + + QRectF thumb(0, 0, s, s); thumb.moveCenter(geometry.center()); diff --git a/components/sliderinternal.h b/components/sliderinternal.h index b2e9a94..f8940e4 100644 --- a/components/sliderinternal.h +++ b/components/sliderinternal.h @@ -19,8 +19,6 @@ public: signals: void changedToMinimum(); void changedFromMinimum(); - void sliderEnabled(); - void sliderDisabled(); void noFocusMouseEnter(); void noFocusMouseLeave(); @@ -35,7 +33,6 @@ class SliderThumb : public QWidget Q_PROPERTY(qreal diameter WRITE setDiameter READ diameter) Q_PROPERTY(qreal borderWidth WRITE setBorderWidth READ borderWidth) Q_PROPERTY(QColor fillColor WRITE setFillColor READ fillColor) - Q_PROPERTY(QColor minFillColor WRITE setMinFillColor READ minFillColor) Q_PROPERTY(qreal haloSize WRITE setHaloSize READ haloSize) Q_PROPERTY(QColor haloColor WRITE setHaloColor READ haloColor) @@ -76,17 +73,6 @@ public: return _fillColor; } - inline void setMinFillColor(const QColor &color) - { - _minFillColor = color; - update(); - } - - inline QColor minFillColor() const - { - return _minFillColor; - } - inline void setHaloSize(qreal size) { _haloSize = size; @@ -121,7 +107,6 @@ private: qreal _borderWidth; qreal _haloSize; QColor _fillColor; - QColor _minFillColor; QColor _haloColor; };