10
|
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 Qt Mobility Components.
|
|
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 |
#if !defined(Q_WS_MAEMO_5) && !defined(Q_WS_MAEMO_6) && \
|
|
249 |
!defined(Q_OS_SYMBIAN) && !defined(Q_OS_WINCE)
|
|
250 |
QString tooltip;
|
|
251 |
|
|
252 |
if (configuration.name().isEmpty())
|
|
253 |
tooltip += tr("<b>HIDDEN NETWORK</b><br>");
|
|
254 |
else
|
|
255 |
tooltip += tr("<b>%1</b><br>").arg(configuration.name());
|
|
256 |
|
|
257 |
const QNetworkInterface interface = session->interface();
|
|
258 |
if (interface.isValid())
|
|
259 |
tooltip += tr("<br>Interface: %1").arg(interface.humanReadableName());
|
|
260 |
tooltip += tr("<br>Id: %1").arg(configuration.identifier());
|
|
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 |
#else
|
|
306 |
Q_UNUSED(state);
|
|
307 |
#endif
|
|
308 |
}
|
|
309 |
//! [2]
|
|
310 |
|
|
311 |
//! [1]
|
|
312 |
void Cloud::newConfigurationActivated()
|
|
313 |
{
|
|
314 |
const QString bearerName = configuration.bearerName();
|
|
315 |
if (!svgCache.contains(bearerName)) {
|
|
316 |
if (bearerName == QLatin1String("WLAN"))
|
|
317 |
svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":wlan.svg")));
|
|
318 |
else if (bearerName == QLatin1String("Ethernet"))
|
|
319 |
svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":lan.svg")));
|
|
320 |
else
|
|
321 |
svgCache.insert(bearerName, new QSvgRenderer(QLatin1String(":unknown.svg")));
|
|
322 |
}
|
|
323 |
|
|
324 |
icon->setSharedRenderer(svgCache[bearerName]);
|
|
325 |
|
|
326 |
if (configuration.name().isEmpty()) {
|
|
327 |
text->setPlainText(tr("HIDDEN NETWORK"));
|
|
328 |
} else {
|
|
329 |
if (configuration.type() == QNetworkConfiguration::ServiceNetwork)
|
|
330 |
text->setHtml("<b>" + configuration.name() + "</b>");
|
|
331 |
else
|
|
332 |
text->setPlainText(configuration.name());
|
|
333 |
}
|
|
334 |
|
|
335 |
const qreal height = icon->boundingRect().height() + text->boundingRect().height();
|
|
336 |
|
|
337 |
icon->setPos(icon->boundingRect().width() / -2, height / -2);
|
|
338 |
|
|
339 |
text->setPos(text->boundingRect().width() / -2,
|
|
340 |
height / 2 - text->boundingRect().height());
|
|
341 |
|
|
342 |
stateChanged(session->state());
|
|
343 |
}
|
|
344 |
//! [1]
|
|
345 |
|
|
346 |
qreal Cloud::getRadiusForState(QNetworkConfiguration::StateFlags state)
|
|
347 |
{
|
|
348 |
switch (state) {
|
|
349 |
case QNetworkConfiguration::Active:
|
|
350 |
return 100;
|
|
351 |
break;
|
|
352 |
case QNetworkConfiguration::Discovered:
|
|
353 |
return 150;
|
|
354 |
break;
|
|
355 |
case QNetworkConfiguration::Defined:
|
|
356 |
return 200;
|
|
357 |
break;
|
|
358 |
case QNetworkConfiguration::Undefined:
|
|
359 |
return 250;
|
|
360 |
break;
|
|
361 |
default:
|
|
362 |
return 300;
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|