Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 2c21526e2a037dc4eaceb3895021e482 > files > 368

qtlocation5-doc-5.12.6-1.mga7.noarch.rpm

<?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.12.6</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qtlocation-index.html">Qt Location</a></td><td ><a href="qtlocation-examples.html">Qt Location Examples</a></td><td >Plane Spotter (QML)</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtlocation-index.html">Qt 5.12.6 Reference Documentation</a></td>
        </tr></table>
      </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-brief -->
<p>The <code>Plane Spotter</code> example demonstrates the tight integration of location and positioning data types into QML.</p>
<!-- @@@planespotter -->
<!-- $$$planespotter-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/planespotter.png" 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 Q_GADGET feature as part of its position controller implementation. It permits direct integration of non-QObject 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">QObject</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">&gt;</span>setContextProperty(<span class="string">&quot;oslo2Berlin&quot;</span><span class="operator">,</span> <span class="operator">&amp;</span>oslo2berlin);
      engine<span class="operator">.</span>rootContext()<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;berlin2London&quot;</span><span class="operator">,</span> <span class="operator">&amp;</span>berlin2london);
      engine<span class="operator">.</span>load(<span class="type">QUrl</span>(<span class="type">QStringLiteral</span>(<span class="string">&quot;qrc:/planespotter.qml&quot;</span>)));

      <span class="keyword">return</span> app<span class="operator">.</span>exec();
  }

</pre>
<p>Similar to QObject 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">

  // Plane.qml
  MapQuickItem {
      id: plane
      property string pilotName;
      property int bearing: 0;

      anchorPoint.x: image.width/2
      anchorPoint.y: image.height/2

      sourceItem: Grid {
          //...
      }
  }

</pre>
<a name="the-c-pilot"></a>
<h3 id="the-c-pilot">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 QBasicTimer and QTimerEvents 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">qreal</span> progress;
      <span class="type">QTime</span> current <span class="operator">=</span> <span class="type">QTime</span><span class="operator">::</span>currentTime();
      <span class="keyword">if</span> (current <span class="operator">&gt;</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">qreal</span>)startTime<span class="operator">.</span>msecsTo(current) <span class="operator">/</span> ANIMATION_DURATION);
      }

      setPosition(<span class="type">QWebMercator</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">

  Plane {
      id: cppPlane
      pilotName: "C++"
      coordinate: berlin2London.position

      MouseArea {
          anchors.fill: parent
          onClicked: {
              if (cppPlaneAnimation.running || berlin2London.isFlying()) {
                  console.log("Plane still in the air.");
                  return;
              }

              berlin2London.swapDestinations();
              cppPlaneAnimation.rotationDirection = berlin2London.position.azimuthTo(berlin2London.to)
              cppPlaneAnimation.start();
              cppPlane.departed();
          }
      }
  }

</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">

  SequentialAnimation {
      id: cppPlaneAnimation
      property real rotationDirection : 0;
      NumberAnimation {
          target: cppPlane; property: "bearing"; duration: 1000
          easing.type: Easing.InOutQuad
          to: cppPlaneAnimation.rotationDirection
      }
      ScriptAction { script: berlin2London.startFlight() }
  }

</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">QTime</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 id="the-qml-pilot">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">

  CoordinateAnimation {
      id: coordinateAnimation; duration: 5000
      target: oslo2Berlin; property: "position"
      easing.type: Easing.InOutQuad
  }

</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">

  MouseArea {
      anchors.fill: parent
      onClicked: {
          if (qmlPlaneAnimation.running) {
              console.log("Plane still in the air.");
              return;
          }

          if (oslo2Berlin.position === berlin) {
              coordinateAnimation.from = berlin;
              coordinateAnimation.to = oslo;
          } else if (oslo2Berlin.position === oslo) {
              coordinateAnimation.from = oslo;
              coordinateAnimation.to = berlin;
          }

          qmlPlaneAnimation.rotationDirection = oslo2Berlin.position.azimuthTo(coordinateAnimation.to)
          qmlPlaneAnimation.start()
      }
  }

</pre>
<p>Files:</p>
<ul>
<li><a href="qtlocation-planespotter-plane-qml.html">planespotter/Plane.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-planespotter-qml.html">planespotter/planespotter.qml</a></li>
<li><a href="qtlocation-planespotter-qml-qrc.html">planespotter/qml.qrc</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/planespotter/airplane.png">planespotter/airplane.png</a></li>
</ul>
</div>
<!-- @@@planespotter -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 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>