homescreenapp/hsutils/src/hssnapline.cpp
changeset 62 341166945d65
child 69 87476091b3f5
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
       
     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:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QGraphicsOpacityEffect>
       
    19 #include <QPropertyAnimation>
       
    20 
       
    21 #include "hsapp_defs.h"
       
    22 #include "HsSnapLine.h"
       
    23 
       
    24 /*!
       
    25     Constructor.
       
    26     
       
    27     \a parent Owner.
       
    28 */
       
    29 HsSnapLine::HsSnapLine(QGraphicsItem *parent)
       
    30   : QGraphicsLineItem(parent),
       
    31     mFadeInAnimation(0),
       
    32     mFadeOutAnimation(0),
       
    33     mOpacity(0.0),
       
    34     mFadeInAnimationDuration(0),
       
    35     mFadeOutAnimationDuration(0)
       
    36 {
       
    37     QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
       
    38     effect->setOpacity(0.0);
       
    39     effect->setEnabled(false);
       
    40     setGraphicsEffect(effect);
       
    41 
       
    42     mFadeInAnimation = new QPropertyAnimation(graphicsEffect(), "opacity", this);
       
    43     connect(mFadeInAnimation, SIGNAL(finished()), SLOT(fadeInAnimationFinished()));
       
    44 
       
    45     mFadeOutAnimation = new QPropertyAnimation(graphicsEffect(), "opacity", this);
       
    46     connect(mFadeOutAnimation, SIGNAL(finished()), SLOT(fadeOutAnimationFinished()));
       
    47 
       
    48     QPen pen;
       
    49     pen.setWidth(3);
       
    50     pen.setColor(Qt::darkCyan); //TODO: Change the color to the Theme element
       
    51     setPen(pen);
       
    52 }
       
    53  
       
    54 /*!
       
    55     Destructor.
       
    56 */
       
    57 HsSnapLine::~HsSnapLine()
       
    58 {
       
    59 }
       
    60 
       
    61 /*!
       
    62     Sets the configuration for the showing the snap line, it includes fade-in / fade-out timeout.
       
    63 */
       
    64 void HsSnapLine::setConfiguration(const QVariantHash &configuration)
       
    65 {
       
    66     bool canConvert = false;
       
    67     //The following values should be in int, so the status received in canConvert is ignored
       
    68     mFadeInAnimationDuration = configuration[SNAPLINEFADEINDURATION].toInt(&canConvert);
       
    69     mFadeOutAnimationDuration = configuration[SNAPLINEFADEOUTDURATION].toInt(&canConvert);
       
    70 }
       
    71 
       
    72 /*!
       
    73     Show the snap line. fade-in animation is started on the line if the line is positioned at a different place.
       
    74     Before starting the fade-in animation, the fade-out animation is stoped if it is running.
       
    75 */
       
    76 void HsSnapLine::showLine(const QLineF &snapLine)
       
    77 {
       
    78     QLineF displayLine = snapLine;
       
    79     qreal angle = displayLine.angle();
       
    80     if (qAbs(angle) == 0.0 || qAbs(angle) == 180.0) { //this is a horizontal line
       
    81         //adding 1 is required below, as the line is 3 pixels wide and is translated by 1 point before displaying
       
    82         if (displayLine.y1() != (line().y1()+1.0) ) { //this horizontal line is at new position horizontally
       
    83             if (isFadeOutAnimationRunning()) { //if fade-out animation is running, stop it, animation is running at old position
       
    84                 stopFadeOutAnimation();
       
    85             }
       
    86             //start fade-in animation at new position.
       
    87             startFadeInAnimation();
       
    88         }
       
    89         else { //this horizontal line is at the old position horizontally
       
    90             if (isFadeOutAnimationRunning()) { //if fade-out animation is running, stop it, animation is running at old position
       
    91                 stopFadeOutAnimation();
       
    92                 //start fade-in animation at the old position
       
    93                 startFadeInAnimation();
       
    94             }
       
    95         }
       
    96         displayLine.translate(0.0, -1.0);
       
    97     }
       
    98     if (qAbs(angle) == 90.0 || qAbs(angle) == 270.0) { //this is a vertical line
       
    99         if (displayLine.x1() != (line().x1()+1)) { //this Vertical line is at different position vertically
       
   100             if (isFadeOutAnimationRunning()) {
       
   101                 stopFadeOutAnimation();
       
   102             }
       
   103             startFadeInAnimation();
       
   104         }
       
   105         else {
       
   106             if (isFadeOutAnimationRunning()) {
       
   107                 stopFadeOutAnimation();
       
   108                 startFadeInAnimation();
       
   109             }
       
   110         }
       
   111         displayLine.translate(-1.0, 0.0);
       
   112     }
       
   113 
       
   114     setLine(displayLine);
       
   115     show();
       
   116 }
       
   117 
       
   118 /*!
       
   119     Hide the snap line. Fade-out animation is started on the line to be hidden.
       
   120     If fade-in animation is running, it is stoped before starting the fade-out animation.
       
   121 */
       
   122 void HsSnapLine::hideLine()
       
   123 {
       
   124     if (isFadeInAnimationRunning()) {
       
   125         stopFadeInAnimation();
       
   126     }
       
   127     startFadeOutAnimation();
       
   128 }
       
   129 
       
   130 /*!
       
   131     Start fade-in animation.
       
   132 */
       
   133 void HsSnapLine::startFadeInAnimation()
       
   134 {
       
   135     mFadeInAnimation->setStartValue(mOpacity);
       
   136     mFadeInAnimation->setEndValue(1.0);
       
   137     mFadeInAnimation->setDuration(getFadeInDuration());
       
   138 
       
   139     graphicsEffect()->setEnabled(true);
       
   140     mFadeInAnimation->start();
       
   141 }
       
   142 
       
   143 /*!
       
   144     Check if fade-in animation is running.
       
   145 */
       
   146 bool HsSnapLine::isFadeInAnimationRunning() const
       
   147 {
       
   148     return mFadeInAnimation->state() == QAbstractAnimation::Running;
       
   149 }
       
   150 
       
   151 /*!
       
   152     Stop the fade-in animation.
       
   153 */
       
   154 void HsSnapLine::stopFadeInAnimation()
       
   155 {
       
   156     mFadeInAnimation->stop();
       
   157     actionOnFadeInAnimationStop();
       
   158 }
       
   159 
       
   160 /*!
       
   161     SLOT called when fade-in animation is finished / reaches it's end.
       
   162 */
       
   163 void HsSnapLine::fadeInAnimationFinished()
       
   164 {
       
   165     actionOnFadeInAnimationStop();
       
   166 }
       
   167 
       
   168 /*!
       
   169     Action when fade-in animation stops running.
       
   170 */
       
   171 void HsSnapLine::actionOnFadeInAnimationStop()
       
   172 {
       
   173     mOpacity = mFadeInAnimation->currentValue().toDouble();
       
   174     graphicsEffect()->setEnabled(false);
       
   175 }
       
   176 
       
   177 /*!
       
   178     Start fade-out animation.
       
   179 */
       
   180 void HsSnapLine::startFadeOutAnimation()
       
   181 {
       
   182     mFadeOutAnimation->setStartValue(mOpacity);
       
   183     mFadeOutAnimation->setEndValue(0.0);
       
   184     mFadeOutAnimation->setDuration(getFadeOutDuration());
       
   185 
       
   186     graphicsEffect()->setEnabled(true);
       
   187     mFadeOutAnimation->start();
       
   188 }
       
   189 
       
   190 /*!
       
   191     Check if fade-out animation is running.
       
   192 */
       
   193 bool HsSnapLine::isFadeOutAnimationRunning() const
       
   194 {
       
   195     return mFadeOutAnimation->state() == QAbstractAnimation::Running;
       
   196 }
       
   197 
       
   198 /*!
       
   199     Stop the fade-out animation.
       
   200 */
       
   201 void HsSnapLine::stopFadeOutAnimation()
       
   202 {
       
   203     mFadeOutAnimation->stop();
       
   204     actionOnFadeOutAnimationStop();
       
   205 }
       
   206 
       
   207 /*!
       
   208     SLOT called when fade-out animation is finished / reaches it's end.
       
   209 */
       
   210 void HsSnapLine::fadeOutAnimationFinished()
       
   211 {
       
   212     actionOnFadeOutAnimationStop();
       
   213 }
       
   214 
       
   215 /*!
       
   216     Action when fade-out animation stops running.
       
   217 */
       
   218 void HsSnapLine::actionOnFadeOutAnimationStop()
       
   219 {
       
   220     mOpacity = mFadeOutAnimation->currentValue().toDouble();
       
   221     graphicsEffect()->setEnabled(false);
       
   222     hide();
       
   223     setLine(QLineF());
       
   224 }