simplify slider code

This commit is contained in:
laserpants 2016-03-26 00:27:49 +03:00
parent db1c8ddee4
commit ef57d5b79d
2 changed files with 6 additions and 15 deletions

View File

@ -83,16 +83,13 @@ void Slider::paintEvent(QPaintEvent *event)
void Slider::mousePressEvent(QMouseEvent *event) void Slider::mousePressEvent(QMouseEvent *event)
{ {
const QSize s = _handle->sizeHint();
const QPoint p = event->pos();
if (Qt::Horizontal == _orientation if (Qt::Horizontal == _orientation
? touchesRail(p.y(), height()/2) ? touchesRail(event->y(), height()/2)
: touchesRail(p.x(), width()/2)) : touchesRail(event->x(), width()/2))
{ {
const QPoint newPos = p - QPoint(s.width()/2, s.height()/2); const QSize s = _handle->sizeHint();
_handle->setPosition(newPos); _handle->setOffset((event->pos() - QPoint(s.width()/2, s.height()/2)) - event->globalPos());
_handle->setOffset(newPos - event->globalPos()); _handle->setRelativePosition(event->globalPos());
_drag = true; _drag = true;
} else { } else {
_drag = false; _drag = false;
@ -113,9 +110,3 @@ void Slider::resizeEvent(QResizeEvent *event)
_handle->refreshGeometry(); _handle->refreshGeometry();
QWidget::resizeEvent(event); QWidget::resizeEvent(event);
} }
bool Slider::touchesRail(int p, int x) const
{
return (p >= x-2 && p < x+2);
}

View File

@ -56,7 +56,7 @@ protected:
void resizeEvent(QResizeEvent *event); void resizeEvent(QResizeEvent *event);
private: private:
bool touchesRail(int p, int x) const; inline bool touchesRail(int p, int x) const { return (p >= x-2 && p < x+2); }
bool _drag; bool _drag;
Handle *const _handle; Handle *const _handle;