dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
spectrogramscopeconfig.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  *
29  * Additional note on redistribution: The copyright and license notices above
30  * must be maintained in each individual source file that is a derivative work
31  * of this source file; otherwise redistribution is prohibited.
32  */
33 
34 #include "spectrogramplotdata.h"
36 #include "coreplugin/icore.h"
38 
43 {
45  50; // TODO: This should not be set here. Probably should come from a define somewhere.
46  yAxisUnits = "";
47  timeHorizon = 60;
48  samplingFrequency = 100;
49  windowWidth = 64;
50  zMaximum = 120;
51  colorMapType = ColorMap::STANDARD;
52 }
53 
59 {
60  timeHorizon = qSettings->value("timeHorizon").toDouble();
61  samplingFrequency = qSettings->value("samplingFrequency").toDouble();
62  windowWidth = qSettings->value("windowWidth").toInt();
63  zMaximum = qSettings->value("zMaximum").toDouble();
64  colorMapType = (ColorMap::ColorMapType)qSettings->value("colorMap").toInt();
65 
66  int plot3dCurveCount = qSettings->value("dataSourceCount").toInt();
67 
68  for (int i = 0; i < plot3dCurveCount; i++) {
70 
71  // Start reading XML block
72  qSettings->beginGroup(QString("spectrogramDataSource") + QString().number(i));
73 
74  plotCurveConf->uavObjectName = qSettings->value("uavObject").toString();
75  plotCurveConf->uavFieldName = qSettings->value("uavField").toString();
76  plotCurveConf->color = qSettings->value("color").value<QRgb>();
77  plotCurveConf->yScalePower = qSettings->value("yScalePower").toInt();
78  plotCurveConf->mathFunction = qSettings->value("mathFunction").toString();
79  plotCurveConf->yMeanSamples = qSettings->value("yMeanSamples").toInt();
80 
81  // Stop reading XML block
82  qSettings->endGroup();
83 
84  m_spectrogramSourceConfigs.append(plotCurveConf);
85  }
86 }
87 
92 SpectrogramScopeConfig::SpectrogramScopeConfig(Ui::ScopeGadgetOptionsPage *options_page)
93 {
94  bool parseOK = false;
95 
96  windowWidth = options_page->sbSpectrogramWidth->value();
97  samplingFrequency = options_page->sbSpectrogramFrequency->value();
98  timeHorizon = options_page->sbSpectrogramTimeHorizon->value();
99  zMaximum = options_page->spnMaxSpectrogramZ->value();
100  colorMapType = (ColorMap::ColorMapType)options_page->cmbColorMapSpectrogram
101  ->itemData(options_page->cmbColorMapSpectrogram->currentIndex())
102  .toInt();
103 
104  Plot3dCurveConfiguration *newPlotCurveConfigs = new Plot3dCurveConfiguration();
105  newPlotCurveConfigs->uavObjectName = options_page->cmbUAVObjectsSpectrogram->currentText();
106  newPlotCurveConfigs->uavFieldName = options_page->cmbUavoFieldSpectrogram->currentText();
107  newPlotCurveConfigs->yScalePower = options_page->sbSpectrogramDataMultiplier->value();
108  newPlotCurveConfigs->yMeanSamples = options_page->spnMeanSamplesSpectrogram->value();
109  newPlotCurveConfigs->mathFunction = options_page->cmbMathFunctionSpectrogram->currentText();
110 
111  QVariant varColor = (int)QColor(options_page->btnColorSpectrogram->text()).rgb();
112  int rgb = varColor.toInt(&parseOK);
113  if (!parseOK)
114  newPlotCurveConfigs->color = QColor(Qt::red).rgb();
115  else
116  newPlotCurveConfigs->color = (QRgb)rgb;
117 
118  m_spectrogramSourceConfigs.append(newPlotCurveConfigs);
119 }
120 
122 
129 {
130  SpectrogramScopeConfig *originalSpectrogramScopeConfig =
131  dynamic_cast<SpectrogramScopeConfig *>(originalScope);
133 
134  cloneObj->timeHorizon = originalSpectrogramScopeConfig->timeHorizon;
135  cloneObj->colorMapType = originalSpectrogramScopeConfig->colorMapType;
136 
137  int plotCurveCount = originalSpectrogramScopeConfig->m_spectrogramSourceConfigs.size();
138 
139  for (int i = 0; i < plotCurveCount; i++) {
140  Plot3dCurveConfiguration *currentPlotCurveConf =
141  originalSpectrogramScopeConfig->m_spectrogramSourceConfigs.at(i);
142  Plot3dCurveConfiguration *newSpectrogramConf = new Plot3dCurveConfiguration();
143 
144  newSpectrogramConf->uavObjectName = currentPlotCurveConf->uavObjectName;
145  newSpectrogramConf->uavFieldName = currentPlotCurveConf->uavFieldName;
146  newSpectrogramConf->color = currentPlotCurveConf->color;
147  newSpectrogramConf->yScalePower = currentPlotCurveConf->yScalePower;
148  newSpectrogramConf->yMeanSamples = currentPlotCurveConf->yMeanSamples;
149  newSpectrogramConf->mathFunction = currentPlotCurveConf->mathFunction;
150 
151  cloneObj->m_spectrogramSourceConfigs.append(newSpectrogramConf);
152  }
153 
154  return cloneObj;
155 }
156 
162 {
163  // Start writing new XML block
164  qSettings->beginGroup(QString("plot3d"));
165 
166  int plot3dCurveCount = m_spectrogramSourceConfigs.size();
167 
168  qSettings->setValue("plot3dType", SPECTROGRAM);
169  qSettings->setValue("dataSourceCount", plot3dCurveCount);
170 
171  qSettings->setValue("colorMap", colorMapType);
172  qSettings->setValue("samplingFrequency", samplingFrequency);
173  qSettings->setValue("timeHorizon", timeHorizon);
174  qSettings->setValue("windowWidth", windowWidth);
175  qSettings->setValue("zMaximum", zMaximum);
176 
177  for (int i = 0; i < plot3dCurveCount; i++) {
178  Plot3dCurveConfiguration *plotCurveConf = m_spectrogramSourceConfigs.at(i);
179 
180  // Start new XML block
181  qSettings->beginGroup(QString("spectrogramDataSource") + QString().number(i));
182 
183  qSettings->setValue("uavObject", plotCurveConf->uavObjectName);
184  qSettings->setValue("uavField", plotCurveConf->uavFieldName);
185  qSettings->setValue("colormap", plotCurveConf->color);
186 
187  // Stop writing XML block
188  qSettings->endGroup();
189  }
190 
191  // Stop writing XML block
192  qSettings->endGroup();
193 }
194 
201  QList<Plot3dCurveConfiguration *> spectrogramSourceConfigs)
202 {
203  m_spectrogramSourceConfigs.clear();
204  m_spectrogramSourceConfigs.append(spectrogramSourceConfigs);
205 }
206 
213 {
214  preparePlot(scopeGadgetWidget);
215  scopeGadgetWidget->setScope(this);
216  scopeGadgetWidget->startTimer(m_refreshInterval);
217 
218  // There should be only one spectrogram per plot //TODO: Upgrade this to handle multiple
219  // spectrograms on a single axis
220  if (m_spectrogramSourceConfigs.length() != 1)
221  return;
222 
223  Plot3dCurveConfiguration *spectrogramSourceConfigs = m_spectrogramSourceConfigs.front();
224  QString uavObjectName = spectrogramSourceConfigs->uavObjectName;
225  QString uavFieldName = spectrogramSourceConfigs->uavFieldName;
226 
227  // Get and store the units
228  units = getUavObjectFieldUnits(uavObjectName, uavFieldName);
229 
230  SpectrogramData *spectrogramData = new SpectrogramData(
231  uavObjectName, uavFieldName, samplingFrequency, windowWidth, timeHorizon);
232  spectrogramData->setXMinimum(0);
233  spectrogramData->setXMaximum(samplingFrequency / 2);
234  spectrogramData->setYMinimum(0);
235  spectrogramData->setYMaximum(timeHorizon);
236  spectrogramData->setZMaximum(zMaximum);
237  spectrogramData->setScalePower(spectrogramSourceConfigs->yScalePower);
238  spectrogramData->setMeanSamples(spectrogramSourceConfigs->yMeanSamples);
239  spectrogramData->setMathFunction(spectrogramSourceConfigs->mathFunction);
240 
241  // Generate the waterfall name
242  QString waterfallName =
243  (spectrogramData->getUavoName()) + "." + (spectrogramData->getUavoFieldName());
244  if (spectrogramData->getHaveSubFieldFlag())
245  waterfallName = waterfallName.append("." + spectrogramData->getUavoSubFieldName());
246 
247  // Get the uav object
248  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
249  UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
250  UAVDataObject *obj =
251  dynamic_cast<UAVDataObject *>(objManager->getObject((spectrogramData->getUavoName())));
252  if (!obj) {
253  qDebug() << "Object " << spectrogramData->getUavoName() << " is missing";
254  return;
255  }
256 
257  // Get the units
258  QString units =
259  getUavObjectFieldUnits(spectrogramData->getUavoName(), spectrogramData->getUavoFieldName());
260 
261  // Generate name with scaling factor appeneded
262  QString waterfallNameScaled;
263  if (spectrogramSourceConfigs->yScalePower == 0)
264  waterfallNameScaled = waterfallName + "(" + units + ")";
265  else
266  waterfallNameScaled = waterfallName + "(x10^"
267  + QString::number(spectrogramSourceConfigs->yScalePower) + " " + units + ")";
268 
269  // Create the waterfall plot
270  QwtPlotSpectrogram *plotSpectrogram = new QwtPlotSpectrogram(waterfallNameScaled);
271  plotSpectrogram->setRenderThreadCount(0); // use system specific thread count
272  plotSpectrogram->setRenderHint(QwtPlotItem::RenderAntialiased);
273  plotSpectrogram->setColorMap(new ColorMap(colorMapType));
274 
275  // Initial raster data
276 
277  QDateTime NOW =
278  QDateTime::currentDateTime(); // TODO: Upgrade this to show UAVO time and not system time
279  for (uint i = 0; i < timeHorizon; i++) {
280  spectrogramData->timeDataHistory->append(NOW.toTime_t() + NOW.time().msec() / 1000.0 + i);
281  }
282 
283  if (((double)windowWidth) * timeHorizon < (double)10000000.0
284  * sizeof(spectrogramData->zDataHistory->front())) { // Don't exceed 10MB for memory
285  for (uint i = 0; i < windowWidth * timeHorizon; i++) {
286  spectrogramData->zDataHistory->append(0);
287  }
288  } else {
289  qDebug() << "For some reason, we're trying to allocate a gigantic spectrogram. This "
290  "probably represents a problem in the configuration file. TimeHorizion: "
291  << timeHorizon << ", windowWidth: " << windowWidth;
292  Q_ASSERT(0);
293  return;
294  }
295 
296  // Set up colorbar on right axis
297  spectrogramData->rightAxis = scopeGadgetWidget->axisWidget(QwtPlot::yRight);
298  spectrogramData->rightAxis->setTitle("Intensity");
299  spectrogramData->rightAxis->setColorBarEnabled(true);
300  spectrogramData->rightAxis->setColorMap(QwtInterval(0, zMaximum), new ColorMap(colorMapType));
301  scopeGadgetWidget->setAxisScale(QwtPlot::yRight, 0, zMaximum);
302  scopeGadgetWidget->enableAxis(QwtPlot::yRight);
303 
304  plotSpectrogram->setData(spectrogramData->getRasterData());
305 
306  plotSpectrogram->attach(scopeGadgetWidget);
307  spectrogramData->setSpectrogram(plotSpectrogram);
308 
309  // Keep the curve details for later
310  scopeGadgetWidget->insertDataSources(waterfallNameScaled, spectrogramData);
311 
312  // Connect the UAVO
313  scopeGadgetWidget->connectUAVO(obj);
314 
315  scopeGadgetWidget->replot();
316 }
317 
323 void SpectrogramScopeConfig::setGuiConfiguration(Ui::ScopeGadgetOptionsPage *options_page)
324 {
325  // Set the tab widget to 3D
326  options_page->tabWidget2d3d->setCurrentWidget(options_page->tabPlot3d);
327 
328  // Set the plot type
329  options_page->cmb3dPlotType->setCurrentIndex(
330  options_page->cmb3dPlotType->findData(SPECTROGRAM));
331 
332  options_page->sbSpectrogramTimeHorizon->setValue(timeHorizon);
333  options_page->sbSpectrogramFrequency->setValue(samplingFrequency);
334  options_page->spnMaxSpectrogramZ->setValue(zMaximum);
335  options_page->cmbColorMapSpectrogram->setCurrentIndex(
336  options_page->cmbColorMapSpectrogram->findData(colorMapType));
337 
338  foreach (Plot3dCurveConfiguration *plot3dData, m_spectrogramSourceConfigs) {
339  int uavoIdx = options_page->cmbUAVObjectsSpectrogram->findText(plot3dData->uavObjectName);
340  options_page->cmbUAVObjectsSpectrogram->setCurrentIndex(uavoIdx);
341  options_page->sbSpectrogramWidth->setValue(windowWidth);
342 
343  int uavoFieldIdx =
344  options_page->cmbUavoFieldSpectrogram->findText(plot3dData->uavFieldName);
345  options_page->cmbUavoFieldSpectrogram->setCurrentIndex(uavoFieldIdx);
346  }
347 }
348 
354 {
355  scopeGadgetWidget->setMinimumSize(64, 64);
356  scopeGadgetWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
357  scopeGadgetWidget->setCanvasBackground(QColor(64, 64, 64));
358 
359  // Remove grid lines
360  scopeGadgetWidget->m_grid->enableX(false);
361  scopeGadgetWidget->m_grid->enableY(false);
362  scopeGadgetWidget->m_grid->enableXMin(false);
363  scopeGadgetWidget->m_grid->enableYMin(false);
364  scopeGadgetWidget->m_grid->setMajorPen(QPen(Qt::gray, 0, Qt::DashLine));
365  scopeGadgetWidget->m_grid->setMinorPen(QPen(Qt::lightGray, 0, Qt::DotLine));
366  scopeGadgetWidget->m_grid->setPen(QPen(Qt::darkGray, 1, Qt::DotLine));
367  scopeGadgetWidget->m_grid->attach(scopeGadgetWidget);
368 
369  // Configure axes
370  configureAxes(scopeGadgetWidget);
371 }
372 
378 {
379  // Configure axes
380  scopeGadgetWidget->setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw());
381  scopeGadgetWidget->setAxisScale(QwtPlot::xBottom, 0,
382  (unsigned int)(samplingFrequency / 2.0 + 0.5));
383  scopeGadgetWidget->setAxisAutoScale(QwtPlot::yLeft);
384  scopeGadgetWidget->setAxisLabelRotation(QwtPlot::xBottom, 0.0);
385  scopeGadgetWidget->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
386 
387  // reduce the axis font size
388  QFont fnt(scopeGadgetWidget->axisFont(QwtPlot::xBottom));
389  fnt.setPointSize(7);
390  scopeGadgetWidget->setAxisFont(QwtPlot::xBottom, fnt); // x-axis
391  scopeGadgetWidget->setAxisFont(QwtPlot::yLeft, fnt); // y-axis
392 }
virtual void preparePlot(ScopeGadgetWidget *)
SpectrogramScopeConfig::preparePlot Prepares the Qwt plot colors and axes.
virtual void setGuiConfiguration(Ui::ScopeGadgetOptionsPage *options_page)
SpectrogramScopeConfig::setGuiConfiguration Set the GUI elements based on values from the XML setting...
virtual void saveConfiguration(QSettings *qSettings)
SpectrogramScopeConfig::saveConfiguration Saves configuration to XML file.
The ColorMap class Defines a program-wide colormap.
Definition: plotdata.h:118
void connectUAVO(UAVDataObject *obj)
ScopeGadgetWidget::connectUAVO Connects UAVO update signal, but only if it hasn't yet been connected...
QVector< double > * timeDataHistory
Definition: plotdata3d.h:49
The SpectrogramScopeConfig class The spectrogram scope configuration.
void setMathFunction(QString val)
Definition: plotdata.h:58
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
void setScalePower(int val)
Definition: plotdata.h:56
QString getUavoName()
Definition: plotdata.h:67
void replaceSpectrogramDataSource(QList< Plot3dCurveConfiguration * > spectrogramSourceConfigs)
SpectrogramScopeConfig::replaceSpectrogramDataSource Replaces the list of spectrogram data sources...
QString getUavoFieldName()
Definition: plotdata.h:68
SpectrogramScopeConfig()
SpectrogramScopeConfig::SpectrogramScopeConfig Default constructor.
void setScope(ScopeConfig *val)
for i
Definition: OPPlots.m:140
virtual ScopeConfig * cloneScope(ScopeConfig *)
SpectrogramScopeConfig::cloneScope Clones scope from existing GUI configuration.
ColorMapType
The ColorMapType enum Defines the different type of color maps.
Definition: plotdata.h:124
QString getUavoSubFieldName()
Definition: plotdata.h:69
void setMeanSamples(int val)
Definition: plotdata.h:57
QwtScaleWidget * rightAxis
Definition: plotdata.h:87
virtual void setXMaximum(double val)
void setYMinimum(double val)
Definition: plotdata.h:53
QVector< double > * zDataHistory
Definition: plotdata3d.h:48
QwtMatrixRasterData * getRasterData()
void setXMinimum(double val)
Definition: plotdata.h:51
virtual void loadConfiguration(ScopeGadgetWidget *scopeGadgetWidget)
SpectrogramScopeConfig::loadConfiguration loads the plot configuration into the scope gadget widget...
The ScopeConfig class The parent class for scope configuration classes data sources.
Definition: scopesconfig.h:56
virtual void setYMaximum(double val)
int m_refreshInterval
Definition: scopesconfig.h:73
virtual void setZMaximum(double val)
void configureAxes(ScopeGadgetWidget *)
SpectrogramScopeConfig::configureAxes Configure the axes.
void insertDataSources(QString stringVal, PlotData *dataVal)
QString getUavObjectFieldUnits(QString uavObjectName, QString uavObjectFieldName)
Definition: scopesconfig.h:77
void startTimer(int)
ScopeGadgetWidget::startTimer Starts timer.
bool getHaveSubFieldFlag()
Definition: plotdata.h:70
UAVObject * getObject(const QString &name, quint32 instId=0)
The SpectrogramData class The spectrogram plot has a fixed size data buffer. All the curves in one pl...
void setSpectrogram(QwtPlotSpectrogram *val)
QwtPlotGrid * m_grid