simplify slider state machine code

This commit is contained in:
laserpants 2016-05-14 11:31:54 +03:00
parent 0c7920188b
commit b0d2a56905
4 changed files with 8 additions and 68 deletions

View File

@ -80,21 +80,6 @@ void Slider::sliderChange(SliderChange change)
QAbstractSlider::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) void Slider::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)

View File

@ -27,8 +27,6 @@ public:
protected: protected:
void sliderChange(SliderChange change) Q_DECL_OVERRIDE; void sliderChange(SliderChange change) Q_DECL_OVERRIDE;
void changeEvent(QEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

View File

@ -22,7 +22,6 @@ SliderStateMachine::SliderStateMachine(Slider *parent,
QState *inactiveState = new QState(fstState); QState *inactiveState = new QState(fstState);
QState *focusState = new QState(fstState); QState *focusState = new QState(fstState);
QState *slidingState = new QState(fstState); QState *slidingState = new QState(fstState);
QState *disabledState = new QState(fstState);
QState *pulseOutState = new QState(focusState); QState *pulseOutState = new QState(focusState);
QState *pulseInState = new QState(focusState); QState *pulseInState = new QState(focusState);
@ -35,23 +34,15 @@ SliderStateMachine::SliderStateMachine(Slider *parent,
pulseOutState->assignProperty(thumb, "haloSize", 35); pulseOutState->assignProperty(thumb, "haloSize", 35);
pulseInState->assignProperty(thumb, "haloSize", 28); pulseInState->assignProperty(thumb, "haloSize", 28);
disabledState->assignProperty(thumb, "diameter", 7);
disabledState->assignProperty(thumb, "fillColor", style.themeColor("disabled"));
inactiveState->assignProperty(thumb, "diameter", 11); inactiveState->assignProperty(thumb, "diameter", 11);
focusState->assignProperty(thumb, "diameter", 11); focusState->assignProperty(thumb, "diameter", 11);
slidingState->assignProperty(thumb, "diameter", 17); slidingState->assignProperty(thumb, "diameter", 17);
QColor fillColor = style.themeColor("primary1"); 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")); inactiveState->assignProperty(track, "fillColor", style.themeColor("accent2"));
slidingState->assignProperty(track, "fillColor", style.themeColor("accent3")); slidingState->assignProperty(track, "fillColor", style.themeColor("accent3"));
focusState->assignProperty(track, "fillColor", style.themeColor("accent3")); focusState->assignProperty(track, "fillColor", style.themeColor("accent3"));
disabledState->assignProperty(track, "fillColor", style.themeColor("disabled"));
addState(topState); addState(topState);
@ -62,24 +53,6 @@ SliderStateMachine::SliderStateMachine(Slider *parent,
QAbstractTransition *transition; QAbstractTransition *transition;
QPropertyAnimation *animation; 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 // Show halo on mouse enter
transition = new QSignalTransition(this, SIGNAL(noFocusMouseEnter())); transition = new QSignalTransition(this, SIGNAL(noFocusMouseEnter()));
@ -187,12 +160,10 @@ SliderStateMachine::SliderStateMachine(Slider *parent,
QColor canvasColor = style.themeColor("canvas"); QColor canvasColor = style.themeColor("canvas");
minState->assignProperty(thumb, "minFillColor", canvasColor);
minState->assignProperty(thumb, "fillColor", canvasColor); minState->assignProperty(thumb, "fillColor", canvasColor);
minState->assignProperty(thumb, "haloColor", minHaloColor); minState->assignProperty(thumb, "haloColor", minHaloColor);
minState->assignProperty(thumb, "borderWidth", 2); minState->assignProperty(thumb, "borderWidth", 2);
normalState->assignProperty(thumb, "fillColor", fillColor); normalState->assignProperty(thumb, "fillColor", fillColor);
normalState->assignProperty(thumb, "minFillColor", fillColor);
normalState->assignProperty(thumb, "haloColor", haloColor); normalState->assignProperty(thumb, "haloColor", haloColor);
normalState->assignProperty(thumb, "borderWidth", 0); normalState->assignProperty(thumb, "borderWidth", 0);
@ -218,7 +189,7 @@ SliderStateMachine::SliderStateMachine(Slider *parent,
transition = new QSignalTransition(this, SIGNAL(changedToMinimum())); transition = new QSignalTransition(this, SIGNAL(changedToMinimum()));
transition->setTargetState(minState); transition->setTargetState(minState);
animation = new QPropertyAnimation(thumb, "minFillColor"); animation = new QPropertyAnimation(thumb, "fillColor");
animation->setDuration(200); animation->setDuration(200);
transition->addAnimation(animation); transition->addAnimation(animation);
@ -295,13 +266,12 @@ void SliderThumb::paintEvent(QPaintEvent *event)
// Knob // Knob
brush.setColor(slider->value() > slider->minimum() brush.setColor(slider->isEnabled()
? (slider->isEnabled() ? _fillColor
? _fillColor : Style::instance().themeColor("disabled")) : Style::instance().themeColor("disabled"));
: _minFillColor);
painter.setBrush(brush); painter.setBrush(brush);
if (_borderWidth > 0) { if (_borderWidth > 0 && slider->isEnabled()) {
QPen pen; QPen pen;
pen.setColor(Style::instance().themeColor("accent3")); pen.setColor(Style::instance().themeColor("accent3"));
pen.setWidthF(_borderWidth); pen.setWidthF(_borderWidth);
@ -316,7 +286,9 @@ void SliderThumb::paintEvent(QPaintEvent *event)
: QRectF(slider->width()/2 - SLIDER_MARGIN, slider->thumbOffset(), : QRectF(slider->width()/2 - SLIDER_MARGIN, slider->thumbOffset(),
SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos()); 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()); thumb.moveCenter(geometry.center());

View File

@ -19,8 +19,6 @@ public:
signals: signals:
void changedToMinimum(); void changedToMinimum();
void changedFromMinimum(); void changedFromMinimum();
void sliderEnabled();
void sliderDisabled();
void noFocusMouseEnter(); void noFocusMouseEnter();
void noFocusMouseLeave(); void noFocusMouseLeave();
@ -35,7 +33,6 @@ class SliderThumb : public QWidget
Q_PROPERTY(qreal diameter WRITE setDiameter READ diameter) Q_PROPERTY(qreal diameter WRITE setDiameter READ diameter)
Q_PROPERTY(qreal borderWidth WRITE setBorderWidth READ borderWidth) Q_PROPERTY(qreal borderWidth WRITE setBorderWidth READ borderWidth)
Q_PROPERTY(QColor fillColor WRITE setFillColor READ fillColor) 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(qreal haloSize WRITE setHaloSize READ haloSize)
Q_PROPERTY(QColor haloColor WRITE setHaloColor READ haloColor) Q_PROPERTY(QColor haloColor WRITE setHaloColor READ haloColor)
@ -76,17 +73,6 @@ public:
return _fillColor; return _fillColor;
} }
inline void setMinFillColor(const QColor &color)
{
_minFillColor = color;
update();
}
inline QColor minFillColor() const
{
return _minFillColor;
}
inline void setHaloSize(qreal size) inline void setHaloSize(qreal size)
{ {
_haloSize = size; _haloSize = size;
@ -121,7 +107,6 @@ private:
qreal _borderWidth; qreal _borderWidth;
qreal _haloSize; qreal _haloSize;
QColor _fillColor; QColor _fillColor;
QColor _minFillColor;
QColor _haloColor; QColor _haloColor;
}; };