/usr/share/qt5/doc/qtlocation/qtlocation-planespotter-example.html is in qtlocation5-doc-html 5.5.1-3ubuntu1.
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 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- planespotter.qdoc -->
<title>Plane Spotter (QML) | Qt Location 5.5</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<ul>
<li>Qt 5.5</li>
<li><a href="qtlocation-index.html">Qt Location</a></li>
<li>Plane Spotter (QML)</li>
<li id="buildversion">Qt 5.5.1 Reference Documentation</li>
</ul>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#overview">Overview</a></li>
<li class="level1"><a href="#steering-the-planes">Steering the Planes</a></li>
<li class="level2"><a href="#the-c-pilot">The C++ Pilot</a></li>
<li class="level2"><a href="#the-qml-pilot">The QML Pilot</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Plane Spotter (QML)</h1>
<span class="subtitle"></span>
<!-- $$$planespotter-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/planespotter.jpg" alt="" /></p><p>The <code>Plane Spotter</code> example demonstrates how to integrate location and positioning related C++ data types into QML and vice versa. This is useful when it is desirable to run CPU intensive position calculations in native environments but the results are supposed to be displayed using QML.</p>
<p>The example shows a map of Europe and airplanes on two routes across Europe. The first airplane commutes between Oslo and Berlin and the second airplane commutes between London and Berlin. The position tracking of each airplane is implemented in C++. The Oslo-Berlin plane is piloted in QML and the London-Berlin plane is commanded by a C++ pilot.</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<a name="overview"></a>
<h2 id="overview">Overview</h2>
<p>This example makes use of the <a href="../qtcore/qobject.html#Q_GADGET">Q_GADGET</a> feature as part of its position controller implementation. It permits direct integration of non-<a href="../qtcore/qobject.html">QObject</a> based C++ value types into QML.</p>
<p>The main purpose of the <code>PlaneController</code> class is to track the current coordinates of the plane at a given time. It exposes the position via its position property.</p>
<pre class="cpp"><span class="keyword">class</span> PlaneController: <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
Q_PROPERTY(<span class="type">QGeoCoordinate</span> position READ position WRITE setPosition NOTIFY positionChanged)
<span class="comment">// ...</span>
};</pre>
<p>The example's <code>main()</code> function is responsible for the binding of the <code>PlaneController</code> class instances into the QML context:</p>
<pre class="cpp"><span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
{
<span class="type">QGuiApplication</span> app(argc<span class="operator">,</span> argv);
PlaneController oslo2berlin;
PlaneController berlin2london;
<span class="type">QQmlApplicationEngine</span> engine;
engine<span class="operator">.</span>rootContext()<span class="operator">-</span><span class="operator">></span>setContextProperty(<span class="string">"oslo2Berlin"</span><span class="operator">,</span> <span class="operator">&</span>oslo2berlin);
engine<span class="operator">.</span>rootContext()<span class="operator">-</span><span class="operator">></span>setContextProperty(<span class="string">"berlin2London"</span><span class="operator">,</span> <span class="operator">&</span>berlin2london);
engine<span class="operator">.</span>load(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>(<span class="type"><a href="../qtcore/qstring.html#QStringLiteral">QStringLiteral</a></span>(<span class="string">"qrc:/planespotter.qml"</span>)));
<span class="keyword">return</span> app<span class="operator">.</span>exec();
}</pre>
<p>Similar to <a href="../qtcore/qobject.html">QObject</a> derived classes, QGeoCoordinate can be integrated without an additional QML wrapper.</p>
<a name="steering-the-planes"></a>
<h2 id="steering-the-planes">Steering the Planes</h2>
<p>As mentioned above, the primary purpose of <code>PlaneController</code> class is to track the current positions of the two planes (Oslo-Berlin and London-Berlin) and advertise them as a property to the QML layer. Its secondary purpose is to set and progress a plane along a given flight path. In a sense it can act as a pilot. This is very much like CoordinateAnimation which can animate the transition from one geo coordinate to another. This example demonstrates how the <code>PlaneController</code>'s position property is modified by C++ code using the PlaneController's own piloting abilities and by QML code using CoordinateAnimation as pilot. The Oslo-Berlin plane is animated using QML code and the London-Berlin plane is animated using C++ code.</p>
<p>No matter which pilot is used, the results to the pilot's actions are visible in C++ and QML and thus the example demonstrates unhindered and direct exchange of position data through the C++/QML boundary.</p>
<p>The visual representation of each <code>Plane</code> is done using the <a href="qml-qtlocation-mapquickitem.html">MapQuickItem</a> type which permits the embedding of arbitrary QtQuick items into a map:</p>
<pre class="qml"><span class="comment">// Plane.qml</span>
<span class="type"><a href="qml-qtlocation-mapquickitem.html">MapQuickItem</a></span> {
<span class="name">id</span>: <span class="name">plane</span>
property <span class="type">string</span> <span class="name">pilotName</span>;
property <span class="type">int</span> <span class="name">bearing</span>: <span class="number">0</span>;
<span class="name">anchorPoint</span>.x: <span class="name">image</span>.<span class="name">width</span><span class="operator">/</span><span class="number">2</span>
<span class="name">anchorPoint</span>.y: <span class="name">image</span>.<span class="name">height</span><span class="operator">/</span><span class="number">2</span>
<span class="name">sourceItem</span>: <span class="name">Grid</span> {
<span class="comment">//...</span>
}
}</pre>
<a name="the-c-pilot"></a>
<h3 >The C++ Pilot</h3>
<p>The C++ plane is steered by C++. The <code>from</code> and <code>to</code> property of the controller class set the origin and destination which the pilot uses to calculate the bearing for the plane:</p>
<pre class="cpp">Q_PROPERTY(<span class="type">QGeoCoordinate</span> from READ from WRITE setFrom NOTIFY fromChanged)
Q_PROPERTY(<span class="type">QGeoCoordinate</span> to READ to WRITE setTo NOTIFY toChanged)</pre>
<p>The pilot employs a <a href="../qtcore/qbasictimer.html">QBasicTimer</a> and <a href="../qtcore/qtimerevent.html">QTimerEvents</a> to constantly update the position. During each timer iteration <code>PlaneController::updatePosition()</code> is called and a new position calculated.</p>
<pre class="cpp"><span class="type">void</span> updatePosition()
{
<span class="comment">// simple progress animation</span>
<span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> progress;
<span class="type"><a href="../qtcore/qtime.html">QTime</a></span> current <span class="operator">=</span> <span class="type"><a href="../qtcore/qtime.html">QTime</a></span><span class="operator">::</span>currentTime();
<span class="keyword">if</span> (current <span class="operator">></span><span class="operator">=</span> finishTime) {
progress <span class="operator">=</span> <span class="number">1.0</span>;
timer<span class="operator">.</span>stop();
} <span class="keyword">else</span> {
progress <span class="operator">=</span> ((<span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span>)startTime<span class="operator">.</span>msecsTo(current) <span class="operator">/</span> ANIMATION_DURATION);
}
setPosition(<span class="type">QGeoProjection</span><span class="operator">::</span>coordinateInterpolation(
fromCoordinate<span class="operator">,</span> toCoordinate<span class="operator">,</span> easingCurve<span class="operator">.</span>valueForProgress(progress)));
<span class="keyword">if</span> (<span class="operator">!</span>timer<span class="operator">.</span>isActive())
<span class="keyword">emit</span> arrived();
}</pre>
<p>Once the new position is calculated, <code>setPosition()</code> is called and the subsequent change notification of the property pushes the new position to the QML layer.</p>
<p>The C++ plane is started by clicking on the plane:</p>
<pre class="qml"><span class="type">Plane</span> {
<span class="name">id</span>: <span class="name">cppPlane</span>
<span class="name">pilotName</span>: <span class="string">"C++"</span>
<span class="name">coordinate</span>: <span class="name">berlin2London</span>.<span class="name">position</span>
<span class="type">MouseArea</span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: {
<span class="keyword">if</span> (<span class="name">cppPlaneAnimation</span>.<span class="name">running</span> <span class="operator">||</span> <span class="name">berlin2London</span>.<span class="name">isFlying</span>()) {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Plane still in the air."</span>);
<span class="keyword">return</span>;
}
<span class="name">berlin2London</span>.<span class="name">swapDestinations</span>();
<span class="name">cppPlaneAnimation</span>.<span class="name">rotationDirection</span> <span class="operator">=</span> <span class="name">berlin2London</span>.<span class="name">position</span>.<span class="name">azimuthTo</span>(<span class="name">berlin2London</span>.<span class="name">to</span>)
<span class="name">cppPlaneAnimation</span>.<span class="name">start</span>();
<span class="name">cppPlane</span>.<span class="name">departed</span>();
}
}
}</pre>
<p>azimuthTo() calculates the bearing in degrees from one coordinate to another. Note that the above code utilizes a QML animation to tie the rotation and the position change into a single animation flow:</p>
<pre class="qml"><span class="type">SequentialAnimation</span> {
<span class="name">id</span>: <span class="name">cppPlaneAnimation</span>
property <span class="type">real</span> <span class="name">rotationDirection</span> : <span class="number">0</span>;
<span class="type">NumberAnimation</span> {
<span class="name">target</span>: <span class="name">cppPlane</span>; <span class="name">property</span>: <span class="string">"bearing"</span>; <span class="name">duration</span>: <span class="number">1000</span>
<span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span>
<span class="name">to</span>: <span class="name">cppPlaneAnimation</span>.<span class="name">rotationDirection</span>
}
<span class="type">ScriptAction</span> { <span class="name">script</span>: <span class="name">berlin2London</span>.<span class="name">startFlight</span>() }
}</pre>
<p>First, NumberAnimation rotates the plane into the correct direction and once that is done the <code>startFlight()</code> function takes care of starting the plane's position change.</p>
<pre class="cpp"><span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="type">void</span> startFlight()
{
<span class="keyword">if</span> (timer<span class="operator">.</span>isActive())
<span class="keyword">return</span>;
startTime <span class="operator">=</span> <span class="type"><a href="../qtcore/qtime.html">QTime</a></span><span class="operator">::</span>currentTime();
finishTime <span class="operator">=</span> startTime<span class="operator">.</span>addMSecs(ANIMATION_DURATION);
timer<span class="operator">.</span>start(<span class="number">15</span><span class="operator">,</span> <span class="keyword">this</span>);
<span class="keyword">emit</span> departed();
}</pre>
<a name="the-qml-pilot"></a>
<h3 >The QML Pilot</h3>
<p>The CoordinateAnimation type is used to control the flight from Oslo to Berlin and vice versa. It replaces the above ScriptAction.</p>
<pre class="qml"><span class="type">CoordinateAnimation</span> {
<span class="name">id</span>: <span class="name">coordinateAnimation</span>; <span class="name">duration</span>: <span class="number">5000</span>
<span class="name">target</span>: <span class="name">oslo2Berlin</span>; <span class="name">property</span>: <span class="string">"position"</span>
<span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span>
}</pre>
<p>The MouseArea of the QML plane implements the logic for the course setting and starts the animation when required.</p>
<pre class="qml"><span class="type">MouseArea</span> {
<span class="name">anchors</span>.fill: <span class="name">parent</span>
<span class="name">onClicked</span>: {
<span class="keyword">if</span> (<span class="name">qmlPlaneAnimation</span>.<span class="name">running</span>) {
<span class="name">console</span>.<span class="name">log</span>(<span class="string">"Plane still in the air."</span>);
<span class="keyword">return</span>;
}
<span class="keyword">if</span> (<span class="name">oslo2Berlin</span>.<span class="name">position</span> <span class="operator">===</span> <span class="name">berlin</span>) {
<span class="name">coordinateAnimation</span>.<span class="name">from</span> <span class="operator">=</span> <span class="name">berlin</span>;
<span class="name">coordinateAnimation</span>.<span class="name">to</span> <span class="operator">=</span> <span class="name">oslo</span>;
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">oslo2Berlin</span>.<span class="name">position</span> <span class="operator">===</span> <span class="name">oslo</span>) {
<span class="name">coordinateAnimation</span>.<span class="name">from</span> <span class="operator">=</span> <span class="name">oslo</span>;
<span class="name">coordinateAnimation</span>.<span class="name">to</span> <span class="operator">=</span> <span class="name">berlin</span>;
}
<span class="name">qmlPlaneAnimation</span>.<span class="name">rotationDirection</span> <span class="operator">=</span> <span class="name">oslo2Berlin</span>.<span class="name">position</span>.<span class="name">azimuthTo</span>(<span class="name">coordinateAnimation</span>.<span class="name">to</span>)
<span class="name">qmlPlaneAnimation</span>.<span class="name">start</span>()
}
}</pre>
<p>Files:</p>
<ul>
<li><a href="qtlocation-planespotter-plane-qml.html">planespotter/Plane.qml</a></li>
<li><a href="qtlocation-planespotter-planespotter-qml.html">planespotter/planespotter.qml</a></li>
<li><a href="qtlocation-planespotter-main-cpp.html">planespotter/main.cpp</a></li>
<li><a href="qtlocation-planespotter-planespotter-pro.html">planespotter/planespotter.pro</a></li>
<li><a href="qtlocation-planespotter-qml-qrc.html">planespotter/qml.qrc</a></li>
</ul>
</div>
<!-- @@@planespotter -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2015 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>
|