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