qt-material-widgets/components/slider_p.h

181 lines
4.5 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>
2016-05-09 20:32:16 +00:00
#include <QDebug>
#include "lib/style.h"
#include "slider.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)
2016-05-10 06:25:44 +00:00
friend class SliderThumb;
2016-05-01 19:56:39 +00:00
public:
2016-05-10 14:17:51 +00:00
SliderPrivate(Slider *parent);
2016-05-01 19:56:39 +00:00
2016-05-10 14:17:51 +00:00
QRectF trackGeometry() const;
QRectF thumbGeometry() const;
2016-05-09 20:32:16 +00:00
2016-05-10 14:17:51 +00:00
void paintTrack(QPainter *painter);
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;
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
2016-05-10 14:17:51 +00:00
SliderPrivate::SliderPrivate(Slider *parent)
: q_ptr(parent),
thumb(new SliderThumb(parent)),
machine(new SliderStateMachine(parent, thumb)),
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-10 14:29:50 +00:00
pageStepMode(false),
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);
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
2016-05-10 14:17:51 +00:00
QRectF SliderPrivate::trackGeometry() const
{
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-11 17:26:19 +00:00
? QRectF(SLIDER_MARGIN, q->rect().height()/2 - hw,
q->rect().width() - SLIDER_MARGIN*2, hw*2)
: QRectF(q->rect().width()/2 - hw, SLIDER_MARGIN, hw*2,
2016-05-11 14:23:17 +00:00
q->rect().height() - SLIDER_MARGIN*2);
2016-05-10 14:17:51 +00:00
}
2016-05-01 23:22:43 +00:00
2016-05-10 14:17:51 +00:00
QRectF SliderPrivate::thumbGeometry() const
{
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-11 14:23:17 +00:00
? QRectF(q->thumbOffset(), q->rect().height()/2 - SLIDER_MARGIN,
SLIDER_MARGIN*2, SLIDER_MARGIN*2)
: QRectF(q->rect().width()/2 - SLIDER_MARGIN, q->thumbOffset(),
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
void SliderPrivate::paintTrack(QPainter *painter)
{
Q_Q(const Slider);
2016-05-01 21:10:31 +00:00
Style &style = Style::instance();
2016-05-10 14:17:51 +00:00
painter->save();
2016-05-01 21:10:31 +00:00
2016-05-10 14:17:51 +00:00
QBrush fg;
fg.setStyle(Qt::SolidPattern);
fg.setColor(q->isEnabled() ? style.themeColor("primary1")
: style.themeColor("disabled"));
2016-05-10 14:17:51 +00:00
QBrush bg;
bg.setStyle(Qt::SolidPattern);
bg.setColor(hover ? QColor(0, 0, 0, 150) // @TODO: set theme color
: style.themeColor("accent3"));
2016-05-09 20:32:16 +00:00
2016-05-11 14:23:17 +00:00
qreal offset = q->thumbOffset() + SLIDER_MARGIN;
2016-05-01 23:22:43 +00:00
2016-05-11 06:09:19 +00:00
QSizeF box(q->isEnabled() ? offset : offset - 7,
qMax(q->width(), q->height()));
2016-05-01 21:10:31 +00:00
2016-05-10 14:17:51 +00:00
if (Qt::Vertical == q->orientation())
box.transpose();
2016-05-09 20:32:16 +00:00
2016-05-10 14:17:51 +00:00
QRectF rect = Qt::Vertical == q->orientation()
2016-05-11 06:09:19 +00:00
? QRectF(0, q->isEnabled() ? offset : offset + 7,
box.width(), box.width())
: QRectF(q->isEnabled() ? offset : offset + 7, 0,
box.height(), box.height());
2016-05-01 21:44:38 +00:00
2016-05-10 14:17:51 +00:00
bool inverted = q->invertedAppearance();
2016-05-01 23:22:43 +00:00
2016-05-10 14:17:51 +00:00
painter->fillRect(QRectF(QPointF(0, 0), box).intersected(trackGeometry()),
inverted ? bg : fg);
painter->fillRect(rect.intersected(trackGeometry()), inverted ? fg : bg);
2016-05-01 21:44:38 +00:00
2016-05-10 14:17:51 +00:00
painter->restore();
2016-05-01 23:22:43 +00:00
#ifdef DEBUG_LAYOUT
2016-05-10 14:17:51 +00:00
if (hoverTrack) {
painter->save();
painter->setPen(Qt::red);
painter->drawRect(trackGeometry());
painter->restore();
2016-05-01 21:10:31 +00:00
}
2016-05-10 14:17:51 +00:00
#endif
}
2016-05-01 21:10:31 +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-11 14:23:17 +00:00
? q->rect().width() - SLIDER_MARGIN*2
: q->rect().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();
}
}
2016-05-01 19:56:39 +00:00
#endif // SLIDER_P_H