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: Analog Clock Widget. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "snsranalogclockwidget.h" |
|
19 |
|
20 #include <QGraphicsSvgItem> |
|
21 #include <QTime> |
|
22 #include <QDebug> |
|
23 |
|
24 #include <HbStyleLoader> |
|
25 #include <HbIconItem> |
|
26 |
|
27 |
|
28 const char *gClockCssFilePath =":/style/snsranalogclockwidget.css"; |
|
29 const char *gClockWidgetMLFilePath = ":/style/snsranalogclockwidget.widgetml"; |
|
30 |
|
31 /*! |
|
32 \class SnsrAnalogClockWidget |
|
33 \ingroup group_snsrbigclockscreensaverprovider |
|
34 \brief Screensaver analog clock widget. |
|
35 */ |
|
36 |
|
37 /*! |
|
38 Constructs a new SnsrAnalogClockWidget. |
|
39 \param parent Parent object. |
|
40 */ |
|
41 SnsrAnalogClockWidget::SnsrAnalogClockWidget(QGraphicsItem* parent): |
|
42 HbWidget(parent), |
|
43 mClockBackground(0), mClockHourHand(0), |
|
44 mClockMinuteHand(0), mClockSecondHand(0) |
|
45 { |
|
46 HbStyleLoader::registerFilePath(gClockCssFilePath); |
|
47 HbStyleLoader::registerFilePath(gClockWidgetMLFilePath); |
|
48 |
|
49 createPrimitives(); |
|
50 updatePrimitives(); |
|
51 } |
|
52 |
|
53 /*! |
|
54 Destructs the class. |
|
55 */ |
|
56 SnsrAnalogClockWidget::~SnsrAnalogClockWidget() |
|
57 { |
|
58 HbStyleLoader::unregisterFilePath( gClockCssFilePath ); |
|
59 HbStyleLoader::unregisterFilePath( gClockWidgetMLFilePath ); |
|
60 |
|
61 } |
|
62 |
|
63 void SnsrAnalogClockWidget::polish( HbStyleParameters& params ) |
|
64 { |
|
65 // TODO: updatePrimitives might not be needed to be called from here anymore (hb changes)? |
|
66 HbWidget::polish(params); |
|
67 updatePrimitives(); |
|
68 } |
|
69 |
|
70 /*! |
|
71 @copydoc HbWidget::resizeEvent() |
|
72 */ |
|
73 void SnsrAnalogClockWidget::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
74 { |
|
75 HbWidget::resizeEvent(event); |
|
76 updatePrimitives(); |
|
77 } |
|
78 |
|
79 /*! |
|
80 Creates all widget primitives. |
|
81 */ |
|
82 void SnsrAnalogClockWidget::createPrimitives() |
|
83 { |
|
84 // TODO: when final graphics are available in platform (qtg_graf_screensaver_clock_xxx) |
|
85 // use them instead. Also am/pm label should be added into the clock face. |
|
86 if (!mClockBackground) { |
|
87 mClockBackground = new HbIconItem(QLatin1String("qtg_graf_clock_day_bg"), this); |
|
88 HbStyle::setItemName(mClockBackground, QLatin1String("analog_clock_background")); |
|
89 } |
|
90 if (!mClockHourHand) { |
|
91 mClockHourHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_hour"), this); |
|
92 HbStyle::setItemName(mClockHourHand, QLatin1String("analog_clock_hour_hand")); |
|
93 } |
|
94 if (!mClockMinuteHand) { |
|
95 mClockMinuteHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_min"), this); |
|
96 HbStyle::setItemName(mClockMinuteHand, QLatin1String("analog_clock_minute_hand")); |
|
97 } |
|
98 if (!mClockSecondHand) { |
|
99 mClockSecondHand = new HbIconItem(QLatin1String("qtg_graf_clock_day_sec"), this); |
|
100 HbStyle::setItemName(mClockSecondHand, QLatin1String("analog_clock_second_hand")); |
|
101 } |
|
102 } |
|
103 |
|
104 /*! |
|
105 @copydoc HbWidget::updatePrimitives() |
|
106 */ |
|
107 void SnsrAnalogClockWidget::updatePrimitives() |
|
108 { |
|
109 if (!mClockHourHand || !mClockMinuteHand || !mClockSecondHand) { |
|
110 createPrimitives(); |
|
111 } |
|
112 |
|
113 // Calculate angles for clock hands. |
|
114 QTime time = QTime::currentTime(); |
|
115 qreal s = 6 * time.second(); |
|
116 qreal m = 6 * (time.minute() + s/360); |
|
117 qreal h = 30 * ((time.hour() % 12) + m/360); |
|
118 |
|
119 int x = mClockHourHand->preferredSize().width()/2; |
|
120 int y = mClockHourHand->preferredSize().height()/2; |
|
121 mClockHourHand->setTransform(QTransform().translate(x, y).rotate(h).translate(-x, -y)); |
|
122 |
|
123 x = mClockMinuteHand->preferredSize().width()/2; |
|
124 y = mClockMinuteHand->preferredSize().height()/2; |
|
125 mClockMinuteHand->setTransform(QTransform().translate(x, y).rotate(m).translate(-x, -y)); |
|
126 |
|
127 x = mClockSecondHand->preferredSize().width()/2; |
|
128 y = mClockSecondHand->preferredSize().height()/2; |
|
129 mClockSecondHand->setTransform(QTransform().translate(x, y).rotate(s).translate(-x, -y)); |
|
130 |
|
131 } |
|
132 |
|
133 /*! |
|
134 Updates primitives when time change. |
|
135 */ |
|
136 void SnsrAnalogClockWidget::tick() |
|
137 { |
|
138 updatePrimitives(); |
|
139 } |
|