diff options
author | Stanislav Ionascu <stanislav.ionascu@nokia.com> | 2010-11-17 15:38:01 +0200 |
---|---|---|
committer | Armin Berres <armin.berres@basyskom.de> | 2010-11-18 14:09:29 +0100 |
commit | 4ea2ddbe6542c7cff998888f5c9b8ecfbb918911 (patch) | |
tree | 1cda6786a7e29986e642990774c89b5770a3c4bc | |
parent | 4fdba0d6e44fd43b4c91cf9a54c578f6d1074e2f (diff) |
Changes: Delay toggle and click events of the switch button until the animation is finished.
RevBy: Peter Penz, Armin Berres
Details: Such as when a heavy task is executing then animation would be finished already and
provide the user some sort of feedback regarding if the state has been enabled or not.
-rw-r--r-- | demos/widgetsgallery/switchpage.cpp | 16 | ||||
-rw-r--r-- | demos/widgetsgallery/switchpage.h | 5 | ||||
-rw-r--r-- | src/views/mbuttonswitchview.cpp | 63 | ||||
-rw-r--r-- | src/views/mbuttonswitchview.h | 13 | ||||
-rw-r--r-- | src/views/mbuttonswitchview_p.h | 8 |
5 files changed, 64 insertions, 41 deletions
diff --git a/demos/widgetsgallery/switchpage.cpp b/demos/widgetsgallery/switchpage.cpp index 188a2b53..a735da58 100644 --- a/demos/widgetsgallery/switchpage.cpp +++ b/demos/widgetsgallery/switchpage.cpp @@ -25,9 +25,7 @@ #include <MGridLayoutPolicy> #include <MLinearLayoutPolicy> #include <MButton> -#include <MButtonGroup> -#include <MDebug> -#include <MApplication> +#include <MMessageBox> #include <QGraphicsLinearLayout> SwitchPage::SwitchPage() : @@ -68,6 +66,7 @@ void SwitchPage::createContent() l->setAlignment(switch1, Qt::AlignCenter); //l->setAlignment(label1, Qt::AlignRight); containerPolicy->addItem(l); + connect(switch1, SIGNAL(toggled(bool)), this, SLOT(switchToggled(bool))); switch2 = new MButton(); switch2->setViewType(MButton::switchType); @@ -148,4 +147,15 @@ void SwitchPage::retranslateUi() label5->setText(qtTrId("xx_switch_page_switch5")); } +void SwitchPage::switchToggled(bool toggle) +{ + MMessageBox *msgBox = new MMessageBox(); + + if (toggle) + msgBox->setText("The feature was enabled"); + else + msgBox->setText("The feature was disabled"); + + msgBox->appear(MDialog::DestroyWhenDone); +} diff --git a/demos/widgetsgallery/switchpage.h b/demos/widgetsgallery/switchpage.h index 1d3f54e1..e39a2c6a 100644 --- a/demos/widgetsgallery/switchpage.h +++ b/demos/widgetsgallery/switchpage.h @@ -39,8 +39,13 @@ public: virtual ~SwitchPage(); virtual QString timedemoTitle(); virtual void createContent(); + protected: virtual void retranslateUi(); + +protected Q_SLOTS: + void switchToggled(bool toggle); + private: MButton *switch1; MButton *switch2; diff --git a/src/views/mbuttonswitchview.cpp b/src/views/mbuttonswitchview.cpp index a93a2818..bcec7804 100644 --- a/src/views/mbuttonswitchview.cpp +++ b/src/views/mbuttonswitchview.cpp @@ -30,20 +30,19 @@ #include <QVariantAnimation> //! \internal -class HandleAnimation : public QVariantAnimation +class ThumbAnimation : public QVariantAnimation { public: - HandleAnimation(MButtonSwitchViewPrivate* viewPrivate, QObject * parent = 0) + ThumbAnimation(MButtonSwitchViewPrivate* viewPrivate, QObject * parent = 0) : QVariantAnimation(parent), m_viewPrivate(viewPrivate) - { - } + { + } protected: - virtual void updateCurrentValue(const QVariant& value) { m_viewPrivate->m_thumbPos.setX(value.toPoint().x()); - m_viewPrivate->updateHandle(); + m_viewPrivate->update(); } private: @@ -58,14 +57,15 @@ MButtonSwitchViewPrivate::MButtonSwitchViewPrivate() : m_feedbackOnPlayed(false), m_thumbPos(), m_thumbPosValid(false), - m_handleAnimation(new HandleAnimation(this)), + toggleOnAnimationFinish(false), + m_thumbAnimation(new ThumbAnimation(this)), m_animationSpeed(500) { } MButtonSwitchViewPrivate::~MButtonSwitchViewPrivate() { - delete m_handleAnimation; + delete m_thumbAnimation; } QSizeF MButtonSwitchViewPrivate::thumbSize() const @@ -92,7 +92,7 @@ QPointF MButtonSwitchViewPrivate::thumbPos() const Q_Q(const MButtonSwitchView); //when thumb is not dragged it is on another end of the view - if (!m_thumbDown && m_handleAnimation->state() == QAbstractAnimation::Stopped) { + if (!m_thumbDown && m_thumbAnimation->state() == QAbstractAnimation::Stopped) { return thumbEndPos(q->model()->checked()); } //return dragged thumb position @@ -112,9 +112,8 @@ QPointF MButtonSwitchViewPrivate::thumbEndPos(bool checked) const return QPointF(q->style()->thumbMargin(), h); } -void MButtonSwitchViewPrivate::updateHandle() +void MButtonSwitchViewPrivate::update() { - //invalidate masked slider image and request redraw Q_Q(MButtonSwitchView); q->update(); } @@ -128,10 +127,10 @@ void MButtonSwitchViewPrivate::drawSwitchSlider(QPainter *painter) const qreal opacity = ((qreal)thumbPos().x() - q->style()->thumbMargin()) / (q->size().width() - thumbSize().width() - q->style()->thumbMargin() * 2.0); painter->setOpacity(1.0 - opacity); if (q->style()->sliderImage()) - q->style()->sliderImage()->draw(QRectF(QPointF(0,0), q->size()), painter); + q->style()->sliderImage()->draw(QPoint(0,0), q->size(), painter); painter->setOpacity(opacity); if (q->style()->sliderImageSelected()) - q->style()->sliderImageSelected()->draw(QRectF(QPointF(0,0), q->size()), painter); + q->style()->sliderImageSelected()->draw(QPoint(0,0), q->size(), painter); painter->setOpacity(oldOpacity); } @@ -145,9 +144,22 @@ void MButtonSwitchViewPrivate::drawSwitchThumb(QPainter *painter) const q->style()->thumbImage()->draw(thumbPos(), thumbSize(), painter); } +void MButtonSwitchViewPrivate::_q_toggleCheck() +{ + Q_Q(MButtonSwitchView); + + if (toggleOnAnimationFinish) + q->model()->click(); + + q->model()->setChecked(checkStateOnAnimationFinish); + toggleOnAnimationFinish = false; +} + MButtonSwitchView::MButtonSwitchView(MButton *controller) : MButtonView(* new MButtonSwitchViewPrivate, controller) { + Q_D(MButtonSwitchView); + connect(d->m_thumbAnimation, SIGNAL(finished()), this, SLOT(_q_toggleCheck())); } MButtonSwitchView::~MButtonSwitchView() @@ -176,13 +188,11 @@ void MButtonSwitchView::applyStyle() void MButtonSwitchView::updateData(const QList<const char *>& modifications) { MButtonView::updateData(modifications); - update(); } void MButtonSwitchView::setupModel() { MButtonView::setupModel(); - update(); } void MButtonSwitchView::mousePressEvent(QGraphicsSceneMouseEvent *event) @@ -195,7 +205,7 @@ void MButtonSwitchView::mousePressEvent(QGraphicsSceneMouseEvent *event) Q_D(MButtonSwitchView); //stop ongoing animation if any - d->m_handleAnimation->stop(); + d->m_thumbAnimation->stop(); //honor MWidgetView's style and play press feedback style()->pressFeedback().play(); @@ -249,22 +259,25 @@ void MButtonSwitchView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) //set the state depending which side the thumb currently is if (d->m_thumbPos.x() + (d->thumbSize().width() / 2) > (size().width() / 2)) { - model()->setChecked(true); + d->checkStateOnAnimationFinish = true; } else { - model()->setChecked(false); + d->checkStateOnAnimationFinish = false; } } //user just clicked the switch, act like normal checkable button else { d->m_thumbDown = false; d->m_thumbDragged = false; + d->toggleOnAnimationFinish = false; QPointF touch = event->scenePos(); QRectF rect = d->controller->sceneBoundingRect(); rect.adjust(-M_RELEASE_MISS_DELTA, -M_RELEASE_MISS_DELTA, M_RELEASE_MISS_DELTA, M_RELEASE_MISS_DELTA); - if (rect.contains(touch)) - model()->click(); + if (rect.contains(touch)) { + d->toggleOnAnimationFinish = true; + d->checkStateOnAnimationFinish = !model()->checked(); + } if (model()->checked() == true) { style()->releaseOnFeedback().play(); @@ -274,11 +287,11 @@ void MButtonSwitchView::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) } //start animating the thumb from current position to proper end position - int delta = d->m_thumbPos.x() - d->thumbEndPos(model()->checked()).x(); - d->m_handleAnimation->setStartValue(d->m_thumbPos); - d->m_handleAnimation->setEndValue(d->thumbEndPos(model()->checked())); - d->m_handleAnimation->setDuration(abs(delta) / (d->m_animationSpeed/1000.0)); - d->m_handleAnimation->start(); + int delta = d->m_thumbPos.x() - d->thumbEndPos(d->checkStateOnAnimationFinish).x(); + d->m_thumbAnimation->setStartValue(d->m_thumbPos); + d->m_thumbAnimation->setEndValue(d->thumbEndPos(d->checkStateOnAnimationFinish)); + d->m_thumbAnimation->setDuration(abs(delta) / (d->m_animationSpeed/1000.0)); + d->m_thumbAnimation->start(); } void MButtonSwitchView::mouseMoveEvent(QGraphicsSceneMouseEvent *event) diff --git a/src/views/mbuttonswitchview.h b/src/views/mbuttonswitchview.h index e0426495..0c777d45 100644 --- a/src/views/mbuttonswitchview.h +++ b/src/views/mbuttonswitchview.h @@ -87,7 +87,6 @@ class M_VIEWS_EXPORT MButtonSwitchView : public MButtonView M_VIEW(MButtonModel, MButtonSwitchStyle) public: - /*! \brief Constructs the view. \param Pointer to the controller. @@ -112,27 +111,19 @@ protected: virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); virtual void cancelEvent(MCancelEvent *event); - //! \reimp_end - -protected slots: - - //! \reimp virtual void updateData(const QList<const char *>& modifications); //! \reimp_end -private slots: - - - private: + Q_PRIVATE_SLOT(d_func(), void _q_toggleCheck()) +private: Q_DISABLE_COPY(MButtonSwitchView) Q_DECLARE_PRIVATE(MButtonSwitchView) #ifdef UNIT_TEST friend class Ut_MButtonSwitchView; #endif - }; #endif diff --git a/src/views/mbuttonswitchview_p.h b/src/views/mbuttonswitchview_p.h index 6ae156be..e1232539 100644 --- a/src/views/mbuttonswitchview_p.h +++ b/src/views/mbuttonswitchview_p.h @@ -36,11 +36,13 @@ public: QSizeF thumbSize() const; QPointF thumbPos() const; QPointF thumbEndPos(bool checked) const; - void updateHandle(); + void update(); void drawSwitchSlider(QPainter *painter) const; void drawSwitchThumb(QPainter *painter) const; + void _q_toggleCheck(); + int mouseOffset; bool m_thumbDown; @@ -48,8 +50,10 @@ public: bool m_feedbackOnPlayed; QPointF m_thumbPos; bool m_thumbPosValid; + bool toggleOnAnimationFinish; + bool checkStateOnAnimationFinish; - QVariantAnimation* m_handleAnimation; + QVariantAnimation* m_thumbAnimation; int m_animationSpeed; //pixels per sec }; |