fix slider state machine issue

This commit is contained in:
FarmRadio Hangar 2016-05-11 17:23:17 +03:00
parent c17500ff37
commit 4ef57f8d3a
5 changed files with 83 additions and 47 deletions

View File

@ -3,6 +3,7 @@
#include <QPropertyAnimation>
#include <QStringBuilder>
#include <QMouseEvent>
#include <QApplication>
#include <QDebug>
#include "slider_p.h"
@ -11,7 +12,7 @@ Slider::Slider(QWidget *parent)
: QAbstractSlider(parent),
d_ptr(new SliderPrivate(this))
{
d_ptr->init(this);
d_func()->init(this);
setFocusPolicy(Qt::StrongFocus);
@ -20,6 +21,8 @@ Slider::Slider(QWidget *parent)
sp.transpose();
setSizePolicy(sp);
setAttribute(Qt::WA_WState_OwnSizePolicy, false);
QCoreApplication::processEvents();
}
Slider::~Slider()
@ -38,7 +41,8 @@ int Slider::thumbOffset() const
maximum(),
sliderPosition(),
Qt::Horizontal == orientation()
? rect().width() - 20 : rect().height() - 20,
? rect().width() -SLIDER_MARGIN*2
: rect().height() - SLIDER_MARGIN*2,
invertedAppearance());
}

View File

@ -11,9 +11,6 @@
#include "lib/style.h"
#include "sliderthumb.h"
// change
#define THUMB_OUTER_SIZE 20
class SliderPrivate
{
Q_DISABLE_COPY(SliderPrivate)
@ -81,15 +78,15 @@ void SliderPrivate::init(Slider *slider)
pulseInState->assignProperty(thumb, "haloSize", 28);
disabledState->assignProperty(thumb, "diameter", 7);
//disabledState->assignProperty(thumb, "fillColor", QColor(200, 200, 200));
disabledState->assignProperty(thumb, "fillColor", QColor(200, 200, 200));
inactiveState->assignProperty(thumb, "diameter", 11);
focusState->assignProperty(thumb, "diameter", 11);
slidingState->assignProperty(thumb, "diameter", 17);
//inactiveState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
//focusState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
//slidingState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
inactiveState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
focusState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
slidingState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
machine.addState(topState);
@ -181,29 +178,25 @@ void SliderPrivate::init(Slider *slider)
QState *minState = new QState(sndState);
QState *normalState = new QState(sndState);
minState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
minState->assignProperty(thumb, "borderWidth", 0);
normalState->assignProperty(thumb, "fillColor", QColor(255, 255, 255));
normalState->assignProperty(thumb, "borderWidth", 1.5);
minState->assignProperty(thumb, "minFillColor", QColor(255, 255, 255));
minState->assignProperty(thumb, "fillColor", QColor(255, 255, 255));
minState->assignProperty(thumb, "borderWidth", 2);
normalState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
normalState->assignProperty(thumb, "minFillColor", QColor(0, 0, 0));
normalState->assignProperty(thumb, "borderWidth", 0);
sndState->setInitialState(minState);
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum()));
transition->setTargetState(normalState);
animation = new QPropertyAnimation(thumb, "fillColor");
animation->setDuration(200);
transition->addAnimation(animation);
animation = new QPropertyAnimation(thumb, "borderWidth");
animation->setDuration(200);
transition->addAnimation(animation);
minState->addTransition(transition);
transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum()));
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
transition->setTargetState(minState);
animation = new QPropertyAnimation(thumb, "fillColor");
animation->setDuration(200);
transition->addAnimation(animation);
animation = new QPropertyAnimation(thumb, "borderWidth");
animation = new QPropertyAnimation(thumb, "minFillColor");
animation->setDuration(200);
transition->addAnimation(animation);
normalState->addTransition(transition);
@ -216,10 +209,10 @@ QRectF SliderPrivate::trackGeometry() const
Q_Q(const Slider);
return Qt::Horizontal == q->orientation()
? QRectF(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1,
q->rect().width() - THUMB_OUTER_SIZE, 2)
: QRectF(q->rect().width()/2 - 1, THUMB_OUTER_SIZE/2, 2,
q->rect().height() - THUMB_OUTER_SIZE);
? QRectF(SLIDER_MARGIN, q->rect().height()/2 - 1,
q->rect().width() - SLIDER_MARGIN*2, 2)
: QRectF(q->rect().width()/2 - 1, SLIDER_MARGIN, 2,
q->rect().height() - SLIDER_MARGIN*2);
}
QRectF SliderPrivate::thumbGeometry() const
@ -227,10 +220,10 @@ QRectF SliderPrivate::thumbGeometry() const
Q_Q(const Slider);
return Qt::Horizontal == q->orientation()
? QRectF(q->thumbOffset(), q->rect().height()/2 - THUMB_OUTER_SIZE/2,
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE)
: QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, q->thumbOffset(),
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE);
? QRectF(q->thumbOffset(), q->rect().height()/2 - SLIDER_MARGIN,
SLIDER_MARGIN*2, SLIDER_MARGIN*2)
: QRectF(q->rect().width()/2 - SLIDER_MARGIN, q->thumbOffset(),
SLIDER_MARGIN*2, SLIDER_MARGIN*2);
}
void SliderPrivate::paintTrack(QPainter *painter)
@ -247,7 +240,7 @@ void SliderPrivate::paintTrack(QPainter *painter)
bg.setStyle(Qt::SolidPattern);
bg.setColor(trackColor);
qreal offset = q->thumbOffset() + THUMB_OUTER_SIZE/2;
qreal offset = q->thumbOffset() + SLIDER_MARGIN;
QSizeF box(q->isEnabled() ? offset : offset - 7,
qMax(q->width(), q->height()));
@ -286,13 +279,13 @@ 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() - THUMB_OUTER_SIZE
: q->rect().height() - THUMB_OUTER_SIZE;
? q->rect().width() - SLIDER_MARGIN*2
: q->rect().height() - SLIDER_MARGIN*2;
return Style::sliderValueFromPosition(
q->minimum(),
q->maximum(),
position - THUMB_OUTER_SIZE/2,
position - SLIDER_MARGIN,
span,
q->invertedAppearance());
}

