From 7486389a0d742a7c9e70c6110692186f70dbf1e5 Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Wed, 8 Jul 2009 10:25:04 +0200 Subject: QMainWindow: made use of QPropertyAnimation for animations This required some refactoring as well. Now code is leaner and cleaner --- src/gui/widgets/qmainwindowlayout.cpp | 75 ++++++++++-------------- src/gui/widgets/qmainwindowlayout_p.h | 3 +- src/gui/widgets/qwidgetanimator.cpp | 107 +++++++--------------------------- src/gui/widgets/qwidgetanimator_p.h | 23 ++------ 4 files changed, 59 insertions(+), 149 deletions(-) diff --git a/src/gui/widgets/qmainwindowlayout.cpp b/src/gui/widgets/qmainwindowlayout.cpp index 1057f5f..a02dca3 100644 --- a/src/gui/widgets/qmainwindowlayout.cpp +++ b/src/gui/widgets/qmainwindowlayout.cpp @@ -1550,37 +1550,10 @@ bool QMainWindowLayout::plug(QLayoutItem *widgetItem) return true; } -void QMainWindowLayout::allAnimationsFinished() -{ -#ifndef QT_NO_DOCKWIDGET - parentWidget()->update(layoutState.dockAreaLayout.separatorRegion()); - -#ifndef QT_NO_TABBAR - foreach (QTabBar *tab_bar, usedTabBars) - tab_bar->show(); -#endif // QT_NO_TABBAR -#endif // QT_NO_DOCKWIDGET - - updateGapIndicator(); -} - void QMainWindowLayout::animationFinished(QWidget *widget) { - - /* This signal is delivered from QWidgetAnimator over a qeued connection. The problem is that - the widget can be deleted. This is handled as follows: - - The animator only ever animates widgets that have been added to this layout. If a widget - is deleted during animation, the widget's destructor removes the widget form this layout. - This in turn aborts the animation (see takeAt()) and this signal will never be delivered. - - If the widget is deleted after the animation is finished but before this qeued signal - is delivered, the widget is no longer in the layout and we catch it here. The key is that - QMainWindowLayoutState::contains() never dereferences the pointer. */ - - if (!layoutState.contains(widget)) - return; - + //this function is called from within the Widget Animator whenever an animation is finished + //on a certain widget #ifndef QT_NO_TOOLBAR if (QToolBar *tb = qobject_cast(widget)) { QToolBarLayout *tbl = qobject_cast(tb->layout()); @@ -1593,32 +1566,44 @@ void QMainWindowLayout::animationFinished(QWidget *widget) } #endif - if (widget != pluggingWidget) - return; + if (widget == pluggingWidget) { #ifndef QT_NO_DOCKWIDGET - if (QDockWidget *dw = qobject_cast(widget)) - dw->d_func()->plug(currentGapRect); + if (QDockWidget *dw = qobject_cast(widget)) + dw->d_func()->plug(currentGapRect); #endif #ifndef QT_NO_TOOLBAR - if (QToolBar *tb = qobject_cast(widget)) - tb->d_func()->plug(currentGapRect); + if (QToolBar *tb = qobject_cast(widget)) + tb->d_func()->plug(currentGapRect); #endif - applyState(layoutState, false); + applyState(layoutState, false); #ifndef QT_NO_DOCKWIDGET #ifndef QT_NO_TABBAR - if (qobject_cast(widget) != 0) { - // info() might return null if the widget is destroyed while - // animating but before the animationFinished signal is received. - if (QDockAreaLayoutInfo *info = layoutState.dockAreaLayout.info(widget)) - info->setCurrentTab(widget); - } + if (qobject_cast(widget) != 0) { + // info() might return null if the widget is destroyed while + // animating but before the animationFinished signal is received. + if (QDockAreaLayoutInfo *info = layoutState.dockAreaLayout.info(widget)) + info->setCurrentTab(widget); + } #endif #endif - savedState.clear(); - currentGapPos.clear(); - pluggingWidget = 0; + savedState.clear(); + currentGapPos.clear(); + pluggingWidget = 0; + } + + if (!widgetAnimator.animating()) { + //all animations are finished +#ifndef QT_NO_DOCKWIDGET + parentWidget()->update(layoutState.dockAreaLayout.separatorRegion()); +#ifndef QT_NO_TABBAR + foreach (QTabBar *tab_bar, usedTabBars) + tab_bar->show(); +#endif // QT_NO_TABBAR +#endif // QT_NO_DOCKWIDGET + } + updateGapIndicator(); } diff --git a/src/gui/widgets/qmainwindowlayout_p.h b/src/gui/widgets/qmainwindowlayout_p.h index 759461b..a7f70b4 100644 --- a/src/gui/widgets/qmainwindowlayout_p.h +++ b/src/gui/widgets/qmainwindowlayout_p.h @@ -297,9 +297,8 @@ public: void restore(bool keepSavedState = false); void updateHIToolBarStatus(); void animationFinished(QWidget *widget); - void allAnimationsFinished(); -private slots: +private Q_SLOTS: #ifndef QT_NO_DOCKWIDGET #ifndef QT_NO_TABBAR void tabChanged(); diff --git a/src/gui/widgets/qwidgetanimator.cpp b/src/gui/widgets/qwidgetanimator.cpp index 56b3f43..d820b59 100644 --- a/src/gui/widgets/qwidgetanimator.cpp +++ b/src/gui/widgets/qwidgetanimator.cpp @@ -39,6 +39,7 @@ ** ****************************************************************************/ +#include #include #include @@ -46,47 +47,25 @@ QT_BEGIN_NAMESPACE -static const int g_animation_steps = 12; -static const int g_animation_interval = 16; - -// 1000 * (x/(1 + x*x) + 0.5) on interval [-1, 1] -static const int g_animate_function[] = -{ - 0, 1, 5, 12, 23, 38, 58, 84, 116, 155, 199, 251, 307, 368, - 433, 500, 566, 631, 692, 748, 799, 844, 883, 915, 941, 961, - 976, 987, 994, 998, 1000 -}; -static const int g_animate_function_points = sizeof(g_animate_function)/sizeof(int); - -static inline int animateHelper(int start, int stop, int step, int steps) -{ - if (start == stop) - return start; - if (step == 0) - return start; - if (step == steps) - return stop; - - int x = g_animate_function_points*step/(steps + 1); - return start + g_animate_function[x]*(stop - start)/1000; -} - QWidgetAnimator::QWidgetAnimator(QMainWindowLayout *layout) : m_mainWindowLayout(layout) { } -QWidgetAnimator::~QWidgetAnimator() +void QWidgetAnimator::abort(QWidget *w) { + AnimationMap::iterator it = m_animation_map.find(w); + if (it == m_animation_map.end()) + return; + QPropertyAnimation *anim = *it; + m_animation_map.erase(it); + anim->stop(); + m_mainWindowLayout->animationFinished(w); } -void QWidgetAnimator::abort(QWidget *w) +void QWidgetAnimator::animationFinished() { - if (m_animation_map.remove(w) == 0) - return; - if (m_animation_map.isEmpty()) { - m_timer.stop(); - m_mainWindowLayout->allAnimationsFinished(); - } + QPropertyAnimation *anim = qobject_cast(sender()); + abort(static_cast(anim->targetObject())); } void QWidgetAnimator::animate(QWidget *widget, const QRect &_final_geometry, bool animate) @@ -101,16 +80,17 @@ void QWidgetAnimator::animate(QWidget *widget, const QRect &_final_geometry, boo animate = false; AnimationMap::const_iterator it = m_animation_map.constFind(widget); - if (it != m_animation_map.constEnd() && (*it).r2 == final_geometry) + if (it != m_animation_map.constEnd() && (*it)->endValue().toRect() == final_geometry) return; if (animate) { - AnimationItem item(widget, r, final_geometry); - m_animation_map[widget] = item; - if (!m_timer.isActive()) { - m_timer.start(g_animation_interval, this); - m_time.start(); - } + QPropertyAnimation *anim = new QPropertyAnimation(widget, "geometry"); + anim->setDuration(200); + anim->setEasingCurve(QEasingCurve::InOutQuad); + anim->setEndValue(final_geometry); + m_animation_map[widget] = anim; + connect(anim, SIGNAL(finished()), SLOT(animationFinished())); + anim->start(QPropertyAnimation::DeleteWhenStopped); } else { if (!final_geometry.isValid() && !widget->isWindow()) { // Make the wigdet go away by sending it to negative space @@ -118,58 +98,15 @@ void QWidgetAnimator::animate(QWidget *widget, const QRect &_final_geometry, boo final_geometry = QRect(-500 - s.width(), -500 - s.height(), s.width(), s.height()); } widget->setGeometry(final_geometry); - - if (m_animation_map.remove(widget)) { - m_mainWindowLayout->animationFinished(widget); - if (m_animation_map.isEmpty()) { - m_timer.stop(); - m_mainWindowLayout->allAnimationsFinished(); - } - } - } -} - -void QWidgetAnimator::timerEvent(QTimerEvent *) -{ - int steps = (1 + m_time.restart())/g_animation_interval; - AnimationMap::iterator it = m_animation_map.begin(); - while (it != m_animation_map.end()) { - AnimationItem &item = *it; - - item.step = qMin(item.step + steps, g_animation_steps); - - int x = animateHelper(item.r1.left(), item.r2.left(), - item.step, g_animation_steps); - int y = animateHelper(item.r1.top(), item.r2.top(), - item.step, g_animation_steps); - int w = animateHelper(item.r1.width(), item.r2.width(), - item.step, g_animation_steps); - int h = animateHelper(item.r1.height(), item.r2.height(), - item.step, g_animation_steps); - - item.widget->setGeometry(x, y, w, h); - - if (item.step == g_animation_steps) { - QWidget *widget = item.widget; - it = m_animation_map.erase(it); - m_mainWindowLayout->animationFinished(widget); - } else { - ++it; - } - } - - if (m_animation_map.isEmpty()) { - m_timer.stop(); - m_mainWindowLayout->allAnimationsFinished(); } } bool QWidgetAnimator::animating() const { - return m_timer.isActive(); + return !m_animation_map.isEmpty(); } -bool QWidgetAnimator::animating(QWidget *widget) +bool QWidgetAnimator::animating(QWidget *widget) const { return m_animation_map.contains(widget); } diff --git a/src/gui/widgets/qwidgetanimator_p.h b/src/gui/widgets/qwidgetanimator_p.h index 0c68e00..edd8e7c 100644 --- a/src/gui/widgets/qwidgetanimator_p.h +++ b/src/gui/widgets/qwidgetanimator_p.h @@ -56,41 +56,30 @@ #include #include #include -#include -#include QT_BEGIN_NAMESPACE class QWidget; class QMainWindowLayout; +class QPropertyAnimation; class QWidgetAnimator : public QObject { + Q_OBJECT public: QWidgetAnimator(QMainWindowLayout *layout); - ~QWidgetAnimator(); void animate(QWidget *widget, const QRect &final_geometry, bool animate); bool animating() const; - bool animating(QWidget *widget); + bool animating(QWidget *widget) const; void abort(QWidget *widget); -protected: - void timerEvent(QTimerEvent *e); +private Q_SLOTS: + void animationFinished(); private: - struct AnimationItem { - AnimationItem(QWidget *_widget = 0, const QRect &_r1 = QRect(), - const QRect &_r2 = QRect()) - : widget(_widget), r1(_r1), r2(_r2), step(0) {} - QWidget *widget; - QRect r1, r2; - int step; - }; - typedef QMap AnimationMap; + typedef QMap AnimationMap; AnimationMap m_animation_map; - QBasicTimer m_timer; - QTime m_time; QMainWindowLayout *m_mainWindowLayout; }; -- cgit v0.12