62
|
1 |
/*
|
|
2 |
* Copyright (c) 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: Example of home screen widget
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
#include <QGraphicsLinearLayout>
|
|
19 |
#include <QPainter>
|
|
20 |
#include <QTimer>
|
|
21 |
#include "BatteryWidget.h"
|
|
22 |
|
|
23 |
|
|
24 |
/*!
|
|
25 |
\ingroup group_helloworld_widget
|
|
26 |
\class BatteryWidget
|
|
27 |
\brief Example implementation for home screen widget.
|
|
28 |
|
|
29 |
BatteryWidget is derived from the HbWidget. It implements
|
|
30 |
needed functions for the home screen widget.
|
|
31 |
BatteryWidget uses <a href="http://doc.qt.nokia.com/qtmobility-1.0/qsystemdeviceinfo.html">QSystemDeviceInfo</a>
|
|
32 |
to get device's battery status.
|
|
33 |
|
|
34 |
Battery widget demonstrates the following home screen widget features:
|
|
35 |
<ol>
|
|
36 |
<li> widget shape, see \ref ssection_shape </li>
|
|
37 |
<li> root path property, see \ref sssection_rootPath and
|
|
38 |
widget resources, see \ref section_widget_resources </li>
|
|
39 |
<li> error signal, see \ref sssection_error </li>
|
|
40 |
<li> onInitialize slot, see \ref sssection_oninitialize </li>
|
|
41 |
<li> onShow slot, see \ref sssection_onshow </li>
|
|
42 |
<li> onHide slot, see \ref sssection_onhide </li>
|
|
43 |
</ol>
|
|
44 |
|
|
45 |
*/
|
|
46 |
|
|
47 |
|
|
48 |
/*!
|
|
49 |
\fn BatteryWidget::error()
|
|
50 |
Signal emitted if onInitialize fails to load batterywidget image.
|
|
51 |
*/
|
|
52 |
|
|
53 |
|
|
54 |
/*!
|
|
55 |
Constructs a widget which is a child of \a parent, with widget flags set to \a flags.
|
|
56 |
*/
|
|
57 |
BatteryWidget::BatteryWidget(QGraphicsItem* parent, Qt::WindowFlags flags)
|
|
58 |
: HbWidget(parent, flags)
|
|
59 |
{
|
|
60 |
mDeviceInfo = new QSystemDeviceInfo(this);
|
|
61 |
mChargingTimer = new QTimer(this);
|
|
62 |
mBatteryValue = mDeviceInfo->batteryLevel();
|
|
63 |
}
|
|
64 |
|
|
65 |
/*!
|
|
66 |
Destructor
|
|
67 |
*/
|
|
68 |
BatteryWidget::~BatteryWidget()
|
|
69 |
{
|
|
70 |
}
|
|
71 |
|
|
72 |
/*!
|
|
73 |
Returns rootPath property.
|
|
74 |
*/
|
|
75 |
// Start of snippet 1
|
|
76 |
QString BatteryWidget::rootPath() const
|
|
77 |
{
|
|
78 |
return mRootPath;
|
|
79 |
}
|
|
80 |
// End of snippet 1
|
|
81 |
|
|
82 |
/*!
|
|
83 |
Sets rootPath property as \a rootPath.
|
|
84 |
*/
|
|
85 |
// Start of snippet 2
|
|
86 |
void BatteryWidget::setRootPath(QString &rootPath)
|
|
87 |
{
|
|
88 |
mRootPath = rootPath;
|
|
89 |
}
|
|
90 |
// End of snippet 2
|
|
91 |
|
|
92 |
|
|
93 |
/*!
|
|
94 |
Return bounding rect.
|
|
95 |
*/
|
|
96 |
QRectF BatteryWidget::boundingRect() const
|
|
97 |
{
|
|
98 |
QRectF currRect = rect();
|
|
99 |
currRect.setHeight(mBatteryBackgroundBitmap.height());
|
|
100 |
currRect.setWidth(mBatteryBackgroundBitmap.width());
|
|
101 |
return currRect;
|
|
102 |
}
|
|
103 |
|
|
104 |
/*!
|
|
105 |
Return shape.
|
|
106 |
*/
|
|
107 |
// Start of snippet 3
|
|
108 |
QPainterPath BatteryWidget::shape() const
|
|
109 |
{
|
|
110 |
QPainterPath path;
|
|
111 |
QRectF ownRect = boundingRect();
|
|
112 |
path.moveTo(ownRect.bottomRight());
|
|
113 |
QRectF ownRect2 = ownRect.adjusted(-2,-1,1, ownRect.height());
|
|
114 |
path.arcTo(ownRect2, 0, 180);
|
|
115 |
path.lineTo(ownRect2.right(), ownRect2.center().y());
|
|
116 |
return path;
|
|
117 |
}
|
|
118 |
// End of snippet 3
|
|
119 |
|
|
120 |
/*!
|
|
121 |
Paint battery image.
|
|
122 |
*/
|
|
123 |
void BatteryWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
124 |
{
|
|
125 |
Q_UNUSED(option);
|
|
126 |
Q_UNUSED(widget);
|
|
127 |
if (!mUnifiedBatteryBitmap.isNull()) {
|
|
128 |
painter->drawPixmap(QPoint(0,0),mUnifiedBatteryBitmap);
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
/*!
|
|
133 |
Called when widget is shown in the home screen.
|
|
134 |
*/
|
|
135 |
// Start of snippet 4
|
|
136 |
void BatteryWidget::onShow()
|
|
137 |
{
|
|
138 |
stateChanged();
|
|
139 |
prepareGeometryChange();
|
|
140 |
resize(mBatteryBackgroundBitmap.size());
|
|
141 |
}
|
|
142 |
// End of snippet 4
|
|
143 |
|
|
144 |
/*!
|
|
145 |
Called when widget is hidden from the home screen.
|
|
146 |
*/
|
|
147 |
// Start of snippet 5
|
|
148 |
void BatteryWidget::onHide()
|
|
149 |
{
|
|
150 |
mDeviceInfo->disconnect(this);
|
|
151 |
mChargingTimer->stop();
|
|
152 |
mChargingTimer->disconnect(this);
|
|
153 |
}
|
|
154 |
// End of snippet 5
|
|
155 |
|
|
156 |
/*!
|
|
157 |
Called when widget is initalized. Widget can start using its resources,
|
|
158 |
for example rootPath is set by system before it calls this.
|
|
159 |
*/
|
|
160 |
// Start of snippet 6
|
|
161 |
void BatteryWidget::onInitialize()
|
|
162 |
{
|
|
163 |
mBatteryBackgroundBitmap.load(mRootPath + "/batterywidget_bg.png");
|
|
164 |
if (mBatteryBackgroundBitmap.isNull()) {
|
|
165 |
// emit error if loading of the image failed. This will remove widget from the home screen.
|
|
166 |
emit error();
|
|
167 |
}
|
|
168 |
}
|
|
169 |
// End of snippet 6
|
|
170 |
|
|
171 |
/*!
|
|
172 |
Called when widget is unintialized, nothing to do here
|
|
173 |
*/
|
|
174 |
void BatteryWidget::onUninitialize()
|
|
175 |
{
|
|
176 |
}
|
|
177 |
|
|
178 |
/*!
|
|
179 |
Called when phone's battery level is changed.
|
|
180 |
*/
|
|
181 |
void BatteryWidget::batteryLevelChanged ( int level )
|
|
182 |
{
|
|
183 |
mBatteryValue=level;
|
|
184 |
drawBatteryImage();
|
|
185 |
update();
|
|
186 |
}
|
|
187 |
|
|
188 |
/*!
|
|
189 |
Called when phone's power state is changed.
|
|
190 |
*/
|
|
191 |
void BatteryWidget::powerStateChanged(QSystemDeviceInfo::PowerState powerState)
|
|
192 |
{
|
|
193 |
Q_UNUSED(powerState);
|
|
194 |
stateChanged();
|
|
195 |
}
|
|
196 |
|
|
197 |
/*!
|
|
198 |
Animates pointer when phone is charging. (TODO: animation fw?)
|
|
199 |
*/
|
|
200 |
void BatteryWidget::updateChargingBatteryValue()
|
|
201 |
{
|
|
202 |
mBatteryValue+=4;
|
|
203 |
if (mBatteryValue>100) mBatteryValue = 1;
|
|
204 |
drawBatteryImage();
|
|
205 |
update();
|
|
206 |
}
|
|
207 |
|
|
208 |
/*!
|
|
209 |
Draws pointer on top of battery image
|
|
210 |
*/
|
|
211 |
void BatteryWidget::drawBatteryImage()
|
|
212 |
{
|
|
213 |
QRectF rect = mBatteryBackgroundBitmap.rect();
|
|
214 |
|
|
215 |
mUnifiedBatteryBitmap = QPixmap(rect.size().toSize());
|
|
216 |
mUnifiedBatteryBitmap.fill(QColor(Qt::transparent));
|
|
217 |
|
|
218 |
QPainter painter(&mUnifiedBatteryBitmap);
|
|
219 |
|
|
220 |
painter.drawPixmap(rect.toRect(), mBatteryBackgroundBitmap);
|
|
221 |
|
|
222 |
static const QPoint pointerHand[3] = {
|
|
223 |
QPoint(3, 3),
|
|
224 |
QPoint(-3, 3),
|
|
225 |
QPoint(0, -80)
|
|
226 |
};
|
|
227 |
|
|
228 |
painter.translate(QPointF(rect.width()/2,rect.height()-10));
|
|
229 |
painter.rotate(180.0 * (mBatteryValue)/100 + 270);
|
|
230 |
|
|
231 |
painter.setPen(Qt::NoPen);
|
|
232 |
painter.setBrush(Qt::red);
|
|
233 |
painter.drawConvexPolygon(pointerHand, 3);
|
|
234 |
}
|
|
235 |
|
|
236 |
/*!
|
|
237 |
Handles battery widget states.
|
|
238 |
*/
|
|
239 |
void BatteryWidget::stateChanged()
|
|
240 |
{
|
|
241 |
mDeviceInfo->disconnect(this);
|
|
242 |
mChargingTimer->stop();
|
|
243 |
mChargingTimer->disconnect(this);
|
|
244 |
connect(mDeviceInfo, SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState)), SLOT(powerStateChanged(QSystemDeviceInfo::PowerState)));
|
|
245 |
if (mDeviceInfo->currentPowerState() == QSystemDeviceInfo::WallPowerChargingBattery) {
|
|
246 |
connect(mChargingTimer, SIGNAL(timeout()), this, SLOT(updateChargingBatteryValue()));
|
|
247 |
mChargingTimer->start(500);
|
|
248 |
} else {
|
|
249 |
connect(mDeviceInfo, SIGNAL(batteryLevelChanged(int)), SLOT(batteryLevelChanged(int)));
|
|
250 |
mBatteryValue = mDeviceInfo->batteryLevel();
|
|
251 |
}
|
|
252 |
drawBatteryImage();
|
|
253 |
update();
|
|
254 |
}
|
|
255 |
|