173 lines
3.8 KiB
C++
173 lines
3.8 KiB
C++
#include "slider.h"
|
|
#include <QPainter>
|
|
#include <QPropertyAnimation>
|
|
#include <QStringBuilder>
|
|
#include <QMouseEvent>
|
|
#include <QDebug>
|
|
|
|
#include "slider_p.h"
|
|
|
|
Slider::Slider(QWidget *parent)
|
|
: QAbstractSlider(parent),
|
|
d_ptr(new SliderPrivate(this))
|
|
{
|
|
QPropertyAnimation *animation;
|
|
|
|
animation = new QPropertyAnimation;
|
|
|
|
animation->setPropertyName("haloScaleFactor");
|
|
animation->setTargetObject(this);
|
|
animation->setStartValue(0.8);
|
|
animation->setEndValue(1);
|
|
animation->setDuration(900);
|
|
animation->setEasingCurve(QEasingCurve::InOutQuad);
|
|
|
|
d_ptr->haloAnimation->addAnimation(animation);
|
|
|
|
animation = new QPropertyAnimation;
|
|
|
|
animation->setPropertyName("haloScaleFactor");
|
|
animation->setTargetObject(this);
|
|
animation->setStartValue(1);
|
|
animation->setEndValue(0.8);
|
|
animation->setDuration(900);
|
|
animation->setEasingCurve(QEasingCurve::InOutQuad);
|
|
|
|
d_ptr->haloAnimation->addAnimation(animation);
|
|
|
|
d_ptr->haloAnimation->setLoopCount(-1);
|
|
d_ptr->haloAnimation->start();
|
|
|
|
connect(this, SIGNAL(actionTriggered(int)), this, SLOT(handleAction(int)));
|
|
}
|
|
|
|
Slider::~Slider()
|
|
{
|
|
}
|
|
|
|
void Slider::setHaloScaleFactor(qreal factor)
|
|
{
|
|
Q_D(Slider);
|
|
|
|
d->haloScaleFactor = factor;
|
|
update();
|
|
}
|
|
|
|
qreal Slider::haloScaleFactor() const
|
|
{
|
|
Q_D(const Slider);
|
|
|
|
return d->haloScaleFactor;
|
|
}
|
|
|
|
void Slider::handleAction(int action)
|
|
{
|
|
Q_D(Slider);
|
|
|
|
if (!d->step)
|
|
return;
|
|
|
|
if ((SliderPageStepAdd == action && sliderPosition() > d->stepTo) ||
|
|
(SliderPageStepSub == action && sliderPosition() < d->stepTo))
|
|
{
|
|
setSliderPosition(d->stepTo);
|
|
}
|
|
}
|
|
|
|
void Slider::paintEvent(QPaintEvent *event)
|
|
{
|
|
Q_UNUSED(event)
|
|
|
|
Q_D(Slider);
|
|
|
|
QPainter painter(this);
|
|
|
|
d->paintTrack(&painter);
|
|
d->paintHalo(&painter);
|
|
d->paintThumb(&painter);
|
|
|
|
#ifdef DEBUG_LAYOUT
|
|
QPen pen;
|
|
pen.setColor(Qt::red);
|
|
pen.setWidth(1);
|
|
painter.setOpacity(1);
|
|
painter.setPen(pen);
|
|
painter.setBrush(Qt::NoBrush);
|
|
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
|
|
|
painter.setFont(QFont("monospace", 8));
|
|
painter.drawText(8, 18, "Value: " % QString::number(value()));
|
|
painter.drawText(8, 36, "Position: " % QString::number(sliderPosition()));
|
|
#endif
|
|
}
|
|
|
|
void Slider::mouseMoveEvent(QMouseEvent *event)
|
|
{
|
|
Q_D(Slider);
|
|
|
|
if (isSliderDown()) {
|
|
setSliderPosition(d->valueFromPosition(event->pos()));
|
|
} else {
|
|
|
|
QRectF track(d->trackGeometry().adjusted(-2, -2, 2, 2));
|
|
|
|
if (track.contains(event->pos()) != d->hoverTrack) {
|
|
d->hoverTrack = !d->hoverTrack;
|
|
update();
|
|
}
|
|
|
|
QRectF thumb(0, 0, 16, 16);
|
|
thumb.moveCenter(d->thumbGeometry().center());
|
|
|
|
if (thumb.contains(event->pos()) != d->hoverThumb) {
|
|
d->hoverThumb = !d->hoverThumb;
|
|
update();
|
|
}
|
|
}
|
|
|
|
QAbstractSlider::mouseMoveEvent(event);
|
|
}
|
|
|
|
void Slider::mousePressEvent(QMouseEvent *event)
|
|
{
|
|
Q_D(Slider);
|
|
|
|
const QPoint pos = event->pos();
|
|
|
|
QRectF thumb(0, 0, 16, 16);
|
|
thumb.moveCenter(d->thumbGeometry().center());
|
|
|
|
if (thumb.contains(pos)) {
|
|
setSliderDown(true);
|
|
return;
|
|
}
|
|
|
|
QRectF 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);
|
|
}
|
|
}
|
|
|
|
void Slider::mouseReleaseEvent(QMouseEvent *event)
|
|
{
|
|
Q_D(Slider);
|
|
|
|
if (isSliderDown()) {
|
|
setSliderDown(false);
|
|
} else if (d->step) {
|
|
d->step = false;
|
|
setRepeatAction(SliderNoAction, 0);
|
|
}
|
|
|
|
QAbstractSlider::mouseReleaseEvent(event);
|
|
}
|