62
|
1 |
/*
|
|
2 |
* Copyright (c) 2009-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: Base class container.
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include "snsrbigclockcontainer.h"
|
|
19 |
|
|
20 |
#include <QPainter>
|
|
21 |
#include <QDebug>
|
|
22 |
#include <QTime>
|
|
23 |
#include <QGraphicsLinearLayout>
|
|
24 |
|
69
|
25 |
#include <HbEvent>
|
|
26 |
#include <HbColorScheme>
|
|
27 |
#include <HbMainWindow>
|
62
|
28 |
|
|
29 |
#include "snsrbigclockcontainer.h"
|
|
30 |
#include "snsrindicatorwidget.h"
|
|
31 |
#include "snsrindicatormodel.h"
|
|
32 |
|
|
33 |
/*!
|
|
34 |
\class SnsrBigClockContainer
|
|
35 |
\ingroup group_snsrbigclockscreensaverplugin
|
|
36 |
\brief Base class. Container used for drawing background and for preparing layout.
|
|
37 |
*/
|
|
38 |
|
|
39 |
const QString snsrBackgroundColorRole("snsrbackground");
|
|
40 |
const int gStep(5);
|
|
41 |
|
|
42 |
|
|
43 |
/*!
|
|
44 |
Constructs a new SnsrBigClockContainer.
|
|
45 |
*/
|
|
46 |
SnsrBigClockContainer::SnsrBigClockContainer() :
|
|
47 |
mBackgroundContainerLayout(0),
|
|
48 |
mMainView(0),
|
|
49 |
mMainContainer(0),
|
|
50 |
mIndicatorModel(0),
|
|
51 |
mIndicatorWidget(0),
|
|
52 |
mCurrentOrientation(-1)
|
|
53 |
{
|
|
54 |
setBackgroundColor();
|
69
|
55 |
mBackgroundContainerLayout = new QGraphicsLinearLayout(Qt::Vertical, this);
|
62
|
56 |
setLayout(mBackgroundContainerLayout);
|
|
57 |
qsrand(QTime::currentTime().msec());
|
|
58 |
}
|
|
59 |
|
|
60 |
/*!
|
|
61 |
Destructs the class.
|
|
62 |
*/
|
|
63 |
SnsrBigClockContainer::~SnsrBigClockContainer()
|
69
|
64 |
{
|
62
|
65 |
// e.g. mIndicatorWidget gets deleted during these calls
|
|
66 |
mDocumentLoader.reset();
|
|
67 |
qDeleteAll(mDocumentObjects);
|
|
68 |
mDocumentObjects.clear();
|
|
69 |
|
|
70 |
//mMainContainer,mBackgroundContainerLayout - deleted by the parent
|
|
71 |
mIndicatorModel=0; // not owned
|
|
72 |
}
|
|
73 |
|
|
74 |
/*!
|
|
75 |
\fn virtual void update() = 0;
|
|
76 |
|
|
77 |
Slot for members update in container e.g. when time or date changed.
|
|
78 |
*/
|
|
79 |
|
|
80 |
/*!
|
69
|
81 |
Changes screensaver layout based on orientation changes.
|
|
82 |
\param orientation Current orientation.
|
|
83 |
*/
|
|
84 |
void SnsrBigClockContainer::changeLayout(Qt::Orientation orientation)
|
|
85 |
{
|
|
86 |
SCREENSAVER_TEST_FUNC_ENTRY("SnsrBigClockContainer::changeLayout")
|
|
87 |
|
|
88 |
if (mCurrentOrientation != orientation) {
|
|
89 |
mCurrentOrientation = orientation;
|
|
90 |
|
|
91 |
// delete any old widgets
|
|
92 |
if ( mBackgroundContainerLayout->count() ) {
|
|
93 |
mBackgroundContainerLayout->removeAt(0);
|
|
94 |
}
|
|
95 |
mDocumentLoader.reset();
|
|
96 |
qDeleteAll(mDocumentObjects);
|
|
97 |
mDocumentObjects.clear();
|
|
98 |
|
|
99 |
// reload widgets from docml
|
|
100 |
loadWidgets();
|
|
101 |
}
|
|
102 |
mBackgroundContainerLayout->setGeometry( mainWindow()->layoutRect() );
|
|
103 |
update();
|
|
104 |
|
|
105 |
SCREENSAVER_TEST_FUNC_EXIT("SnsrBigClockContainer::changeLayout")
|
|
106 |
}
|
62
|
107 |
|
69
|
108 |
/*!
|
|
109 |
Set used indicator model that is owned by the screensaver class.
|
|
110 |
Model's life cycle must be the same as screensaver's so that indicators'
|
|
111 |
status data can be kept in memory and one can receive updates.
|
|
112 |
This method should be called when the current container is set.
|
|
113 |
*/
|
|
114 |
void SnsrBigClockContainer::setIndicatorModel(SnsrIndicatorModel &model)
|
|
115 |
{
|
|
116 |
mIndicatorModel = &model;
|
|
117 |
}
|
|
118 |
|
|
119 |
/*!
|
|
120 |
@copydoc Screensaver::getActiveScreenRows()
|
62
|
121 |
*/
|
69
|
122 |
void SnsrBigClockContainer::getActiveScreenRows(int *firstActiveRow, int *lastActiveRow)
|
|
123 |
{
|
|
124 |
// This default implementation return the whole area of the
|
|
125 |
// container. Inherited low power mode containers can and should
|
|
126 |
// return smaller area which just barely encloses all the content.
|
|
127 |
if ( mMainContainer ) {
|
|
128 |
QRect mainRect = mMainContainer->rect().toRect();
|
|
129 |
if ( mCurrentOrientation == Qt::Vertical ) {
|
|
130 |
*firstActiveRow = mainRect.top();
|
|
131 |
*lastActiveRow = mainRect.bottom();
|
|
132 |
}
|
|
133 |
else {
|
|
134 |
*firstActiveRow = mainRect.left();
|
|
135 |
*lastActiveRow = mainRect.right();
|
|
136 |
}
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
/*!
|
|
141 |
Tell if this container wants to lock the screen orientation.
|
|
142 |
Default implementation in not locked but inherited classes may
|
|
143 |
override this.
|
|
144 |
*/
|
|
145 |
bool SnsrBigClockContainer::isOrientationLocked()
|
|
146 |
{
|
|
147 |
return false;
|
|
148 |
}
|
62
|
149 |
|
|
150 |
/*!
|
|
151 |
\fn virtual int updateIntervalInMilliseconds() = 0;
|
|
152 |
|
|
153 |
Concrete inherited container classes must implement this to return
|
|
154 |
the desired update interval for that clock mode.
|
|
155 |
*/
|
|
156 |
|
|
157 |
/*!
|
69
|
158 |
\fn virtual int loadWidgets() = 0;
|
|
159 |
|
|
160 |
Concrete inherited container classes must implement this to instantiate
|
|
161 |
all the widgets shown in the container. The base class calls this
|
|
162 |
method when screen layuot is changed. The old widgets are already
|
|
163 |
deleted by the base class before this is called. Also changing the visible
|
|
164 |
container is treated as a layout change, and results in call to this method.
|
|
165 |
Thus, inherited containers don't have to load their widgets yet in their
|
|
166 |
constructors.
|
62
|
167 |
*/
|
|
168 |
|
|
169 |
/*!
|
|
170 |
\reimp
|
|
171 |
*/
|
|
172 |
void SnsrBigClockContainer::changeEvent(QEvent *event)
|
|
173 |
{
|
|
174 |
if (event->type() == HbEvent::ThemeChanged) {
|
|
175 |
setBackgroundColor();
|
|
176 |
}
|
|
177 |
return QGraphicsWidget::changeEvent(event);
|
|
178 |
}
|
|
179 |
|
|
180 |
/*!
|
|
181 |
Returns random point for given range.
|
|
182 |
\param rect Area within which the generated point will be.
|
|
183 |
*/
|
|
184 |
QPointF SnsrBigClockContainer::randomPosition(const QRectF &rect)
|
|
185 |
{
|
|
186 |
int width( rect.width() );
|
|
187 |
int height( rect.height() );
|
|
188 |
if ( width > 0 && height > 0 ) {
|
|
189 |
return rect.topLeft() + QPointF( qrand()%width, qrand()%height );
|
|
190 |
}
|
|
191 |
else {
|
|
192 |
return QPointF();
|
|
193 |
}
|
|
194 |
|
|
195 |
}
|
|
196 |
|
|
197 |
/*!
|
|
198 |
Returns new position between curRect position and destPos position.
|
|
199 |
\param curPos Current position.
|
|
200 |
\param destPos Destination position. When current position is near this
|
|
201 |
position or outside of the container, a new value is generated.
|
|
202 |
\param containerRect The container within which the destination position will always
|
|
203 |
be after a call.
|
|
204 |
*/
|
|
205 |
QPointF SnsrBigClockContainer::nextRandomPosition(const QPointF &curPos, QPointF &destPos, const QRectF &containerRect)
|
|
206 |
{
|
|
207 |
const int delta(gStep+2);
|
|
208 |
const int minCntDimension(3*delta);
|
|
209 |
|
|
210 |
// The random movement logic can work only if the container has enough space to move the
|
|
211 |
// clock around. If the container is too small, just return the middle point of the container.
|
|
212 |
if ( containerRect.width() < minCntDimension && containerRect.height() < minCntDimension ) {
|
|
213 |
return containerRect.center();
|
|
214 |
}
|
|
215 |
|
|
216 |
int xDistance = abs( destPos.x() - curPos.x() );
|
|
217 |
int yDistance = abs( destPos.y() - curPos.y() );
|
|
218 |
|
|
219 |
// Generate new destination position when current widget position is close to
|
|
220 |
// destination random position or when current destination position is out of bounds.
|
|
221 |
// It is possible that the new random position is very close to the current position,
|
|
222 |
// in which case the random position is generated again.
|
|
223 |
// It is paramount that container is large enough when next loop is entered
|
|
224 |
// to prevent infinite looping.
|
|
225 |
while ( (xDistance < delta && yDistance < delta)
|
|
226 |
|| !containerRect.contains(destPos) ) {
|
|
227 |
destPos = randomPosition( containerRect );
|
|
228 |
xDistance = abs( destPos.x() - curPos.x() );
|
|
229 |
yDistance = abs( destPos.y() - curPos.y() );
|
|
230 |
}
|
|
231 |
|
|
232 |
// If, for some reason, the current position is out-of-bounds, then there's no
|
|
233 |
// point to slowly move towards the destination. In that case, move immediately
|
|
234 |
// to destination point.
|
|
235 |
if ( !containerRect.contains(curPos) ) {
|
|
236 |
return destPos;
|
|
237 |
}
|
|
238 |
// Otherwise, this is normal case and we will calculate a point which is just
|
|
239 |
// a bit closer to the destination.
|
|
240 |
|
|
241 |
// Version 1:
|
|
242 |
// o-----o---------------------------o
|
|
243 |
// p1 p2 p3
|
|
244 |
//
|
|
245 |
// Version 2:
|
|
246 |
// o---------------------------o-----o
|
|
247 |
// p3 p2 p1
|
|
248 |
//
|
|
249 |
// p1 - current widget position
|
|
250 |
// p2 - next position to compute
|
|
251 |
// p3 - destination random position
|
|
252 |
QPointF p1(curPos);
|
|
253 |
QPointF p2(0,0);
|
|
254 |
QPointF p3(destPos);
|
|
255 |
|
|
256 |
// Computes point p2 - new position between p1 and p3
|
|
257 |
|
|
258 |
// Move the coordinate which is further away from the destination
|
|
259 |
// and calculate the other coordinate from that so that the
|
|
260 |
// result point p2 lies on the straigth line between p1 and p3.
|
|
261 |
if ( yDistance > xDistance ) {
|
|
262 |
if (p3.y() > p1.y()) {
|
|
263 |
p2.setY(p1.y()+gStep);
|
|
264 |
}
|
|
265 |
else {
|
|
266 |
p2.setY(p1.y()-gStep);
|
|
267 |
}
|
|
268 |
p2.setX((((p2.y()-p1.y())*(p3.x()-p1.x())) / (p3.y()-p1.y())) + p1.x()); // x2 = (((y2-y1)*(x3-x1)) / (y3-y1)) + x1
|
|
269 |
}
|
|
270 |
else {
|
|
271 |
if (p3.x() > p1.x()) {
|
|
272 |
p2.setX(p1.x()+gStep);
|
|
273 |
}
|
|
274 |
else {
|
|
275 |
p2.setX(p1.x()-gStep);
|
|
276 |
}
|
|
277 |
p2.setY((((p3.y()-p1.y())*(p2.x()-p1.x())) / (p3.x()-p1.x())) + p1.y()); // y2 = (((y3-y1)*(x2-x1)) / (x3-x1)) + y1
|
|
278 |
}
|
|
279 |
|
|
280 |
// Return new position between points p1 and p3.
|
|
281 |
return p2;
|
|
282 |
}
|
|
283 |
|
|
284 |
/*!
|
69
|
285 |
Do necessary initializations to show currently active indicators.
|
|
286 |
Should be called after the indicator widget is created.
|
|
287 |
*/
|
|
288 |
void SnsrBigClockContainer::initIndicatorWidget()
|
|
289 |
{
|
|
290 |
Q_ASSERT(mIndicatorModel && mIndicatorWidget);
|
|
291 |
|
|
292 |
connect(mIndicatorModel, SIGNAL(indicatorsUpdated(QList<SnsrIndicatorInfo>)),
|
|
293 |
mIndicatorWidget, SLOT(showIndicators(QList<SnsrIndicatorInfo>)));
|
|
294 |
|
|
295 |
connect(mIndicatorModel, SIGNAL(allIndicatorsDeactivated()),
|
|
296 |
mIndicatorWidget, SLOT(removeAllIndicators()));
|
|
297 |
|
|
298 |
mIndicatorModel->initializeIndicatorWidget();
|
|
299 |
}
|
|
300 |
|
|
301 |
/*!
|
62
|
302 |
Disconnect connections between indicator model and widget.
|
69
|
303 |
Should be called before deleting the indicator widget.
|
62
|
304 |
*/
|
|
305 |
void SnsrBigClockContainer::resetIndicatorConnections()
|
69
|
306 |
{
|
|
307 |
if (mIndicatorWidget && mIndicatorModel) {
|
|
308 |
disconnect(mIndicatorModel, SIGNAL(indicatorsUpdated(QList<SnsrIndicatorInfo>)),
|
|
309 |
mIndicatorWidget, SLOT(showIndicators(QList<SnsrIndicatorInfo>)));
|
|
310 |
|
|
311 |
disconnect(mIndicatorModel, SIGNAL(allIndicatorsDeactivated()),
|
|
312 |
mIndicatorWidget, SLOT(removeAllIndicators()));
|
|
313 |
}
|
62
|
314 |
}
|
|
315 |
|
|
316 |
/*!
|
|
317 |
Set background color.
|
|
318 |
*/
|
|
319 |
void SnsrBigClockContainer::setBackgroundColor()
|
|
320 |
{
|
|
321 |
QColor backgroundColor = HbColorScheme::color(snsrBackgroundColorRole);
|
|
322 |
if (backgroundColor.isValid()) {
|
|
323 |
mBackgroundColor = backgroundColor;
|
|
324 |
}
|
|
325 |
else {
|
|
326 |
mBackgroundColor = Qt::black;
|
|
327 |
}
|
|
328 |
}
|
|
329 |
|
|
330 |
/*!
|
|
331 |
Paints the contents of an item in local coordinates.
|
|
332 |
*/
|
|
333 |
#ifdef COVERAGE_MEASUREMENT
|
|
334 |
#pragma CTC SKIP
|
|
335 |
#endif //COVERAGE_MEASUREMENT
|
|
336 |
void SnsrBigClockContainer::paint(
|
|
337 |
QPainter *painter,
|
|
338 |
const QStyleOptionGraphicsItem *option,
|
|
339 |
QWidget *widget
|
|
340 |
)
|
|
341 |
{
|
|
342 |
Q_UNUSED(option)
|
|
343 |
Q_UNUSED(widget)
|
|
344 |
|
|
345 |
painter->fillRect(rect(), mBackgroundColor);
|
|
346 |
}
|
|
347 |
#ifdef COVERAGE_MEASUREMENT
|
|
348 |
#pragma CTC ENDSKIP
|
|
349 |
#endif //COVERAGE_MEASUREMENT
|
|
350 |
|