fix handle positioning
This commit is contained in:
parent
b07e24edcc
commit
9440519bc8
|
@ -13,12 +13,6 @@ Handle::~Handle()
|
|||
{
|
||||
}
|
||||
|
||||
void Handle::setPosition(const QPoint &pos)
|
||||
{
|
||||
_position = pos;
|
||||
refreshGeometry();
|
||||
}
|
||||
|
||||
void Handle::refreshGeometry()
|
||||
{
|
||||
QWidget *container = parentWidget();
|
||||
|
@ -31,13 +25,6 @@ void Handle::refreshGeometry()
|
|||
update();
|
||||
}
|
||||
|
||||
bool Handle::event(QEvent *event)
|
||||
{
|
||||
qDebug() << event;
|
||||
|
||||
return QWidget::event(event);
|
||||
}
|
||||
|
||||
void Handle::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(this);
|
||||
|
@ -52,22 +39,17 @@ void Handle::paintEvent(QPaintEvent *event)
|
|||
|
||||
void Handle::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
_eventPos = event->globalPos();
|
||||
_offset = pos();
|
||||
}
|
||||
|
||||
void Handle::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
_offset = pos() - event->globalPos();
|
||||
}
|
||||
|
||||
void Handle::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
setPosition(_offset + event->globalPos() - _eventPos);
|
||||
setPosition(_offset + event->globalPos());
|
||||
}
|
||||
|
||||
Slider::Slider(QWidget *parent)
|
||||
: QWidget(parent),
|
||||
_drag(false),
|
||||
_handle(new Handle(this)),
|
||||
_orientation(Qt::Horizontal)
|
||||
{
|
||||
|
@ -98,17 +80,40 @@ void Slider::paintEvent(QPaintEvent *event)
|
|||
QWidget::paintEvent(event);
|
||||
}
|
||||
|
||||
|
||||
void Slider::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
const QSize s = _handle->sizeHint();
|
||||
_handle->setPosition(event->pos() - QPoint(s.width()/2, s.height()/2));
|
||||
const QPoint p = event->pos();
|
||||
const QPoint newPos = p - QPoint(s.width()/2, s.height()/2);
|
||||
|
||||
if (Qt::Horizontal == _orientation ? touchesRail(p.y(), height()/2) : touchesRail(p.x(), width()/2)) {
|
||||
_handle->setPosition(newPos);
|
||||
_handle->setOffset(newPos - event->globalPos());
|
||||
_drag = true;
|
||||
} else {
|
||||
_drag = false;
|
||||
}
|
||||
QWidget::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void Slider::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
if (_drag) {
|
||||
_handle->setPosition(_handle->offset() + event->globalPos());
|
||||
}
|
||||
QWidget::mouseMoveEvent(event);
|
||||
}
|
||||
|
||||
void Slider::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
_handle->refreshGeometry();
|
||||
|
||||
QWidget::resizeEvent(event);
|
||||
}
|
||||
|
||||
|
||||
bool Slider::touchesRail(int p, int x) const
|
||||
{
|
||||
return (p >= x-2 && p < x+2);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,15 +15,15 @@ public:
|
|||
~Handle();
|
||||
|
||||
inline QSize sizeHint() const { return QSize(16, 16); }
|
||||
inline void setPosition(const QPoint &pos) { _position = pos; refreshGeometry(); }
|
||||
inline const QPoint &position() const { return _position; }
|
||||
inline void setPosition(const QPoint &pos);
|
||||
inline void setOffset(const QPoint &offset) { _offset = offset; update(); }
|
||||
inline const QPoint &offset() const { return _offset; }
|
||||
void refreshGeometry();
|
||||
|
||||
protected:
|
||||
bool event(QEvent *event);
|
||||
void paintEvent(QPaintEvent *event);
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
void mouseReleaseEvent(QMouseEvent *event);
|
||||
void mouseMoveEvent(QMouseEvent *event);
|
||||
|
||||
private:
|
||||
|
@ -47,9 +47,13 @@ public:
|
|||
protected:
|
||||
void paintEvent(QPaintEvent *event);
|
||||
void mousePressEvent(QMouseEvent *event);
|
||||
void mouseMoveEvent(QMouseEvent *event);
|
||||
void resizeEvent(QResizeEvent *event);
|
||||
|
||||
private:
|
||||
bool touchesRail(int p, int x) const;
|
||||
|
||||
bool _drag;
|
||||
Handle *const _handle;
|
||||
Qt::Orientation _orientation;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue