1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qwidgetanimator_p.h"
41
42 #if QT_CONFIG(animation)
43 #include <QtCore/qpropertyanimation.h>
44 #endif
45 #include <QtWidgets/qwidget.h>
46 #include <QtWidgets/qstyle.h>
47 #if QT_CONFIG(mainwindow)
48 #include <private/qmainwindowlayout_p.h>
49 #endif
50
51 QT_BEGIN_NAMESPACE
52
QWidgetAnimator(QMainWindowLayout * layout)53 QWidgetAnimator::QWidgetAnimator(QMainWindowLayout *layout)
54 #if QT_CONFIG(mainwindow)
55 : m_mainWindowLayout(layout)
56 #endif
57 {
58 Q_UNUSED(layout)
59 }
60
abort(QWidget * w)61 void QWidgetAnimator::abort(QWidget *w)
62 {
63 #if QT_CONFIG(animation)
64 const auto it = m_animation_map.constFind(w);
65 if (it == m_animation_map.cend())
66 return;
67 QPropertyAnimation *anim = *it;
68 m_animation_map.erase(it);
69 if (anim) {
70 anim->stop();
71 }
72 #if QT_CONFIG(mainwindow)
73 m_mainWindowLayout->animationFinished(w);
74 #endif
75 #else
76 Q_UNUSED(w); //there is no animation to abort
77 #endif // animation
78 }
79
80 #if QT_CONFIG(animation)
animationFinished()81 void QWidgetAnimator::animationFinished()
82 {
83 QPropertyAnimation *anim = qobject_cast<QPropertyAnimation*>(sender());
84 abort(static_cast<QWidget*>(anim->targetObject()));
85 }
86 #endif // animation
87
animate(QWidget * widget,const QRect & _final_geometry,bool animate)88 void QWidgetAnimator::animate(QWidget *widget, const QRect &_final_geometry, bool animate)
89 {
90 QRect r = widget->geometry();
91 if (r.right() < 0 || r.bottom() < 0)
92 r = QRect();
93
94 animate = animate && !r.isNull() && !_final_geometry.isNull();
95
96 // might make the wigdet go away by sending it to negative space
97 const QRect final_geometry = _final_geometry.isValid() || widget->isWindow() ? _final_geometry :
98 QRect(QPoint(-500 - widget->width(), -500 - widget->height()), widget->size());
99
100 #if QT_CONFIG(animation)
101 //If the QStyle has animations, animate
102 if (const int animationDuration = widget->style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, widget)) {
103 AnimationMap::const_iterator it = m_animation_map.constFind(widget);
104 if (it != m_animation_map.constEnd() && (*it)->endValue().toRect() == final_geometry)
105 return;
106
107 QPropertyAnimation *anim = new QPropertyAnimation(widget, "geometry", widget);
108 anim->setDuration(animate ? animationDuration : 0);
109 anim->setEasingCurve(QEasingCurve::InOutQuad);
110 anim->setEndValue(final_geometry);
111 m_animation_map[widget] = anim;
112 connect(anim, SIGNAL(finished()), SLOT(animationFinished()));
113 anim->start(QPropertyAnimation::DeleteWhenStopped);
114 } else
115 #endif // animation
116 {
117 //we do it in one shot
118 widget->setGeometry(final_geometry);
119 #if QT_CONFIG(mainwindow)
120 m_mainWindowLayout->animationFinished(widget);
121 #endif // QT_CONFIG(mainwindow)
122 }
123 }
124
animating() const125 bool QWidgetAnimator::animating() const
126 {
127 return !m_animation_map.isEmpty();
128 }
129
130 QT_END_NAMESPACE
131
132 #include "moc_qwidgetanimator_p.cpp"
133