File: network-bearercloud.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (383 lines) | stat: -rw-r--r-- 44,893 bytes parent folder | download
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- bearercloud.qdoc -->
  <title>Qt 4.8: Bearer Cloud Example</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Bearer Cloud Example</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#setting-the-scene">Setting the scene</a></li>
<li class="level1"><a href="#responding-to-changes">Responding to changes</a></li>
</ul>
</div>
<h1 class="title">Bearer Cloud Example</h1>
<span class="subtitle"></span>
<!-- $$$network/bearercloud-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="network-bearercloud-bearercloud-cpp.html">network/bearercloud/bearercloud.cpp</a></li>
<li><a href="network-bearercloud-bearercloud-h.html">network/bearercloud/bearercloud.h</a></li>
<li><a href="network-bearercloud-bluetooth-svg.html">network/bearercloud/bluetooth.svg</a></li>
<li><a href="network-bearercloud-cell-svg.html">network/bearercloud/cell.svg</a></li>
<li><a href="network-bearercloud-cloud-cpp.html">network/bearercloud/cloud.cpp</a></li>
<li><a href="network-bearercloud-cloud-h.html">network/bearercloud/cloud.h</a></li>
<li><a href="network-bearercloud-gprs-svg.html">network/bearercloud/gprs.svg</a></li>
<li><a href="network-bearercloud-lan-svg.html">network/bearercloud/lan.svg</a></li>
<li><a href="network-bearercloud-umts-svg.html">network/bearercloud/umts.svg</a></li>
<li><a href="network-bearercloud-unknown-svg.html">network/bearercloud/unknown.svg</a></li>
<li><a href="network-bearercloud-wlan-svg.html">network/bearercloud/wlan.svg</a></li>
<li><a href="network-bearercloud-main-cpp.html">network/bearercloud/main.cpp</a></li>
<li><a href="network-bearercloud-bearercloud-pro.html">network/bearercloud/bearercloud.pro</a></li>
<li><a href="network-bearercloud-icons-qrc.html">network/bearercloud/icons.qrc</a></li>
</ul>
<p>The Bearer Cloud example shows how to use the Bearer Management API to monitor the connectivity state of the local device.<p class="centerAlign"><img src="images/bearercloud-example.png" alt="Screenshot of the Bearer Cloud example" /></p><p>Bearer Management provides the <a href="qnetworkconfigurationmanager.html">QNetworkConfigurationManager</a> class which can be used to monitor changes in the available <a href="qnetworkconfiguration.html">network configurations</a> and the <a href="qnetworksession.html">QNetworkSession</a> class which is used to <a href="qnetworksession.html#open">open</a> and <a href="qnetworksession.html#close">close</a> a session bringing a network interface up or down if necessary.</p>
<p>This example displays all known <a href="qnetworkconfiguration.html">network configurations</a> in a cloud orbiting the local device. There are four orbits representing the four possible <a href="qnetworkconfiguration.html#StateFlag-enum">states</a> that the network configuration can be in. The closer the orbit the more useful the network configuration is in its current state. The inner orbit is populated with network configurations that are in the <a href="qnetworkconfiguration.html#StateFlag-enum">Active</a> state. The second orbit is populated with network configurations that are in the <a href="qnetworkconfiguration.html#StateFlag-enum">Discovered</a> state. The third orbit is populated with network configurations that are in the <a href="qnetworkconfiguration.html#StateFlag-enum">Defined</a> state. Finally the outer orbit is populated by configurations that are in the <a href="qnetworkconfiguration.html#StateFlag-enum">Undefined</a> state.</p>
<p>Hovering the mouse over a network configuration will display information about the network configuration in a tool tip.</p>
<p>Double clicking on an Active or Discovered network configuration will <a href="qnetworksession.html#close">close</a> or <a href="qnetworksession.html#open">open</a> a network session, respectively.</p>
<p>Lastly you can reorganize the cloud without changing the state of the network configurations by dragging them around.</p>
<p>This example consists of two main classes, the BearerCloud and Cloud classes. The Cloud class represents a single network session and associated network configuration. The BearerCloud class implements a Graphics View scene and manages the life-cycle of Cloud objects in response to notification signals from <a href="qnetworkconfigurationmanager.html">QNetworkConfigurationManager</a>.</p>
<a name="setting-the-scene"></a>
<h2>Setting the scene</h2>
<p>When constructing the scene we first calculate some random offsets using the global qsand() and <a href="qtglobal.html#qrand">qrand</a>() functions. We will use these offsets to scatter the initial position of new Cloud objects.</p>
<p>Next we place a text item in the center of the scene to represent the local device and surround it with four concentric circles to help visualize the orbits.</p>
<p>Finally we connect up the network configuration notification signals and queue the initial population of the scene during the next iteration of the event loop.</p>
<pre class="cpp"> BearerCloud<span class="operator">::</span>BearerCloud(<span class="type"><a href="qobject.html">QObject</a></span> <span class="operator">*</span>parent)
 :   <span class="type"><a href="qgraphicsscene.html">QGraphicsScene</a></span>(parent)<span class="operator">,</span> timerId(<span class="number">0</span>)
 {
     setSceneRect(<span class="operator">-</span><span class="number">300</span><span class="operator">,</span> <span class="operator">-</span><span class="number">300</span><span class="operator">,</span> <span class="number">600</span><span class="operator">,</span> <span class="number">600</span>);

     qsrand(<span class="type"><a href="qdatetime.html">QDateTime</a></span><span class="operator">::</span>currentDateTime()<span class="operator">.</span>toTime_t());

     offset<span class="operator">[</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Active<span class="operator">]</span> <span class="operator">=</span> <span class="number">2</span> <span class="operator">*</span> M_PI <span class="operator">*</span> qrand() <span class="operator">/</span> RAND_MAX;
     offset<span class="operator">[</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Discovered<span class="operator">]</span> <span class="operator">=</span> offset<span class="operator">[</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Active<span class="operator">]</span> <span class="operator">+</span> M_PI <span class="operator">/</span> <span class="number">6</span>;
     offset<span class="operator">[</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Defined<span class="operator">]</span> <span class="operator">=</span> offset<span class="operator">[</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Discovered<span class="operator">]</span> <span class="operator">-</span> M_PI <span class="operator">/</span> <span class="number">6</span>;
     offset<span class="operator">[</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Undefined<span class="operator">]</span> <span class="operator">=</span> offset<span class="operator">[</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Undefined<span class="operator">]</span> <span class="operator">+</span> M_PI <span class="operator">/</span> <span class="number">6</span>;

     thisDevice <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicstextitem.html">QGraphicsTextItem</a></span>(<span class="type"><a href="qhostinfo.html">QHostInfo</a></span><span class="operator">::</span>localHostName());
     thisDevice<span class="operator">-</span><span class="operator">&gt;</span>setData(<span class="number">0</span><span class="operator">,</span> QLatin1String(<span class="string">&quot;This Device&quot;</span>));
     thisDevice<span class="operator">-</span><span class="operator">&gt;</span>setPos(thisDevice<span class="operator">-</span><span class="operator">&gt;</span>boundingRect()<span class="operator">.</span>width() <span class="operator">/</span> <span class="operator">-</span><span class="number">2</span><span class="operator">,</span>
                        thisDevice<span class="operator">-</span><span class="operator">&gt;</span>boundingRect()<span class="operator">.</span>height() <span class="operator">/</span> <span class="operator">-</span><span class="number">2</span>);
     addItem(thisDevice);

     <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> radius <span class="operator">=</span> Cloud<span class="operator">::</span>getRadiusForState(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Active);
     <span class="type"><a href="qgraphicsellipseitem.html">QGraphicsEllipseItem</a></span> <span class="operator">*</span>orbit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsellipseitem.html">QGraphicsEllipseItem</a></span>(<span class="operator">-</span>radius<span class="operator">,</span> <span class="operator">-</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius);
     orbit<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="qcolor.html">QColor</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>green));
     addItem(orbit);
     radius <span class="operator">=</span> Cloud<span class="operator">::</span>getRadiusForState(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Discovered);
     orbit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsellipseitem.html">QGraphicsEllipseItem</a></span>(<span class="operator">-</span>radius<span class="operator">,</span> <span class="operator">-</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius);
     orbit<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="qcolor.html">QColor</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>blue));
     addItem(orbit);
     radius <span class="operator">=</span> Cloud<span class="operator">::</span>getRadiusForState(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Defined);
     orbit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsellipseitem.html">QGraphicsEllipseItem</a></span>(<span class="operator">-</span>radius<span class="operator">,</span> <span class="operator">-</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius);
     orbit<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="qcolor.html">QColor</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>darkGray));
     addItem(orbit);
     radius <span class="operator">=</span> Cloud<span class="operator">::</span>getRadiusForState(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Undefined);
     orbit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicsellipseitem.html">QGraphicsEllipseItem</a></span>(<span class="operator">-</span>radius<span class="operator">,</span> <span class="operator">-</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius<span class="operator">,</span> <span class="number">2</span><span class="operator">*</span>radius);
     orbit<span class="operator">-</span><span class="operator">&gt;</span>setPen(<span class="type"><a href="qcolor.html">QColor</a></span>(<span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>lightGray));
     addItem(orbit);

     connect(<span class="operator">&amp;</span>manager<span class="operator">,</span> SIGNAL(configurationAdded(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(configurationAdded(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span>)));
     connect(<span class="operator">&amp;</span>manager<span class="operator">,</span> SIGNAL(configurationRemoved(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(configurationRemoved(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span>)));
     connect(<span class="operator">&amp;</span>manager<span class="operator">,</span> SIGNAL(configurationChanged(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span>))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(configurationChanged(<span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span>)));

     <span class="type"><a href="qtimer.html">QTimer</a></span><span class="operator">::</span>singleShot(<span class="number">0</span><span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(updateConfigurations()));
 }</pre>
<p>Populating the scene with the initial list of known network configuration is easy. Iterate over the list returned by <a href="qnetworkconfigurationmanager.html#allConfigurations">QNetworkConfigurationManager::allConfigurations</a>(), calling our configurationAdded() slot on each one.</p>
<p>We finishing off by calling cloudMoved() to ensure that animations are started.</p>
<pre class="cpp"> <span class="type">void</span> BearerCloud<span class="operator">::</span>updateConfigurations()
 {
     <span class="type"><a href="qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">&gt;</span> allConfigurations <span class="operator">=</span> manager<span class="operator">.</span>allConfigurations();

     <span class="keyword">while</span> (<span class="operator">!</span>allConfigurations<span class="operator">.</span>isEmpty())
         configurationAdded(allConfigurations<span class="operator">.</span>takeFirst());

     cloudMoved();
 }</pre>
<p>The configurationAdded() slot gets called when a new network configuration is added to the system.</p>
<p>It stores the <a href="qnetworkconfiguration.html#identifier">identifier</a> of the network configuration in the <i>configStates</i> map, which is used to keep a count of the number of network configurations in each state. This in turn is used to calculate the initial position of new Cloud objects.</p>
<p>Next we create a new Cloud object for this network configuration. Set its initial position and store it in the <i>configurations</i> hash.</p>
<p>The last step is to add it to the scene by calling <a href="qgraphicsscene.html#addItem">QGraphicsScene::addItem</a>().</p>
<pre class="cpp"> <span class="type">void</span> BearerCloud<span class="operator">::</span>configurationAdded(<span class="keyword">const</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span> <span class="operator">&amp;</span>config)
 {
     <span class="keyword">const</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>StateFlags state <span class="operator">=</span> config<span class="operator">.</span>state();

     configStates<span class="operator">.</span>insert(state<span class="operator">,</span> config<span class="operator">.</span>identifier());

     <span class="keyword">const</span> <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> radius <span class="operator">=</span> Cloud<span class="operator">::</span>getRadiusForState(state);
     <span class="keyword">const</span> <span class="type">int</span> count <span class="operator">=</span> configStates<span class="operator">.</span>count(state);
     <span class="keyword">const</span> <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> angle <span class="operator">=</span> <span class="number">2</span> <span class="operator">*</span> M_PI <span class="operator">/</span> count;

     Cloud <span class="operator">*</span>item <span class="operator">=</span> <span class="keyword">new</span> Cloud(config);
     configurations<span class="operator">.</span>insert(config<span class="operator">.</span>identifier()<span class="operator">,</span> item);

     item<span class="operator">-</span><span class="operator">&gt;</span>setPos(radius <span class="operator">*</span> cos((count<span class="operator">-</span><span class="number">1</span>) <span class="operator">*</span> angle <span class="operator">+</span> offset<span class="operator">[</span>state<span class="operator">]</span>)<span class="operator">,</span>
                  radius <span class="operator">*</span> sin((count<span class="operator">-</span><span class="number">1</span>) <span class="operator">*</span> angle <span class="operator">+</span> offset<span class="operator">[</span>state<span class="operator">]</span>));

     addItem(item);

     cloudMoved();
 }</pre>
<p>The configurationRemoved() slot gets called when a network configuration is removed from the system.</p>
<p>First we remove all references to the network configuration from the <i>configStates</i> and <i>configurations</i> member variables.</p>
<p>Next we initiate animation by setting a final scale value on the Cloud object associated with the removed network configuration.</p>
<p>Finally we flag the Cloud object to delete itself after it has finished animating.</p>
<pre class="cpp"> <span class="type">void</span> BearerCloud<span class="operator">::</span>configurationRemoved(<span class="keyword">const</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span> <span class="operator">&amp;</span>config)
 {
     foreach (<span class="keyword">const</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>StateFlags <span class="operator">&amp;</span>state<span class="operator">,</span> configStates<span class="operator">.</span>uniqueKeys())
         configStates<span class="operator">.</span>remove(state<span class="operator">,</span> config<span class="operator">.</span>identifier());

     Cloud <span class="operator">*</span>item <span class="operator">=</span> configurations<span class="operator">.</span>take(config<span class="operator">.</span>identifier());

     item<span class="operator">-</span><span class="operator">&gt;</span>setFinalScale(<span class="number">0.0</span>);
     item<span class="operator">-</span><span class="operator">&gt;</span>setDeleteAfterAnimation(<span class="keyword">true</span>);

     cloudMoved();
 }</pre>
<p>The Cloud object will take care of most of the work required when a network configuration changes. All we do in the configurationChanged() slot is update the <i>configStates</i> member variable.</p>
<pre class="cpp"> <span class="type">void</span> BearerCloud<span class="operator">::</span>configurationChanged(<span class="keyword">const</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span> <span class="operator">&amp;</span>config)
 {
     foreach (<span class="keyword">const</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>StateFlags <span class="operator">&amp;</span>state<span class="operator">,</span> configStates<span class="operator">.</span>uniqueKeys())
         configStates<span class="operator">.</span>remove(state<span class="operator">,</span> config<span class="operator">.</span>identifier());

     configStates<span class="operator">.</span>insert(config<span class="operator">.</span>state()<span class="operator">,</span> config<span class="operator">.</span>identifier());

     cloudMoved();
 }</pre>
<a name="responding-to-changes"></a>
<h2>Responding to changes</h2>
<p>Each network session and associated network configuration known to the system is represented in the scene as a Cloud object.</p>
<p>In the Cloud constructor we first initialize member variables. Then we create a new <a href="qnetworksession.html">QNetworkSession</a> object bound to the network configuration. Next we connect the <a href="qnetworksession.html">QNetworkSession</a> signals which we use to monitor it for state changes.</p>
<p>Next we set some <a href="qgraphicsitem.html">QGraphicsItem</a> properties. The <a href="qgraphicsitem.html#GraphicsItemFlag-enum">QGraphicsItem::ItemIsMovable</a> flag enables mouse interaction with the Cloud object.</p>
<p>The Cloud object consists of an icon and a text caption, these are constructed here. We will assign values to them later, as these will change as the sessions state changes.</p>
<p>Next we set the initial animation state and call our newConfigurationActivated() slot to finish setting up the Cloud object based on the state of network session.</p>
<pre class="cpp"> Cloud<span class="operator">::</span>Cloud(<span class="keyword">const</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span> <span class="operator">&amp;</span>config<span class="operator">,</span> <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span> <span class="operator">*</span>parent)
 :   <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span>(parent)<span class="operator">,</span> configuration(config)<span class="operator">,</span> deleteAfterAnimation(<span class="keyword">false</span>)
 {
     session <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span>(configuration<span class="operator">,</span> <span class="keyword">this</span>);
     connect(session<span class="operator">,</span> SIGNAL(newConfigurationActivated())<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(newConfigurationActivated()));
     connect(session<span class="operator">,</span> SIGNAL(stateChanged(<span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>State))<span class="operator">,</span>
             <span class="keyword">this</span><span class="operator">,</span> SLOT(stateChanged(<span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>State)));

     setFlag(ItemIsMovable);
 <span class="preprocessor">#if (QT_VERSION &gt;= QT_VERSION_CHECK(4, 6, 0))</span>
     setFlag(ItemSendsGeometryChanges);
 <span class="preprocessor">#endif</span>
     setZValue(<span class="number">1</span>);

     icon <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicssvgitem.html">QGraphicsSvgItem</a></span>(<span class="keyword">this</span>);
     text <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgraphicstextitem.html">QGraphicsTextItem</a></span>(<span class="keyword">this</span>);

     currentScale <span class="operator">=</span> <span class="number">0</span>;
     finalScale <span class="operator">=</span> <span class="number">1</span>;
     setTransform(<span class="type"><a href="qtransform.html">QTransform</a></span><span class="operator">::</span>fromScale(currentScale<span class="operator">,</span> currentScale)<span class="operator">,</span> <span class="keyword">false</span>);
     setOpacity(<span class="number">0</span>);

     newConfigurationActivated();
 }</pre>
<p>The newConfigurationActivated() slot is called when a session has successfully roamed from one access point to another.</p>
<p>The first thing we do is set the icon, inserting it into a shared SVG renderer cache if it is not already available. Next we set the text caption to the name of the network configuration.</p>
<p>We then set the position of the icon and text caption so that they are centered horizontally.</p>
<p>Finally we call our stateChanged() slot.</p>
<pre class="cpp"> <span class="type">void</span> Cloud<span class="operator">::</span>newConfigurationActivated()
 {
     <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>BearerType bearerType <span class="operator">=</span> configuration<span class="operator">.</span>bearerType();
     <span class="keyword">if</span> (<span class="operator">!</span>svgCache<span class="operator">.</span>contains(bearerType)) {
         <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span> <span class="operator">*</span>renderer <span class="operator">=</span> <span class="number">0</span>;
         <span class="keyword">switch</span> (bearerType) {
         <span class="keyword">case</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>BearerWLAN:
             renderer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span>(QLatin1String(<span class="string">&quot;:wlan.svg&quot;</span>));
             <span class="keyword">break</span>;
         <span class="keyword">case</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>BearerEthernet:
             renderer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span>(QLatin1String(<span class="string">&quot;:lan.svg&quot;</span>));
             <span class="keyword">break</span>;
         <span class="keyword">case</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>Bearer2G:
             renderer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span>(QLatin1String(<span class="string">&quot;:cell.svg&quot;</span>));
             <span class="keyword">break</span>;
         <span class="keyword">case</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>BearerBluetooth:
             renderer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span>(QLatin1String(<span class="string">&quot;:bluetooth.svg&quot;</span>));
             <span class="keyword">break</span>;
         <span class="keyword">case</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>BearerCDMA2000:
         <span class="keyword">case</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>BearerWCDMA:
         <span class="keyword">case</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>BearerHSPA:
             renderer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span>(QLatin1String(<span class="string">&quot;:umts.svg&quot;</span>));
             <span class="keyword">break</span>;
         <span class="keyword">default</span>:
             renderer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsvgrenderer.html">QSvgRenderer</a></span>(QLatin1String(<span class="string">&quot;:unknown.svg&quot;</span>));
         }

         <span class="keyword">if</span> (renderer)
             svgCache<span class="operator">.</span>insert(bearerType<span class="operator">,</span> renderer);
     }

     icon<span class="operator">-</span><span class="operator">&gt;</span>setSharedRenderer(svgCache<span class="operator">[</span>bearerType<span class="operator">]</span>);

     <span class="keyword">if</span> (configuration<span class="operator">.</span>name()<span class="operator">.</span>isEmpty()) {
         text<span class="operator">-</span><span class="operator">&gt;</span>setPlainText(tr(<span class="string">&quot;HIDDEN NETWORK&quot;</span>));
     } <span class="keyword">else</span> {
         <span class="keyword">if</span> (configuration<span class="operator">.</span>type() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qnetworkconfiguration.html">QNetworkConfiguration</a></span><span class="operator">::</span>ServiceNetwork)
             text<span class="operator">-</span><span class="operator">&gt;</span>setHtml(<span class="string">&quot;&lt;b&gt;&quot;</span> <span class="operator">+</span> configuration<span class="operator">.</span>name() <span class="operator">+</span> <span class="string">&quot;&lt;/b&gt;&quot;</span>);
         <span class="keyword">else</span>
             text<span class="operator">-</span><span class="operator">&gt;</span>setPlainText(configuration<span class="operator">.</span>name());
     }

     <span class="keyword">const</span> <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span> height <span class="operator">=</span> icon<span class="operator">-</span><span class="operator">&gt;</span>boundingRect()<span class="operator">.</span>height() <span class="operator">+</span> text<span class="operator">-</span><span class="operator">&gt;</span>boundingRect()<span class="operator">.</span>height();

     icon<span class="operator">-</span><span class="operator">&gt;</span>setPos(icon<span class="operator">-</span><span class="operator">&gt;</span>boundingRect()<span class="operator">.</span>width() <span class="operator">/</span> <span class="operator">-</span><span class="number">2</span><span class="operator">,</span> height <span class="operator">/</span> <span class="operator">-</span><span class="number">2</span>);

     text<span class="operator">-</span><span class="operator">&gt;</span>setPos(text<span class="operator">-</span><span class="operator">&gt;</span>boundingRect()<span class="operator">.</span>width() <span class="operator">/</span> <span class="operator">-</span><span class="number">2</span><span class="operator">,</span>
                  height <span class="operator">/</span> <span class="number">2</span> <span class="operator">-</span> text<span class="operator">-</span><span class="operator">&gt;</span>boundingRect()<span class="operator">.</span>height());

     stateChanged(session<span class="operator">-</span><span class="operator">&gt;</span>state());
 }</pre>
<p>The stateChanged() slot is called when the session state changes.</p>
<p>In this slot we set lower the opacity of Cloud objects with network sessions that cannot be <a href="qnetworksession.html#open">opened</a>, and set a detailed tool tip describing the sessions state.</p>
<pre class="cpp"> <span class="type">void</span> Cloud<span class="operator">::</span>stateChanged(<span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>State state)
 {
     <span class="keyword">if</span> (configuration<span class="operator">.</span>name()<span class="operator">.</span>isEmpty())
         finalOpacity <span class="operator">=</span> <span class="type"><a href="qtglobal.html#qreal-typedef">qreal</a></span>(<span class="number">0.1</span>);
     <span class="keyword">else</span> <span class="keyword">if</span> (session<span class="operator">-</span><span class="operator">&gt;</span>state() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>NotAvailable)
         finalOpacity <span class="operator">=</span> <span class="number">0.5</span>;
     <span class="keyword">else</span>
         finalOpacity <span class="operator">=</span> <span class="number">1.0</span>;

 <span class="preprocessor">#if !defined(Q_WS_MAEMO_5) &amp;&amp; !defined(Q_WS_MAEMO_6) &amp;&amp; \
     !defined(Q_OS_SYMBIAN) &amp;&amp; !defined(Q_OS_WINCE)</span>
     <span class="type"><a href="qstring.html">QString</a></span> tooltip;

     <span class="keyword">if</span> (configuration<span class="operator">.</span>name()<span class="operator">.</span>isEmpty())
         tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;b&gt;HIDDEN NETWORK&lt;/b&gt;&lt;br&gt;&quot;</span>);
     <span class="keyword">else</span>
         tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;b&gt;%1&lt;/b&gt;&lt;br&gt;&quot;</span>)<span class="operator">.</span>arg(configuration<span class="operator">.</span>name());

 <span class="preprocessor">#ifndef QT_NO_NETWORKINTERFACE</span>
     <span class="keyword">const</span> <span class="type"><a href="qnetworkinterface.html">QNetworkInterface</a></span> interface <span class="operator">=</span> session<span class="operator">-</span><span class="operator">&gt;</span>interface();
     <span class="keyword">if</span> (interface<span class="operator">.</span>isValid())
         tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;br&gt;Interface: %1&quot;</span>)<span class="operator">.</span>arg(interface<span class="operator">.</span>humanReadableName());
     tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;br&gt;Id: %1&quot;</span>)<span class="operator">.</span>arg(configuration<span class="operator">.</span>identifier());
 <span class="preprocessor">#endif</span>

     <span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> bearerTypeName <span class="operator">=</span> configuration<span class="operator">.</span>bearerTypeName();
     <span class="keyword">if</span> (<span class="operator">!</span>bearerTypeName<span class="operator">.</span>isEmpty())
         tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;br&gt;Bearer: %1&quot;</span>)<span class="operator">.</span>arg(bearerTypeName);

     <span class="type"><a href="qstring.html">QString</a></span> s <span class="operator">=</span> tr(<span class="string">&quot;&lt;br&gt;State: %1 (%2)&quot;</span>);
     <span class="keyword">switch</span> (state) {
     <span class="keyword">case</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>Invalid:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Invalid&quot;</span>));
         <span class="keyword">break</span>;
     <span class="keyword">case</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>NotAvailable:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Not Available&quot;</span>));
         <span class="keyword">break</span>;
     <span class="keyword">case</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>Connecting:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Connecting&quot;</span>));
         <span class="keyword">break</span>;
     <span class="keyword">case</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>Connected:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Connected&quot;</span>));
         <span class="keyword">break</span>;
     <span class="keyword">case</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>Closing:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Closing&quot;</span>));
         <span class="keyword">break</span>;
     <span class="keyword">case</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>Disconnected:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Disconnected&quot;</span>));
         <span class="keyword">break</span>;
     <span class="keyword">case</span> <span class="type"><a href="qnetworksession.html">QNetworkSession</a></span><span class="operator">::</span>Roaming:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Roaming&quot;</span>));
         <span class="keyword">break</span>;
     <span class="keyword">default</span>:
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Unknown&quot;</span>));
     }

     <span class="keyword">if</span> (session<span class="operator">-</span><span class="operator">&gt;</span>isOpen())
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Open&quot;</span>));
     <span class="keyword">else</span>
         s <span class="operator">=</span> s<span class="operator">.</span>arg(tr(<span class="string">&quot;Closed&quot;</span>));

     tooltip <span class="operator">+</span><span class="operator">=</span> s;

     tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;br&gt;&lt;br&gt;Active time: %1 seconds&quot;</span>)<span class="operator">.</span>arg(session<span class="operator">-</span><span class="operator">&gt;</span>activeTime());
     tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;br&gt;Received data: %1 bytes&quot;</span>)<span class="operator">.</span>arg(session<span class="operator">-</span><span class="operator">&gt;</span>bytesReceived());
     tooltip <span class="operator">+</span><span class="operator">=</span> tr(<span class="string">&quot;&lt;br&gt;Sent data: %1 bytes&quot;</span>)<span class="operator">.</span>arg(session<span class="operator">-</span><span class="operator">&gt;</span>bytesWritten());

     setToolTip(tooltip);
 <span class="preprocessor">#else</span>
     Q_UNUSED(state);
 <span class="preprocessor">#endif</span>
 }</pre>
