From fa58f946984b13c0396640b5be69ee2917c9995b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sami=20R=C3=A4m=C3=B6?= Date: Thu, 2 Sep 2010 17:10:08 +0300 Subject: [PATCH] Added animation - Animation for hiding and showing the ListItemContextButtonBar - When list item context buttons are changed, the ListItemContextButtonBar is hidden before the buttons are actually changed. If the buttons are requested to be shown before the change is made, then the request is remembered and buttons are shown after changing the buttons is done. --- src/ui/listitemcontextbuttonbar.cpp | 105 +++++++++++++++++++++++++++-------- src/ui/listitemcontextbuttonbar.h | 21 +++++++ src/ui/panelbase.cpp | 2 +- 3 files changed, 105 insertions(+), 23 deletions(-) diff --git a/src/ui/listitemcontextbuttonbar.cpp b/src/ui/listitemcontextbuttonbar.cpp index 722f123..96c0b75 100644 --- a/src/ui/listitemcontextbuttonbar.cpp +++ b/src/ui/listitemcontextbuttonbar.cpp @@ -20,6 +20,8 @@ */ #include +#include +#include #include "panelcommon.h" @@ -27,49 +29,108 @@ ListItemContextButtonBar::ListItemContextButtonBar(QWidget *parent) : QWidget(parent), - m_contextButtons(0) + m_waitForOpen(false), + m_contextButtons(0), + m_newContextButtons(0), + m_state(StateHidden) { qDebug() << __PRETTY_FUNCTION__; -} -void ListItemContextButtonBar::hideContextButtonBar() -{ - qDebug() << __PRETTY_FUNCTION__; + const int ANIMATION_DURATION_MS = 150; -} - -void ListItemContextButtonBar::onListItemSelectionChanged(bool itemIsSelected) -{ - qDebug() << __PRETTY_FUNCTION__; + m_animation = new QPropertyAnimation(this, "pos", this); + m_animation->setDuration(ANIMATION_DURATION_MS); - m_contextButtons->setVisible(itemIsSelected); + connect(m_animation, SIGNAL(finished()), + this, SLOT(onAnimationFinished())); } -void ListItemContextButtonBar::setContextButtons(QWidget *contextButtons) +void ListItemContextButtonBar::changeButtons() { - qDebug() << __PRETTY_FUNCTION__; + qWarning() << __PRETTY_FUNCTION__; + + Q_ASSERT(m_animation->state() == QAbstractAnimation::Stopped); // Hide previous buttons (if any) if (m_contextButtons) m_contextButtons->setParent(0); - m_contextButtons = contextButtons; - + m_contextButtons = m_newContextButtons; + m_newContextButtons = 0; m_contextButtons->setParent(this); - - // widget must be temporarily visible so we can get the size, doesn't work with invisible widget - m_contextButtons->setVisible(true); + m_contextButtons->show(); setFixedSize(m_contextButtons->size()); - m_contextButtons->setVisible(false); - // center this widget horizontally to middle of the panel contents area + // center this widget horizontally to middle of the panel contents area and set outside of + // the view const int FROM_PANEL_CONTENTS_LEFT = PANEL_WIDTH / 2 - m_contextButtons->width() / 2; + move(PANEL_TAB_BAR_WIDTH + PANEL_BAR_WIDTH + FROM_PANEL_CONTENTS_LEFT, -size().height()); + + // update new target values for animations + m_animation->setStartValue(pos()); const int Y = 0; - move(PANEL_TAB_BAR_WIDTH + PANEL_BAR_WIDTH + FROM_PANEL_CONTENTS_LEFT, Y); + m_animation->setEndValue(QPoint(pos().x(), Y)); +} + +void ListItemContextButtonBar::hideContextButtonBar() +{ + qWarning() << __PRETTY_FUNCTION__; + + m_state = StateClosing; + m_animation->setDirection(QAbstractAnimation::Backward); + m_animation->start(); +} + +void ListItemContextButtonBar::onAnimationFinished() +{ + qWarning() << __PRETTY_FUNCTION__; + + if (m_animation->direction() == QAbstractAnimation::Backward) { + m_state = StateHidden; + if (m_newContextButtons) { + changeButtons(); + if (m_waitForOpen) { + m_waitForOpen = false; + showContextButtonBar(); + } + } + } else { + m_state = StateVisible; + } +} + +void ListItemContextButtonBar::onListItemSelectionChanged(bool itemIsSelected) +{ + qWarning() << __PRETTY_FUNCTION__; + + /// @todo m_waitForOpen is not cleared if item selection is removed while waiting + + if (m_newContextButtons) + m_waitForOpen = true; + else if (itemIsSelected && (m_state != StateVisible)) + showContextButtonBar(); + else if (!itemIsSelected && (m_state != StateHidden)) + hideContextButtonBar(); +} + +void ListItemContextButtonBar::setContextButtons(QWidget *contextButtons) +{ + qWarning() << __PRETTY_FUNCTION__; + + m_newContextButtons = contextButtons; + m_waitForOpen = false; + + if (m_state != StateHidden) + hideContextButtonBar(); + else + changeButtons(); } void ListItemContextButtonBar::showContextButtonBar() { - qDebug() << __PRETTY_FUNCTION__; + qWarning() << __PRETTY_FUNCTION__; + m_state = StateOpening; + m_animation->setDirection(QAbstractAnimation::Forward); + m_animation->start(); } diff --git a/src/ui/listitemcontextbuttonbar.h b/src/ui/listitemcontextbuttonbar.h index afbd971..49d4846 100644 --- a/src/ui/listitemcontextbuttonbar.h +++ b/src/ui/listitemcontextbuttonbar.h @@ -25,6 +25,8 @@ #include +class QPropertyAnimation; + class ListItemContextButtonBar : public QWidget { Q_OBJECT @@ -48,8 +50,27 @@ public slots: void onListItemSelectionChanged(bool itemIsSelected); private: + void changeButtons(); + +private slots: + void onAnimationFinished(); + +private: + enum AnimationState { + StateHidden, + StateOpening, + StateClosing, + StateVisible + }; + + bool m_waitForOpen; + + QPropertyAnimation *m_animation; + QWidget *m_contextButtons; ///< Widget for context buttons + QWidget *m_newContextButtons; ///< Temporary pointer for new context buttons + AnimationState m_state; }; #endif // LISTITEMCONTEXTBUTTONBAR_H diff --git a/src/ui/panelbase.cpp b/src/ui/panelbase.cpp index a623abd..b9f8839 100644 --- a/src/ui/panelbase.cpp +++ b/src/ui/panelbase.cpp @@ -55,7 +55,7 @@ PanelBase::PanelBase(QWidget *parent) m_itemButtonsLayout = new QHBoxLayout; /// @todo set margins m_itemButtons->setLayout(m_itemButtonsLayout); - m_itemButtons->hide(); +// m_itemButtons->hide(); } QWidget* PanelBase::genericPanelButtons() const -- 1.7.9.5