Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/gui/widgets/qwidgetanimator.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the QtGui module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     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.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
    4040****************************************************************************/
    4141
    42 #include <QtCore/qtimer.h>
    43 #include <QtCore/qdatetime.h>
     42#include <QtCore/qpropertyanimation.h>
    4443#include <QtGui/qwidget.h>
    45 #include <QtGui/qtextedit.h>
    46 #include <QtGui/private/qwidget_p.h>
    47 #include <qdebug.h>
     44#include <QtGui/private/qmainwindowlayout_p.h>
    4845
    4946#include "qwidgetanimator_p.h"
     
    5148QT_BEGIN_NAMESPACE
    5249
    53 static const int g_animation_steps = 12;
    54 static const int g_animation_interval = 16;
    55 
    56 // 1000 * (x/(1 + x*x) + 0.5) on interval [-1, 1]
    57 static const int g_animate_function[] =
     50QWidgetAnimator::QWidgetAnimator(QMainWindowLayout *layout) : m_mainWindowLayout(layout)
    5851{
    59     0, 1, 5, 12, 23, 38, 58, 84, 116, 155, 199, 251, 307, 368,
    60     433, 500, 566, 631, 692, 748, 799, 844, 883, 915, 941, 961,
    61     976, 987, 994, 998, 1000
    62 };
    63 static const int g_animate_function_points = sizeof(g_animate_function)/sizeof(int);
    64 
    65 static inline int animateHelper(int start, int stop, int step, int steps)
    66 {
    67     if (start == stop)
    68         return start;
    69     if (step == 0)
    70         return start;
    71     if (step == steps)
    72         return stop;
    73 
    74     int x = g_animate_function_points*step/(steps + 1);
    75     return start + g_animate_function[x]*(stop - start)/1000;
    76 }
    77 
    78 QWidgetAnimator::QWidgetAnimator(QObject *parent)
    79     : QObject(parent)
    80 {
    81     m_time = new QTime();
    82     m_timer = new QTimer(this);
    83     m_timer->setInterval(g_animation_interval);
    84     connect(m_timer, SIGNAL(timeout()), this, SLOT(animationStep()));
    85 }
    86 
    87 QWidgetAnimator::~QWidgetAnimator()
    88 {
    89     delete m_time;
    9052}
    9153
    9254void QWidgetAnimator::abort(QWidget *w)
    9355{
    94     if (m_animation_map.remove(w) == 0)
     56#ifndef QT_NO_ANIMATION
     57    AnimationMap::iterator it = m_animation_map.find(w);
     58    if (it == m_animation_map.end())
    9559        return;
    96     if (m_animation_map.isEmpty()) {
    97         m_timer->stop();
    98         emit finishedAll();
    99     }
     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
    10069}
     70
     71#ifndef QT_NO_ANIMATION
     72void QWidgetAnimator::animationFinished()
     73{
     74    QPropertyAnimation *anim = qobject_cast<QPropertyAnimation*>(sender());
     75    abort(static_cast<QWidget*>(anim->targetObject()));
     76}
     77#endif //QT_NO_ANIMATION
    10178
    10279void QWidgetAnimator::animate(QWidget *widget, const QRect &_final_geometry, bool animate)
    10380{
    104     QRect final_geometry = _final_geometry;
    105 
    10681    QRect r = widget->geometry();
    10782    if (r.right() < 0 || r.bottom() < 0)
    10883        r = QRect();
    10984
    110     if (r.isNull() || final_geometry.isNull())
    111         animate = false;
     85    animate = animate && !r.isNull() && !_final_geometry.isNull();
    11286
     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#ifndef QT_NO_ANIMATION
    11392    AnimationMap::const_iterator it = m_animation_map.constFind(widget);
    114     if (it == m_animation_map.constEnd()) {
    115         if (r == final_geometry) {
    116             emit finished(widget);
    117             return;
    118         }
    119     } else {
    120         if ((*it).r2 == final_geometry)
    121             return;
    122     }
     93    if (it != m_animation_map.constEnd() && (*it)->endValue().toRect() == final_geometry)
     94        return;
    12395
    124     if (animate) {
    125         AnimationItem item(widget, r, final_geometry);
    126         m_animation_map[widget] = item;
    127         if (!m_timer->isActive()) {
    128             m_timer->start();
    129             m_time->start();
    130         }
    131     } else {
    132         m_animation_map.remove(widget);
    133         if (m_animation_map.isEmpty())
    134             m_timer->stop();
    135 
    136         if (!final_geometry.isValid() && !widget->isWindow()) {
    137             // Make the wigdet go away by sending it to negative space
    138             QSize s = widget->size();
    139             final_geometry = QRect(-500 - s.width(), -500 - s.height(), s.width(), s.height());
    140         }
    141         widget->setGeometry(final_geometry);
    142 
    143         emit finished(widget);
    144 
    145         if (m_animation_map.isEmpty())
    146             emit finishedAll();
    147 
    148         return;
    149     }
    150 }
    151 
    152 void QWidgetAnimator::animationStep()
    153 {
    154     int steps = (1 + m_time->restart())/g_animation_interval;
    155     AnimationMap::iterator it = m_animation_map.begin();
    156     while (it != m_animation_map.end()) {
    157         AnimationItem &item = *it;
    158 
    159         item.step = qMin(item.step + steps, g_animation_steps);
    160 
    161         int x = animateHelper(item.r1.left(), item.r2.left(),
    162                             item.step, g_animation_steps);
    163         int y = animateHelper(item.r1.top(), item.r2.top(),
    164                             item.step, g_animation_steps);
    165         int w = animateHelper(item.r1.width(), item.r2.width(),
    166                             item.step, g_animation_steps);
    167         int h = animateHelper(item.r1.height(), item.r2.height(),
    168                             item.step, g_animation_steps);
    169 
    170         item.widget->setGeometry(x, y, w, h);
    171 
    172         if (item.step == g_animation_steps) {
    173             emit finished(item.widget);
    174             AnimationMap::iterator tmp = it;
    175             ++it;
    176             m_animation_map.erase(tmp);
    177         } else {
    178             ++it;
    179         }
    180     }
    181 
    182     if (m_animation_map.isEmpty()) {
    183         m_timer->stop();
    184         emit finishedAll();
    185     }
     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);
     103#else
     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
    186110}
    187111
    188112bool QWidgetAnimator::animating() const
    189113{
    190     return m_timer->isActive();
    191 }
    192 
    193 bool QWidgetAnimator::animating(QWidget *widget)
    194 {
    195     return m_animation_map.contains(widget);
     114    return !m_animation_map.isEmpty();
    196115}
    197116
Note: See TracChangeset for help on using the changeset viewer.