tweak various slider settings
This commit is contained in:
parent
e19f8b4fa4
commit
d388183f84
|
@ -39,52 +39,21 @@ int Slider::thumbOffset() const
|
|||
sliderPosition(),
|
||||
Qt::Horizontal == orientation()
|
||||
? rect().width() - 20 : rect().height() - 20,
|
||||
false);
|
||||
invertedAppearance());
|
||||
}
|
||||
|
||||
void Slider::setThumbSize(qreal size)
|
||||
void Slider::setPageStepMode(bool pageStep)
|
||||
{
|
||||
Q_D(Slider);
|
||||
|
||||
d->thumbSize = size;
|
||||
update();
|
||||
d->pageStepMode = pageStep;
|
||||
}
|
||||
|
||||
qreal Slider::thumbSize() const
|
||||
bool Slider::pageStepMode() const
|
||||
{
|
||||
Q_D(const Slider);
|
||||
|
||||
return d->thumbSize;
|
||||
}
|
||||
|
||||
void Slider::setThumbPenWidth(qreal width)
|
||||
{
|
||||
Q_D(Slider);
|
||||
|
||||
d->thumbPenWidth = width;
|
||||
update();
|
||||
}
|
||||
|
||||
qreal Slider::thumbPenWidth() const
|
||||
{
|
||||
Q_D(const Slider);
|
||||
|
||||
return d->thumbPenWidth;
|
||||
}
|
||||
|
||||
void Slider::setThumbColor(const QColor &color)
|
||||
{
|
||||
Q_D(Slider);
|
||||
|
||||
d->thumbColor = color;
|
||||
update();
|
||||
}
|
||||
|
||||
QColor Slider::thumbColor() const
|
||||
{
|
||||
Q_D(const Slider);
|
||||
|
||||
return d->thumbColor;
|
||||
return d->pageStepMode;
|
||||
}
|
||||
|
||||
void Slider::sliderChange(SliderChange change)
|
||||
|
@ -119,7 +88,6 @@ void Slider::paintEvent(QPaintEvent *event)
|
|||
QPainter painter(this);
|
||||
|
||||
d->paintTrack(&painter);
|
||||
d->paintThumb(&painter);
|
||||
|
||||
#ifdef DEBUG_LAYOUT
|
||||
if (hasFocus())
|
||||
|
@ -178,6 +146,12 @@ void Slider::mousePressEvent(QMouseEvent *event)
|
|||
return;
|
||||
}
|
||||
|
||||
if (!d->pageStepMode) {
|
||||
setSliderPosition(d->valueFromPosition(event->pos()));
|
||||
//setSliderDown(true);
|
||||
return;
|
||||
}
|
||||
|
||||
d->step = true;
|
||||
d->stepTo = d->valueFromPosition(pos);
|
||||
|
||||
|
|
|
@ -10,26 +10,16 @@ class Slider : public QAbstractSlider
|
|||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(qreal thumbSize WRITE setThumbSize READ thumbSize)
|
||||
Q_PROPERTY(qreal thumbPenWidth WRITE setThumbPenWidth READ thumbPenWidth)
|
||||
Q_PROPERTY(QColor thumbColor WRITE setThumbColor READ thumbColor)
|
||||
|
||||
public:
|
||||
explicit Slider(QWidget *parent = 0);
|
||||
~Slider();
|
||||
|
||||
QSize minimumSizeHint() const;
|
||||
QSize minimumSizeHint() const Q_DECL_OVERRIDE;
|
||||
|
||||
int thumbOffset() const;
|
||||
|
||||
void setThumbSize(qreal size);
|
||||
qreal thumbSize() const;
|
||||
|
||||
void setThumbPenWidth(qreal width);
|
||||
qreal thumbPenWidth() const;
|
||||
|
||||
void setThumbColor(const QColor &color);
|
||||
QColor thumbColor() const;
|
||||
void setPageStepMode(bool pageStep);
|
||||
bool pageStepMode() const;
|
||||
|
||||
signals:
|
||||
void changedToMinimum();
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
// @todo -- separate decl. and impl.
|
||||
// inverse mode
|
||||
// paint track differently left of thumb
|
||||
// direct click mode (mode thumb to click pos)
|
||||
// todo: disabled mode
|
||||
|
||||
#ifndef SLIDER_P_H
|
||||
#define SLIDER_P_H
|
||||
|
||||
|
@ -15,6 +13,7 @@
|
|||
#include "lib/style.h"
|
||||
#include "sliderthumb.h"
|
||||
|
||||
// change
|
||||
#define THUMB_OUTER_SIZE 20
|
||||
|
||||
class SliderPrivate
|
||||
|
@ -25,277 +24,246 @@ class SliderPrivate
|
|||
friend class SliderThumb;
|
||||
|
||||
public:
|
||||
SliderPrivate(Slider *parent)
|
||||
: q_ptr(parent),
|
||||
thumb(new SliderThumb(this)),
|
||||
hoverTrack(false),
|
||||
hoverThumb(false),
|
||||
step(false),
|
||||
stepTo(0),
|
||||
oldValue(parent->value()),
|
||||
thumbSize(11),
|
||||
thumbPenWidth(0),
|
||||
thumbColor(Qt::white)
|
||||
{
|
||||
parent->setMouseTracking(true);
|
||||
}
|
||||
SliderPrivate(Slider *parent);
|
||||
|
||||
void init(Slider *slider)
|
||||
{
|
||||
QState *s1 = new QState(QState::ParallelStates);
|
||||
void init(Slider *slider);
|
||||
|
||||
QState *s11 = new QState(s1);
|
||||
QRectF trackGeometry() const;
|
||||
QRectF thumbGeometry() const;
|
||||
|
||||
QState *inactiveState = new QState(s11);
|
||||
QState *focusState = new QState(s11);
|
||||
QState *pulseState = new QState(focusState);
|
||||
QState *pulse2State = new QState(focusState);
|
||||
QState *downState = new QState(s11);
|
||||
|
||||
focusState->setInitialState(pulseState);
|
||||
|
||||
inactiveState->assignProperty(thumb, "haloSize", 0);
|
||||
pulseState->assignProperty(thumb, "haloSize", 35);
|
||||
pulse2State->assignProperty(thumb, "haloSize", 28);
|
||||
downState->assignProperty(thumb, "haloSize", 0);
|
||||
|
||||
inactiveState->assignProperty(slider, "thumbSize", 11);
|
||||
focusState->assignProperty(slider, "thumbSize", 11);
|
||||
downState->assignProperty(slider, "thumbSize", 17);
|
||||
|
||||
machine.addState(s1);
|
||||
|
||||
s11->setInitialState(inactiveState);
|
||||
|
||||
//machine.addState(inactiveState);
|
||||
//machine.addState(focusState);
|
||||
//machine.addState(downState);
|
||||
|
||||
//machine.setInitialState(inactiveState);
|
||||
|
||||
machine.setInitialState(s1);
|
||||
|
||||
QAbstractTransition *transition;
|
||||
QPropertyAnimation *animation;
|
||||
|
||||
// Show halo on focus in
|
||||
|
||||
transition = new QEventTransition(slider, QEvent::FocusIn);
|
||||
transition->setTargetState(focusState);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
transition->addAnimation(animation);
|
||||
inactiveState->addTransition(transition);
|
||||
|
||||
// Hide halo on focus out
|
||||
|
||||
transition = new QEventTransition(slider, QEvent::FocusOut);
|
||||
transition->setTargetState(inactiveState);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
transition->addAnimation(animation);
|
||||
focusState->addTransition(transition);
|
||||
|
||||
// Pulse in
|
||||
|
||||
transition = new QSignalTransition(pulseState, SIGNAL(propertiesAssigned()));
|
||||
transition->setTargetState(pulse2State);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||
animation->setDuration(1000);
|
||||
transition->addAnimation(animation);
|
||||
pulseState->addTransition(transition);
|
||||
|
||||
// Pulse out
|
||||
|
||||
transition = new QSignalTransition(pulse2State, SIGNAL(propertiesAssigned()));
|
||||
transition->setTargetState(pulseState);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||
animation->setDuration(1000);
|
||||
transition->addAnimation(animation);
|
||||
pulse2State->addTransition(transition);
|
||||
|
||||
// Slider pressed
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(sliderPressed()));
|
||||
transition->setTargetState(downState);
|
||||
transition->addAnimation(new QPropertyAnimation(slider, "thumbSize"));
|
||||
transition->addAnimation(new QPropertyAnimation(thumb, "haloSize"));
|
||||
focusState->addTransition(transition);
|
||||
|
||||
// Slider released
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(sliderReleased()));
|
||||
transition->setTargetState(focusState);
|
||||
transition->addAnimation(new QPropertyAnimation(slider, "thumbSize"));
|
||||
transition->addAnimation(new QPropertyAnimation(thumb, "haloSize"));
|
||||
downState->addTransition(transition);
|
||||
|
||||
//
|
||||
|
||||
QState *s12 = new QState(s1);
|
||||
|
||||
QState *t1 = new QState(s12);
|
||||
QState *t2 = new QState(s12);
|
||||
|
||||
t1->assignProperty(slider, "thumbColor", QColor(0, 0, 0));
|
||||
t1->assignProperty(slider, "thumbPenWidth", 0);
|
||||
t2->assignProperty(slider, "thumbColor", QColor(255, 255, 255));
|
||||
t2->assignProperty(slider, "thumbPenWidth", 1.5);
|
||||
|
||||
//machine.addState(t1);
|
||||
//machine.addState(t2);
|
||||
|
||||
//machine.addState(s12);
|
||||
|
||||
s12->setInitialState(t1);
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
|
||||
transition->setTargetState(t2);
|
||||
animation = new QPropertyAnimation(slider, "thumbColor");
|
||||
animation->setDuration(700);
|
||||
transition->addAnimation(animation);
|
||||
animation = new QPropertyAnimation(slider, "thumbPenWidth");
|
||||
animation->setDuration(700);
|
||||
transition->addAnimation(animation);
|
||||
t1->addTransition(transition);
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum()));
|
||||
transition->setTargetState(t1);
|
||||
animation = new QPropertyAnimation(slider, "thumbColor");
|
||||
animation->setDuration(700);
|
||||
transition->addAnimation(animation);
|
||||
animation = new QPropertyAnimation(slider, "thumbPenWidth");
|
||||
animation->setDuration(700);
|
||||
transition->addAnimation(animation);
|
||||
t2->addTransition(transition);
|
||||
|
||||
machine.start();
|
||||
}
|
||||
|
||||
QRectF 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);
|
||||
}
|
||||
|
||||
void paintTrack(QPainter *painter)
|
||||
{
|
||||
painter->save();
|
||||
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
brush.setColor(QColor(0, 0, 0, 255));
|
||||
|
||||
painter->fillRect(trackGeometry(), brush);
|
||||
|
||||
painter->restore();
|
||||
|
||||
#ifdef DEBUG_LAYOUT
|
||||
if (hoverTrack) {
|
||||
painter->save();
|
||||
painter->setPen(Qt::red);
|
||||
painter->drawRect(trackGeometry());
|
||||
painter->restore();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// remove
|
||||
// remove
|
||||
// remove
|
||||
QRectF 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);
|
||||
}
|
||||
|
||||
// remove
|
||||
// remove
|
||||
void paintThumb(QPainter *painter)
|
||||
{
|
||||
painter->drawRect(thumbGeometry().adjusted(0, 0, -1, -1));
|
||||
|
||||
/*
|
||||
painter->save();
|
||||
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
brush.setColor(thumbColor);
|
||||
painter->setBrush(brush);
|
||||
|
||||
if (thumbPenWidth) {
|
||||
QPen pen;
|
||||
pen.setWidthF(thumbPenWidth);
|
||||
painter->setPen(pen);
|
||||
} else {
|
||||
painter->setPen(Qt::NoPen);
|
||||
}
|
||||
|
||||
painter->setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QRectF thumb(0, 0, thumbSize, thumbSize);
|
||||
thumb.moveCenter(thumbGeometry().center());
|
||||
|
||||
painter->drawEllipse(thumb);
|
||||
|
||||
painter->restore();
|
||||
|
||||
#ifdef DEBUG_LAYOUT
|
||||
painter->drawRect(thumbGeometry());
|
||||
|
||||
if (hoverThumb) {
|
||||
painter->save();
|
||||
painter->setPen(Qt::red);
|
||||
painter->drawRect(thumbGeometry());
|
||||
painter->restore();
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
}
|
||||
|
||||
int valueFromPosition(const QPoint &pos) const
|
||||
{
|
||||
Q_Q(const Slider);
|
||||
|
||||
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;
|
||||
|
||||
return Style::sliderValueFromPosition(
|
||||
q->minimum(),
|
||||
q->maximum(),
|
||||
position - THUMB_OUTER_SIZE/2,
|
||||
span,
|
||||
false);
|
||||
}
|
||||
void paintTrack(QPainter *painter);
|
||||
int valueFromPosition(const QPoint &pos) const;
|
||||
|
||||
Slider *const q_ptr;
|
||||
SliderThumb *const thumb;
|
||||
QStateMachine machine;
|
||||
bool hoverTrack;
|
||||
bool hoverThumb;
|
||||
bool step;
|
||||
int stepTo;
|
||||
int oldValue;
|
||||
qreal thumbSize; // move VVV
|
||||
qreal thumbPenWidth;
|
||||
QColor thumbColor;
|
||||
bool hoverTrack;
|
||||
bool hoverThumb;
|
||||
bool step;
|
||||
bool pageStepMode;
|
||||
int stepTo;
|
||||
int oldValue;
|
||||
};
|
||||
|
||||
SliderPrivate::SliderPrivate(Slider *parent)
|
||||
: q_ptr(parent),
|
||||
thumb(new SliderThumb(parent)),
|
||||
hoverTrack(false),
|
||||
hoverThumb(false),
|
||||
step(false),
|
||||
pageStepMode(true),
|
||||
stepTo(0),
|
||||
oldValue(parent->value())
|
||||
{
|
||||
parent->setMouseTracking(true);
|
||||
}
|
||||
|
||||
void SliderPrivate::init(Slider *slider)
|
||||
{
|
||||
QState *topState = new QState(QState::ParallelStates);
|
||||
|
||||
QState *fstState = new QState(topState);
|
||||
|
||||
QState *inactiveState = new QState(fstState);
|
||||
QState *focusState = new QState(fstState);
|
||||
QState *pulseOutState = new QState(focusState);
|
||||
QState *pulseInState = new QState(focusState);
|
||||
QState *slidingState = new QState(fstState);
|
||||
|
||||
focusState->setInitialState(pulseOutState);
|
||||
|
||||
inactiveState->assignProperty(thumb, "haloSize", 0);
|
||||
pulseOutState->assignProperty(thumb, "haloSize", 35);
|
||||
pulseInState->assignProperty(thumb, "haloSize", 28);
|
||||
slidingState->assignProperty(thumb, "haloSize", 0);
|
||||
|
||||
inactiveState->assignProperty(thumb, "diameter", 11);
|
||||
focusState->assignProperty(thumb, "diameter", 11);
|
||||
slidingState->assignProperty(thumb, "diameter", 17);
|
||||
|
||||
machine.addState(topState);
|
||||
|
||||
fstState->setInitialState(inactiveState);
|
||||
|
||||
machine.setInitialState(topState);
|
||||
|
||||
QAbstractTransition *transition;
|
||||
QPropertyAnimation *animation;
|
||||
|
||||
// Show halo on focus in
|
||||
|
||||
transition = new QEventTransition(slider, QEvent::FocusIn);
|
||||
transition->setTargetState(focusState);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
transition->addAnimation(animation);
|
||||
inactiveState->addTransition(transition);
|
||||
|
||||
// Hide halo on focus out
|
||||
|
||||
transition = new QEventTransition(slider, QEvent::FocusOut);
|
||||
transition->setTargetState(inactiveState);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
transition->addAnimation(animation);
|
||||
focusState->addTransition(transition);
|
||||
|
||||
// Pulse in
|
||||
|
||||
transition = new QSignalTransition(pulseOutState, SIGNAL(propertiesAssigned()));
|
||||
transition->setTargetState(pulseInState);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||
animation->setDuration(1000);
|
||||
transition->addAnimation(animation);
|
||||
pulseOutState->addTransition(transition);
|
||||
|
||||
// Pulse out
|
||||
|
||||
transition = new QSignalTransition(pulseInState, SIGNAL(propertiesAssigned()));
|
||||
transition->setTargetState(pulseOutState);
|
||||
|
||||
animation = new QPropertyAnimation(thumb, "haloSize");
|
||||
animation->setEasingCurve(QEasingCurve::InOutSine);
|
||||
animation->setDuration(1000);
|
||||
transition->addAnimation(animation);
|
||||
pulseInState->addTransition(transition);
|
||||
|
||||
// Slider pressed
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(sliderPressed()));
|
||||
transition->setTargetState(slidingState);
|
||||
transition->addAnimation(new QPropertyAnimation(thumb, "diameter"));
|
||||
transition->addAnimation(new QPropertyAnimation(thumb, "haloSize"));
|
||||
focusState->addTransition(transition);
|
||||
|
||||
// Slider released
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(sliderReleased()));
|
||||
transition->setTargetState(focusState);
|
||||
transition->addAnimation(new QPropertyAnimation(thumb, "diameter"));
|
||||
transition->addAnimation(new QPropertyAnimation(thumb, "haloSize"));
|
||||
slidingState->addTransition(transition);
|
||||
|
||||
// Min. value animation
|
||||
|
||||
QState *sndState = new QState(topState);
|
||||
|
||||
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);
|
||||
|
||||
sndState->setInitialState(minState);
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(changedToMinimum()));
|
||||
transition->setTargetState(normalState);
|
||||
animation = new QPropertyAnimation(thumb, "fillColor");
|
||||
animation->setDuration(400);
|
||||
transition->addAnimation(animation);
|
||||
animation = new QPropertyAnimation(thumb, "borderWidth");
|
||||
animation->setDuration(400);
|
||||
transition->addAnimation(animation);
|
||||
minState->addTransition(transition);
|
||||
|
||||
transition = new QSignalTransition(slider, SIGNAL(changedFromMinimum()));
|
||||
transition->setTargetState(minState);
|
||||
animation = new QPropertyAnimation(thumb, "fillColor");
|
||||
animation->setDuration(400);
|
||||
transition->addAnimation(animation);
|
||||
animation = new QPropertyAnimation(thumb, "borderWidth");
|
||||
animation->setDuration(400);
|
||||
transition->addAnimation(animation);
|
||||
normalState->addTransition(transition);
|
||||
|
||||
machine.start();
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
void SliderPrivate::paintTrack(QPainter *painter)
|
||||
{
|
||||
Q_Q(const Slider);
|
||||
|
||||
painter->save();
|
||||
|
||||
QBrush fg;
|
||||
fg.setStyle(Qt::SolidPattern);
|
||||
fg.setColor(QColor(255, 0, 0));
|
||||
|
||||
QBrush bg;
|
||||
bg.setStyle(Qt::SolidPattern);
|
||||
bg.setColor(QColor(0, 0, 0));
|
||||
|
||||
qreal offset = q->thumbOffset() + THUMB_OUTER_SIZE/2;
|
||||
|
||||
QSizeF box(offset, qMax(q->width(), q->height()));
|
||||
|
||||
if (Qt::Vertical == q->orientation())
|
||||
box.transpose();
|
||||
|
||||
QRectF rect = Qt::Vertical == q->orientation()
|
||||
? QRectF(0, offset, box.width(), box.width())
|
||||
: QRectF(offset, 0, box.height(), box.height());
|
||||
|
||||
bool inverted = q->invertedAppearance();
|
||||
|
||||
painter->fillRect(QRectF(QPointF(0, 0), box).intersected(trackGeometry()),
|
||||
inverted ? bg : fg);
|
||||
painter->fillRect(rect.intersected(trackGeometry()), inverted ? fg : bg);
|
||||
|
||||
painter->restore();
|
||||
|
||||
#ifdef DEBUG_LAYOUT
|
||||
if (hoverTrack) {
|
||||
painter->save();
|
||||
painter->setPen(Qt::red);
|
||||
painter->drawRect(trackGeometry());
|
||||
painter->restore();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int SliderPrivate::valueFromPosition(const QPoint &pos) const
|
||||
{
|
||||
Q_Q(const Slider);
|
||||
|
||||
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;
|
||||
|
||||
return Style::sliderValueFromPosition(
|
||||
q->minimum(),
|
||||
q->maximum(),
|
||||
position - THUMB_OUTER_SIZE/2,
|
||||
span,
|
||||
q->invertedAppearance());
|
||||
}
|
||||
|
||||
#endif // SLIDER_P_H
|
||||
|
|
|
@ -1,18 +1,24 @@
|
|||
#include "sliderthumb.h"
|
||||
#include "slider_p.h"
|
||||
#include <QEvent>
|
||||
#include <QPainter>
|
||||
#include "slider.h"
|
||||
|
||||
SliderThumb::SliderThumb(SliderPrivate *slider)
|
||||
: QWidget(slider->q_func()),
|
||||
// change
|
||||
#define THUMB_OUTER_SIZE 20
|
||||
|
||||
SliderThumb::SliderThumb(Slider *slider)
|
||||
: QWidget(slider->parentWidget()),
|
||||
slider(slider),
|
||||
_diameter(11),
|
||||
_borderWidth(0),
|
||||
_fillColor(Qt::white),
|
||||
_haloSize(0)
|
||||
{
|
||||
Slider *parent = slider->q_func();
|
||||
|
||||
parent->installEventFilter(this);
|
||||
slider->installEventFilter(this);
|
||||
setAttribute(Qt::WA_TransparentForMouseEvents, true);
|
||||
|
||||
connect(parent, SIGNAL(sliderMoved(int)), this, SLOT(update()));
|
||||
connect(parent, SIGNAL(valueChanged(int)), this, SLOT(update()));
|
||||
connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(update()));
|
||||
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(update()));
|
||||
}
|
||||
|
||||
SliderThumb::~SliderThumb()
|
||||
|
@ -22,15 +28,13 @@ SliderThumb::~SliderThumb()
|
|||
bool SliderThumb::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
if (QEvent::ParentChange == event->type()) {
|
||||
setParent(slider->q_func()->parentWidget());
|
||||
setParent(slider->parentWidget());
|
||||
}
|
||||
return QWidget::eventFilter(obj, event);
|
||||
}
|
||||
|
||||
void SliderThumb::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Slider *const q = slider->q_func();
|
||||
|
||||
QPainter painter(this);
|
||||
|
||||
// Halo
|
||||
|
@ -43,56 +47,52 @@ void SliderThumb::paintEvent(QPaintEvent *event)
|
|||
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
QPointF disp = Qt::Horizontal == q->orientation()
|
||||
? QPointF(THUMB_OUTER_SIZE/2 + q->thumbOffset(),
|
||||
q->height()/2)
|
||||
: QPointF(q->width()/2,
|
||||
THUMB_OUTER_SIZE/2 + q->thumbOffset());
|
||||
QPointF disp = Qt::Horizontal == slider->orientation()
|
||||
? QPointF(THUMB_OUTER_SIZE/2 + slider->thumbOffset(),
|
||||
slider->height()/2)
|
||||
: QPointF(slider->width()/2,
|
||||
THUMB_OUTER_SIZE/2 + slider->thumbOffset());
|
||||
|
||||
QRectF halo((q->pos() - QPointF(_haloSize, _haloSize)/2) + disp,
|
||||
QSize(_haloSize, _haloSize));
|
||||
QRectF halo((slider->pos() - QPointF(_haloSize, _haloSize)/2) + disp,
|
||||
QSizeF(_haloSize, _haloSize));
|
||||
|
||||
painter.drawEllipse(halo);
|
||||
|
||||
// Knob
|
||||
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
brush.setColor(slider->thumbColor);
|
||||
brush.setColor(_fillColor);
|
||||
painter.setBrush(brush);
|
||||
|
||||
if (slider->thumbPenWidth > 0) {
|
||||
if (_borderWidth > 0) {
|
||||
QPen pen;
|
||||
pen.setWidthF(slider->thumbPenWidth);
|
||||
pen.setWidthF(_borderWidth);
|
||||
painter.setPen(pen);
|
||||
} else {
|
||||
painter.setPen(Qt::NoPen);
|
||||
}
|
||||
|
||||
QRectF thumb(0, 0, slider->thumbSize, slider->thumbSize);
|
||||
thumb.moveCenter(thumbGeometry().center());
|
||||
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 thumb(0, 0, _diameter, _diameter);
|
||||
|
||||
thumb.moveCenter(geometry.center());
|
||||
|
||||
painter.drawEllipse(thumb);
|
||||
|
||||
#ifdef DEBUG_LAYOUT
|
||||
QPen pen;
|
||||
pen.setColor(Qt::red);
|
||||
pen.setWidth(2);
|
||||
painter.setPen(pen);
|
||||
painter.setBrush(Qt::NoBrush);
|
||||
QPen pen;
|
||||
pen.setColor(Qt::red);
|
||||
pen.setWidth(2);
|
||||
painter.setPen(pen);
|
||||
painter.setBrush(Qt::NoBrush);
|
||||
|
||||
painter.drawRect(rect().adjusted(0, 0, -2, -2));
|
||||
painter.drawRect(rect().adjusted(0, 0, -2, -2));
|
||||
#endif
|
||||
|
||||
QWidget::paintEvent(event);
|
||||
}
|
||||
|
||||
QRectF SliderThumb::thumbGeometry() const
|
||||
{
|
||||
const Slider *const q = slider->q_func();
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -3,20 +3,57 @@
|
|||
|
||||
#include <QWidget>
|
||||
|
||||
class Slider;
|
||||
class SliderPrivate;
|
||||
|
||||
class SliderThumb : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
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(qreal haloSize WRITE setHaloSize READ haloSize)
|
||||
|
||||
friend class SliderPrivate;
|
||||
explicit SliderThumb(SliderPrivate *slider);
|
||||
explicit SliderThumb(Slider *slider);
|
||||
|
||||
public:
|
||||
~SliderThumb();
|
||||
|
||||
inline void setDiameter(qreal diameter)
|
||||
{
|
||||
_diameter = diameter;
|
||||
update();
|
||||
}
|
||||
|
||||
inline qreal diameter() const
|
||||
{
|
||||
return _diameter;
|
||||
}
|
||||
|
||||
inline void setBorderWidth(qreal width)
|
||||
{
|
||||
_borderWidth = width;
|
||||
update();
|
||||
}
|
||||
|
||||
inline qreal borderWidth() const
|
||||
{
|
||||
return _borderWidth;
|
||||
}
|
||||
|
||||
inline void setFillColor(const QColor &color)
|
||||
{
|
||||
_fillColor = color;
|
||||
update();
|
||||
}
|
||||
|
||||
inline QColor fillColor() const
|
||||
{
|
||||
return _fillColor;
|
||||
}
|
||||
|
||||
inline void setHaloSize(qreal size)
|
||||
{
|
||||
_haloSize = size;
|
||||
|
@ -32,10 +69,12 @@ protected:
|
|||
private:
|
||||
Q_DISABLE_COPY(SliderThumb)
|
||||
|
||||
QRectF thumbGeometry() const;
|
||||
const Slider *const slider;
|
||||
qreal _diameter;
|
||||
qreal _borderWidth;
|
||||
QColor _fillColor;
|
||||
qreal _haloSize;
|
||||
|
||||
SliderPrivate *const slider;
|
||||
qreal _haloSize;
|
||||
};
|
||||
|
||||
#endif // SLIDERTHUMB_H
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <QPushButton>
|
||||
#include <QLineEdit>
|
||||
#include <QSlider>
|
||||
#include <QCheckBox>
|
||||
#include "sliderexamples.h"
|
||||
#include "components/slider.h"
|
||||
#include "exampleview.h"
|
||||
|
@ -14,16 +15,28 @@ SliderExamples::SliderExamples(QWidget *parent)
|
|||
_edit2(new QLineEdit),
|
||||
_slider(new Slider),
|
||||
_slider2(new Slider),
|
||||
_slider3(new Slider),
|
||||
__slider(new QSlider)
|
||||
{
|
||||
QLayout *mainLayout = widget()->layout();
|
||||
|
||||
{
|
||||
Slider *slider = new Slider;
|
||||
slider->setMinimumWidth(250);
|
||||
QHBoxLayout *layout = new QHBoxLayout;
|
||||
QWidget *widget = new QWidget;
|
||||
|
||||
widget->setLayout(layout);
|
||||
|
||||
_slider3->setMinimumWidth(250);
|
||||
|
||||
QCheckBox *checkbox = new QCheckBox;
|
||||
QCheckBox *checkbox2 = new QCheckBox;
|
||||
|
||||
layout->addWidget(_slider3);
|
||||
layout->addWidget(checkbox);
|
||||
layout->addWidget(checkbox2);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(slider);
|
||||
view->setWidget(widget);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
|
@ -32,6 +45,9 @@ SliderExamples::SliderExamples(QWidget *parent)
|
|||
frame->setWidget(view);
|
||||
|
||||
mainLayout->addWidget(frame);
|
||||
|
||||
connect(checkbox, SIGNAL(toggled(bool)), this, SLOT(inv()));
|
||||
connect(checkbox2, SIGNAL(toggled(bool)), this, SLOT(togglePageStepMode()));
|
||||
}
|
||||
{
|
||||
Slider *slider = new Slider;
|
||||
|
@ -200,3 +216,13 @@ void SliderExamples::updateSliderValue()
|
|||
{
|
||||
_slider2->setValue(_edit2->text().toInt());
|
||||
}
|
||||
|
||||
void SliderExamples::inv()
|
||||
{
|
||||
_slider3->setInvertedAppearance(!_slider3->invertedAppearance());
|
||||
}
|
||||
|
||||
void SliderExamples::togglePageStepMode()
|
||||
{
|
||||
_slider3->setPageStepMode(!_slider3->pageStepMode());
|
||||
}
|
||||
|
|
|
@ -20,12 +20,15 @@ protected slots:
|
|||
void flip();
|
||||
void flip2();
|
||||
void updateSliderValue();
|
||||
void inv();
|
||||
void togglePageStepMode();
|
||||
|
||||
private:
|
||||
QLineEdit *const _edit;
|
||||
QLineEdit *const _edit2;
|
||||
Slider *const _slider;
|
||||
Slider *const _slider2;
|
||||
Slider *const _slider3;
|
||||
QSlider *const __slider;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue