refactor slider a bit
This commit is contained in:
parent
f83a9608b0
commit
5a76f719b9
|
@ -163,6 +163,8 @@ void Slider::mouseMoveEvent(QMouseEvent *event)
|
||||||
d->hoverThumb = !d->hoverThumb;
|
d->hoverThumb = !d->hoverThumb;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
d->setHovered(d->hoverTrack || d->hoverThumb);
|
||||||
}
|
}
|
||||||
|
|
||||||
QAbstractSlider::mouseMoveEvent(event);
|
QAbstractSlider::mouseMoveEvent(event);
|
||||||
|
@ -213,3 +215,21 @@ void Slider::mouseReleaseEvent(QMouseEvent *event)
|
||||||
|
|
||||||
QAbstractSlider::mouseReleaseEvent(event);
|
QAbstractSlider::mouseReleaseEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Slider::leaveEvent(QEvent *event)
|
||||||
|
{
|
||||||
|
Q_D(Slider);
|
||||||
|
|
||||||
|
if (d->hoverTrack) {
|
||||||
|
d->hoverTrack = false;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
if (d->hoverThumb) {
|
||||||
|
d->hoverThumb = false;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
d->setHovered(false);
|
||||||
|
|
||||||
|
QAbstractSlider::leaveEvent(event);
|
||||||
|
}
|
||||||
|
|
|
@ -31,10 +31,12 @@ public:
|
||||||
int trackWidth() const;
|
int trackWidth() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void changedToMinimum();
|
void changedToMinimum(); // @TODO: create custom event
|
||||||
void changedFromMinimum();
|
void changedFromMinimum();
|
||||||
void sliderEnabled();
|
void sliderEnabled();
|
||||||
void sliderDisabled();
|
void sliderDisabled();
|
||||||
|
void mouseEnter(); // rename/change
|
||||||
|
void mouseLeave(); // rename/change
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sliderChange(SliderChange change) Q_DECL_OVERRIDE;
|
void sliderChange(SliderChange change) Q_DECL_OVERRIDE;
|
||||||
|
@ -44,6 +46,7 @@ protected:
|
||||||
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;
|
||||||
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
const QScopedPointer<SliderPrivate> d_ptr;
|
const QScopedPointer<SliderPrivate> d_ptr;
|
||||||
|
|
||||||
|
|
|
@ -29,11 +29,14 @@ public:
|
||||||
void paintTrack(QPainter *painter);
|
void paintTrack(QPainter *painter);
|
||||||
int valueFromPosition(const QPoint &pos) const;
|
int valueFromPosition(const QPoint &pos) const;
|
||||||
|
|
||||||
|
void setHovered(bool hovered);
|
||||||
|
|
||||||
Slider *const q_ptr;
|
Slider *const q_ptr;
|
||||||
SliderThumb *const thumb;
|
SliderThumb *const thumb;
|
||||||
QStateMachine machine;
|
QStateMachine machine;
|
||||||
bool hoverTrack;
|
bool hoverTrack;
|
||||||
bool hoverThumb;
|
bool hoverThumb;
|
||||||
|
bool hover;
|
||||||
bool step;
|
bool step;
|
||||||
bool pageStepMode;
|
bool pageStepMode;
|
||||||
int stepTo;
|
int stepTo;
|
||||||
|
@ -47,6 +50,7 @@ SliderPrivate::SliderPrivate(Slider *parent)
|
||||||
thumb(new SliderThumb(parent)),
|
thumb(new SliderThumb(parent)),
|
||||||
hoverTrack(false),
|
hoverTrack(false),
|
||||||
hoverThumb(false),
|
hoverThumb(false),
|
||||||
|
hover(false),
|
||||||
step(false),
|
step(false),
|
||||||
pageStepMode(false),
|
pageStepMode(false),
|
||||||
stepTo(0),
|
stepTo(0),
|
||||||
|
@ -129,12 +133,23 @@ void SliderPrivate::init(Slider *slider)
|
||||||
transition->setTargetState(inactiveState);
|
transition->setTargetState(inactiveState);
|
||||||
disabledState->addTransition(transition);
|
disabledState->addTransition(transition);
|
||||||
|
|
||||||
|
// Show halo on mouse enter
|
||||||
|
|
||||||
|
transition = new QSignalTransition(slider, SIGNAL(mouseEnter()));
|
||||||
|
transition->setTargetState(focusState);
|
||||||
|
|
||||||
|
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||||
|
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||||
|
transition->addAnimation(animation);
|
||||||
|
inactiveState->addTransition(transition);
|
||||||
|
|
||||||
// Show halo on focus in
|
// Show halo on focus in
|
||||||
|
|
||||||
transition = new QEventTransition(slider, QEvent::FocusIn);
|
transition = new QEventTransition(slider, QEvent::FocusIn);
|
||||||
transition->setTargetState(focusState);
|
transition->setTargetState(focusState);
|
||||||
|
|
||||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||||
|
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||||
transition->addAnimation(animation);
|
transition->addAnimation(animation);
|
||||||
inactiveState->addTransition(transition);
|
inactiveState->addTransition(transition);
|
||||||
|
|
||||||
|
@ -144,6 +159,17 @@ void SliderPrivate::init(Slider *slider)
|
||||||
transition->setTargetState(inactiveState);
|
transition->setTargetState(inactiveState);
|
||||||
|
|
||||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||||
|
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||||
|
transition->addAnimation(animation);
|
||||||
|
focusState->addTransition(transition);
|
||||||
|
|
||||||
|
// Hide halo on mouse leave, except if widget has focus
|
||||||
|
|
||||||
|
transition = new QSignalTransition(slider, SIGNAL(mouseLeave()));
|
||||||
|
transition->setTargetState(inactiveState);
|
||||||
|
|
||||||
|
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||||
|
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||||
transition->addAnimation(animation);
|
transition->addAnimation(animation);
|
||||||
focusState->addTransition(transition);
|
focusState->addTransition(transition);
|
||||||
|
|
||||||
|
@ -174,7 +200,10 @@ void SliderPrivate::init(Slider *slider)
|
||||||
transition = new QSignalTransition(slider, SIGNAL(sliderPressed()));
|
transition = new QSignalTransition(slider, SIGNAL(sliderPressed()));
|
||||||
transition->setTargetState(slidingState);
|
transition->setTargetState(slidingState);
|
||||||
transition->addAnimation(new QPropertyAnimation(thumb, "diameter"));
|
transition->addAnimation(new QPropertyAnimation(thumb, "diameter"));
|
||||||
transition->addAnimation(new QPropertyAnimation(thumb, "haloSize"));
|
|
||||||
|
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||||
|
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||||
|
transition->addAnimation(animation);
|
||||||
focusState->addTransition(transition);
|
focusState->addTransition(transition);
|
||||||
|
|
||||||
// Slider released
|
// Slider released
|
||||||
|
@ -182,20 +211,24 @@ void SliderPrivate::init(Slider *slider)
|
||||||
transition = new QSignalTransition(slider, SIGNAL(sliderReleased()));
|
transition = new QSignalTransition(slider, SIGNAL(sliderReleased()));
|
||||||
transition->setTargetState(focusState);
|
transition->setTargetState(focusState);
|
||||||
transition->addAnimation(new QPropertyAnimation(thumb, "diameter"));
|
transition->addAnimation(new QPropertyAnimation(thumb, "diameter"));
|
||||||
transition->addAnimation(new QPropertyAnimation(thumb, "haloSize"));
|
|
||||||
|
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||||
|
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||||
|
transition->addAnimation(animation);
|
||||||
slidingState->addTransition(transition);
|
slidingState->addTransition(transition);
|
||||||
|
|
||||||
// Min. value animation
|
// Min. value transitions
|
||||||
|
|
||||||
QState *sndState = new QState(topState);
|
QState *sndState = new QState(topState);
|
||||||
|
|
||||||
QState *minState = new QState(sndState);
|
QState *minState = new QState(sndState);
|
||||||
QState *normalState = new QState(sndState);
|
QState *normalState = new QState(sndState);
|
||||||
|
|
||||||
QColor minHaloColor(trackColor);
|
QColor minHaloColor = style.themeColor("accent3");
|
||||||
|
minHaloColor.setAlphaF(0.15);
|
||||||
|
|
||||||
QColor haloColor = style.themeColor("primary1");
|
QColor haloColor = style.themeColor("primary1");
|
||||||
minHaloColor.setAlphaF(0.2);
|
haloColor.setAlphaF(0.15);
|
||||||
haloColor.setAlphaF(0.2);
|
|
||||||
|
|
||||||
QColor canvasColor = style.themeColor("canvas");
|
QColor canvasColor = style.themeColor("canvas");
|
||||||
|
|
||||||
|
@ -212,28 +245,42 @@ void SliderPrivate::init(Slider *slider)
|
||||||
|
|
||||||
transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum()));
|
transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum()));
|
||||||
transition->setTargetState(normalState);
|
transition->setTargetState(normalState);
|
||||||
|
|
||||||
animation = new QPropertyAnimation(thumb, "fillColor");
|
animation = new QPropertyAnimation(thumb, "fillColor");
|
||||||
animation->setDuration(200);
|
animation->setDuration(200);
|
||||||
transition->addAnimation(animation);
|
transition->addAnimation(animation);
|
||||||
minState->addTransition(transition);
|
|
||||||
animation = new QPropertyAnimation(thumb, "haloColor");
|
animation = new QPropertyAnimation(thumb, "haloColor");
|
||||||
animation->setDuration(200);
|
animation->setDuration(200);
|
||||||
transition->addAnimation(animation);
|
transition->addAnimation(animation);
|
||||||
|
|
||||||
|
animation = new QPropertyAnimation(thumb, "borderWidth");
|
||||||
|
animation->setDuration(400);
|
||||||
|
transition->addAnimation(animation);
|
||||||
|
|
||||||
minState->addTransition(transition);
|
minState->addTransition(transition);
|
||||||
|
|
||||||
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
|
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
|
||||||
transition->setTargetState(minState);
|
transition->setTargetState(minState);
|
||||||
|
|
||||||
animation = new QPropertyAnimation(thumb, "minFillColor");
|
animation = new QPropertyAnimation(thumb, "minFillColor");
|
||||||
animation->setDuration(200);
|
animation->setDuration(200);
|
||||||
transition->addAnimation(animation);
|
transition->addAnimation(animation);
|
||||||
normalState->addTransition(transition);
|
|
||||||
animation = new QPropertyAnimation(thumb, "haloColor");
|
animation = new QPropertyAnimation(thumb, "haloColor");
|
||||||
animation->setDuration(200);
|
animation->setDuration(200);
|
||||||
transition->addAnimation(animation);
|
transition->addAnimation(animation);
|
||||||
|
|
||||||
|
animation = new QPropertyAnimation(thumb, "borderWidth");
|
||||||
|
animation->setDuration(400);
|
||||||
|
transition->addAnimation(animation);
|
||||||
|
|
||||||
normalState->addTransition(transition);
|
normalState->addTransition(transition);
|
||||||
|
|
||||||
machine.start();
|
machine.start();
|
||||||
|
|
||||||
|
// End of state machine code
|
||||||
|
|
||||||
slider->setFocusPolicy(Qt::StrongFocus);
|
slider->setFocusPolicy(Qt::StrongFocus);
|
||||||
|
|
||||||
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
||||||
|
@ -277,11 +324,14 @@ void SliderPrivate::paintTrack(QPainter *painter)
|
||||||
|
|
||||||
QBrush fg;
|
QBrush fg;
|
||||||
fg.setStyle(Qt::SolidPattern);
|
fg.setStyle(Qt::SolidPattern);
|
||||||
fg.setColor(thumb->fillColor());
|
fg.setColor(q->isEnabled() ? Style::instance().themeColor("primary1")
|
||||||
|
: trackColor);
|
||||||
|
// @TODO -- clean up this
|
||||||
|
|
||||||
QBrush bg;
|
QBrush bg;
|
||||||
bg.setStyle(Qt::SolidPattern);
|
bg.setStyle(Qt::SolidPattern);
|
||||||
bg.setColor(trackColor);
|
bg.setColor(hover ? QColor(0, 0, 0, 150)
|
||||||
|
: trackColor);
|
||||||
|
|
||||||
qreal offset = q->thumbOffset() + SLIDER_MARGIN;
|
qreal offset = q->thumbOffset() + SLIDER_MARGIN;
|
||||||
|
|
||||||
|
@ -333,4 +383,21 @@ int SliderPrivate::valueFromPosition(const QPoint &pos) const
|
||||||
q->invertedAppearance());
|
q->invertedAppearance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SliderPrivate::setHovered(bool hovered)
|
||||||
|
{
|
||||||
|
Q_Q(Slider);
|
||||||
|
|
||||||
|
if (hover != hovered) {
|
||||||
|
hover = hovered;
|
||||||
|
if (!q->hasFocus()) {
|
||||||
|
if (hovered) {
|
||||||
|
emit q->mouseEnter();
|
||||||
|
} else {
|
||||||
|
emit q->mouseLeave();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q->update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif // SLIDER_P_H
|
#endif // SLIDER_P_H
|
||||||
|
|
Loading…
Reference in New Issue