1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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 "bearercloud.h"
|
---|
42 | #include "cloud.h"
|
---|
43 |
|
---|
44 | #include <QGraphicsTextItem>
|
---|
45 | #include <QTimer>
|
---|
46 | #include <QDateTime>
|
---|
47 | #include <QHostInfo>
|
---|
48 |
|
---|
49 | #include <QDebug>
|
---|
50 |
|
---|
51 | #include <math.h>
|
---|
52 |
|
---|
53 | #ifndef M_PI
|
---|
54 | #define M_PI 3.14159265358979323846
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | //! [0]
|
---|
58 | BearerCloud::BearerCloud(QObject *parent)
|
---|
59 | : QGraphicsScene(parent), timerId(0)
|
---|
60 | {
|
---|
61 | setSceneRect(-300, -300, 600, 600);
|
---|
62 |
|
---|
63 | qsrand(QDateTime::currentDateTime().toTime_t());
|
---|
64 |
|
---|
65 | offset[QNetworkConfiguration::Active] = 2 * M_PI * qrand() / RAND_MAX;
|
---|
66 | offset[QNetworkConfiguration::Discovered] = offset[QNetworkConfiguration::Active] + M_PI / 6;
|
---|
67 | offset[QNetworkConfiguration::Defined] = offset[QNetworkConfiguration::Discovered] - M_PI / 6;
|
---|
68 | offset[QNetworkConfiguration::Undefined] = offset[QNetworkConfiguration::Undefined] + M_PI / 6;
|
---|
69 |
|
---|
70 | thisDevice = new QGraphicsTextItem(QHostInfo::localHostName());
|
---|
71 | thisDevice->setData(0, QLatin1String("This Device"));
|
---|
72 | thisDevice->setPos(thisDevice->boundingRect().width() / -2,
|
---|
73 | thisDevice->boundingRect().height() / -2);
|
---|
74 | addItem(thisDevice);
|
---|
75 |
|
---|
76 | qreal radius = Cloud::getRadiusForState(QNetworkConfiguration::Active);
|
---|
77 | QGraphicsEllipseItem *orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
|
---|
78 | orbit->setPen(QColor(Qt::green));
|
---|
79 | addItem(orbit);
|
---|
80 | radius = Cloud::getRadiusForState(QNetworkConfiguration::Discovered);
|
---|
81 | orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
|
---|
82 | orbit->setPen(QColor(Qt::blue));
|
---|
83 | addItem(orbit);
|
---|
84 | radius = Cloud::getRadiusForState(QNetworkConfiguration::Defined);
|
---|
85 | orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
|
---|
86 | orbit->setPen(QColor(Qt::darkGray));
|
---|
87 | addItem(orbit);
|
---|
88 | radius = Cloud::getRadiusForState(QNetworkConfiguration::Undefined);
|
---|
89 | orbit = new QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius);
|
---|
90 | orbit->setPen(QColor(Qt::lightGray));
|
---|
91 | addItem(orbit);
|
---|
92 |
|
---|
93 | connect(&manager, SIGNAL(configurationAdded(QNetworkConfiguration)),
|
---|
94 | this, SLOT(configurationAdded(QNetworkConfiguration)));
|
---|
95 | connect(&manager, SIGNAL(configurationRemoved(QNetworkConfiguration)),
|
---|
96 | this, SLOT(configurationRemoved(QNetworkConfiguration)));
|
---|
97 | connect(&manager, SIGNAL(configurationChanged(QNetworkConfiguration)),
|
---|
98 | this, SLOT(configurationChanged(QNetworkConfiguration)));
|
---|
99 |
|
---|
100 | QTimer::singleShot(0, this, SLOT(updateConfigurations()));
|
---|
101 | }
|
---|
102 | //! [0]
|
---|
103 |
|
---|
104 | BearerCloud::~BearerCloud()
|
---|
105 | {
|
---|
106 | }
|
---|
107 |
|
---|
108 | void BearerCloud::cloudMoved()
|
---|
109 | {
|
---|
110 | if (!timerId)
|
---|
111 | timerId = startTimer(1000 / 25);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void BearerCloud::timerEvent(QTimerEvent *)
|
---|
115 | {
|
---|
116 | QList<Cloud *> clouds;
|
---|
117 | foreach (QGraphicsItem *item, items()) {
|
---|
118 | if (Cloud *cloud = qgraphicsitem_cast<Cloud *>(item))
|
---|
119 | clouds << cloud;
|
---|
120 | }
|
---|
121 |
|
---|
122 | foreach (Cloud *cloud, clouds)
|
---|
123 | cloud->calculateForces();
|
---|
124 |
|
---|
125 | bool cloudsMoved = false;
|
---|
126 | foreach (Cloud *cloud, clouds)
|
---|
127 | cloudsMoved |= cloud->advance();
|
---|
128 |
|
---|
129 | if (!cloudsMoved) {
|
---|
130 | killTimer(timerId);
|
---|
131 | timerId = 0;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | //! [2]
|
---|
136 | void BearerCloud::configurationAdded(const QNetworkConfiguration &config)
|
---|
137 | {
|
---|
138 | const QNetworkConfiguration::StateFlags state = config.state();
|
---|
139 |
|
---|
140 | configStates.insert(state, config.identifier());
|
---|
141 |
|
---|
142 | const qreal radius = Cloud::getRadiusForState(state);
|
---|
143 | const int count = configStates.count(state);
|
---|
144 | const qreal angle = 2 * M_PI / count;
|
---|
145 |
|
---|
146 | Cloud *item = new Cloud(config);
|
---|
147 | configurations.insert(config.identifier(), item);
|
---|
148 |
|
---|
149 | item->setPos(radius * cos((count-1) * angle + offset[state]),
|
---|
150 | radius * sin((count-1) * angle + offset[state]));
|
---|
151 |
|
---|
152 | addItem(item);
|
---|
153 |
|
---|
154 | cloudMoved();
|
---|
155 | }
|
---|
156 | //! [2]
|
---|
157 |
|
---|
158 | //! [3]
|
---|
159 | void BearerCloud::configurationRemoved(const QNetworkConfiguration &config)
|
---|
160 | {
|
---|
161 | foreach (const QNetworkConfiguration::StateFlags &state, configStates.uniqueKeys())
|
---|
162 | configStates.remove(state, config.identifier());
|
---|
163 |
|
---|
164 | Cloud *item = configurations.take(config.identifier());
|
---|
165 |
|
---|
166 | item->setFinalScale(0.0);
|
---|
167 | item->setDeleteAfterAnimation(true);
|
---|
168 |
|
---|
169 | cloudMoved();
|
---|
170 | }
|
---|
171 | //! [3]
|
---|
172 |
|
---|
173 | //! [4]
|
---|
174 | void BearerCloud::configurationChanged(const QNetworkConfiguration &config)
|
---|
175 | {
|
---|
176 | foreach (const QNetworkConfiguration::StateFlags &state, configStates.uniqueKeys())
|
---|
177 | configStates.remove(state, config.identifier());
|
---|
178 |
|
---|
179 | configStates.insert(config.state(), config.identifier());
|
---|
180 |
|
---|
181 | cloudMoved();
|
---|
182 | }
|
---|
183 | //! [4]
|
---|
184 |
|
---|
185 | //! [1]
|
---|
186 | void BearerCloud::updateConfigurations()
|
---|
187 | {
|
---|
188 | QList<QNetworkConfiguration> allConfigurations = manager.allConfigurations();
|
---|
189 |
|
---|
190 | while (!allConfigurations.isEmpty())
|
---|
191 | configurationAdded(allConfigurations.takeFirst());
|
---|
192 |
|
---|
193 | cloudMoved();
|
---|
194 | }
|
---|
195 | //! [1]
|
---|
196 |
|
---|