clean up Toggle implementation
This commit is contained in:
parent
5eac1af474
commit
1d47c85c49
|
@ -32,13 +32,14 @@ void Thumb::setProgress(qreal progress)
|
||||||
{
|
{
|
||||||
if (_progress == progress)
|
if (_progress == progress)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_progress = progress;
|
_progress = progress;
|
||||||
|
|
||||||
const QSize s(Qt::Horizontal == _toggle->orientation()
|
const QSize s(Qt::Horizontal == _toggle->orientation()
|
||||||
? size() : size().transposed());
|
? size() : size().transposed());
|
||||||
setOffset(progress*static_cast<qreal>(s.width()-s.height()));
|
setOffset(progress*static_cast<qreal>(s.width()-s.height()));
|
||||||
|
|
||||||
emit progressChanged(progress);
|
_toggle->updateOverlayGeometry();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +98,16 @@ void Thumb::paintEvent(QPaintEvent *event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Toggle::updateOverlayGeometry()
|
||||||
|
{
|
||||||
|
const int offset = _thumb->offset();
|
||||||
|
if (Qt::Horizontal == _orientation) {
|
||||||
|
_overlay->setGeometry(geometry().adjusted(-10+offset, -20, 10+offset, 20));
|
||||||
|
} else {
|
||||||
|
_overlay->setGeometry(geometry().adjusted(-10, -20+offset, 10, 20+offset));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Toggle::Toggle(QWidget *parent)
|
Toggle::Toggle(QWidget *parent)
|
||||||
: QAbstractButton(parent),
|
: QAbstractButton(parent),
|
||||||
_thumb(new Thumb(this)),
|
_thumb(new Thumb(this)),
|
||||||
|
@ -115,7 +126,6 @@ Toggle::Toggle(QWidget *parent)
|
||||||
_thumb->installEventFilter(this);
|
_thumb->installEventFilter(this);
|
||||||
|
|
||||||
connect(_thumb, SIGNAL(clicked()), this, SLOT(addRipple()));
|
connect(_thumb, SIGNAL(clicked()), this, SLOT(addRipple()));
|
||||||
connect(_thumb, SIGNAL(progressChanged(qreal)), this, SLOT(updateOverlayGeometry()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Toggle::~Toggle()
|
Toggle::~Toggle()
|
||||||
|
@ -149,17 +159,6 @@ void Toggle::addRipple()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Toggle::updateOverlayGeometry()
|
|
||||||
{
|
|
||||||
const int d = _thumb->offset();
|
|
||||||
|
|
||||||
if (Qt::Horizontal == _orientation) {
|
|
||||||
_overlay->setGeometry(geometry().adjusted(-10+d, -20, 10+d, 20));
|
|
||||||
} else {
|
|
||||||
_overlay->setGeometry(geometry().adjusted(-10, -20+d, 10, 20+d));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Toggle::event(QEvent *event)
|
bool Toggle::event(QEvent *event)
|
||||||
{
|
{
|
||||||
const QEvent::Type type = event->type();
|
const QEvent::Type type = event->type();
|
||||||
|
|
|
@ -11,7 +11,7 @@ class Thumb : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(qreal progress WRITE setProgress READ progress NOTIFY progressChanged)
|
Q_PROPERTY(qreal progress WRITE setProgress READ progress)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Thumb(Toggle *parent);
|
explicit Thumb(Toggle *parent);
|
||||||
|
@ -25,7 +25,6 @@ public:
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void clicked();
|
void clicked();
|
||||||
void progressChanged(qreal);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
@ -43,6 +42,10 @@ class Toggle : public QAbstractButton
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
friend class Thumb;
|
||||||
|
|
||||||
|
void updateOverlayGeometry();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Toggle(QWidget *parent = 0);
|
explicit Toggle(QWidget *parent = 0);
|
||||||
~Toggle();
|
~Toggle();
|
||||||
|
@ -54,14 +57,12 @@ public:
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void addRipple();
|
void addRipple();
|
||||||
void updateOverlayGeometry();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
Thumb *const _thumb;
|
Thumb *const _thumb;
|
||||||
RippleOverlay *const _overlay;
|
RippleOverlay *const _overlay;
|
||||||
Qt::Orientation _orientation;
|
Qt::Orientation _orientation;
|
||||||
|
|
Loading…
Reference in New Issue