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);
}
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)

View File

@ -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;

View File

@ -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());

View File

@ -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;
};