<p>In our reimplementation of the <a href="qgraphicsitem.html#mouseDoubleClickEvent">QGraphicsItem::mouseDoubleClickEvent</a>() function we call <a href="qnetworksession.html#open">QNetworkSession::open</a>() or <a href="qnetworksession.html#close">QNetworkSession::close</a>() to open or close the session in response to a double left click.</p>
<pre class="cpp"> <span class="type">void</span> Cloud<span class="operator">::</span>mouseDoubleClickEvent(<span class="type"><a href="qgraphicsscenemouseevent.html">QGraphicsSceneMouseEvent</a></span> <span class="operator">*</span>event)
 {
     <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>button() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="qt.html">Qt</a></span><span class="operator">::</span>LeftButton) {
         <span class="keyword">if</span> (session<span class="operator">-</span><span class="operator">&gt;</span>isOpen())
             session<span class="operator">-</span><span class="operator">&gt;</span>close();
         <span class="keyword">else</span>
             session<span class="operator">-</span><span class="operator">&gt;</span>open();

         event<span class="operator">-</span><span class="operator">&gt;</span>accept();
     }
 }</pre>
<p>As we support the user dragging Cloud objects around we need to restart animations when the position of the Cloud object changes. This is accomplished by reimplementing the QGraphicsItem::itemChanged() function and calling the cloudMoved() function of the BearerCloud object.</p>
<pre class="cpp"> <span class="type"><a href="qvariant.html">QVariant</a></span> Cloud<span class="operator">::</span>itemChange(GraphicsItemChange change<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qvariant.html">QVariant</a></span> <span class="operator">&amp;</span>value)
 {
     <span class="keyword">switch</span> (change) {
     <span class="keyword">case</span> ItemPositionHasChanged:
         <span class="keyword">if</span> (BearerCloud <span class="operator">*</span>bearercloud <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>BearerCloud <span class="operator">*</span><span class="operator">&gt;</span>(scene()))
             bearercloud<span class="operator">-</span><span class="operator">&gt;</span>cloudMoved();
     <span class="keyword">default</span>:
         ;
     };

     <span class="keyword">return</span> <span class="type"><a href="qgraphicsitem.html">QGraphicsItem</a></span><span class="operator">::</span>itemChange(change<span class="operator">,</span> value);
 }</pre>
<p>The remainder of the code for the Cloud object implements the animations. The calculateForces() function calculates the new position of the Cloud object based on the position of all the other Cloud objects in the scene. The new position is set when the advance() function is called to update the Cloud object for the current animation frame.</p>
</div>
<!-- @@@network/bearercloud -->
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>