homescreenapp/hsutils/src/hssnapline.cpp
author hgs
Fri, 23 Jul 2010 13:47:57 +0300
changeset 69 87476091b3f5
parent 62 341166945d65
child 81 7dd137878ff8
permissions -rw-r--r--
201029
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
#include <QGraphicsOpacityEffect>
hgs
parents:
diff changeset
    19
#include <QPropertyAnimation>
69
hgs
parents: 62
diff changeset
    20
#include <hbcolorscheme.h>
62
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
#include "hsapp_defs.h"
69
hgs
parents: 62
diff changeset
    23
#include "hssnapline.h"
62
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
/*!
hgs
parents:
diff changeset
    26
    Constructor.
hgs
parents:
diff changeset
    27
    
hgs
parents:
diff changeset
    28
    \a parent Owner.
hgs
parents:
diff changeset
    29
*/
hgs
parents:
diff changeset
    30
HsSnapLine::HsSnapLine(QGraphicsItem *parent)
hgs
parents:
diff changeset
    31
  : QGraphicsLineItem(parent),
hgs
parents:
diff changeset
    32
    mFadeInAnimation(0),
hgs
parents:
diff changeset
    33
    mFadeOutAnimation(0),
hgs
parents:
diff changeset
    34
    mOpacity(0.0),
hgs
parents:
diff changeset
    35
    mFadeInAnimationDuration(0),
hgs
parents:
diff changeset
    36
    mFadeOutAnimationDuration(0)
hgs
parents:
diff changeset
    37
{
hgs
parents:
diff changeset
    38
    QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(this);
hgs
parents:
diff changeset
    39
    effect->setOpacity(0.0);
hgs
parents:
diff changeset
    40
    effect->setEnabled(false);
hgs
parents:
diff changeset
    41
    setGraphicsEffect(effect);
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
    mFadeInAnimation = new QPropertyAnimation(graphicsEffect(), "opacity", this);
hgs
parents:
diff changeset
    44
    connect(mFadeInAnimation, SIGNAL(finished()), SLOT(fadeInAnimationFinished()));
hgs
parents:
diff changeset
    45
hgs
parents:
diff changeset
    46
    mFadeOutAnimation = new QPropertyAnimation(graphicsEffect(), "opacity", this);
hgs
parents:
diff changeset
    47
    connect(mFadeOutAnimation, SIGNAL(finished()), SLOT(fadeOutAnimationFinished()));
hgs
parents:
diff changeset
    48
}
hgs
parents:
diff changeset
    49
 
hgs
parents:
diff changeset
    50
/*!
hgs
parents:
diff changeset
    51
    Destructor.
hgs
parents:
diff changeset
    52
*/
hgs
parents:
diff changeset
    53
HsSnapLine::~HsSnapLine()
hgs
parents:
diff changeset
    54
{
hgs
parents:
diff changeset
    55
}
hgs
parents:
diff changeset
    56
hgs
parents:
diff changeset
    57
/*!
hgs
parents:
diff changeset
    58
    Sets the configuration for the showing the snap line, it includes fade-in / fade-out timeout.
hgs
parents:
diff changeset
    59
*/
hgs
parents:
diff changeset
    60
void HsSnapLine::setConfiguration(const QVariantHash &configuration)
hgs
parents:
diff changeset
    61
{
hgs
parents:
diff changeset
    62
    bool canConvert = false;
hgs
parents:
diff changeset
    63
    //The following values should be in int, so the status received in canConvert is ignored
hgs
parents:
diff changeset
    64
    mFadeInAnimationDuration = configuration[SNAPLINEFADEINDURATION].toInt(&canConvert);
hgs
parents:
diff changeset
    65
    mFadeOutAnimationDuration = configuration[SNAPLINEFADEOUTDURATION].toInt(&canConvert);
hgs
parents:
diff changeset
    66
}
hgs
parents:
diff changeset
    67
hgs
parents:
diff changeset
    68
/*!
hgs
parents:
diff changeset
    69
    Show the snap line. fade-in animation is started on the line if the line is positioned at a different place.
hgs
parents:
diff changeset
    70
    Before starting the fade-in animation, the fade-out animation is stoped if it is running.
hgs
parents:
diff changeset
    71
*/
hgs
parents:
diff changeset
    72
