qt-material-widgets/components/slider_p.h

144 lines
3.4 KiB
C
Raw Normal View History

2016-05-01 19:56:39 +00:00
#ifndef SLIDER_P_H
#define SLIDER_P_H
#include "slider.h"
2016-05-01 21:10:31 +00:00
#include <QPainter>
#include "lib/style.h"
2016-05-01 19:56:39 +00:00
2016-05-01 23:22:43 +00:00
#define THUMB_OUTER_SIZE 30
2016-05-01 19:56:39 +00:00
class SliderPrivate
{
Q_DISABLE_COPY(SliderPrivate)
Q_DECLARE_PUBLIC(Slider)
public:
SliderPrivate(Slider *parent)
2016-05-01 21:10:31 +00:00
: q_ptr(parent),
2016-05-01 23:22:43 +00:00
hoverTrack(false),
2016-05-03 05:48:43 +00:00
hoverThumb(false),
2016-05-03 22:40:34 +00:00
slide(false),
step(false),
stepTo(0)
2016-05-01 19:56:39 +00:00
{
2016-05-01 23:22:43 +00:00
parent->setMouseTracking(true);
2016-05-01 19:56:39 +00:00
}
2016-05-01 21:10:31 +00:00
QRect trackGeometry() const
{
Q_Q(const Slider);
2016-05-03 22:40:34 +00:00
return Qt::Horizontal == q->orientation()
2016-05-01 23:22:43 +00:00
? QRect(THUMB_OUTER_SIZE/2, q->rect().height()/2 - 1,
q->rect().width() - THUMB_OUTER_SIZE, 2)
: QRect(q->rect().width()/2 - 1, THUMB_OUTER_SIZE/2, 2,
q->rect().height() - THUMB_OUTER_SIZE);
2016-05-01 21:10:31 +00:00
}
void paintTrack(QPainter *painter)
{
Q_Q(Slider);
2016-05-01 23:22:43 +00:00
painter->save();
2016-05-01 21:10:31 +00:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(0, 0, 0, 255));
painter->fillRect(trackGeometry(), brush);
2016-05-01 21:44:38 +00:00
2016-05-01 21:10:31 +00:00
painter->restore();
#ifdef DEBUG_LAYOUT
2016-05-01 23:22:43 +00:00
if (hoverTrack) {
painter->save();
painter->setPen(Qt::red);
painter->drawRect(trackGeometry());
painter->restore();
}
2016-05-01 21:10:31 +00:00
#endif
}
2016-05-01 21:44:38 +00:00
QRectF thumbGeometry() const
{
Q_Q(const Slider);
2016-05-03 22:40:34 +00:00
const int span = Qt::Horizontal == q->orientation()
? q->rect().width() - THUMB_OUTER_SIZE
: q->rect().height() - THUMB_OUTER_SIZE;
const int pos = Style::sliderPositionFromValue(
q->minimum(),
q->maximum(),
2016-05-03 05:53:21 +00:00
q->sliderPosition(),
span,
false);
2016-05-03 22:40:34 +00:00
return Qt::Horizontal == q->orientation()
? QRectF(pos, q->rect().height()/2 - THUMB_OUTER_SIZE/2,
2016-05-01 23:22:43 +00:00
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE)
: QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, pos,
2016-05-01 23:22:43 +00:00
THUMB_OUTER_SIZE, THUMB_OUTER_SIZE);
2016-05-01 21:44:38 +00:00
}
2016-05-01 21:10:31 +00:00
void paintThumb(QPainter *painter)
{
2016-05-01 21:44:38 +00:00
Q_Q(Slider);
2016-05-01 23:22:43 +00:00
painter->save();
2016-05-01 21:44:38 +00:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(0, 0, 0, 255));
2016-05-01 23:22:43 +00:00
painter->setBrush(brush);
2016-05-01 21:10:31 +00:00
2016-05-01 21:44:38 +00:00
painter->setRenderHint(QPainter::Antialiasing);
2016-05-01 23:22:43 +00:00
QRectF thumb(0, 0, 12, 12);
thumb.moveCenter(thumbGeometry().center());
painter->drawEllipse(thumb);
2016-05-01 21:44:38 +00:00
2016-05-01 21:10:31 +00:00
painter->restore();
2016-05-01 23:22:43 +00:00
#ifdef DEBUG_LAYOUT
painter->drawRect(thumbGeometry());
if (hoverThumb) {
painter->save();
painter->setPen(Qt::red);
painter->drawRect(thumbGeometry());
painter->restore();
}
#endif
2016-05-01 21:10:31 +00:00
}
2016-05-02 20:57:57 +00:00
int valueFromPosition(const QPoint &pos) const
{
Q_Q(const Slider);
2016-05-03 22:40:34 +00:00
int position = Qt::Horizontal == q->orientation() ? pos.x() : pos.y();
int span = Qt::Horizontal == q->orientation()
2016-05-02 20:57:57 +00:00
? 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);
}
2016-05-01 19:56:39 +00:00
Slider *const q_ptr;
2016-05-01 21:10:31 +00:00
2016-05-03 22:40:34 +00:00
bool hoverTrack;
bool hoverThumb;
bool slide;
bool step;
int stepTo;
2016-05-01 19:56:39 +00:00
};
#endif // SLIDER_P_H