src/gui/widgets/qwidgetanimator.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtCore/qpropertyanimation.h>
       
    43 #include <QtGui/qwidget.h>
       
    44 #include <QtGui/private/qmainwindowlayout_p.h>
       
    45 
       
    46 #include "qwidgetanimator_p.h"
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 QWidgetAnimator::QWidgetAnimator(QMainWindowLayout *layout) : m_mainWindowLayout(layout)
       
    51 {
       
    52 }
       
    53 
       
    54 void QWidgetAnimator::abort(QWidget *w)
       
    55 {
       
    56 #ifndef QT_NO_ANIMATION
       
    57     AnimationMap::iterator it = m_animation_map.find(w);
       
    58     if (it == m_animation_map.end())
       
    59         return;
       
    60     QPropertyAnimation *anim = *it;
       
    61     m_animation_map.erase(it);
       
    62     anim->stop();
       
    63 #ifndef QT_NO_MAINWINDOW
       
    64     m_mainWindowLayout->animationFinished(w);
       
    65 #endif
       
    66 #else
       
    67     Q_UNUSED(w); //there is no animation to abort
       
    68 #endif //QT_NO_ANIMATION
       
    69 }
       
    70 
       
    71 #ifndef QT_NO_ANIMATION
       
    72 void QWidgetAnimator::animationFinished()
       
    73 {
       
    74     QPropertyAnimation *anim = qobject_cast<QPropertyAnimation*>(sender());
       
    75     abort(static_cast<QWidget*>(anim->targetObject()));
       
    76 }
       
    77 #endif //QT_NO_ANIMATION
       
    78 
       
    79 void QWidgetAnimator::animate(QWidget *widget, const QRect &_final_geometry, bool animate)
       
    80 {
       
    81     QRect r = widget->geometry();
       
    82     if (r.right() < 0 || r.bottom() < 0)
       
    83         r = QRect();
       
    84 
       
    85     animate = animate && !r.isNull() && !_final_geometry.isNull();
       
    86 
       
    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());
       
    90 
       
    91     if (r == final_geometry)
       
    92         return; //the widget is already where it should be
       
    93 #ifndef QT_NO_ANIMATION
       
    94     AnimationMap::const_iterator it = m_animation_map.constFind(widget);
       
    95     if (it != m_animation_map.constEnd() && (*it)->endValue().toRect() == final_geometry)
       
    96         return;
       
    97 
       
    98     QPropertyAnimation *anim = new QPropertyAnimation(widget, "geometry", widget);
       
    99     anim->setDuration(animate ? 200 : 0);
       
   100     anim->setEasingCurve(QEasingCurve::InOutQuad);
       
   101     anim->setEndValue(final_geometry);
       
   102     m_animation_map[widget] = anim;
       
   103     connect(anim, SIGNAL(finished()), SLOT(animationFinished()));
       
   104     anim->start(QPropertyAnimation::DeleteWhenStopped);
       
   105 #else
       
   106     //we do it in one shot
       
   107     widget->setGeometry(final_geometry);
       
   108     m_mainWindowLayout->animationFinished(widget);
       
   109 #endif //QT_NO_ANIMATION
       
   110 }
       
   111 
       
   112 bool QWidgetAnimator::animating() const
       
   113 {
       
   114     return !m_animation_map.isEmpty();
       
   115 }
       
   116 
       
   117 bool QWidgetAnimator::animating(QWidget *widget) const
       
   118 {
       
   119     return m_animation_map.contains(widget);
       
   120 }
       
   121 
       
   122 QT_END_NAMESPACE