void HsSnapLine::showLine(const QLineF &snapLine)
hgs
parents:
diff changeset
    73
{
hgs
parents:
diff changeset
    74
    QLineF displayLine = snapLine;
hgs
parents:
diff changeset
    75
    qreal angle = displayLine.angle();
hgs
parents:
diff changeset
    76
    if (qAbs(angle) == 0.0 || qAbs(angle) == 180.0) { //this is a horizontal line
hgs
parents:
diff changeset
    77
        //adding 1 is required below, as the line is 3 pixels wide and is translated by 1 point before displaying
hgs
parents:
diff changeset
    78
        if (displayLine.y1() != (line().y1()+1.0) ) { //this horizontal line is at new position horizontally
hgs
parents:
diff changeset
    79
            if (isFadeOutAnimationRunning()) { //if fade-out animation is running, stop it, animation is running at old position
hgs
parents:
diff changeset
    80
                stopFadeOutAnimation();
hgs
parents:
diff changeset
    81
            }
hgs
parents:
diff changeset
    82
            //start fade-in animation at new position.
hgs
parents:
diff changeset
    83
            startFadeInAnimation();
hgs
parents:
diff changeset
    84
        }
hgs
parents:
diff changeset
    85
        else { //this horizontal line is at the old position horizontally
hgs
parents:
diff changeset
    86
            if (isFadeOutAnimationRunning()) { //if fade-out animation is running, stop it, animation is running at old position
hgs
parents:
diff changeset
    87
                stopFadeOutAnimation();
hgs
parents:
diff changeset
    88
                //start fade-in animation at the old position
hgs
parents:
diff changeset
    89
                startFadeInAnimation();
hgs
parents:
diff changeset
    90
            }
hgs
parents:
diff changeset
    91
        }
hgs
parents:
diff changeset
    92
        displayLine.translate(0.0, -1.0);
hgs
parents:
diff changeset
    93
    }
hgs
parents:
diff changeset
    94
    if (qAbs(angle) == 90.0 || qAbs(angle) == 270.0) { //this is a vertical line
hgs
parents:
diff changeset
    95
        if (displayLine.x1() != (line().x1()+1)) { //this Vertical line is at different position vertically
hgs
parents:
diff changeset
    96
            if (isFadeOutAnimationRunning()) {
hgs
parents:
diff changeset
    97
                stopFadeOutAnimation();
hgs
parents:
diff changeset
    98
            }
hgs
parents:
diff changeset
    99
            startFadeInAnimation();
hgs
parents:
diff changeset
   100
        }
hgs
parents:
diff changeset
   101
        else {
hgs
parents:
diff changeset
   102
            if (isFadeOutAnimationRunning()) {
hgs
parents:
diff changeset
   103
                stopFadeOutAnimation();
hgs
parents:
diff changeset
   104
                startFadeInAnimation();
hgs
parents:
diff changeset
   105
            }
hgs
parents:
diff changeset
   106
        }
hgs
parents:
diff changeset
   107
        displayLine.translate(-1.0, 0.0);
hgs
parents:
diff changeset
   108
    }
hgs
parents:
diff changeset
   109
69
hgs
parents: 62
diff changeset
   110
    QLinearGradient gradient(displayLine.p1(), displayLine.p2());
hgs
parents: 62
diff changeset
   111
    gradient.setColorAt(0.0, Qt::white);
hgs
parents: 62
diff changeset
   112
    QColor snapLineColor = HbColorScheme::color("qtc_hs_snapguide");
hgs
parents: 62
diff changeset
   113
    if (!snapLineColor.isValid()) {
hgs
parents: 62
diff changeset
   114
        //if valid color is not loaded from the theme, the darkCyan color is used as a backup.color
hgs
parents: 62
diff changeset
   115
        snapLineColor = Qt::darkCyan;
hgs
parents: 62
diff changeset
   116
    }
hgs
parents: 62
diff changeset
   117
    gradient.setColorAt(0.4, snapLineColor);
hgs
parents: 62
diff changeset
   118
    gradient.setColorAt(0.6, snapLineColor);
hgs
parents: 62
diff changeset
   119
    gradient.setColorAt(1.0, Qt::white);
hgs
parents: 62
diff changeset
   120
    QBrush brush(gradient);
hgs
parents: 62
diff changeset
   121
    QPen pen;
hgs
parents: 62
diff changeset
   122
    pen.setWidth(3);
hgs
parents: 62
diff changeset
   123
    pen.setCapStyle(Qt::RoundCap);
hgs
parents: 62
diff changeset
   124
    pen.setBrush(brush);
hgs
parents: 62
diff changeset
   125
    setPen(pen);
hgs
parents: 62
diff changeset
   126
62
hgs
parents:
diff changeset
   127
    setLine(displayLine);
hgs
parents:
diff changeset
   128
    show();
hgs
parents:
diff changeset
   129
}
hgs
parents:
diff changeset
   130
