fix slider state machine issue
This commit is contained in:
parent
c17500ff37
commit
4ef57f8d3a
|
@ -3,6 +3,7 @@
|
||||||
#include <QPropertyAnimation>
|
#include <QPropertyAnimation>
|
||||||
#include <QStringBuilder>
|
#include <QStringBuilder>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
|
#include <QApplication>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "slider_p.h"
|
#include "slider_p.h"
|
||||||
|
@ -11,7 +12,7 @@ Slider::Slider(QWidget *parent)
|
||||||
: QAbstractSlider(parent),
|
: QAbstractSlider(parent),
|
||||||
d_ptr(new SliderPrivate(this))
|
d_ptr(new SliderPrivate(this))
|
||||||
{
|
{
|
||||||
d_ptr->init(this);
|
d_func()->init(this);
|
||||||
|
|
||||||
setFocusPolicy(Qt::StrongFocus);
|
setFocusPolicy(Qt::StrongFocus);
|
||||||
|
|
||||||
|
@ -20,6 +21,8 @@ Slider::Slider(QWidget *parent)
|
||||||
sp.transpose();
|
sp.transpose();
|
||||||
setSizePolicy(sp);
|
setSizePolicy(sp);
|
||||||
setAttribute(Qt::WA_WState_OwnSizePolicy, false);
|
setAttribute(Qt::WA_WState_OwnSizePolicy, false);
|
||||||
|
|
||||||
|
QCoreApplication::processEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
Slider::~Slider()
|
Slider::~Slider()
|
||||||
|
@ -38,7 +41,8 @@ int Slider::thumbOffset() const
|
||||||
maximum(),
|
maximum(),
|
||||||
sliderPosition(),
|
sliderPosition(),
|
||||||
Qt::Horizontal == orientation()
|
Qt::Horizontal == orientation()
|
||||||
? rect().width() - 20 : rect().height() - 20,
|
? rect().width() -SLIDER_MARGIN*2
|
||||||
|
: rect().height() - SLIDER_MARGIN*2,
|
||||||
invertedAppearance());
|
invertedAppearance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,9 +11,6 @@
|
||||||
#include "lib/style.h"
|
#include "lib/style.h"
|
||||||
#include "sliderthumb.h"
|
#include "sliderthumb.h"
|
||||||
|
|
||||||
// change
|
|
||||||
#define THUMB_OUTER_SIZE 20
|
|
||||||
|
|
||||||
class SliderPrivate
|
class SliderPrivate
|
||||||
{
|
{
|
||||||
Q_DISABLE_COPY(SliderPrivate)
|
Q_DISABLE_COPY(SliderPrivate)
|
||||||
|
@ -81,15 +78,15 @@ void SliderPrivate::init(Slider *slider)
|
||||||
pulseInState->assignProperty(thumb, "haloSize", 28);
|
pulseInState->assignProperty(thumb, "haloSize", 28);
|
||||||
|
|
||||||
disabledState->assignProperty(thumb, "diameter", 7);
|
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);
|
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);
|
||||||
|
|
||||||
//inactiveState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
inactiveState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
||||||
//focusState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
focusState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
||||||
//slidingState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
slidingState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
||||||
|
|
||||||
machine.addState(topState);
|
machine.addState(topState);
|
||||||
|
|
||||||
|
@ -181,29 +178,25 @@ void SliderPrivate::init(Slider *slider)
|
||||||
QState *minState = new QState(sndState);
|
QState *minState = new QState(sndState);
|
||||||
QState *normalState = new QState(sndState);
|
QState *normalState = new QState(sndState);
|
||||||
|
|
||||||
minState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
minState->assignProperty(thumb, "minFillColor", QColor(255, 255, 255));
|
||||||
minState->assignProperty(thumb, "borderWidth", 0);
|
minState->assignProperty(thumb, "fillColor", QColor(255, 255, 255));
|
||||||
normalState->assignProperty(thumb, "fillColor", QColor(255, 255, 255));
|
minState->assignProperty(thumb, "borderWidth", 2);
|
||||||
normalState->assignProperty(thumb, "borderWidth", 1.5);
|
normalState->assignProperty(thumb, "fillColor", QColor(0, 0, 0));
|
||||||
|
normalState->assignProperty(thumb, "minFillColor", QColor(0, 0, 0));
|
||||||
|
normalState->assignProperty(thumb, "borderWidth", 0);
|
||||||
|
|
||||||
sndState->setInitialState(minState);
|
sndState->setInitialState(minState);
|
||||||
|
|
||||||
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
|
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);
|
||||||
animation = new QPropertyAnimation(thumb, "borderWidth");
|
|
||||||
animation->setDuration(200);
|
|
||||||
transition->addAnimation(animation);
|
|
||||||
minState->addTransition(transition);
|
minState->addTransition(transition);
|
||||||
|
|
||||||
transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum()));
|
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
|
||||||
transition->setTargetState(minState);
|
transition->setTargetState(minState);
|
||||||
animation = new QPropertyAnimation(thumb, "fillColor");
|
animation = new QPropertyAnimation(thumb, "minFillColor");
|
||||||
animation->setDuration(200);
|
|
||||||
transition->addAnimation(animation);
|
|
||||||
animation = new QPropertyAnimation(thumb, "borderWidth");
|
|
||||||
animation->setDuration(200);
|
animation->setDuration(200);
|
||||||
transition->addAnimation(animation);
|
transition->addAnimation(animation);
|
||||||
normalState->addTransition(transition);
|
normalState->addTransition(transition);
|
||||||
|
@ -216,10 +209,10 @@ QRectF SliderPrivate::trackGeometry() const
|
||||||
Q_Q(const Slider);
|
Q_Q(const Slider);
|
||||||
|
|
||||||
return Qt::Horizontal == q->orientation()
|
return Qt::Horizontal == q->orientation()
|
||||||
? QRectF(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1,
|
? QRectF(SLIDER_MARGIN, q->rect().height()/2 - 1,
|
||||||
q->rect().width() - THUMB_OUTER_SIZE, 2)
|
q->rect().width() - SLIDER_MARGIN*2, 2)
|
||||||
: QRectF(q->rect().width()/2 - 1, THUMB_OUTER_SIZE/2, 2,
|
: QRectF(q->rect().width()/2 - 1, SLIDER_MARGIN, 2,
|
||||||
q->rect().height() - THUMB_OUTER_SIZE);
|
q->rect().height() - SLIDER_MARGIN*2);
|
||||||
}
|
}
|
||||||
|
|
||||||
QRectF SliderPrivate::thumbGeometry() const
|
QRectF SliderPrivate::thumbGeometry() const
|
||||||
|
@ -227,10 +220,10 @@ QRectF SliderPrivate::thumbGeometry() const
|
||||||
Q_Q(const Slider);
|
Q_Q(const Slider);
|
||||||
|
|
||||||
return Qt::Horizontal == q->orientation()
|
return Qt::Horizontal == q->orientation()
|
||||||
? QRectF(q->thumbOffset(), q->rect().height()/2 - THUMB_OUTER_SIZE/2,
|
? QRectF(q->thumbOffset(), q->rect().height()/2 - SLIDER_MARGIN,
|
||||||
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE)
|
SLIDER_MARGIN*2, SLIDER_MARGIN*2)
|
||||||
: QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, q->thumbOffset(),
|
: QRectF(q->rect().width()/2 - SLIDER_MARGIN, q->thumbOffset(),
|
||||||
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE);
|
SLIDER_MARGIN*2, SLIDER_MARGIN*2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SliderPrivate::paintTrack(QPainter *painter)
|
void SliderPrivate::paintTrack(QPainter *painter)
|
||||||
|
@ -247,7 +240,7 @@ void SliderPrivate::paintTrack(QPainter *painter)
|
||||||
bg.setStyle(Qt::SolidPattern);
|
bg.setStyle(Qt::SolidPattern);
|
||||||
bg.setColor(trackColor);
|
bg.setColor(trackColor);
|
||||||
|
|
||||||
qreal offset = q->thumbOffset() + THUMB_OUTER_SIZE/2;
|
qreal offset = q->thumbOffset() + SLIDER_MARGIN;
|
||||||
|
|
||||||
QSizeF box(q->isEnabled() ? offset : offset - 7,
|
QSizeF box(q->isEnabled() ? offset : offset - 7,
|
||||||
qMax(q->width(), q->height()));
|
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 position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y();
|
||||||
|
|
||||||
int span = Qt::Horizontal == q->orientation()
|
int span = Qt::Horizontal == q->orientation()
|
||||||
? q->rect().width() - THUMB_OUTER_SIZE
|
? q->rect().width() - SLIDER_MARGIN*2
|
||||||
: q->rect().height() - THUMB_OUTER_SIZE;
|
: q->rect().height() - SLIDER_MARGIN*2;
|
||||||
|
|
||||||
return Style::sliderValueFromPosition(
|
return Style::sliderValueFromPosition(
|
||||||
q->minimum(),
|
q->minimum(),
|
||||||
q->maximum(),
|
q->maximum(),
|
||||||
position - THUMB_OUTER_SIZE/2,
|
position - SLIDER_MARGIN,
|
||||||
span,
|
span,
|
||||||
q->invertedAppearance());
|
q->invertedAppearance());
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,16 +3,11 @@
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include "slider.h"
|
#include "slider.h"
|
||||||
|
|
||||||
// change
|
|
||||||
#define THUMB_OUTER_SIZE 20
|
|
||||||
|
|
||||||
SliderThumb::SliderThumb(Slider *slider)
|
SliderThumb::SliderThumb(Slider *slider)
|
||||||
: QWidget(slider->parentWidget()),
|
: QWidget(slider->parentWidget()),
|
||||||
slider(slider),
|
slider(slider),
|
||||||
_diameter(11),
|
_diameter(11),
|
||||||
_borderWidth(0),
|
_borderWidth(2),
|
||||||
_fillColor(Qt::white),
|
|
||||||
_minFillColor(Qt::white),
|
|
||||||
_haloSize(0)
|
_haloSize(0)
|
||||||
{
|
{
|
||||||
slider->installEventFilter(this);
|
slider->installEventFilter(this);
|
||||||
|
@ -49,10 +44,10 @@ void SliderThumb::paintEvent(QPaintEvent *event)
|
||||||
painter.setRenderHint(QPainter::Antialiasing);
|
painter.setRenderHint(QPainter::Antialiasing);
|
||||||
|
|
||||||
QPointF disp = Qt::Horizontal == slider->orientation()
|
QPointF disp = Qt::Horizontal == slider->orientation()
|
||||||
? QPointF(THUMB_OUTER_SIZE/2 + slider->thumbOffset(),
|
? QPointF(SLIDER_MARGIN + slider->thumbOffset(),
|
||||||
slider->height()/2)
|
slider->height()/2)
|
||||||
: QPointF(slider->width()/2,
|
: QPointF(slider->width()/2,
|
||||||
THUMB_OUTER_SIZE/2 + slider->thumbOffset());
|
SLIDER_MARGIN + slider->thumbOffset());
|
||||||
|
|
||||||
QRectF halo((slider->pos() - QPointF(_haloSize, _haloSize)/2) + disp,
|
QRectF halo((slider->pos() - QPointF(_haloSize, _haloSize)/2) + disp,
|
||||||
QSizeF(_haloSize, _haloSize));
|
QSizeF(_haloSize, _haloSize));
|
||||||
|
@ -62,12 +57,13 @@ void SliderThumb::paintEvent(QPaintEvent *event)
|
||||||
// Knob
|
// Knob
|
||||||
|
|
||||||
brush.setStyle(Qt::SolidPattern);
|
brush.setStyle(Qt::SolidPattern);
|
||||||
brush.setColor(slider->sliderPosition() > slider->minimum()
|
brush.setColor(slider->value() > slider->minimum()
|
||||||
? _fillColor : _minFillColor);
|
? _fillColor : _minFillColor);
|
||||||
painter.setBrush(brush);
|
painter.setBrush(brush);
|
||||||
|
|
||||||
if (_borderWidth > 0) {
|
if (_borderWidth > 0) {
|
||||||
QPen pen;
|
QPen pen;
|
||||||
|
pen.setColor(slider->trackColor());
|
||||||
pen.setWidthF(_borderWidth);
|
pen.setWidthF(_borderWidth);
|
||||||
painter.setPen(pen);
|
painter.setPen(pen);
|
||||||
} else {
|
} else {
|
||||||
|
@ -75,10 +71,10 @@ void SliderThumb::paintEvent(QPaintEvent *event)
|
||||||
}
|
}
|
||||||
|
|
||||||
QRectF geometry = Qt::Horizontal == slider->orientation()
|
QRectF geometry = Qt::Horizontal == slider->orientation()
|
||||||
? QRectF(slider->thumbOffset(), slider->height()/2 - THUMB_OUTER_SIZE/2,
|
? QRectF(slider->thumbOffset(), slider->height()/2 - SLIDER_MARGIN,
|
||||||
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE).translated(slider->pos())
|
SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos())
|
||||||
: QRectF(slider->width()/2 - THUMB_OUTER_SIZE/2, slider->thumbOffset(),
|
: QRectF(slider->width()/2 - SLIDER_MARGIN, slider->thumbOffset(),
|
||||||
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE).translated(slider->pos());
|
SLIDER_MARGIN*2, SLIDER_MARGIN*2).translated(slider->pos());
|
||||||
|
|
||||||
QRectF thumb(0, 0, _diameter, _diameter);
|
QRectF thumb(0, 0, _diameter, _diameter);
|
||||||
|
|
||||||
|
@ -93,6 +89,8 @@ void SliderThumb::paintEvent(QPaintEvent *event)
|
||||||
painter.setPen(pen);
|
painter.setPen(pen);
|
||||||
painter.setBrush(Qt::NoBrush);
|
painter.setBrush(Qt::NoBrush);
|
||||||
|
|
||||||
|
painter.drawRect(geometry);
|
||||||
|
|
||||||
painter.drawRect(rect().adjusted(0, 0, -2, -2));
|
painter.drawRect(rect().adjusted(0, 0, -2, -2));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef SLIDERTHUMB_H
|
#ifndef SLIDERTHUMB_H
|
||||||
#define SLIDERTHUMB_H
|
#define SLIDERTHUMB_H
|
||||||
|
|
||||||
|
#define SLIDER_MARGIN 30
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
class Slider;
|
class Slider;
|
||||||
|
|
|
@ -176,6 +176,7 @@ SliderExamples::SliderExamples(QWidget *parent)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
Slider *slider = new Slider;
|
Slider *slider = new Slider;
|
||||||
|
//slider->setValue(20);
|
||||||
slider->setDisabled(true);
|
slider->setDisabled(true);
|
||||||
slider->setMinimumWidth(250);
|
slider->setMinimumWidth(250);
|
||||||
|
|
||||||
|
@ -191,6 +192,44 @@ SliderExamples::SliderExamples(QWidget *parent)
|
||||||
|
|
||||||
mainLayout->addWidget(frame);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue