qtdatavisualization-qmloscilloscope-datasource-cpp.html 18.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 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
<?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>datasource.cpp Example File | Qt Data Visualization 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="qtdatavisualization-index.html">Qt Data Visualization</a></td><td ><a href="qtdatavisualization-qmloscilloscope-example.html">Qt Quick 2 Oscilloscope Example</a></td><td >datasource.cpp Example File</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">datasource.cpp Example File</h1>
<span class="subtitle">qmloscilloscope/datasource.cpp</span>
<!-- $$$qmloscilloscope/datasource.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the Qt Data Visualization module of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:GPL$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** GNU General Public License Usage
  ** Alternatively, this file may be used under the terms of the GNU
  ** General Public License version 3 or (at your option) any later version
  ** approved by the KDE Free Qt Foundation. The licenses are as published by
  ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  ** included in the packaging of this file. Please review the following
  ** information to ensure the GNU General Public License requirements will
  ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;datasource.h&quot;</span>
  <span class="preprocessor">#include &lt;QtCore/qmath.h&gt;</span>
  <span class="preprocessor">#include &lt;QtCore/qrandom.h&gt;</span>

  <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="type"><a href="qtdatavisualization-qmlmodule.html">QtDataVisualization</a></span>;

  Q_DECLARE_METATYPE(QSurface3DSeries <span class="operator">*</span>)

  DataSource<span class="operator">::</span>DataSource(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent) :
      <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>(parent)<span class="operator">,</span>
      m_index(<span class="operator">-</span><span class="number">1</span>)<span class="operator">,</span>
      m_resetArray(nullptr)
  {
      <a href="../qtcore/qmetatype.html#qRegisterMetaType-1">qRegisterMetaType</a><span class="operator">&lt;</span>QSurface3DSeries <span class="operator">*</span><span class="operator">&gt;</span>();
  }

  DataSource<span class="operator">::</span><span class="operator">~</span>DataSource()
  {
      clearData();
  }

  <span class="type">void</span> DataSource<span class="operator">::</span>generateData(<span class="type">int</span> cacheCount<span class="operator">,</span> <span class="type">int</span> rowCount<span class="operator">,</span> <span class="type">int</span> columnCount<span class="operator">,</span>
                                <span class="type">float</span> xMin<span class="operator">,</span> <span class="type">float</span> xMax<span class="operator">,</span> <span class="type">float</span> yMin<span class="operator">,</span> <span class="type">float</span> yMax<span class="operator">,</span>
                                <span class="type">float</span> zMin<span class="operator">,</span> <span class="type">float</span> zMax)
  {
      <span class="keyword">if</span> (<span class="operator">!</span>cacheCount <span class="operator">|</span><span class="operator">|</span> <span class="operator">!</span>rowCount <span class="operator">|</span><span class="operator">|</span> <span class="operator">!</span>columnCount)
          <span class="keyword">return</span>;

      clearData();
      <span class="comment">// Re-create the cache array</span>
      m_data<span class="operator">.</span>resize(cacheCount);
      <span class="keyword">for</span> (<span class="type">int</span> i(<span class="number">0</span>); i <span class="operator">&lt;</span> cacheCount; i<span class="operator">+</span><span class="operator">+</span>) {
          <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataArray-typedef">QSurfaceDataArray</a></span> <span class="operator">&amp;</span>array <span class="operator">=</span> m_data<span class="operator">[</span>i<span class="operator">]</span>;
          array<span class="operator">.</span>reserve(rowCount);
          <span class="keyword">for</span> (<span class="type">int</span> j(<span class="number">0</span>); j <span class="operator">&lt;</span> rowCount; j<span class="operator">+</span><span class="operator">+</span>)
              array<span class="operator">.</span>append(<span class="keyword">new</span> <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataRow-typedef">QSurfaceDataRow</a></span>(columnCount));
      }

      <span class="type">float</span> xRange <span class="operator">=</span> xMax <span class="operator">-</span> xMin;
      <span class="type">float</span> yRange <span class="operator">=</span> yMax <span class="operator">-</span> yMin;
      <span class="type">float</span> zRange <span class="operator">=</span> zMax <span class="operator">-</span> zMin;
      <span class="type">int</span> cacheIndexStep <span class="operator">=</span> columnCount <span class="operator">/</span> cacheCount;
      <span class="type">float</span> cacheStep <span class="operator">=</span> <span class="type">float</span>(cacheIndexStep) <span class="operator">*</span> xRange <span class="operator">/</span> <span class="type">float</span>(columnCount);

      <span class="comment">// Populate caches</span>
      <span class="keyword">for</span> (<span class="type">int</span> i(<span class="number">0</span>); i <span class="operator">&lt;</span> cacheCount; i<span class="operator">+</span><span class="operator">+</span>) {
          <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataArray-typedef">QSurfaceDataArray</a></span> <span class="operator">&amp;</span>cache <span class="operator">=</span> m_data<span class="operator">[</span>i<span class="operator">]</span>;
          <span class="type">float</span> cacheXAdjustment <span class="operator">=</span> cacheStep <span class="operator">*</span> i;
          <span class="type">float</span> cacheIndexAdjustment <span class="operator">=</span> cacheIndexStep <span class="operator">*</span> i;
          <span class="keyword">for</span> (<span class="type">int</span> j(<span class="number">0</span>); j <span class="operator">&lt;</span> rowCount; j<span class="operator">+</span><span class="operator">+</span>) {
              <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataRow-typedef">QSurfaceDataRow</a></span> <span class="operator">&amp;</span>row <span class="operator">=</span> <span class="operator">*</span>(cache<span class="operator">[</span>j<span class="operator">]</span>);
              <span class="type">float</span> rowMod <span class="operator">=</span> (<span class="type">float</span>(j)) <span class="operator">/</span> <span class="type">float</span>(rowCount);
              <span class="type">float</span> yRangeMod <span class="operator">=</span> yRange <span class="operator">*</span> rowMod;
              <span class="type">float</span> zRangeMod <span class="operator">=</span> zRange <span class="operator">*</span> rowMod;
              <span class="type">float</span> z <span class="operator">=</span> zRangeMod <span class="operator">+</span> zMin;
              <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> rowColWaveAngleMul <span class="operator">=</span> M_PI <span class="operator">*</span> M_PI <span class="operator">*</span> rowMod;
              <span class="type">float</span> rowColWaveMul <span class="operator">=</span> yRangeMod <span class="operator">*</span> <span class="number">0.2f</span>;
              <span class="keyword">for</span> (<span class="type">int</span> k(<span class="number">0</span>); k <span class="operator">&lt;</span> columnCount; k<span class="operator">+</span><span class="operator">+</span>) {
                  <span class="type">float</span> colMod <span class="operator">=</span> (<span class="type">float</span>(k)) <span class="operator">/</span> <span class="type">float</span>(columnCount);
                  <span class="type">float</span> xRangeMod <span class="operator">=</span> xRange <span class="operator">*</span> colMod;
                  <span class="type">float</span> x <span class="operator">=</span> xRangeMod <span class="operator">+</span> xMin <span class="operator">+</span> cacheXAdjustment;
                  <span class="type">float</span> colWave <span class="operator">=</span> <span class="type">float</span>(<a href="../qtcore/qtmath.html#qSin">qSin</a>((<span class="number">2.0</span> <span class="operator">*</span> M_PI <span class="operator">*</span> colMod) <span class="operator">-</span> (<span class="number">1.0</span> <span class="operator">/</span> <span class="number">2.0</span> <span class="operator">*</span> M_PI)) <span class="operator">+</span> <span class="number">1.0</span>);
                  <span class="type">float</span> y <span class="operator">=</span> (colWave <span class="operator">*</span> ((<span class="type">float</span>(<a href="../qtcore/qtmath.html#qSin">qSin</a>(rowColWaveAngleMul <span class="operator">*</span> colMod) <span class="operator">+</span> <span class="number">1.0</span>))))
                          <span class="operator">*</span> rowColWaveMul
                          <span class="operator">+</span> <span class="type"><a href="../qtcore/qrandomgenerator.html">QRandomGenerator</a></span><span class="operator">::</span>global()<span class="operator">-</span><span class="operator">&gt;</span>bounded(<span class="number">0.15f</span>) <span class="operator">*</span> yRangeMod;

                  <span class="type">int</span> index <span class="operator">=</span> k <span class="operator">+</span> cacheIndexAdjustment;
                  <span class="keyword">if</span> (index <span class="operator">&gt;</span><span class="operator">=</span> columnCount) {
                      <span class="comment">// Wrap over</span>
                      index <span class="operator">-</span><span class="operator">=</span> columnCount;
                      x <span class="operator">-</span><span class="operator">=</span> xRange;
                  }
                  row<span class="operator">[</span>index<span class="operator">]</span> <span class="operator">=</span> QVector3D(x<span class="operator">,</span> y<span class="operator">,</span> z);
              }
          }
      }
  }

  <span class="type">void</span> DataSource<span class="operator">::</span>update(QSurface3DSeries <span class="operator">*</span>series)
  {
      <span class="keyword">if</span> (series <span class="operator">&amp;</span><span class="operator">&amp;</span> m_data<span class="operator">.</span>size()) {
          <span class="comment">// Each iteration uses data from a different cached array</span>
          m_index<span class="operator">+</span><span class="operator">+</span>;
          <span class="keyword">if</span> (m_index <span class="operator">&gt;</span> m_data<span class="operator">.</span>count() <span class="operator">-</span> <span class="number">1</span>)
              m_index <span class="operator">=</span> <span class="number">0</span>;

          <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataArray-typedef">QSurfaceDataArray</a></span> array <span class="operator">=</span> m_data<span class="operator">.</span>at(m_index);
          <span class="type">int</span> newRowCount <span class="operator">=</span> array<span class="operator">.</span>size();
          <span class="type">int</span> newColumnCount <span class="operator">=</span> array<span class="operator">.</span>at(<span class="number">0</span>)<span class="operator">-</span><span class="operator">&gt;</span>size();

          <span class="comment">// If the first time or the dimensions of the cache array have changed,</span>
          <span class="comment">// reconstruct the reset array</span>
          <span class="keyword">if</span> (<span class="operator">!</span>m_resetArray <span class="operator">|</span><span class="operator">|</span> series<span class="operator">-</span><span class="operator">&gt;</span>dataProxy()<span class="operator">-</span><span class="operator">&gt;</span>rowCount() <span class="operator">!</span><span class="operator">=</span> newRowCount
                  <span class="operator">|</span><span class="operator">|</span> series<span class="operator">-</span><span class="operator">&gt;</span>dataProxy()<span class="operator">-</span><span class="operator">&gt;</span>columnCount() <span class="operator">!</span><span class="operator">=</span> newColumnCount) {
              m_resetArray <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataArray-typedef">QSurfaceDataArray</a></span>();
              m_resetArray<span class="operator">-</span><span class="operator">&gt;</span>reserve(newRowCount);
              <span class="keyword">for</span> (<span class="type">int</span> i(<span class="number">0</span>); i <span class="operator">&lt;</span> newRowCount; i<span class="operator">+</span><span class="operator">+</span>)
                  m_resetArray<span class="operator">-</span><span class="operator">&gt;</span>append(<span class="keyword">new</span> <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataRow-typedef">QSurfaceDataRow</a></span>(newColumnCount));
          }

          <span class="comment">// Copy items from our cache to the reset array</span>
          <span class="keyword">for</span> (<span class="type">int</span> i(<span class="number">0</span>); i <span class="operator">&lt;</span> newRowCount; i<span class="operator">+</span><span class="operator">+</span>) {
              <span class="keyword">const</span> <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataRow-typedef">QSurfaceDataRow</a></span> <span class="operator">&amp;</span>sourceRow <span class="operator">=</span> <span class="operator">*</span>(array<span class="operator">.</span>at(i));
              <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataRow-typedef">QSurfaceDataRow</a></span> <span class="operator">&amp;</span>row <span class="operator">=</span> <span class="operator">*</span>(<span class="operator">*</span>m_resetArray)<span class="operator">[</span>i<span class="operator">]</span>;
              <span class="keyword">for</span> (<span class="type">int</span> j(<span class="number">0</span>); j <span class="operator">&lt;</span> newColumnCount; j<span class="operator">+</span><span class="operator">+</span>)
                  row<span class="operator">[</span>j<span class="operator">]</span><span class="operator">.</span>setPosition(sourceRow<span class="operator">.</span>at(j)<span class="operator">.</span>position());
          }

          <span class="comment">// Notify the proxy that data has changed</span>
          series<span class="operator">-</span><span class="operator">&gt;</span>dataProxy()<span class="operator">-</span><span class="operator">&gt;</span>resetArray(m_resetArray);
      }
  }

  <span class="type">void</span> DataSource<span class="operator">::</span>clearData()
  {
      <span class="keyword">for</span> (<span class="type">int</span> i(<span class="number">0</span>); i <span class="operator">&lt;</span> m_data<span class="operator">.</span>size(); i<span class="operator">+</span><span class="operator">+</span>) {
          <span class="type"><a href="qsurfacedataproxy.html#QSurfaceDataArray-typedef">QSurfaceDataArray</a></span> <span class="operator">&amp;</span>array <span class="operator">=</span> m_data<span class="operator">[</span>i<span class="operator">]</span>;
          <span class="keyword">for</span> (<span class="type">int</span> j(<span class="number">0</span>); j <span class="operator">&lt;</span> array<span class="operator">.</span>size(); j<span class="operator">+</span><span class="operator">+</span>)
              <span class="keyword">delete</span> array<span class="operator">[</span>j<span class="operator">]</span>;
          array<span class="operator">.</span>clear();
      }
  }

</pre>
</div>
<!-- @@@qmloscilloscope/datasource.cpp -->
        </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>