/usr/share/qt5/doc/qtbluetooth/qtbluetooth-pingpong-pingpong-cpp.html is in qtconnectivity5-doc-html 5.7.1~20161021-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>pingpong.cpp Example File | Qt Bluetooth 5.7</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
window.onload = function(){document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");};
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.7</td><td ><a href="qtbluetooth-index.html">Qt Bluetooth</a></td><td ><a href="qtbluetooth-pingpong-example.html">Bluetooth QML Ping Pong example</a></td><td >pingpong.cpp Example File</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.7.1 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">pingpong.cpp Example File</h1>
<span class="subtitle">pingpong/pingpong.cpp</span>
<!-- $$$pingpong/pingpong.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">
<span class="comment">/***************************************************************************
**
** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/</span>
<span class="preprocessor">#include "pingpong.h"</span>
<span class="preprocessor">#include <QDebug></span>
PingPong<span class="operator">::</span>PingPong():
m_serverInfo(<span class="number">0</span>)<span class="operator">,</span> socket(<span class="number">0</span>)<span class="operator">,</span> discoveryAgent(<span class="number">0</span>)<span class="operator">,</span> interval(<span class="number">5</span>)<span class="operator">,</span> m_resultLeft(<span class="number">0</span>)<span class="operator">,</span> m_resultRight(<span class="number">0</span>)<span class="operator">,</span>
m_showDialog(<span class="keyword">false</span>)<span class="operator">,</span> m_role(<span class="number">0</span>)<span class="operator">,</span> m_proportionX(<span class="number">0</span>)<span class="operator">,</span> m_proportionY(<span class="number">0</span>)<span class="operator">,</span> m_serviceFound(<span class="keyword">false</span>)
{
m_timer <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QTimer</span>(<span class="keyword">this</span>);
connect(m_timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(update()));
}
PingPong<span class="operator">::</span><span class="operator">~</span>PingPong()
{
<span class="keyword">delete</span> m_timer;
<span class="keyword">delete</span> m_serverInfo;
<span class="keyword">delete</span> socket;
<span class="keyword">delete</span> discoveryAgent;
}
<span class="type">void</span> PingPong<span class="operator">::</span>startGame()
{
m_showDialog <span class="operator">=</span> <span class="keyword">false</span>;
<span class="keyword">emit</span> showDialogChanged();
<span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>)
updateDirection();
m_timer<span class="operator">-</span><span class="operator">></span>start(<span class="number">50</span>);
}
<span class="type">void</span> PingPong<span class="operator">::</span>update()
{
<span class="type">QByteArray</span> size;
<span class="comment">// Server is only updating the coordinates</span>
<span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) {
checkBoundaries();
m_ballPreviousX <span class="operator">=</span> m_ballX;
m_ballPreviousY <span class="operator">=</span> m_ballY;
m_ballY <span class="operator">=</span> m_direction<span class="operator">*</span>(m_ballX<span class="operator">+</span>interval) <span class="operator">-</span> m_direction<span class="operator">*</span>m_ballX <span class="operator">+</span> m_ballY;
m_ballX <span class="operator">=</span> m_ballX <span class="operator">+</span> interval;
size<span class="operator">.</span>setNum(m_ballX);
size<span class="operator">.</span>append(<span class="char">' '</span>);
<span class="type">QByteArray</span> size1;
size1<span class="operator">.</span>setNum(m_ballY);
size<span class="operator">.</span>append(size1);
size<span class="operator">.</span>append(<span class="char">' '</span>);
size1<span class="operator">.</span>setNum(m_leftBlockY);
size<span class="operator">.</span>append(size1);
size<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(size<span class="operator">.</span>constData());
<span class="keyword">emit</span> ballChanged();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) {
size<span class="operator">.</span>setNum(m_rightBlockY);
size<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(size<span class="operator">.</span>constData());
}
}
<span class="type">void</span> PingPong<span class="operator">::</span>setSize(<span class="keyword">const</span> <span class="type">float</span> <span class="operator">&</span>x<span class="operator">,</span> <span class="keyword">const</span> <span class="type">float</span> <span class="operator">&</span>y)
{
m_boardWidth <span class="operator">=</span> x;
m_boardHeight <span class="operator">=</span> y;
m_targetX <span class="operator">=</span> m_boardWidth;
m_targetY <span class="operator">=</span> m_boardHeight<span class="operator">/</span><span class="number">2</span>;
m_ballPreviousX <span class="operator">=</span> m_ballX <span class="operator">=</span> m_boardWidth<span class="operator">/</span><span class="number">2</span>;
m_ballPreviousY <span class="operator">=</span> m_ballY <span class="operator">=</span> m_boardHeight <span class="operator">-</span> m_boardWidth<span class="operator">/</span><span class="number">54</span>;
<span class="keyword">emit</span> ballChanged();
}
<span class="type">void</span> PingPong<span class="operator">::</span>updateBall(<span class="keyword">const</span> <span class="type">float</span> <span class="operator">&</span>bX<span class="operator">,</span> <span class="keyword">const</span> <span class="type">float</span> <span class="operator">&</span>bY)
{
m_ballX <span class="operator">=</span> bX;
m_ballY <span class="operator">=</span> bY;
}
<span class="type">void</span> PingPong<span class="operator">::</span>updateLeftBlock(<span class="keyword">const</span> <span class="type">float</span> <span class="operator">&</span>lY)
{
m_leftBlockY <span class="operator">=</span> lY;
}
<span class="type">void</span> PingPong<span class="operator">::</span>updateRightBlock(<span class="keyword">const</span> <span class="type">float</span> <span class="operator">&</span>rY)
{
m_rightBlockY <span class="operator">=</span> rY;
}
<span class="type">void</span> PingPong<span class="operator">::</span>checkBoundaries()
{
<span class="type">float</span> ballWidth <span class="operator">=</span> m_boardWidth<span class="operator">/</span><span class="number">54</span>;
<span class="type">float</span> blockSize <span class="operator">=</span> m_boardWidth<span class="operator">/</span><span class="number">27</span>;
<span class="type">float</span> blockHeight <span class="operator">=</span> m_boardHeight<span class="operator">/</span><span class="number">5</span>;
<span class="keyword">if</span> (((m_ballX <span class="operator">+</span> ballWidth) <span class="operator">></span> (m_boardWidth <span class="operator">-</span> blockSize)) <span class="operator">&</span><span class="operator">&</span> ((m_ballY <span class="operator">+</span> ballWidth) <span class="operator"><</span> (m_rightBlockY <span class="operator">+</span> blockHeight))
<span class="operator">&</span><span class="operator">&</span> (m_ballY <span class="operator">></span> m_rightBlockY)) {
m_targetY <span class="operator">=</span> <span class="number">2</span> <span class="operator">*</span> m_ballY <span class="operator">-</span> m_ballPreviousY;
m_targetX <span class="operator">=</span> m_ballPreviousX;
interval <span class="operator">=</span> <span class="operator">-</span><span class="number">5</span>;
updateDirection();
}
<span class="keyword">else</span> <span class="keyword">if</span> ((m_ballX <span class="operator"><</span> blockSize) <span class="operator">&</span><span class="operator">&</span> ((m_ballY <span class="operator">+</span> ballWidth) <span class="operator"><</span> (m_leftBlockY <span class="operator">+</span> blockHeight))
<span class="operator">&</span><span class="operator">&</span> (m_ballY <span class="operator">></span> m_leftBlockY)) {
m_targetY <span class="operator">=</span> <span class="number">2</span> <span class="operator">*</span> m_ballY <span class="operator">-</span> m_ballPreviousY;
m_targetX <span class="operator">=</span> m_ballPreviousX;
interval <span class="operator">=</span> <span class="number">5</span>;
updateDirection();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_ballY <span class="operator"><</span> <span class="number">0</span> <span class="operator">|</span><span class="operator">|</span> (m_ballY <span class="operator">+</span> ballWidth <span class="operator">></span> m_boardHeight)) {
m_targetY <span class="operator">=</span> m_ballPreviousY;
m_targetX <span class="operator">=</span> m_ballX <span class="operator">+</span> interval;
updateDirection();
}
<span class="keyword">else</span> <span class="keyword">if</span> ((m_ballX <span class="operator">+</span> ballWidth) <span class="operator">></span> m_boardWidth) {
m_resultLeft<span class="operator">+</span><span class="operator">+</span>;
m_targetX <span class="operator">=</span> m_boardWidth;
m_targetY <span class="operator">=</span> m_boardHeight<span class="operator">/</span><span class="number">2</span>;
m_ballPreviousX <span class="operator">=</span> m_ballX <span class="operator">=</span> m_boardWidth<span class="operator">/</span><span class="number">2</span>;
m_ballPreviousY <span class="operator">=</span> m_ballY <span class="operator">=</span> m_boardHeight <span class="operator">-</span> m_boardWidth<span class="operator">/</span><span class="number">54</span>;
updateDirection();
checkResult();
<span class="type">QByteArray</span> result;
result<span class="operator">.</span>append(<span class="string">"result "</span>);
<span class="type">QByteArray</span> res;
res<span class="operator">.</span>setNum(m_resultLeft);
result<span class="operator">.</span>append(res);
result<span class="operator">.</span>append(<span class="char">' '</span>);
res<span class="operator">.</span>setNum(m_resultRight);
result<span class="operator">.</span>append(res);
result<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(result);
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> result;
<span class="keyword">emit</span> resultChanged();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_ballX <span class="operator"><</span> <span class="number">0</span>) {
m_resultRight<span class="operator">+</span><span class="operator">+</span>;
m_targetX <span class="operator">=</span> <span class="number">0</span>;
m_targetY <span class="operator">=</span> m_boardHeight<span class="operator">/</span><span class="number">2</span>;
m_ballPreviousX <span class="operator">=</span> m_ballX <span class="operator">=</span> m_boardWidth<span class="operator">/</span><span class="number">2</span>;
m_ballPreviousY <span class="operator">=</span> m_ballY <span class="operator">=</span> m_boardHeight <span class="operator">-</span> m_boardWidth<span class="operator">/</span><span class="number">54</span>;
updateDirection();
checkResult();
<span class="type">QByteArray</span> result;
result<span class="operator">.</span>append(<span class="string">"result "</span>);
<span class="type">QByteArray</span> res;
res<span class="operator">.</span>setNum(m_resultLeft);
result<span class="operator">.</span>append(res);
result<span class="operator">.</span>append(<span class="char">' '</span>);
res<span class="operator">.</span>setNum(m_resultRight);
result<span class="operator">.</span>append(res);
result<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(result);
<span class="keyword">emit</span> resultChanged();
}
}
<span class="type">void</span> PingPong<span class="operator">::</span>checkResult()
{
<span class="keyword">if</span> (m_resultRight <span class="operator">=</span><span class="operator">=</span> <span class="number">10</span> <span class="operator">&</span><span class="operator">&</span> m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) {
setMessage(<span class="string">"Game over. You win!"</span>);
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_resultRight <span class="operator">=</span><span class="operator">=</span> <span class="number">10</span> <span class="operator">&</span><span class="operator">&</span> m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) {
setMessage(<span class="string">"Game over. You lose!"</span>);
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_resultLeft <span class="operator">=</span><span class="operator">=</span> <span class="number">10</span> <span class="operator">&</span><span class="operator">&</span> m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) {
setMessage(<span class="string">"Game over. You win!"</span>);
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_resultLeft <span class="operator">=</span><span class="operator">=</span> <span class="number">10</span> <span class="operator">&</span><span class="operator">&</span> m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) {
setMessage(<span class="string">"Game over. You lose!"</span>);
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
}
<span class="type">void</span> PingPong<span class="operator">::</span>updateDirection()
{
m_direction <span class="operator">=</span> (m_targetY <span class="operator">-</span> m_ballY)<span class="operator">/</span>(m_targetX <span class="operator">-</span> m_ballX);
}
<span class="type">void</span> PingPong<span class="operator">::</span>startServer()
{
setMessage(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"Starting the server"</span>));
m_serverInfo <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothServer</span>(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>RfcommProtocol<span class="operator">,</span> <span class="keyword">this</span>);
connect(m_serverInfo<span class="operator">,</span> SIGNAL(newConnection())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(clientConnected()));
connect(m_serverInfo<span class="operator">,</span> SIGNAL(error(<span class="type">QBluetoothServer</span><span class="operator">::</span>Error))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(serverError(<span class="type">QBluetoothServer</span><span class="operator">::</span>Error)));
<span class="keyword">const</span> <span class="type">QBluetoothUuid</span> uuid(serviceUuid);
m_serverInfo<span class="operator">-</span><span class="operator">></span>listen(uuid<span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"PingPong server"</span>));
setMessage(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"Server started, waiting for the client. You are the left player."</span>));
<span class="comment">// m_role is set to 1 if it is a server</span>
m_role <span class="operator">=</span> <span class="number">1</span>;
<span class="keyword">emit</span> roleChanged();
}
<span class="type">void</span> PingPong<span class="operator">::</span>startClient()
{
discoveryAgent <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothServiceDiscoveryAgent</span>(<span class="type">QBluetoothAddress</span>());
connect(discoveryAgent<span class="operator">,</span> SIGNAL(serviceDiscovered(<span class="type">QBluetoothServiceInfo</span>))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(addService(<span class="type">QBluetoothServiceInfo</span>)));
connect(discoveryAgent<span class="operator">,</span> SIGNAL(finished())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(done()));
connect(discoveryAgent<span class="operator">,</span> SIGNAL(error(<span class="type">QBluetoothServiceDiscoveryAgent</span><span class="operator">::</span>Error))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(serviceScanError(<span class="type">QBluetoothServiceDiscoveryAgent</span><span class="operator">::</span>Error)));
discoveryAgent<span class="operator">-</span><span class="operator">></span>setUuidFilter(<span class="type">QBluetoothUuid</span>(serviceUuid));
discoveryAgent<span class="operator">-</span><span class="operator">></span>start(<span class="type">QBluetoothServiceDiscoveryAgent</span><span class="operator">::</span>FullDiscovery);
setMessage(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"Starting server discovery. You are the right player"</span>));
<span class="comment">// m_role is set to 2 if it is a client</span>
m_role <span class="operator">=</span> <span class="number">2</span>;
<span class="keyword">emit</span> roleChanged();
}
<span class="type">void</span> PingPong<span class="operator">::</span>clientConnected()
{
<span class="keyword">if</span> (<span class="operator">!</span>m_serverInfo<span class="operator">-</span><span class="operator">></span>hasPendingConnections()) {
setMessage(<span class="string">"FAIL: expected pending server connection"</span>);
<span class="keyword">return</span>;
}
socket <span class="operator">=</span> m_serverInfo<span class="operator">-</span><span class="operator">></span>nextPendingConnection();
<span class="keyword">if</span> (<span class="operator">!</span>socket)
<span class="keyword">return</span>;
socket<span class="operator">-</span><span class="operator">></span>setParent(<span class="keyword">this</span>);
connect(socket<span class="operator">,</span> SIGNAL(readyRead())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(readSocket()));
connect(socket<span class="operator">,</span> SIGNAL(disconnected())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(clientDisconnected()));
connect(socket<span class="operator">,</span> SIGNAL(error(<span class="type">QBluetoothSocket</span><span class="operator">::</span>SocketError))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(socketError(<span class="type">QBluetoothSocket</span><span class="operator">::</span>SocketError)));
setMessage(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"Client connected."</span>));
<span class="type">QByteArray</span> size;
size<span class="operator">.</span>setNum(m_boardWidth);
size<span class="operator">.</span>append(<span class="char">' '</span>);
<span class="type">QByteArray</span> size1;
size1<span class="operator">.</span>setNum(m_boardHeight);
size<span class="operator">.</span>append(size1);
size<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(size<span class="operator">.</span>constData());
}
<span class="type">void</span> PingPong<span class="operator">::</span>clientDisconnected()
{
setMessage(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"Client disconnected"</span>));
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
<span class="type">void</span> PingPong<span class="operator">::</span>socketError(<span class="type">QBluetoothSocket</span><span class="operator">::</span>SocketError error)
{
Q_UNUSED(error);
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
<span class="type">void</span> PingPong<span class="operator">::</span>serverError(<span class="type">QBluetoothServer</span><span class="operator">::</span>Error error)
{
Q_UNUSED(error);
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
<span class="type">void</span> PingPong<span class="operator">::</span>done()
{
<a href="../qtcore/qtglobal.html#qDebug">qDebug</a>() <span class="operator"><</span><span class="operator"><</span> <span class="string">"Service scan done"</span>;
<span class="keyword">if</span> (<span class="operator">!</span>m_serviceFound)
setMessage(<span class="string">"PingPong service not found"</span>);
}
<span class="type">void</span> PingPong<span class="operator">::</span>addService(<span class="keyword">const</span> <span class="type">QBluetoothServiceInfo</span> <span class="operator">&</span>service)
{
setMessage(<span class="string">"Service found. Setting parameters..."</span>);
socket <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QBluetoothSocket</span>(<span class="type">QBluetoothServiceInfo</span><span class="operator">::</span>RfcommProtocol);
socket<span class="operator">-</span><span class="operator">></span>connectToService(service);
connect(socket<span class="operator">,</span> SIGNAL(readyRead())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(readSocket()));
connect(socket<span class="operator">,</span> SIGNAL(connected())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(serverConnected()));
connect(socket<span class="operator">,</span> SIGNAL(disconnected())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(serverDisconnected()));
m_serviceFound <span class="operator">=</span> <span class="keyword">true</span>;
}
<span class="type">void</span> PingPong<span class="operator">::</span>serviceScanError(<span class="type">QBluetoothServiceDiscoveryAgent</span><span class="operator">::</span>Error error)
{
setMessage(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"Scanning error"</span>) <span class="operator">+</span> error);
}
bool PingPong<span class="operator">::</span>showDialog() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_showDialog;
}
<span class="type">QString</span> PingPong<span class="operator">::</span>message() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_message;
}
<span class="type">void</span> PingPong<span class="operator">::</span>serverConnected()
{
setMessage(<span class="string">"Server Connected"</span>);
<span class="type">QByteArray</span> size;
size<span class="operator">.</span>setNum(m_boardWidth);
size<span class="operator">.</span>append(<span class="char">' '</span>);
<span class="type">QByteArray</span> size1;
size1<span class="operator">.</span>setNum(m_boardHeight);
size<span class="operator">.</span>append(size1);
size<span class="operator">.</span>append(<span class="string">" \n"</span>);
socket<span class="operator">-</span><span class="operator">></span>write(size<span class="operator">.</span>constData());
}
<span class="type">void</span> PingPong<span class="operator">::</span>serverDisconnected()
{
setMessage(<span class="string">"Server Disconnected"</span>);
m_timer<span class="operator">-</span><span class="operator">></span>stop();
}
<span class="type">void</span> PingPong<span class="operator">::</span>readSocket()
{
<span class="keyword">if</span> (<span class="operator">!</span>socket)
<span class="keyword">return</span>;
<span class="keyword">const</span> <span class="type">char</span> sep <span class="operator">=</span> <span class="char">' '</span>;
<span class="type">QByteArray</span> line;
<span class="keyword">while</span> (socket<span class="operator">-</span><span class="operator">></span>canReadLine()) {
line <span class="operator">=</span> socket<span class="operator">-</span><span class="operator">></span>readLine();
<span class="comment">//qDebug() << QString::fromUtf8(line.constData(), line.length());</span>
<span class="keyword">if</span> (line<span class="operator">.</span>contains(<span class="string">"result"</span>)) {
<span class="type">QList</span><span class="operator"><</span><span class="type">QByteArray</span><span class="operator">></span> result <span class="operator">=</span> line<span class="operator">.</span>split(sep);
<span class="keyword">if</span> (result<span class="operator">.</span>size() <span class="operator">></span> <span class="number">2</span>) {
<span class="type">QByteArray</span> leftSide <span class="operator">=</span> result<span class="operator">.</span>at(<span class="number">1</span>);
<span class="type">QByteArray</span> rightSide <span class="operator">=</span> result<span class="operator">.</span>at(<span class="number">2</span>);
m_resultLeft <span class="operator">=</span> leftSide<span class="operator">.</span>toInt();
m_resultRight <span class="operator">=</span> rightSide<span class="operator">.</span>toInt();
<span class="keyword">emit</span> resultChanged();
checkResult();
}
}
}
<span class="keyword">if</span> ((m_proportionX <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span> <span class="operator">|</span><span class="operator">|</span> m_proportionY <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>)) {
<span class="type">QList</span><span class="operator"><</span><span class="type">QByteArray</span><span class="operator">></span> boardSize <span class="operator">=</span> line<span class="operator">.</span>split(sep);
<span class="keyword">if</span> (boardSize<span class="operator">.</span>size() <span class="operator">></span> <span class="number">1</span>) {
<span class="type">QByteArray</span> boardWidth <span class="operator">=</span> boardSize<span class="operator">.</span>at(<span class="number">0</span>);
<span class="type">QByteArray</span> boardHeight <span class="operator">=</span> boardSize<span class="operator">.</span>at(<span class="number">1</span>);
m_proportionX <span class="operator">=</span> m_boardWidth<span class="operator">/</span>boardWidth<span class="operator">.</span>toFloat();
m_proportionY <span class="operator">=</span> m_boardHeight<span class="operator">/</span>boardHeight<span class="operator">.</span>toFloat();
setMessage(<span class="string">"Screen adjusted. Get ready!"</span>);
<span class="type">QTimer</span><span class="operator">::</span>singleShot(<span class="number">3000</span><span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(startGame()));
}
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>) {
<span class="type">QList</span><span class="operator"><</span><span class="type">QByteArray</span><span class="operator">></span> boardSize <span class="operator">=</span> line<span class="operator">.</span>split(sep);
<span class="keyword">if</span> (boardSize<span class="operator">.</span>size() <span class="operator">></span> <span class="number">1</span>) {
<span class="type">QByteArray</span> rightBlockY <span class="operator">=</span> boardSize<span class="operator">.</span>at(<span class="number">0</span>);
m_rightBlockY <span class="operator">=</span> m_proportionY <span class="operator">*</span> rightBlockY<span class="operator">.</span>toFloat();
<span class="keyword">emit</span> rightBlockChanged();
}
}
<span class="keyword">else</span> <span class="keyword">if</span> (m_role <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) {
<span class="type">QList</span><span class="operator"><</span><span class="type">QByteArray</span><span class="operator">></span> boardSize <span class="operator">=</span> line<span class="operator">.</span>split(sep);
<span class="keyword">if</span> (boardSize<span class="operator">.</span>size() <span class="operator">></span> <span class="number">2</span>) {
<span class="type">QByteArray</span> ballX <span class="operator">=</span> boardSize<span class="operator">.</span>at(<span class="number">0</span>);
<span class="type">QByteArray</span> ballY <span class="operator">=</span> boardSize<span class="operator">.</span>at(<span class="number">1</span>);
<span class="type">QByteArray</span> leftBlockY <span class="operator">=</span> boardSize<span class="operator">.</span>at(<span class="number">2</span>);
m_ballX <span class="operator">=</span> m_proportionX <span class="operator">*</span> ballX<span class="operator">.</span>toFloat();
m_ballY <span class="operator">=</span> m_proportionY <span class="operator">*</span> ballY<span class="operator">.</span>toFloat();
m_leftBlockY <span class="operator">=</span> m_proportionY <span class="operator">*</span> leftBlockY<span class="operator">.</span>toFloat();
<span class="keyword">emit</span> leftBlockChanged();
<span class="keyword">emit</span> ballChanged();
}
}
}
<span class="type">void</span> PingPong<span class="operator">::</span>setMessage(<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>message)
{
m_showDialog <span class="operator">=</span> <span class="keyword">true</span>;
m_message <span class="operator">=</span> message;
<span class="keyword">emit</span> showDialogChanged();
}
<span class="type">int</span> PingPong<span class="operator">::</span>role() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_role;
}
<span class="type">int</span> PingPong<span class="operator">::</span>leftResult() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_resultLeft;
}
<span class="type">int</span> PingPong<span class="operator">::</span>rightResult() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_resultRight;
}
<span class="type">float</span> PingPong<span class="operator">::</span>ballX() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_ballX;
}
<span class="type">float</span> PingPong<span class="operator">::</span>ballY() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_ballY;
}
<span class="type">float</span> PingPong<span class="operator">::</span>leftBlockY() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_leftBlockY;
}
<span class="type">float</span> PingPong<span class="operator">::</span>rightBlockY() <span class="keyword">const</span>
{
<span class="keyword">return</span> m_rightBlockY;
}
</pre>
</div>
<!-- @@@pingpong/pingpong.cpp -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2016 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> 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.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|