View File

@ -3,16 +3,11 @@
#include <QPainter>
#include "slider.h"
// change
#define THUMB_OUTER_SIZE 20
SliderThumb::SliderThumb(Slider *slider)
: QWidget(slider->parentWidget()),
slider(slider),
_diameter(11),
_borderWidth(0),
_fillColor(Qt::white),
_minFillColor(Qt::white),
_borderWidth(2),
_haloSize(0)
{
slider->installEventFilter(this);
@ -49,10 +44,10 @@ void SliderThumb::paintEvent(QPaintEvent *event)
painter.setRenderHint(QPainter::Antialiasing);
QPointF disp = Qt::Horizontal == slider->orientation()
? QPointF(THUMB_OUTER_SIZE/2 + slider->thumbOffset(),
? QPointF(SLIDER_MARGIN + slider->thumbOffset(),
slider->height()/2)
: QPointF(slider->width()/2,
THUMB_OUTER_SIZE/2 + slider->thumbOffset());
SLIDER_MARGIN + slider->thumbOffset());
QRectF halo((slider->pos() - QPointF(_haloSize, _haloSize)/2) + disp,
QSizeF(_haloSize, _haloSize));
@ -62,12 +57,13 @@ void SliderThumb::paintEvent(QPaintEvent *event)
// Knob
brush.setStyle(Qt::SolidPattern);
brush.setColor(slider->sliderPosition() > slider->minimum()
brush.setColor(slider->value() > slider->minimum()
? _fillColor : _minFillColor);
painter.setBrush(brush);
if (_borderWidth > 0) {
QPen pen;
pen.setColor(slider->trackColor());
pen.setWidthF(_borderWidth);
painter.setPen(pen);
} else {
@ -75,10 +71,10 @@ void SliderThumb::paintEvent(QPaintEvent *event)
}
QRectF geometry = Qt::Horizontal == slider->orientation()
? QRectF(slider->thumbOffset(), slider->height()/2 - THUMB_OUTER_SIZE/2,
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE).translated(slider->pos())
: QRectF(slider->width()/2 - THUMB_OUTER_SIZE/2, slider->thumbOffset(),
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE).translated(slider->pos());
? QRectF(slider->thumbOffset(), slider->height()/2 - SLIDER_MARGIN,
SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos())
: QRectF(slider->width()/2 - SLIDER_MARGIN, slider->thumbOffset(),
SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos());
QRectF thumb(0, 0, _diameter, _diameter);
@ -93,6 +89,8 @@ void SliderThumb::paintEvent(QPaintEvent *event)
painter.setPen(pen);
painter.setBrush(Qt::NoBrush);
painter.drawRect(geometry);
painter.drawRect(rect().adjusted(0, 0, -2, -2));
#endif

View File

@ -1,6 +1,8 @@
#ifndef SLIDERTHUMB_H
#define SLIDERTHUMB_H
#define SLIDER_MARGIN 30
#include <QWidget>
class Slider;

View File

@ -176,6 +176,7 @@ SliderExamples::SliderExamples(QWidget *parent)
}
{
Slider *slider = new Slider;
//slider->setValue(20);
slider->setDisabled(true);
slider->setMinimumWidth(250);
@ -191,6 +192,44 @@ SliderExamples::SliderExamples(QWidget *parent)
mainLayout->addWidget(frame);
}
{
QVBoxLayout *layout = new QVBoxLayout;
QWidget *widget = new QWidget;
widget->setLayout(layout);
widget->setMinimumWidth(350);
Slider *slider = new Slider;
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(slider);
layout->addLayout(hLayout);
slider = new Slider;
hLayout = new QHBoxLayout;
hLayout->addWidget(slider);
layout->addLayout(hLayout);
slider = new Slider;
hLayout = new QHBoxLayout;
hLayout->addWidget(slider);
layout->addLayout(hLayout);
ExampleView *view = new ExampleView;
view->setWidget(widget);
Frame *frame = new Frame;
frame->setCodeSnippet(
"xx"
);
frame->setWidget(view);
mainLayout->addWidget(frame);
}
}