qt-material-widgets/components/slider.cpp

124 lines
2.7 KiB
C++
Raw Normal View History

2016-03-22 15:05:02 +00:00
#include <QPainter>
2016-03-23 05:35:41 +00:00
#include <QDebug>
#include <QMouseEvent>
2016-03-19 08:32:49 +00:00
#include "slider.h"
2016-03-22 14:16:49 +00:00
2016-03-23 05:35:41 +00:00
Handle::Handle(Slider *slider)
: QWidget(slider),
_slider(slider)
2016-03-22 14:16:49 +00:00
{
}
2016-03-22 15:05:02 +00:00
Handle::~Handle()
{
}
void Handle::refreshGeometry()
{
QWidget *container = parentWidget();
const QSize s = sizeHint();
setGeometry(QRect(_slider->orientation() == Qt::Horizontal
? QPoint(qBound(0, _position.x(), container->width()-s.width()), container->height()/2-s.height()/2)
: QPoint(container->width()/2-s.width()/2, qBound(0, _position.y(), container->height()-s.height())), s));
update();
}
2016-03-22 15:05:02 +00:00
void Handle::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
2016-03-27 14:12:35 +00:00
painter.setRenderHint(QPainter::Antialiasing);
// QPen pen;
// pen.setColor(Qt::black);
// pen.setWidth(1);
// painter.setPen(pen);
// painter.drawRect(rect().adjusted(0, 0, -1, -1));
QBrush brush;
brush.setColor(QColor(200, 200, 200));
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.drawEllipse(0, 0, width(), height());
2016-03-22 15:05:02 +00:00
QWidget::paintEvent(event);
}
2016-03-23 05:35:41 +00:00
void Handle::mousePressEvent(QMouseEvent *event)
{
2016-03-25 20:30:28 +00:00
_offset = pos() - event->globalPos();
2016-03-23 05:35:41 +00:00
}
void Handle::mouseMoveEvent(QMouseEvent *event)
{
2016-03-25 20:37:59 +00:00
setRelativePosition(event->globalPos());
2016-03-23 05:35:41 +00:00
}
2016-03-22 15:05:02 +00:00
Slider::Slider(QWidget *parent)
: QWidget(parent),
2016-03-25 20:30:28 +00:00
_drag(false),
2016-03-23 05:35:41 +00:00
_handle(new Handle(this)),
_orientation(Qt::Horizontal)
2016-03-22 15:05:02 +00:00
{
}
2016-03-22 14:16:49 +00:00
Slider::~Slider()
{
}
2016-03-22 15:05:02 +00:00
void Slider::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
2016-03-23 05:35:41 +00:00
const int x = width()/2;
const int y = height()/2;
QRect r = Qt::Vertical == _orientation
? QRect(x-2, 0, 4, height())
: QRect(0, y-2, width(), 4);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(QColor(0, 0, 0));
painter.fillRect(r, brush);
2016-03-22 15:05:02 +00:00
painter.drawRect(rect().adjusted(0, 0, -1, -1));
QWidget::paintEvent(event);
}
2016-03-23 05:35:41 +00:00
2016-03-25 20:30:28 +00:00
2016-03-23 05:35:41 +00:00
void Slider::mousePressEvent(QMouseEvent *event)
{
2016-03-25 20:37:59 +00:00
if (Qt::Horizontal == _orientation
2016-03-26 12:38:24 +00:00
? isOnTrack(event->y(), height()/2)
: isOnTrack(event->x(), width()/2))
2016-03-25 20:37:59 +00:00
{
2016-03-25 21:27:49 +00:00
const QSize s = _handle->sizeHint();
_handle->setOffset((event->pos() - QPoint(s.width()/2, s.height()/2)) - event->globalPos());
_handle->setRelativePosition(event->globalPos());
2016-03-25 20:30:28 +00:00
_drag = true;
} else {
_drag = false;
}
2016-03-23 05:35:41 +00:00
QWidget::mousePressEvent(event);
}
2016-03-25 20:30:28 +00:00
void Slider::mouseMoveEvent(QMouseEvent *event)
{
if (_drag) {
2016-03-25 20:37:59 +00:00
_handle->setRelativePosition(event->globalPos());
2016-03-25 20:30:28 +00:00
}
QWidget::mouseMoveEvent(event);
}
void Slider::resizeEvent(QResizeEvent *event)
{
_handle->refreshGeometry();
QWidget::resizeEvent(event);
}