qtpositioning-logfilepositionsource-example.html 13.6 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- logfilepositionsource.qdoc -->
  <title>Log File Position Source (C++) | Qt Positioning 5.11</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 ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtpositioning-index.html">Qt Positioning</a></td><td ><a href="qtpositioning-examples.html">Qt Positioning Examples</a></td><td >Log File Position Source (C++)</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 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">Log File Position Source (C++)</h1>
<span class="subtitle"></span>
<!-- $$$logfilepositionsource-brief -->
<p>The Logfile Position Source shows how to create and work with a custom NMEA position source, for platforms without GPS.</p>
<!-- @@@logfilepositionsource -->
<!-- $$$logfilepositionsource-description -->
<div class="descr"> <a name="details"></a>
<p>The data is read from a file which has positional data in NMEA format. The resulting time and position information is then displayed to the screen as simple text in date/time and latitude/longitude format.</p>
<p>This example class reads position data from a text file, <i>log.txt</i>. The file specifies position data using a simple text format: it contains one position update per line, where each line contains a date/time, a latitude and a longitude, separated by spaces. The date/time is in ISO 8601 format and the latitude and longitude are in degrees decimal format. Here is an excerpt from <i>log.txt</i>:</p>
<pre class="cpp">

  <span class="number">2009</span><span class="operator">-</span><span class="number">08</span><span class="operator">-</span><span class="number">24T22</span>:<span class="number">25</span>:<span class="number">01</span> <span class="operator">-</span><span class="number">27.576082</span> <span class="number">153.092415</span>
  <span class="number">2009</span><span class="operator">-</span><span class="number">08</span><span class="operator">-</span><span class="number">24T22</span>:<span class="number">25</span>:<span class="number">02</span> <span class="operator">-</span><span class="number">27.576223</span> <span class="number">153.092530</span>
  <span class="number">2009</span><span class="operator">-</span><span class="number">08</span><span class="operator">-</span><span class="number">24T22</span>:<span class="number">25</span>:<span class="number">03</span> <span class="operator">-</span><span class="number">27.576364</span> <span class="number">153.092648</span>