hgs
parents:
diff changeset
   131
/*!
hgs
parents:
diff changeset
   132
    Hide the snap line. Fade-out animation is started on the line to be hidden.
hgs
parents:
diff changeset
   133
    If fade-in animation is running, it is stoped before starting the fade-out animation.
hgs
parents:
diff changeset
   134
*/
hgs
parents:
diff changeset
   135
void HsSnapLine::hideLine()
hgs
parents:
diff changeset
   136
{
hgs
parents:
diff changeset
   137
    if (isFadeInAnimationRunning()) {
hgs
parents:
diff changeset
   138
        stopFadeInAnimation();
hgs
parents:
diff changeset
   139
    }
hgs
parents:
diff changeset
   140
    startFadeOutAnimation();
hgs
parents:
diff changeset
   141
}
hgs
parents:
diff changeset
   142
hgs
parents:
diff changeset
   143
/*!
hgs
parents:
diff changeset
   144
    Start fade-in animation.
hgs
parents:
diff changeset
   145
*/
hgs
parents:
diff changeset
   146
void HsSnapLine::startFadeInAnimation()
hgs
parents:
diff changeset
   147
{
hgs
parents:
diff changeset
   148
    mFadeInAnimation->setStartValue(mOpacity);
hgs
parents:
diff changeset
   149
    mFadeInAnimation->setEndValue(1.0);
hgs
parents:
diff changeset
   150
    mFadeInAnimation->setDuration(getFadeInDuration());
hgs
parents:
diff changeset
   151
hgs
parents:
diff changeset
   152
    graphicsEffect()->setEnabled(true);
hgs
parents:
diff changeset
   153
    mFadeInAnimation->start();
hgs
parents:
diff changeset
   154
}
hgs
parents:
diff changeset
   155
hgs
parents:
diff changeset
   156
/*!
hgs
parents:
diff changeset
   157
    Check if fade-in animation is running.
hgs
parents:
diff changeset
   158
*/
hgs
parents:
diff changeset
   159
bool HsSnapLine::isFadeInAnimationRunning() const
hgs
parents:
diff changeset
   160
{
hgs
parents:
diff changeset
   161
    return mFadeInAnimation->state() == QAbstractAnimation::Running;
hgs
parents:
diff changeset
   162
}
hgs
parents:
diff changeset
   163
hgs
parents:
diff changeset
   164
/*!
hgs
parents:
diff changeset
   165
    Stop the fade-in animation.
hgs
parents:
diff changeset
   166
*/
hgs
parents:
diff changeset
   167
void HsSnapLine::stopFadeInAnimation()
hgs
parents:
diff changeset
   168
{
hgs
parents:
diff changeset
   169
    mFadeInAnimation->stop();
hgs
parents:
diff changeset
   170
    actionOnFadeInAnimationStop();
hgs
parents:
diff changeset
   171
}
hgs
parents:
diff changeset
   172
hgs
parents:
diff changeset
   173
/*!
hgs
parents:
diff changeset
   174
    SLOT called when fade-in animation is finished / reaches it's end.
hgs
parents:
diff changeset
   175
*/
hgs
parents:
diff changeset
   176
