qt-material-widgets/components/slider_p.h

132 lines
3.1 KiB
C
Raw Normal View History

2016-05-01 19:56:39 +00:00
#ifndef SLIDER_P_H
#define SLIDER_P_H
2016-05-01 21:10:31 +00:00
#include <QPainter>
#include <QApplication>
#include "lib/style.h"
#include "slider.h"
2016-05-12 11:19:59 +00:00
#include "slidertrack.h"
2016-05-10 06:25:44 +00:00
#include "sliderthumb.h"
#include "sliderstatemachine.h"
2016-05-01 19:56:39 +00:00
class SliderPrivate
{
Q_DISABLE_COPY(SliderPrivate)
Q_DECLARE_PUBLIC(Slider)
public:
2016-05-10 14:17:51 +00:00
SliderPrivate(Slider *parent);
2016-05-01 19:56:39 +00:00
QRectF trackBoundingRect() const;
QRectF thumbBoundingRect() const;
2016-05-09 20:32:16 +00:00
2016-05-10 14:17:51 +00:00
int valueFromPosition(const QPoint &pos) const;
2016-05-09 20:32:16 +00:00
void setHovered(bool status);
2016-05-12 06:37:30 +00:00
Slider *const q_ptr;
SliderThumb *const thumb;
2016-05-12 14:34:06 +00:00
SliderTrack *const track;
SliderStateMachine *const machine;
2016-05-10 14:17:51 +00:00
bool hoverTrack;
bool hoverThumb;
2016-05-12 06:37:30 +00:00
bool hover;
2016-05-10 14:17:51 +00:00
bool step;
bool pageStepMode;
int stepTo;
int oldValue;
2016-05-11 17:26:19 +00:00
int trackWidth;
2016-05-10 14:17:51 +00:00
};
2016-05-09 20:32:16 +00:00
#endif // SLIDER_P_H
2016-05-10 14:17:51 +00:00
SliderPrivate::SliderPrivate(Slider *parent)
: q_ptr(parent),
thumb(new SliderThumb(parent)),
2016-05-12 14:34:06 +00:00
track(new SliderTrack(parent)),
machine(new SliderStateMachine(parent, thumb, track)),
2016-05-10 14:17:51 +00:00
hoverTrack(false),
hoverThumb(false),
2016-05-12 06:37:30 +00:00
hover(false),
2016-05-10 14:17:51 +00:00
step(false),
2016-05-12 20:13:45 +00:00
pageStepMode(true),
2016-05-10 14:17:51 +00:00
stepTo(0),
2016-05-11 06:09:19 +00:00
oldValue(parent->value()),
2016-05-11 22:18:09 +00:00
trackWidth(2)
2016-05-10 14:17:51 +00:00
{
Q_Q(Slider);
2016-05-12 06:37:30 +00:00
q->setMouseTracking(true);
q->setFocusPolicy(Qt::StrongFocus);
2016-05-12 20:13:45 +00:00
q->setPageStep(1);
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed);
if (q->orientation() == Qt::Vertical)
sp.transpose();
q->setSizePolicy(sp);
q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
machine->start();
QCoreApplication::processEvents();
2016-05-10 14:17:51 +00:00
}
2016-05-01 21:10:31 +00:00
QRectF SliderPrivate::trackBoundingRect() const
2016-05-10 14:17:51 +00:00
{
Q_Q(const Slider);
2016-05-01 21:10:31 +00:00
2016-05-11 17:26:19 +00:00
qreal hw = static_cast<qreal>(trackWidth)/2;
2016-05-10 14:17:51 +00:00
return Qt::Horizontal == q->orientation()
2016-05-12 11:19:59 +00:00
? QRectF(SLIDER_MARGIN, q->height()/2 - hw,
q->width() - SLIDER_MARGIN*2, hw*2)
: QRectF(q->width()/2 - hw, SLIDER_MARGIN, hw*2,
q->height() - SLIDER_MARGIN*2);
2016-05-10 14:17:51 +00:00
}
2016-05-01 23:22:43 +00:00
QRectF SliderPrivate::thumbBoundingRect() const
2016-05-10 14:17:51 +00:00
{
Q_Q(const Slider);
2016-05-01 21:10:31 +00:00
2016-05-10 14:17:51 +00:00
return Qt::Horizontal == q->orientation()
2016-05-12 11:19:59 +00:00
? QRectF(q->thumbOffset(), q->height()/2 - SLIDER_MARGIN,
2016-05-11 14:23:17 +00:00
SLIDER_MARGIN*2, SLIDER_MARGIN*2)
2016-05-12 11:19:59 +00:00
: QRectF(q->width()/2 - SLIDER_MARGIN, q->thumbOffset(),
2016-05-11 14:23:17 +00:00
SLIDER_MARGIN*2, SLIDER_MARGIN*2);
2016-05-10 14:17:51 +00:00
}
2016-05-01 21:44:38 +00:00
2016-05-10 14:17:51 +00:00
int SliderPrivate::valueFromPosition(const QPoint &pos) const
{
Q_Q(const Slider);
2016-05-03 22:40:34 +00:00
2016-05-10 14:17:51 +00:00
int position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y();
2016-05-02 20:57:57 +00:00
2016-05-10 14:17:51 +00:00
int span = Qt::Horizontal == q->orientation()
2016-05-12 14:34:06 +00:00
? q->width() - SLIDER_MARGIN*2
: q->height() - SLIDER_MARGIN*2;
2016-05-02 20:57:57 +00:00
2016-05-10 14:17:51 +00:00
return Style::sliderValueFromPosition(
q->minimum(),
q->maximum(),
2016-05-11 14:23:17 +00:00
position - SLIDER_MARGIN,
2016-05-10 14:17:51 +00:00
span,
q->invertedAppearance());
}
2016-05-01 19:56:39 +00:00
void SliderPrivate::setHovered(bool status)
2016-05-12 06:37:30 +00:00
{
Q_Q(Slider);
if (hover != status) {
hover = status;
2016-05-12 06:37:30 +00:00
if (!q->hasFocus()) {
if (status) {
emit machine->noFocusMouseEnter();
2016-05-12 06:37:30 +00:00
} else {
emit machine->noFocusMouseLeave();
2016-05-12 06:37:30 +00:00
}
}
q->update();
}
}