|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 #include "cloud.h" |
|
42 #include "bearercloud.h" |
|
43 |
|
44 #include <qnetworksession.h> |
|
45 |
|
46 #include <QGraphicsTextItem> |
|
47 #include <QGraphicsSvgItem> |
|
48 #include <QGraphicsSceneMouseEvent> |
|
49 #include <QSvgRenderer> |
|
50 #include <QPainter> |
|
51 |
|
52 #include <QDebug> |
|
53 |
|
54 #include <math.h> |
|
55 |
|
56 static QMap<QString, QSvgRenderer *> svgCache; |
|
57 |
|
58 //! [0] |
|
59 Cloud::Cloud(const QNetworkConfiguration &config, QGraphicsItem *parent) |
|
60 : QGraphicsItem(parent), configuration(config), deleteAfterAnimation(false) |
|
61 { |
|
62 session = new QNetworkSession(configuration, this); |
|
63 connect(session, SIGNAL(newConfigurationActivated()), |
|
64 this, SLOT(newConfigurationActivated())); |
|
65 connect(session, SIGNAL(stateChanged(QNetworkSession::State)), |
|
66 this, SLOT(stateChanged(QNetworkSession::State))); |
|
67 |
|
68 setFlag(ItemIsMovable); |
|
69 #if (QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)) |
|
70 setFlag(ItemSendsGeometryChanges); |
|
71 #endif |
|
72 setZValue(1); |
|
73 |
|
74 icon = new QGraphicsSvgItem(this); |
|
75 text = new QGraphicsTextItem(this); |
|
76 |
|
77 currentScale = 0; |
|
78 finalScale = 1; |
|
79 setTransform(QTransform::fromScale(currentScale, currentScale), false); |
|
80 setOpacity(0); |
|
81 |
|
82 newConfigurationActivated(); |
|
83 } |
|
84 //! [0] |
|
85 |
|
86 Cloud::~Cloud() |
|
87 { |
|
88 } |
|
89 |
|
90 void Cloud::setFinalScale(qreal factor) |
|
91 { |
|
92 finalScale = factor; |
|
93 } |
|
94 |
|
95 void Cloud::setDeleteAfterAnimation(bool deleteAfter) |
|
96 { |
|
97 deleteAfterAnimation = deleteAfter; |
|
98 } |
|
99 |
|
100 void Cloud::calculateForces() |
|
101 { |
|
102 if (!scene() || scene()->mouseGrabberItem() == this) { |
|
103 newPos = pos(); |
|
104 return; |
|
105 } |
|
106 |
|
107 // sum up all the forces push this item away |
|
108 qreal xvel = 0; |
|
109 qreal yvel = 0; |
|
110 QLineF orbitForce; |
|
111 foreach (QGraphicsItem *item, scene()->items()) { |
|
112 // other clouds |
|
113 Cloud *cloud = qgraphicsitem_cast<Cloud *>(item); |
|
114 if (!cloud && item->data(0) != QLatin1String("This Device")) |
|
115 continue; |
|
116 |
|
117 qreal factor = 1.0; |
|
118 |
|
119 QLineF line(cloud ? item->mapToScene(0, 0) : QPointF(0, 0), mapToScene(0, 0)); |
|
120 if (item->data(0) == QLatin1String("This Device")) |
|
121 orbitForce = line; |
|
122 |
|
123 if (cloud) |
|
124 factor = cloud->currentScale; |
|
125 |
|
126 qreal dx = line.dx(); |
|
127 qreal dy = line.dy(); |
|
128 double l = 2.0 * (dx * dx + dy * dy); |
|
129 if (l > 0) { |
|
130 xvel += factor * dx * 200.0 / l; |
|
131 yvel += factor * dy * 200.0 / l; |
|
132 } |
|
133 } |
|
134 |
|
135 // tendency to stay at a fixed orbit |
|
136 qreal orbit = getRadiusForState(configuration.state()); |
|
137 qreal distance = orbitForce.length(); |
|
138 |
|
139 QLineF unit = orbitForce.unitVector(); |
|
140 |
|
141 orbitForce.setLength(xvel * unit.dx() + yvel * unit.dy()); |
|
142 |
|
143 qreal w = 2 - exp(-pow(distance-orbit, 2)/(2 * 50)); |
|
144 |
|
145 if (distance < orbit) { |
|
146 xvel += orbitForce.dx() * w; |
|
147 yvel += orbitForce.dy() * w; |
|
148 } else { |
|
149 xvel -= orbitForce.dx() * w; |
|
150 yvel -= orbitForce.dy() * w; |
|
151 } |
|
152 |
|
153 if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1) |
|
154 xvel = yvel = 0; |
|
155 |
|
156 QRectF sceneRect = scene()->sceneRect(); |
|
157 newPos = pos() + QPointF(xvel, yvel); |
|
158 newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10)); |
|
159 newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10)); |
|
160 } |
|
161 |
|
162 bool Cloud::advance() |
|
163 { |
|
164 static const qreal scaleDelta = 0.01; |
|
165 |
|
166 bool animated = false; |
|
167 |
|
168 if (currentScale < finalScale) { |
|
169 animated = true; |
|
170 currentScale = qMin<qreal>(currentScale + scaleDelta, finalScale); |
|
171 setTransform(QTransform::fromScale(currentScale, currentScale), false); |
|
172 } else if (currentScale > finalScale) { |
|
173 animated = true; |
|
174 currentScale = qMax<qreal>(currentScale - scaleDelta, finalScale); |
|
175 setTransform(QTransform::fromScale(currentScale, currentScale), false); |
|
176 } |
|
177 |
|
178 if (newPos != pos()) { |
|
179 setPos(newPos); |
|
180 animated = true; |
|
181 } |
|
182 |
|
183 if (opacity() != finalOpacity) { |
|
184 animated = true; |
|
185 if (qAbs(finalScale - currentScale) > 0.0) { |
|
186 // use scale as reference |
|
187 setOpacity(opacity() + scaleDelta * (finalOpacity - opacity()) / |
|
188 qAbs(finalScale - currentScale)); |
|
189 } else { |
|
190 setOpacity(finalOpacity); |
|
191 } |
|
192 } |
|
193 |
|
194 if (!animated && deleteAfterAnimation) |
|
195 deleteLater(); |
|
196 |
|
197 return animated; |
|
198 } |
|
199 |
|
200 QRectF Cloud::boundingRect() const |
|
201 { |
|
202 return childrenBoundingRect(); |
|
203 } |
|
204 |
|
205 void Cloud::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) |
|
206 { |
|
207 } |
|
208 |
|
209 //! [4] |
|
210 QVariant Cloud::itemChange(GraphicsItemChange change, const QVariant &value) |
|
211 { |
|
212 switch (change) { |
|
213 case ItemPositionHasChanged: |
|
214 if (BearerCloud *bearercloud = qobject_cast<BearerCloud *>(scene())) |
|
215 bearercloud->cloudMoved(); |
|
216 default: |
|
217 ; |
|
218 }; |
|
219 |
|
220 return QGraphicsItem::itemChange(change, value); |
|
221 } |
|
222 //! [4] |
|
223 |
|
224 //! [3] |
|
225 void Cloud::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) |
|
226 { |
|
227 if (event->button() == Qt::LeftButton) { |
|
228 if (session->isOpen()) |
|
229 session->close(); |
|
230 else |
|
231 session->open(); |
|
232 |
|
233 event->accept(); |
|
234 } |
|
235 } |
|
236 //! [3] |
|
237 |
|
238 //! [2] |
|
239 void Cloud::stateChanged(QNetworkSession::State state) |
|
240 { |
|
241 if (configuration.name().isEmpty()) |
|
242 finalOpacity = qreal(0.1); |
|
243 else if (session->state() == QNetworkSession::NotAvailable) |
|
244 finalOpacity = 0.5; |
|
245 else |
|
246 finalOpacity = 1.0; |
|
247 |
|
248 QString tooltip; |
|
249 |
|
250 if (configuration.name().isEmpty()) |
|
251 tooltip += tr("<b>HIDDEN NETWORK</b><br>"); |
|
252 else |
|
253 tooltip += tr("<b>%1</b><br>").arg(configuration.name()); |
|
254 |
|
255 #ifndef QT_NO_NETWORKINTERFACE |
|
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 #endif |
|
261 |
|
262 const QString bearerName = configuration.bearerName(); |
|
263 if (!bearerName.isEmpty()) |
|
264 tooltip += tr("<br>Bearer: %1").arg(bearerName); |
|
265 |
|
266 QString s = tr("<br>State: %1 (%2)"); |
|
267 switch (state) { |
|
268 case QNetworkSession::Invalid: |
|
269 s = s.arg(tr("Invalid")); |
|
270 break; |
|
271 case QNetworkSession::NotAvailable: |
|
272 s = s.arg(tr("Not Available")); |
|
273 break; |
|
274 case QNetworkSession::Connecting: |
|
275 s = s.arg(tr("Connecting")); |
|
276 break; |
|
277 case QNetworkSession::Connected: |
|
278 s = s.arg(tr("Connected")); |
|
279 break; |
|
280 case QNetworkSession::Closing: |
|
281 s = s.arg(tr("Closing")); |
|
282 break; |
|
283 case QNetworkSession::Disconnected: |
|
284 s = s.arg(tr("Disconnected")); |
|
285 break; |
|
286 case QNetworkSession::Roaming: |
|
287 s = s.arg(tr("Roaming")); |
|
288 break; |
|
289 default: |
|
290 s = s.arg(tr("Unknown")); |
|
291 } |
|
292 |
|
293 if (session->isOpen()) |
|
294 s = s.arg(tr("Open")); |
|
295 else |
|
296 s = s.arg(tr("Closed")); |
|
297 |
|
298 tooltip += s; |
|
299 |
|
300 tooltip += tr("<br><br>Active time: %1 seconds").arg(session->activeTime()); |
|
301 tooltip += tr("<br>Received data: %1 bytes").arg(session->bytesReceived()); |
|
302 tooltip += tr("<br>Sent data: %1 bytes").arg(session->bytesWritten()); |
|
303 |
|
304 setToolTip(tooltip); |
|
305 } |
|
306 //! [2] |
|
307 |
|
308 //! [1] |
|
309 void Cloud::newConfigurationActivated() |
|
310 { |
|
311 const QString bearerName = configuration.bearerName(); |
|
312 if (!svgCache.contains(bearerName)) { |
|
313 if (bearerName == QLatin1String("WLAN")) |
|
314 svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":wlan.svg"))); |
|
315 else if (bearerName == QLatin1String("Ethernet")) |
|
316 svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":lan.svg"))); |
|
317 else |
|
318 svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":unknown.svg"))); |
|
319 } |
|
320 |
|
321 icon->setSharedRenderer(svgCache[bearerName]); |
|
322 |
|
323 if (configuration.name().isEmpty()) { |
|
324 text->setPlainText(tr("HIDDEN NETWORK")); |
|
325 } else { |
|
326 if (configuration.type() == QNetworkConfiguration::ServiceNetwork) |
|
327 text->setHtml("<b>" + configuration.name() + "</b>"); |
|
328 else |
|
329 text->setPlainText(configuration.name()); |
|
330 } |
|
331 |
|
332 const qreal height = icon->boundingRect().height() + text->boundingRect().height(); |
|
333 |
|
334 icon->setPos(icon->boundingRect().width() / -2, height / -2); |
|
335 |
|
336 text->setPos(text->boundingRect().width() / -2, |
|
337 height / 2 - text->boundingRect().height()); |
|
338 |
|
339 stateChanged(session->state()); |
|
340 } |
|
341 //! [1] |
|
342 |
|
343 qreal Cloud::getRadiusForState(QNetworkConfiguration::StateFlags state) |
|
344 { |
|
345 switch (state) { |
|
346 case QNetworkConfiguration::Active: |
|
347 return 100; |
|
348 break; |
|
349 case QNetworkConfiguration::Discovered: |
|
350 return 150; |
|
351 break; |
|
352 case QNetworkConfiguration::Defined: |
|
353 return 200; |
|
354 break; |
|
355 case QNetworkConfiguration::Undefined: |
|
356 return 250; |
|
357 break; |
|
358 default: |
|
359 return 300; |
|
360 } |
|
361 } |
|
362 |