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