1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtCore/qpropertyanimation.h>
43 #include <QtWidgets/qwidget.h>
44 #include <private/qmainwindowlayout_p.h>
46 #include "qwidgetanimator_p.h"
50 QWidgetAnimator::QWidgetAnimator(QMainWindowLayout *layout) : m_mainWindowLayout(layout)
54 void QWidgetAnimator::abort(QWidget *w)
56 #ifndef QT_NO_ANIMATION
57 AnimationMap::iterator it = m_animation_map.find(w);
58 if (it == m_animation_map.end())
60 QPropertyAnimation *anim = *it;
61 m_animation_map.erase(it);
63 #ifndef QT_NO_MAINWINDOW
64 m_mainWindowLayout->animationFinished(w);
67 Q_UNUSED(w); //there is no animation to abort
68 #endif //QT_NO_ANIMATION
71 #ifndef QT_NO_ANIMATION
72 void QWidgetAnimator::animationFinished()
74 QPropertyAnimation *anim = qobject_cast<QPropertyAnimation*>(sender());
75 abort(static_cast<QWidget*>(anim->targetObject()));
77 #endif //QT_NO_ANIMATION
79 void QWidgetAnimator::animate(QWidget *widget, const QRect &_final_geometry, bool animate)
81 QRect r = widget->geometry();
82 if (r.right() < 0 || r.bottom() < 0)
85 animate = animate && !r.isNull() && !_final_geometry.isNull();
87 // might make the wigdet go away by sending it to negative space
88 const QRect final_geometry = _final_geometry.isValid() || widget->isWindow() ? _final_geometry :
89 QRect(QPoint(-500 - widget->width(), -500 - widget->height()), widget->size());
91 #ifndef QT_NO_ANIMATION
92 AnimationMap::const_iterator it = m_animation_map.constFind(widget);
93 if (it != m_animation_map.constEnd() && (*it)->endValue().toRect() == final_geometry)
96 QPropertyAnimation *anim = new QPropertyAnimation(widget, "geometry", widget);
97 anim->setDuration(animate ? 200 : 0);
98 anim->setEasingCurve(QEasingCurve::InOutQuad);
99 anim->setEndValue(final_geometry);
100 m_animation_map[widget] = anim;
101 connect(anim, SIGNAL(finished()), SLOT(animationFinished()));
102 anim->start(QPropertyAnimation::DeleteWhenStopped);
104 //we do it in one shot
105 widget->setGeometry(final_geometry);
106 #ifndef QT_NO_MAINWINDOW
107 m_mainWindowLayout->animationFinished(widget);
108 #endif //QT_NO_MAINWINDOW
109 #endif //QT_NO_ANIMATION
112 bool QWidgetAnimator::animating() const
114 return !m_animation_map.isEmpty();