|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "cloud.h" |
|
43 #include "bearercloud.h" |
|
44 |
|
45 #include <qnetworksession.h> |
|
46 |
|
47 #include <QGraphicsTextItem> |
|
48 #include <QGraphicsSvgItem> |
|
49 #include <QGraphicsSceneMouseEvent> |
|
50 #include <QSvgRenderer> |
|
51 #include <QPainter> |
|
52 |
|
53 #include <QDebug> |
|
54 |
|
55 #include <math.h> |
|
56 |
|
57 static QMap<QString, QSvgRenderer *> svgCache; |
|
58 |
|
59 //! [0] |
|
60 Cloud::Cloud(const QNetworkConfiguration &config, QGraphicsItem *parent) |
|
61 : QGraphicsItem(parent), configuration(config), deleteAfterAnimation(false) |
|
62 { |
|
63 session = new QNetworkSession(configuration, this); |
|
64 connect(session, SIGNAL(newConfigurationActivated()), |
|
65 this, SLOT(newConfigurationActivated())); |
|
66 connect(session, SIGNAL(stateChanged(QNetworkSession::State)), |
|
67 this, SLOT(stateChanged(QNetworkSession::State))); |
|
68 |
|
69 setFlag(ItemIsMovable); |
|
70 #if (QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)) |
|
71 setFlag(ItemSendsGeometryChanges); |
|
72 #endif |
|
73 setZValue(1); |
|
74 |
|
75 icon = new QGraphicsSvgItem(this); |
|
76 text = new QGraphicsTextItem(this); |
|
77 |
|
78 currentScale = 0; |
|
79 finalScale = 1; |
|
80 setTransform(QTransform::fromScale(currentScale, currentScale), false); |
|
81 setOpacity(0); |
|
82 |
|
83 newConfigurationActivated(); |
|
84 } |
|
85 //! [0] |
|
86 |
|
87 Cloud::~Cloud() |
|
88 { |
|
89 } |
|
90 |
|
91 void Cloud::setFinalScale(qreal factor) |
|
92 { |
|
93 finalScale = factor; |
|
94 } |
|
95 |
|
96 void Cloud::setDeleteAfterAnimation(bool deleteAfter) |
|
97 { |
|
98 deleteAfterAnimation = deleteAfter; |
|
99 } |
|
100 |
|
101 void Cloud::calculateForces() |
|
102 { |
|
103 if (!scene() || scene()->mouseGrabberItem() == this) { |
|
104 newPos = pos(); |
|
105 return; |
|
106 } |
|
107 |
|
108 // sum up all the forces push this item away |
|
109 qreal xvel = 0; |
|
110 qreal yvel = 0; |
|
111 QLineF orbitForce; |
|
112 foreach (QGraphicsItem *item, scene()->items()) { |
|
113 // other clouds |
|
114 Cloud *cloud = qgraphicsitem_cast<Cloud *>(item); |
|
115 if (!cloud && item->data(0) != QLatin1String("This Device")) |
|
116 continue; |
|
117 |
|
118 qreal factor = 1.0; |
|
119 |
|
120 QLineF line(cloud ? item->mapToScene(0, 0) : QPointF(0, 0), mapToScene(0, 0)); |
|
121 if (item->data(0) == QLatin1String("This Device")) |
|
122 orbitForce = line; |
|
123 |
|
124 if (cloud) |
|
125 factor = cloud->currentScale; |
|
126 |
|
127 qreal dx = line.dx(); |
|
128 qreal dy = line.dy(); |
|
129 double l = 2.0 * (dx * dx + dy * dy); |
|
130 if (l > 0) { |
|
131 xvel += factor * dx * 200.0 / l; |
|
132 yvel += factor * dy * 200.0 / l; |
|
133 } |
|
134 } |
|
135 |
|
136 // tendency to stay at a fixed orbit |
|
137 qreal orbit = getRadiusForState(configuration.state()); |
|
138 qreal distance = orbitForce.length(); |
|
139 |
|
140 QLineF unit = orbitForce.unitVector(); |
|
141 |
|
142 orbitForce.setLength(xvel * unit.dx() + yvel * unit.dy()); |
|
143 |
|
144 qreal w = 2 - exp(-pow(distance-orbit, 2)/(2 * 50)); |
|
145 |
|
146 if (distance < orbit) { |
|
147 xvel += orbitForce.dx() * w; |
|
148 yvel += orbitForce.dy() * w; |
|
149 } else { |
|
150 xvel -= orbitForce.dx() * w; |
|
151 yvel -= orbitForce.dy() * w; |
|
152 } |
|
153 |
|
154 if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1) |
|
155 xvel = yvel = 0; |
|
156 |
|
157 QRectF sceneRect = scene()->sceneRect(); |
|
158 newPos = pos() + QPointF(xvel, yvel); |
|
159 newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10)); |
|
160 newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10)); |
|
161 } |
|
162 |
|
163 bool Cloud::advance() |
|
164 { |
|
165 static const qreal scaleDelta = 0.01; |
|
166 |
|
167 bool animated = false; |
|
168 |
|
169 if (currentScale < finalScale) { |
|
170 animated = true; |
|
171 currentScale = qMin<qreal>(currentScale + scaleDelta, finalScale); |
|
172 setTransform(QTransform::fromScale(currentScale, currentScale), false); |
|
173 } else if (currentScale > finalScale) { |
|
174 animated = true; |
|
175 currentScale = qMax<qreal>(currentScale - scaleDelta, finalScale); |
|
176 setTransform(QTransform::fromScale(currentScale, currentScale), false); |
|
177 } |
|
178 |
|
179 if (newPos != pos()) { |
|
180 setPos(newPos); |
|
181 animated = true; |
|
182 } |
|
183 |
|
184 if (opacity() != finalOpacity) { |
|
185 animated = true; |
|
186 if (qAbs(finalScale - currentScale) > 0.0) { |
|
187 // use scale as reference |
|
188 setOpacity(opacity() + scaleDelta * (finalOpacity - opacity()) / |
|
189 qAbs(finalScale - currentScale)); |
|
190 } else { |
|
191 setOpacity(finalOpacity); |
|
192 } |
|
193 } |
|
194 |
|
195 if (!animated && deleteAfterAnimation) |
|
196 deleteLater(); |
|
197 |
|
198 return animated; |
|
199 } |
|
200 |
|
201 QRectF Cloud::boundingRect() const |
|
202 { |
|
203 return childrenBoundingRect(); |
|
204 } |
|
205 |
|
206 void Cloud::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) |
|
207 { |
|
208 } |
|
209 |
|
210 //! [4] |
|
211 QVariant Cloud::itemChange(GraphicsItemChange change, const QVariant &value) |
|
212 { |
|
213 switch (change) { |
|
214 case ItemPositionHasChanged: |
|
215 if (BearerCloud *bearercloud = qobject_cast<BearerCloud *>(scene())) |
|
216 bearercloud->cloudMoved(); |
|
217 default: |
|
218 ; |
|
219 }; |
|
220 |
|
221 return QGraphicsItem::itemChange(change, value); |
|
222 } |
|
223 //! [4] |
|
224 |
|
225 //! [3] |
|
226 void Cloud::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) |
|
227 { |
|
228 if (event->button() == Qt::LeftButton) { |
|
229 if (session->isOpen()) |
|
230 session->close(); |
|
231 else |
|
232 session->open(); |
|
233 |
|
234 event->accept(); |
|
235 } |
|
236 } |
|
237 //! [3] |
|
238 |
|
239 //! [2] |
|
240 void Cloud::stateChanged(QNetworkSession::State state) |
|
241 { |
|
242 if (configuration.name().isEmpty()) |
|
243 finalOpacity = qreal(0.1); |
|
244 else if (session->state() == QNetworkSession::NotAvailable) |
|
245 finalOpacity = 0.5; |
|
246 else |
|
247 finalOpacity = 1.0; |
|
248 |
|
249 QString tooltip; |
|
250 |
|
251 if (configuration.name().isEmpty()) |
|
252 tooltip += tr("<b>HIDDEN NETWORK</b><br>"); |
|
253 else |
|
254 tooltip += tr("<b>%1</b><br>").arg(configuration.name()); |
|
255 |
|
256 const QNetworkInterface interface = session->interface(); |
|
257 if (interface.isValid()) |
|
258 tooltip += tr("<br>Interface: %1").arg(interface.humanReadableName()); |
|
259 tooltip += tr("<br>Id: %1").arg(configuration.identifier()); |
|
260 |
|
261 const QString bearerName = configuration.bearerName(); |
|
262 if (!bearerName.isEmpty()) |
|
263 tooltip += tr("<br>Bearer: %1").arg(bearerName); |
|
264 |
|
265 QString s = tr("<br>State: %1 (%2)"); |
|
266 switch (state) { |
|
267 case QNetworkSession::Invalid: |
|
268 s = s.arg(tr("Invalid")); |
|
269 break; |
|
270 case QNetworkSession::NotAvailable: |
|
271 s = s.arg(tr("Not Available")); |
|
272 break; |
|
273 case QNetworkSession::Connecting: |
|
274 s = s.arg(tr("Connecting")); |
|
275 break; |
|
276 case QNetworkSession::Connected: |
|
277 s = s.arg(tr("Connected")); |
|
278 break; |
|
279 case QNetworkSession::Closing: |
|
280 s = s.arg(tr("Closing")); |
|
281 break; |
|
282 case QNetworkSession::Disconnected: |
|
283 s = s.arg(tr("Disconnected")); |
|
284 break; |
|
285 case QNetworkSession::Roaming: |
|
286 s = s.arg(tr("Roaming")); |
|
287 break; |
|
288 default: |
|
289 s = s.arg(tr("Unknown")); |
|
290 } |
|
291 |
|
292 if (session->isOpen()) |
|
293 s = s.arg(tr("Open")); |
|
294 else |
|
295 s = s.arg(tr("Closed")); |
|
296 |
|
297 tooltip += s; |
|
298 |
|
299 tooltip += tr("<br><br>Active time: %1 seconds").arg(session->activeTime()); |
|
300 tooltip += tr("<br>Received data: %1 bytes").arg(session->bytesReceived()); |
|
301 tooltip += tr("<br>Sent data: %1 bytes").arg(session->bytesWritten()); |
|
302 |
|
303 setToolTip(tooltip); |
|
304 } |
|
305 //! [2] |
|
306 |
|
307 //! [1] |
|
308 void Cloud::newConfigurationActivated() |
|
309 { |
|
310 const QString bearerName = configuration.bearerName(); |
|
311 if (!svgCache.contains(bearerName)) { |
|
312 if (bearerName == QLatin1String("WLAN")) |
|
313 svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":wlan.svg"))); |
|
314 else if (bearerName == QLatin1String("Ethernet")) |
|
315 svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":lan.svg"))); |
|
316 else |
|
317 svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":unknown.svg"))); |
|
318 } |
|
319 |
|
320 icon->setSharedRenderer(svgCache[bearerName]); |
|
321 |
|
322 if (configuration.name().isEmpty()) { |
|
323 text->setPlainText(tr("HIDDEN NETWORK")); |
|
324 } else { |
|
325 if (configuration.type() == QNetworkConfiguration::ServiceNetwork) |
|
326 text->setHtml("<b>" + configuration.name() + "</b>"); |
|
327 else |
|
328 text->setPlainText(configuration.name()); |
|
329 } |
|
330 |
|
331 const qreal height = icon->boundingRect().height() + text->boundingRect().height(); |
|
332 |
|
333 icon->setPos(icon->boundingRect().width() / -2, height / -2); |
|
334 |
|
335 text->setPos(text->boundingRect().width() / -2, |
|
336 height / 2 - text->boundingRect().height()); |
|
337 |
|
338 stateChanged(session->state()); |
|
339 } |
|
340 //! [1] |
|
341 |
|
342 qreal Cloud::getRadiusForState(QNetworkConfiguration::StateFlags state) |
|
343 { |
|
344 switch (state) { |
|
345 case QNetworkConfiguration::Active: |
|
346 return 100; |
|
347 break; |
|
348 case QNetworkConfiguration::Discovered: |
|
349 return 150; |
|
350 break; |
|
351 case QNetworkConfiguration::Defined: |
|
352 return 200; |
|
353 break; |
|
354 case QNetworkConfiguration::Undefined: |
|
355 return 250; |
|
356 break; |
|
357 default: |
|
358 return 300; |
|
359 } |
|
360 } |
|
361 |