qt-material-widgets/components/slider.cpp

158 lines
3.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-04-22 20:31:55 +00:00
//_slider->update();
_slider->updateValue();
2016-03-23 05:35:41 +00:00
}
2016-03-22 15:05:02 +00:00
Slider::Slider(QWidget *parent)
2016-04-03 17:08:26 +00:00
: QAbstractSlider(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);
painter.save();
brush.setColor(QColor(255, 0, 0));
const QPoint p = Qt::Vertical == _orientation
? QPoint(width(), _handle->y())
: QPoint(_handle->x(), height());
painter.fillRect(r.intersected(QRect(QPoint(0, 0), p)), brush);
painter.restore();
2016-03-22 15:05:02 +00:00
painter.drawRect(rect().adjusted(0, 0, -1, -1));
2016-04-03 17:08:26 +00:00
QAbstractSlider::paintEvent(event);
2016-03-22 15:05:02 +00:00
}
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;
2016-04-22 20:31:55 +00:00
//update();
updateValue();
2016-03-25 20:30:28 +00:00
} else {
_drag = false;
}
2016-04-03 17:08:26 +00:00
QAbstractSlider::mousePressEvent(event);
2016-03-23 05:35:41 +00:00
}
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-04-22 20:31:55 +00:00
//update();
updateValue();
2016-03-25 20:30:28 +00:00
}
2016-04-03 17:08:26 +00:00
QAbstractSlider::mouseMoveEvent(event);
2016-03-25 20:30:28 +00:00
}
void Slider::resizeEvent(QResizeEvent *event)
{
_handle->refreshGeometry();
2016-04-03 17:08:26 +00:00
QAbstractSlider::resizeEvent(event);
}
2016-04-22 20:31:55 +00:00
void Slider::updateValue()
{
const qreal tot = Qt::Horizontal == _orientation
? geometry().width()-_handle->width()
: geometry().height()-_handle->height();
const qreal r = Qt::Horizontal == _orientation
? _handle->geometry().left() / tot
: _handle->geometry().top() / tot;
// use QStyle::sliderValueFromPosition ??
setValue((1-r)*minimum()+r*maximum());
//setSliderPosition((1-r)*minimum()+r*maximum());
//triggerAction(QAbstractSlider::SliderMove);
update();
}