ginebra/graphicsitemanimation.h
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 2 bf4420e9fa4d
parent 6 1c3b8676e58c
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef GRAPHICSITEMANIMATION_H
       
    20 #define GRAPHICSITEMANIMATION_H
       
    21 
       
    22 #include <QObject>
       
    23 #include <QTimeLine>
       
    24 #include <assert.h>
       
    25 #include "chromesnippet.h"
       
    26 #include "utilities.h"
       
    27 
       
    28 // Base class for ChromeSnippet animations.
       
    29 class GraphicsItemAnimator
       
    30 {
       
    31     friend class GraphicsItemAnimation;
       
    32 
       
    33     virtual void apply(ChromeSnippet *snippet, qreal value) = 0;
       
    34   public:
       
    35     virtual ~GraphicsItemAnimator() {}
       
    36 };
       
    37 
       
    38 // ----------------------
       
    39 
       
    40 /** 
       
    41  * \brief Provides fade animations for ChromeSnippets.
       
    42  * 
       
    43  * \sa GraphicsItemAnimation
       
    44  */
       
    45 class GraphicsItemAnimatorFade : public GraphicsItemAnimator
       
    46 {
       
    47 public:
       
    48     GraphicsItemAnimatorFade(ChromeSnippet *snippet, qreal opacityDelta)
       
    49       : m_opacityOriginal(snippet->opacity()),
       
    50         m_opacityDelta(opacityDelta) {
       
    51     }
       
    52 
       
    53     virtual void apply(ChromeSnippet *snippet, qreal value) {
       
    54         snippet->setOpacity(m_opacityOriginal + value * m_opacityDelta);
       
    55     }
       
    56 
       
    57 private:
       
    58     qreal m_opacityOriginal;
       
    59     qreal m_opacityDelta;
       
    60 };
       
    61 
       
    62 // ----------------------
       
    63 
       
    64 /** 
       
    65  * \brief Provides translation animations for ChromeSnippets.
       
    66  * 
       
    67  * \sa GraphicsItemAnimation
       
    68  */
       
    69 class GraphicsItemAnimatorTranslate : public GraphicsItemAnimator
       
    70 {
       
    71 public:
       
    72     GraphicsItemAnimatorTranslate(ChromeSnippet *snippet, qreal dx, qreal dy)
       
    73       : m_posOriginal(snippet->pos()),
       
    74         m_xDelta(dx),
       
    75         m_yDelta(dy) {
       
    76     }
       
    77 
       
    78     virtual void apply(ChromeSnippet *snippet, qreal value) {
       
    79         snippet->setPos(m_posOriginal.x() + m_xDelta * value, m_posOriginal.y() + m_yDelta * value);
       
    80     }
       
    81 
       
    82 private:
       
    83     QPointF m_posOriginal;
       
    84     qreal m_xDelta;
       
    85     qreal m_yDelta;
       
    86 };
       
    87 
       
    88 // ----------------------
       
    89 
       
    90 /** 
       
    91  * \brief Provides rotation animations for ChromeSnippets.
       
    92  * 
       
    93  * \sa GraphicsItemAnimation
       
    94  */
       
    95 class GraphicsItemAnimatorRotate : public GraphicsItemAnimator
       
    96 {
       
    97 public:
       
    98     GraphicsItemAnimatorRotate(ChromeSnippet *snippet, qreal angleDelta, qreal xCenter, qreal yCenter)
       
    99       : m_originalTransform(snippet->transform()),
       
   100         m_angleDelta(angleDelta),
       
   101         m_xCenter(xCenter),
       
   102         m_yCenter(yCenter)
       
   103     {
       
   104     }
       
   105 
       
   106     virtual void apply(ChromeSnippet *snippet, qreal value) {
       
   107         QTransform transform = m_originalTransform;
       
   108         transform.translate(m_xCenter, m_yCenter);
       
   109         transform.rotate(m_angleDelta * value);
       
   110         transform.translate(-m_xCenter, -m_yCenter);
       
   111         snippet->setTransform(transform);
       
   112     }
       
   113 
       
   114 private:
       
   115     QTransform m_originalTransform;
       
   116     qreal m_angleDelta;
       
   117     qreal m_xCenter, m_yCenter;
       
   118 };
       
   119 
       
   120 // ----------------------
       
   121 
       
   122 /**
       
   123  * \brief Basic animations for ChromeSnippets
       
   124  * 
       
   125  * The GraphicsItemAnimation class provides basic animations for ChromeSnippets.
       
   126  * 
       
   127  * Calls to translateTo(), translateBy(), fadeTo() etc. can be chained together in javascript as follows:
       
   128  *   \code window.snippets.TopChromeId.animate(500).translateBy(10,30).fadeBy(-0.5).start()\endcode
       
   129  *   
       
   130  * \sa ChromeSnippet::animate()
       
   131  * \sa GraphicsItemAnimator
       
   132  * \sa AttentionAnimator
       
   133  * \sa VisibilityAnimator
       
   134  */
       
   135 class GraphicsItemAnimation : public QObject
       
   136 {
       
   137     Q_OBJECT
       
   138 public:
       
   139     GraphicsItemAnimation(ChromeSnippet *snippet, int duration = 500)
       
   140       : m_snippet(snippet),
       
   141         m_timeLine(duration)
       
   142     {
       
   143         setObjectName("animation");
       
   144         m_timeLine.setFrameRange(0, duration/50);
       
   145         safe_connect(&m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(update(qreal)));
       
   146         safe_connect(&m_timeLine, SIGNAL(finished()), this, SIGNAL(finished()));
       
   147     }
       
   148 
       
   149     ~GraphicsItemAnimation();
       
   150 
       
   151 public slots:
       
   152     QObject *translateTo(int x, int y)
       
   153     {
       
   154         QPointF pos = m_snippet->pos();
       
   155         m_animators.append(new GraphicsItemAnimatorTranslate(m_snippet, x - pos.x(), y - pos.y()));
       
   156         return this;
       
   157     }
       
   158 
       
   159     QObject *translateBy(int dx, int dy)
       
   160     {
       
   161         m_animators.append(new GraphicsItemAnimatorTranslate(m_snippet, dx, dy));
       
   162         return this;
       
   163     }
       
   164 
       
   165     QObject *fadeTo(qreal opacity)
       
   166     {
       
   167         m_animators.append(new GraphicsItemAnimatorFade(m_snippet, opacity - m_snippet->opacity()));
       
   168         return this;
       
   169     }
       
   170 
       
   171     QObject *fadeBy(qreal dOpacity)
       
   172     {
       
   173         m_animators.append(new GraphicsItemAnimatorFade(m_snippet, dOpacity));
       
   174         return this;
       
   175     }
       
   176 
       
   177     QObject *rotateBy(qreal angle, qreal xCenter, qreal yCenter)
       
   178     {
       
   179         m_animators.append(new GraphicsItemAnimatorRotate(m_snippet, angle, xCenter, yCenter));
       
   180         return this;
       
   181     }
       
   182 
       
   183     QObject *start()
       
   184     {
       
   185         m_timeLine.stop();
       
   186         m_timeLine.start();
       
   187         return this;
       
   188     }
       
   189 
       
   190     QObject *stop()
       
   191     {
       
   192         m_timeLine.stop();
       
   193         return this;
       
   194     }
       
   195 
       
   196 signals:
       
   197     void finished();
       
   198     void updated(qreal value);
       
   199 
       
   200 protected slots:
       
   201     void update(qreal value)
       
   202     {
       
   203         foreach(GraphicsItemAnimator *animator, m_animators)
       
   204         {
       
   205             animator->apply(m_snippet, value);
       
   206         }
       
   207         emit updated(value);
       
   208     }
       
   209 
       
   210 private:
       
   211     ChromeSnippet *m_snippet;
       
   212     QTimeLine m_timeLine;
       
   213     QList<GraphicsItemAnimator *> m_animators;
       
   214 };
       
   215 
       
   216 #endif // GRAPHICSITEMANIMATION_H