</pre>
<p>The class reads this data and distributes it via the <a href="qgeopositioninfosource.html#positionUpdated">positionUpdated()</a> signal.</p>
<p>Here is the definition of the <code>LogFilePositionSource</code> class:</p>
<pre class="cpp">

  <span class="keyword">class</span> LogFilePositionSource : <span class="keyword">public</span> <span class="type"><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></span>
  {
      Q_OBJECT
  <span class="keyword">public</span>:
      LogFilePositionSource(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

      <span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span> lastKnownPosition(bool fromSatellitePositioningMethodsOnly <span class="operator">=</span> <span class="keyword">false</span>) <span class="keyword">const</span>;

      PositioningMethods supportedPositioningMethods() <span class="keyword">const</span>;
      <span class="type">int</span> minimumUpdateInterval() <span class="keyword">const</span>;
      Error error() <span class="keyword">const</span>;

  <span class="keyword">public</span> <span class="keyword">slots</span>:
      <span class="keyword">virtual</span> <span class="type">void</span> startUpdates();
      <span class="keyword">virtual</span> <span class="type">void</span> stopUpdates();

      <span class="keyword">virtual</span> <span class="type">void</span> requestUpdate(<span class="type">int</span> timeout <span class="operator">=</span> <span class="number">5000</span>);

  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> readNextPosition();

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> <span class="operator">*</span>logFile;
      <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span> <span class="operator">*</span>timer;
      <span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span> lastPosition;
  };

</pre>
<p>The main methods overrided by the subclass are:</p>
<ul>
<li><a href="qgeopositioninfosource.html#startUpdates">startUpdates()</a>: called by client applications to start regular position updates.</li>
<li><a href="qgeopositioninfosource.html#stopUpdates">stopUpdates()</a>: called by client applications to stop regular position updates.</li>
<li><a href="qgeopositioninfosource.html#requestUpdate">requestUpdate()</a>: called by client applications to request a single update, with a specified timeout.</li>
</ul>
<p>When a position update is available, the subclass emits the <a href="qgeopositioninfosource.html#positionUpdated">positionUpdated()</a> signal.</p>
<p>Here are the key methods in the class implementation:</p>
<pre class="cpp">

  LogFilePositionSource<span class="operator">::</span>LogFilePositionSource(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qgeopositioninfosource.html">QGeoPositionInfoSource</a></span>(parent)<span class="operator">,</span>
        logFile(<span class="keyword">new</span> <span class="type"><a href="../qtcore/qfile.html">QFile</a></span>(<span class="keyword">this</span>))<span class="operator">,</span>
        timer(<span class="keyword">new</span> <span class="type"><a href="../qtcore/qtimer.html">QTimer</a></span>(<span class="keyword">this</span>))
  {
      connect(timer<span class="operator">,</span> SIGNAL(timeout())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(readNextPosition()));

      logFile<span class="operator">-</span><span class="operator">&gt;</span>setFileName(<span class="string">&quot;:/simplelog.txt&quot;</span>);
      <span class="keyword">if</span> (<span class="operator">!</span>logFile<span class="operator">-</span><span class="operator">&gt;</span>open(<span class="type"><a href="../qtcore/qiodevice.html">QIODevice</a></span><span class="operator">::</span>ReadOnly))
          <a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;Error: cannot open source file&quot;</span> <span class="operator">&lt;</span><span class="operator">&lt;</span> logFile<span class="operator">-</span><span class="operator">&gt;</span>fileName();
  }

  <span class="type">void</span> LogFilePositionSource<span class="operator">::</span>startUpdates()
  {
      <span class="type">int</span> interval <span class="operator">=</span> updateInterval();
      <span class="keyword">if</span> (interval <span class="operator">&lt;</span> minimumUpdateInterval())
          interval <span class="operator">=</span> minimumUpdateInterval();

      timer<span class="operator">-</span><span class="operator">&gt;</span>start(interval);
  }

  <span class="type">void</span> LogFilePositionSource<span class="operator">::</span>stopUpdates()
  {
      timer<span class="operator">-</span><span class="operator">&gt;</span>stop();
  }

  <span class="type">void</span> LogFilePositionSource<span class="operator">::</span>requestUpdate(<span class="type">int</span> <span class="comment">/*timeout*/</span>)
  {
      <span class="comment">// For simplicity, ignore timeout - assume that if data is not available</span>
      <span class="comment">// now, no data will be added to the file later</span>
      <span class="keyword">if</span> (logFile<span class="operator">-</span><span class="operator">&gt;</span>canReadLine())
          readNextPosition();
      <span class="keyword">else</span>
          <span class="keyword">emit</span> updateTimeout();
  }

  <span class="type">void</span> LogFilePositionSource<span class="operator">::</span>readNextPosition()
  {
      <span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span> line <span class="operator">=</span> logFile<span class="operator">-</span><span class="operator">&gt;</span>readLine()<span class="operator">.</span>trimmed();
      <span class="keyword">if</span> (<span class="operator">!</span>line<span class="operator">.</span>isEmpty()) {
          <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtcore/qbytearray.html">QByteArray</a></span><span class="operator">&gt;</span> data <span class="operator">=</span> line<span class="operator">.</span>split(<span class="char">' '</span>);
          <span class="type">double</span> latitude;
          <span class="type">double</span> longitude;
          bool hasLatitude <span class="operator">=</span> <span class="keyword">false</span>;
          bool hasLongitude <span class="operator">=</span> <span class="keyword">false</span>;
          <span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span> timestamp <span class="operator">=</span> <span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span><span class="operator">::</span>fromString(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>(data<span class="operator">.</span>value(<span class="number">0</span>))<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ISODate);
          latitude <span class="operator">=</span> data<span class="operator">.</span>value(<span class="number">1</span>)<span class="operator">.</span>toDouble(<span class="operator">&amp;</span>hasLatitude);
          longitude <span class="operator">=</span> data<span class="operator">.</span>value(<span class="number">2</span>)<span class="operator">.</span>toDouble(<span class="operator">&amp;</span>hasLongitude);

          <span class="keyword">if</span> (hasLatitude <span class="operator">&amp;</span><span class="operator">&amp;</span> hasLongitude <span class="operator">&amp;</span><span class="operator">&amp;</span> timestamp<span class="operator">.</span>isValid()) {
              <span class="type"><a href="qgeocoordinate.html">QGeoCoordinate</a></span> coordinate(latitude<span class="operator">,</span> longitude);
              <span class="type"><a href="qgeopositioninfo.html">QGeoPositionInfo</a></span> info(coordinate<span class="operator">,</span> timestamp);
              <span class="keyword">if</span> (info<span class="operator">.</span>isValid()) {
                  lastPosition <span class="operator">=</span> info;
                  <span class="keyword">emit</span> positionUpdated(info);
              }
          }
      }
  }

</pre>
<p>Files:</p>
<ul>
<li><a href="qtpositioning-logfilepositionsource-clientapplication-cpp.html">logfilepositionsource/clientapplication.cpp</a></li>
<li><a href="qtpositioning-logfilepositionsource-clientapplication-h.html">logfilepositionsource/clientapplication.h</a></li>
<li><a href="qtpositioning-logfilepositionsource-logfile-qrc.html">logfilepositionsource/logfile.qrc</a></li>
<li><a href="qtpositioning-logfilepositionsource-logfilepositionsource-cpp.html">logfilepositionsource/logfilepositionsource.cpp</a></li>
<li><a href="qtpositioning-logfilepositionsource-logfilepositionsource-h.html">logfilepositionsource/logfilepositionsource.h</a></li>
<li><a href="qtpositioning-logfilepositionsource-logfilepositionsource-pro.html">logfilepositionsource/logfilepositionsource.pro</a></li>
<li><a href="qtpositioning-logfilepositionsource-main-cpp.html">logfilepositionsource/main.cpp</a></li>
</ul>
</div>
<!-- @@@logfilepositionsource -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 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>