void HsSnapLine::fadeInAnimationFinished()
hgs
parents:
diff changeset
   177
{
hgs
parents:
diff changeset
   178
    actionOnFadeInAnimationStop();
hgs
parents:
diff changeset
   179
}
hgs
parents:
diff changeset
   180
hgs
parents:
diff changeset
   181
/*!
hgs
parents:
diff changeset
   182
    Action when fade-in animation stops running.
hgs
parents:
diff changeset
   183
*/
hgs
parents:
diff changeset
   184
void HsSnapLine::actionOnFadeInAnimationStop()
hgs
parents:
diff changeset
   185
{
hgs
parents:
diff changeset
   186
    mOpacity = mFadeInAnimation->currentValue().toDouble();
hgs
parents:
diff changeset
   187
    graphicsEffect()->setEnabled(false);
hgs
parents:
diff changeset
   188
}
hgs
parents:
diff changeset
   189
hgs
parents:
diff changeset
   190
/*!
hgs
parents:
diff changeset
   191
    Start fade-out animation.
hgs
parents:
diff changeset
   192
*/
hgs
parents:
diff changeset
   193
void HsSnapLine::startFadeOutAnimation()
hgs
parents:
diff changeset
   194
{
hgs
parents:
diff changeset
   195
    mFadeOutAnimation->setStartValue(mOpacity);
hgs
parents:
diff changeset
   196
    mFadeOutAnimation->setEndValue(0.0);
hgs
parents:
diff changeset
   197
    mFadeOutAnimation->setDuration(getFadeOutDuration());
hgs
parents:
diff changeset
   198
hgs
parents:
diff changeset
   199
    graphicsEffect()->setEnabled(true);
hgs
parents:
diff changeset
   200
    mFadeOutAnimation->start();
hgs
parents:
diff changeset
   201
}
hgs
parents:
diff changeset
   202
hgs
parents:
diff changeset
   203
/*!
hgs
parents:
diff changeset
   204
    Check if fade-out animation is running.
hgs
parents:
diff changeset
   205
*/
hgs
parents:
diff changeset
   206
bool HsSnapLine::isFadeOutAnimationRunning() const
hgs
parents:
diff changeset
   207
{
hgs
parents:
diff changeset
   208
    return mFadeOutAnimation->state() == QAbstractAnimation::Running;
hgs
parents:
diff changeset
   209
}
hgs
parents:
diff changeset
   210
hgs
parents:
diff changeset
   211
/*!
hgs
parents:
diff changeset
   212
    Stop the fade-out animation.
hgs
parents:
diff changeset
   213
*/
hgs
parents:
diff changeset
   214
void HsSnapLine::stopFadeOutAnimation()
hgs
parents:
diff changeset
   215
{
hgs
parents:
diff changeset
   216
    mFadeOutAnimation->stop();
hgs
parents:
diff changeset
   217
    actionOnFadeOutAnimationStop();
hgs
parents:
diff changeset
   218
}
hgs
parents:
diff changeset
   219
hgs
parents:
diff changeset
   220
/*!
hgs
parents:
diff changeset
   221
    SLOT called when fade-out animation is finished / reaches it's end.
hgs
parents:
diff changeset
   222
*/
hgs
parents:
diff changeset
   223
void HsSnapLine::fadeOutAnimationFinished()
hgs
parents:
diff changeset
   224
{
hgs
parents:
diff changeset
   225
    actionOnFadeOutAnimationStop();
hgs
parents:
diff changeset
   226
}
hgs
parents:
diff changeset
   227
hgs
parents:
diff changeset
   228
/*!
hgs
parents:
diff changeset
   229
    Action when fade-out animation stops running.
hgs
parents:
diff changeset
   230
*/
hgs
parents:
diff changeset
   231
void HsSnapLine::actionOnFadeOutAnimationStop()
hgs
parents:
diff changeset
   232
{
hgs
parents:
diff changeset
   233
    mOpacity = mFadeOutAnimation->currentValue().toDouble();
hgs
parents:
diff changeset
   234
    graphicsEffect()->setEnabled(false);
hgs
parents:
diff changeset
   235
    hide();
hgs
parents:
diff changeset
   236
    setLine(QLineF());
hgs
parents:
diff changeset
   237
}