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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-24 05:52:27 +00:00
|
|
|
void Handle::setPosition(const QPoint &pos)
|
|
|
|
{
|
|
|
|
_position = pos;
|
|
|
|
refreshGeometry();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Handle::event(QEvent *event)
|
|
|
|
{
|
|
|
|
qDebug() << event;
|
|
|
|
|
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
|
|
|
|
2016-03-22 15:05:02 +00:00
|
|
|
void Handle::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
QPen pen;
|
|
|
|
pen.setColor(Qt::black);
|
|
|
|
pen.setWidth(1);
|
|
|
|
painter.setPen(pen);
|
|
|
|
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
|
|
|
|
|
|
|
QWidget::paintEvent(event);
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:35:41 +00:00
|
|
|
void Handle::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
_eventPos = event->globalPos();
|
|
|
|
_offset = pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Handle::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
void Handle::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
2016-03-24 05:52:27 +00:00
|
|
|
setPosition(_offset + event->globalPos() - _eventPos);
|
2016-03-23 05:35:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 15:05:02 +00:00
|
|
|
Slider::Slider(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
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
|
|
|
|
|
|
|
void Slider::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
const QSize s = _handle->sizeHint();
|
2016-03-24 05:52:27 +00:00
|
|
|
_handle->setPosition(event->pos() - QPoint(s.width()/2, s.height()/2));
|
2016-03-23 05:35:41 +00:00
|
|
|
|
|
|
|
QWidget::mousePressEvent(event);
|
|
|
|
}
|
2016-03-24 05:52:27 +00:00
|
|
|
|
|
|
|
void Slider::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
_handle->refreshGeometry();
|
|
|
|
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
}
|