phoneuis/bubblemanager2/bubblestyle/src/bubbleanimiconitem.cpp
changeset 30 ebdbd102c78a
parent 27 2f8f8080a020
child 32 14cdbae33453
child 36 2eacb6118286
equal deleted inserted replaced
27:2f8f8080a020 30:ebdbd102c78a
     1 /*!
       
     2 * Copyright (c) 2009 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:  Animated icon.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QTimer>
       
    19 #include <QGraphicsSceneResizeEvent>
       
    20 #include "bubbleanimiconitem.h"
       
    21 
       
    22 BubbleAnimIconItem::BubbleAnimIconItem( int interval, QGraphicsItem* parent )
       
    23     : HbIconItem( parent ), mInterval(interval), mAnimationTimerId(0)
       
    24 {
       
    25     reset();
       
    26 }
       
    27 
       
    28 BubbleAnimIconItem::~BubbleAnimIconItem()
       
    29 {
       
    30     reset();
       
    31 }
       
    32 
       
    33 void BubbleAnimIconItem::appendIcon( const QString& iconName )
       
    34 {
       
    35     HbIcon* icon = new HbIcon(iconName);
       
    36     icon->setSize(rect().size());
       
    37     mFrames.append(icon);
       
    38     if (mFrames.count()==1) {
       
    39         setIcon(*icon); // initial frame
       
    40     }
       
    41 }
       
    42 
       
    43 void BubbleAnimIconItem::startAnimation(int interval)
       
    44 {
       
    45     mAnimationTimerId = startTimer(interval);
       
    46 }
       
    47 
       
    48 void BubbleAnimIconItem::stopAnimation()
       
    49 {
       
    50      if ( mAnimationTimerId ) {
       
    51          killTimer(mAnimationTimerId);
       
    52          mAnimationTimerId = 0;
       
    53      }
       
    54 }
       
    55 
       
    56 void BubbleAnimIconItem::reset()
       
    57 {
       
    58     stopAnimation();
       
    59     foreach (HbIcon* icon, mFrames) {
       
    60         delete icon;
       
    61     }
       
    62     mFrames.clear();
       
    63     mCurrentFrame = 0;
       
    64 }
       
    65 
       
    66 void BubbleAnimIconItem::timerEvent(QTimerEvent *event)
       
    67 {
       
    68    Q_UNUSED(event)
       
    69    
       
    70    mCurrentFrame = (mCurrentFrame + 1) % mFrames.count();
       
    71    setIcon(*mFrames.at(mCurrentFrame));
       
    72    update();
       
    73 }
       
    74 
       
    75 void BubbleAnimIconItem::paint(
       
    76     QPainter *painter,
       
    77     const QStyleOptionGraphicsItem *option,
       
    78     QWidget *widget )
       
    79 {
       
    80     HbIconItem::paint( painter, option, widget );
       
    81 
       
    82     if ( mFrames.count() && mAnimationTimerId == 0 ) {
       
    83         startAnimation(mInterval); // begin animation
       
    84     }
       
    85 }
       
    86 
       
    87 QVariant BubbleAnimIconItem::itemChange(
       
    88     GraphicsItemChange change,
       
    89     const QVariant& value )
       
    90 {
       
    91     if (change==QGraphicsItem::ItemVisibleHasChanged) {
       
    92         if (mAnimationTimerId && value.toBool()==false) {
       
    93             stopAnimation(); // stop animation when going invisible
       
    94         }
       
    95     }
       
    96 
       
    97     return HbIconItem::itemChange( change, value );
       
    98 }
       
    99 
       
   100 void BubbleAnimIconItem::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   101 {
       
   102     HbIconItem::resizeEvent(event);
       
   103     foreach (HbIcon* icon, mFrames) {
       
   104         icon->setSize(event->newSize());
       
   105     }
       
   106 }