implement most of slider functionality
This commit is contained in:
parent
0f1ad9ad85
commit
6b354ecd04
|
@ -10,23 +10,27 @@ Slider::Slider(QWidget *parent)
|
||||||
: QAbstractSlider(parent),
|
: QAbstractSlider(parent),
|
||||||
d_ptr(new SliderPrivate(this))
|
d_ptr(new SliderPrivate(this))
|
||||||
{
|
{
|
||||||
|
connect(this, SIGNAL(actionTriggered(int)), this, SLOT(handleAction(int)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Slider::~Slider()
|
Slider::~Slider()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Slider::setOrientation(Qt::Orientation orientation)
|
void Slider::handleAction(int action)
|
||||||
{
|
{
|
||||||
Q_D(Slider);
|
Q_D(Slider);
|
||||||
d->orientation = orientation;
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
Qt::Orientation Slider::orientation() const
|
if (!d->step)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ((SliderPageStepAdd == action && sliderPosition() > d->stepTo) ||
|
||||||
|
(SliderPageStepSub == action && sliderPosition() < d->stepTo))
|
||||||
{
|
{
|
||||||
Q_D(const Slider);
|
d->step = false;
|
||||||
return d->orientation;
|
setRepeatAction(SliderNoAction);
|
||||||
|
setSliderPosition(d->stepTo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Slider::paintEvent(QPaintEvent *event)
|
void Slider::paintEvent(QPaintEvent *event)
|
||||||
|
@ -59,9 +63,11 @@ void Slider::mouseMoveEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
Q_D(Slider);
|
Q_D(Slider);
|
||||||
|
|
||||||
if (d->slide) {
|
if (d->slide)
|
||||||
|
{
|
||||||
setSliderPosition(d->valueFromPosition(event->pos()));
|
setSliderPosition(d->valueFromPosition(event->pos()));
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2));
|
QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2));
|
||||||
|
|
||||||
|
@ -86,11 +92,28 @@ void Slider::mousePressEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
Q_D(Slider);
|
Q_D(Slider);
|
||||||
|
|
||||||
|
const QPoint pos = event->pos();
|
||||||
|
|
||||||
QRectF thumb(0, 0, 16, 16);
|
QRectF thumb(0, 0, 16, 16);
|
||||||
thumb.moveCenter(d->thumbGeometry().center());
|
thumb.moveCenter(d->thumbGeometry().center());
|
||||||
|
|
||||||
if (thumb.contains(event->pos())) {
|
if (thumb.contains(pos)) {
|
||||||
d->slide = true;
|
d->slide = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2));
|
||||||
|
|
||||||
|
if (track.contains(pos)) {
|
||||||
|
d->step = true;
|
||||||
|
d->stepTo = d->valueFromPosition(pos);
|
||||||
|
|
||||||
|
SliderAction action = d->stepTo > sliderPosition()
|
||||||
|
? SliderPageStepAdd
|
||||||
|
: SliderPageStepSub;
|
||||||
|
|
||||||
|
triggerAction(action);
|
||||||
|
setRepeatAction(action, 200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,12 +125,9 @@ void Slider::mouseReleaseEvent(QMouseEvent *event)
|
||||||
d->slide = false;
|
d->slide = false;
|
||||||
setValue(sliderPosition());
|
setValue(sliderPosition());
|
||||||
return QAbstractSlider::mouseReleaseEvent(event);
|
return QAbstractSlider::mouseReleaseEvent(event);
|
||||||
}
|
} else if (d->step) {
|
||||||
|
d->step = false;
|
||||||
QRect track(d->trackGeometry().adjusted(-2, -2, 2, 2));
|
setRepeatAction(SliderNoAction);
|
||||||
|
|
||||||
if (track.contains(event->pos())) {
|
|
||||||
setValue(d->valueFromPosition(event->pos()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QAbstractSlider::mouseReleaseEvent(event);
|
QAbstractSlider::mouseReleaseEvent(event);
|
||||||
|
|
|
@ -14,8 +14,8 @@ public:
|
||||||
explicit Slider(QWidget *parent = 0);
|
explicit Slider(QWidget *parent = 0);
|
||||||
~Slider();
|
~Slider();
|
||||||
|
|
||||||
void setOrientation(Qt::Orientation orientation);
|
protected slots:
|
||||||
Qt::Orientation orientation() const;
|
void handleAction(int action);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
|
@ -15,10 +15,11 @@ class SliderPrivate
|
||||||
public:
|
public:
|
||||||
SliderPrivate(Slider *parent)
|
SliderPrivate(Slider *parent)
|
||||||
: q_ptr(parent),
|
: q_ptr(parent),
|
||||||
orientation(Qt::Horizontal),
|
|
||||||
hoverTrack(false),
|
hoverTrack(false),
|
||||||
hoverThumb(false),
|
hoverThumb(false),
|
||||||
slide(false)
|
slide(false),
|
||||||
|
step(false),
|
||||||
|
stepTo(0)
|
||||||
{
|
{
|
||||||
parent->setMouseTracking(true);
|
parent->setMouseTracking(true);
|
||||||
}
|
}
|
||||||
|
@ -27,7 +28,7 @@ public:
|
||||||
{
|
{
|
||||||
Q_Q(const Slider);
|
Q_Q(const Slider);
|
||||||
|
|
||||||
return Qt::Horizontal == orientation
|
return Qt::Horizontal == q->orientation()
|
||||||
? QRect(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1,
|
? QRect(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1,
|
||||||
q->rect().width() - THUMB_OUTER_SIZE, 2)
|
q->rect().width() - THUMB_OUTER_SIZE, 2)
|
||||||
: QRect(q->rect().width()/2 - 1, THUMB_OUTER_SIZE/2, 2,
|
: QRect(q->rect().width()/2 - 1, THUMB_OUTER_SIZE/2, 2,
|
||||||
|
@ -62,7 +63,7 @@ public:
|
||||||
{
|
{
|
||||||
Q_Q(const Slider);
|
Q_Q(const Slider);
|
||||||
|
|
||||||
const int span = Qt::Horizontal == orientation
|
const int span = Qt::Horizontal == q->orientation()
|
||||||
? q->rect().width() - THUMB_OUTER_SIZE
|
? q->rect().width() - THUMB_OUTER_SIZE
|
||||||
: q->rect().height() - THUMB_OUTER_SIZE;
|
: q->rect().height() - THUMB_OUTER_SIZE;
|
||||||
|
|
||||||
|
@ -73,7 +74,7 @@ public:
|
||||||
span,
|
span,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
return Qt::Horizontal == orientation
|
return Qt::Horizontal == q->orientation()
|
||||||
? QRectF(pos, q->rect().height()/2 - THUMB_OUTER_SIZE/2,
|
? QRectF(pos, q->rect().height()/2 - THUMB_OUTER_SIZE/2,
|
||||||
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE)
|
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE)
|
||||||
: QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, pos,
|
: QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, pos,
|
||||||
|
@ -116,12 +117,12 @@ public:
|
||||||
{
|
{
|
||||||
Q_Q(const Slider);
|
Q_Q(const Slider);
|
||||||
|
|
||||||
const int span = Qt::Horizontal == orientation
|
int position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y();
|
||||||
|
|
||||||
|
int span = Qt::Horizontal == q->orientation()
|
||||||
? q->rect().width() - THUMB_OUTER_SIZE
|
? q->rect().width() - THUMB_OUTER_SIZE
|
||||||
: q->rect().height() - THUMB_OUTER_SIZE;
|
: q->rect().height() - THUMB_OUTER_SIZE;
|
||||||
|
|
||||||
const int position = Qt::Horizontal == orientation ? pos.x() : pos.y();
|
|
||||||
|
|
||||||
return Style::sliderValueFromPosition(
|
return Style::sliderValueFromPosition(
|
||||||
q->minimum(),
|
q->minimum(),
|
||||||
q->maximum(),
|
q->maximum(),
|
||||||
|
@ -132,10 +133,11 @@ public:
|
||||||
|
|
||||||
Slider *const q_ptr;
|
Slider *const q_ptr;
|
||||||
|
|
||||||
Qt::Orientation orientation;
|
|
||||||
bool hoverTrack;
|
bool hoverTrack;
|
||||||
bool hoverThumb;
|
bool hoverThumb;
|
||||||
bool slide;
|
bool slide;
|
||||||
|
bool step;
|
||||||
|
int stepTo;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SLIDER_P_H
|
#endif // SLIDER_P_H
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
|
#include <QSlider>
|
||||||
#include "sliderexamples.h"
|
#include "sliderexamples.h"
|
||||||
#include "components/slider.h"
|
#include "components/slider.h"
|
||||||
#include "exampleview.h"
|
#include "exampleview.h"
|
||||||
|
@ -113,6 +114,8 @@ SliderExamples::SliderExamples(QWidget *parent)
|
||||||
layout->addWidget(_slider2);
|
layout->addWidget(_slider2);
|
||||||
layout->addLayout(hLayout);
|
layout->addLayout(hLayout);
|
||||||
|
|
||||||
|
layout->addWidget(new QSlider(Qt::Horizontal));
|
||||||
|
|
||||||
ExampleView *view = new ExampleView;
|
ExampleView *view = new ExampleView;
|
||||||
view->setWidget(widget);
|
view->setWidget(widget);
|
||||||
|
|
||||||
|
@ -128,6 +131,7 @@ SliderExamples::SliderExamples(QWidget *parent)
|
||||||
|
|
||||||
_slider2->setTracking(false);
|
_slider2->setTracking(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SliderExamples::~SliderExamples()
|
SliderExamples::~SliderExamples()
|
||||||
|
|
Loading…
Reference in New Issue