dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
histogramscopeconfig.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 "histogramplotdata.h"
36 #include "scopegadgetoptionspage.h"
37 
38 #include "coreplugin/icore.h"
40 
45 {
46  binWidth = 1;
47  maxNumberOfBins = 1000;
48  m_refreshInterval = 50;
49 }
50 
56 {
57  binWidth = qSettings->value("binWidth").toDouble();
58  // Ensure binWidth is not too small
59  if (binWidth < 1e-3)
60  binWidth = 1e-3;
61 
62  maxNumberOfBins = qSettings->value("maxNumberOfBins").toInt();
65 
66  int dataSourceCount = qSettings->value("dataSourceCount").toInt();
67  for (int i = 0; i < dataSourceCount; i++) {
68  // Start reading XML block
69  qSettings->beginGroup(QString("histogramDataSource") + QString().number(i));
70 
72 
73  plotCurveConf->uavObjectName = qSettings->value("uavObject").toString();
74  plotCurveConf->uavFieldName = qSettings->value("uavField").toString();
75  plotCurveConf->color = qSettings->value("color").value<QRgb>();
76  plotCurveConf->yScalePower = qSettings->value("yScalePower").toInt();
77  plotCurveConf->mathFunction = qSettings->value("mathFunction").toString();
78  plotCurveConf->yMeanSamples = qSettings->value("yMeanSamples").toUInt();
79 
80  // Stop reading XML block
81  qSettings->endGroup();
82 
83  m_HistogramSourceConfigs.append(plotCurveConf);
84  }
85 }
86 
91 HistogramScopeConfig::HistogramScopeConfig(Ui::ScopeGadgetOptionsPage *options_page)
92 {
93  bool parseOK = false;
94 
95  binWidth = options_page->spnBinWidth->value();
96  maxNumberOfBins = options_page->spnMaxNumBins->value();
97 
98  // For each y-data source in the list
99  for (int iIndex = 0; iIndex < options_page->lst2dCurves->count(); iIndex++) {
100  QListWidgetItem *listItem = options_page->lst2dCurves->item(iIndex);
101 
102  // Store some additional data for the plot curve on the list item
103  Plot2dCurveConfiguration *newPlotCurveConfigs = new Plot2dCurveConfiguration();
104  newPlotCurveConfigs->uavObjectName =
105  listItem->data(Qt::UserRole + ScopeGadgetOptionsPage::UR_UAVOBJECT).toString();
106  newPlotCurveConfigs->uavFieldName =
107  listItem->data(Qt::UserRole + ScopeGadgetOptionsPage::UR_UAVFIELD).toString();
108  newPlotCurveConfigs->yScalePower =
109  listItem->data(Qt::UserRole + ScopeGadgetOptionsPage::UR_SCALE).toInt(&parseOK);
110  if (!parseOK)
111  newPlotCurveConfigs->yScalePower = 0;
112 
113  QVariant varColor = listItem->data(Qt::UserRole + ScopeGadgetOptionsPage::UR_COLOR);
114  int rgb = varColor.toInt(&parseOK);
115  if (!parseOK)
116  newPlotCurveConfigs->color = QColor(Qt::black).rgb();
117  else
118  newPlotCurveConfigs->color = (QRgb)rgb;
119 
120  newPlotCurveConfigs->yMeanSamples =
121  listItem->data(Qt::UserRole + ScopeGadgetOptionsPage::UR_MEAN).toUInt(&parseOK);
122  if (!parseOK)
123  newPlotCurveConfigs->yMeanSamples = 1;
124 
125  newPlotCurveConfigs->mathFunction =
126  listItem->data(Qt::UserRole + ScopeGadgetOptionsPage::UR_MATHFUNCTION).toString();
127 
128  m_HistogramSourceConfigs.append(newPlotCurveConfigs);
129  }
130 }
131 
133 
140 {
141  HistogramScopeConfig *originalHistogramScopeConfig =
142  reinterpret_cast<HistogramScopeConfig *>(originalScope);
143  HistogramScopeConfig *cloneObj = new HistogramScopeConfig();
144 
145  cloneObj->binWidth = originalHistogramScopeConfig->binWidth;
146  cloneObj->maxNumberOfBins = originalHistogramScopeConfig->maxNumberOfBins;
147  cloneObj->m_refreshInterval = originalHistogramScopeConfig->m_refreshInterval;
148 
149  int histogramSourceCount = originalHistogramScopeConfig->m_HistogramSourceConfigs.size();
150 
151  for (int i = 0; i < histogramSourceCount; i++) {
152  Plot2dCurveConfiguration *currentHistogramSourceConf =
153  originalHistogramScopeConfig->m_HistogramSourceConfigs.at(i);
154  Plot2dCurveConfiguration *newHistogramSourceConf = new Plot2dCurveConfiguration();
155 
156  newHistogramSourceConf->uavObjectName = currentHistogramSourceConf->uavObjectName;
157  newHistogramSourceConf->uavFieldName = currentHistogramSourceConf->uavFieldName;
158  newHistogramSourceConf->color = currentHistogramSourceConf->color;
159  newHistogramSourceConf->yScalePower = currentHistogramSourceConf->yScalePower;
160  newHistogramSourceConf->yMeanSamples = currentHistogramSourceConf->yMeanSamples;
161  newHistogramSourceConf->mathFunction = currentHistogramSourceConf->mathFunction;
162 
163  cloneObj->m_HistogramSourceConfigs.append(newHistogramSourceConf);
164  }
165 
166  return cloneObj;
167 }
168 
173 void HistogramScopeConfig::saveConfiguration(QSettings *qSettings)
174 {
175  // Stop writing XML blocks
176  qSettings->beginGroup(QString("plot2d"));
177 
178  qSettings->setValue("plot2dType", HISTOGRAM);
179  qSettings->setValue("binWidth", binWidth);
180  qSettings->setValue("maxNumberOfBins", maxNumberOfBins);
181 
182  int dataSourceCount = m_HistogramSourceConfigs.size();
183  qSettings->setValue("dataSourceCount", dataSourceCount);
184 
185  // For each curve source in the plot
186  for (int i = 0; i < dataSourceCount; i++) {
187  Plot2dCurveConfiguration *plotCurveConf = m_HistogramSourceConfigs.at(i);
188  qSettings->beginGroup(QString("histogramDataSource") + QString().number(i));
189 
190  qSettings->setValue("uavObject", plotCurveConf->uavObjectName);
191  qSettings->setValue("uavField", plotCurveConf->uavFieldName);
192  qSettings->setValue("color", plotCurveConf->color);
193  qSettings->setValue("mathFunction", plotCurveConf->mathFunction);
194  qSettings->setValue("yScalePower", plotCurveConf->yScalePower);
195  qSettings->setValue("yMeanSamples", plotCurveConf->yMeanSamples);
196 
197  // Stop writing XML blocks
198  qSettings->endGroup();
199  }
200 
201  // Stop writing XML block
202  qSettings->endGroup();
203 }
204 
210  QList<Plot2dCurveConfiguration *> histogramSourceConfigs)
211 {
212  m_HistogramSourceConfigs.clear();
213  m_HistogramSourceConfigs.append(histogramSourceConfigs);
214 }
215 
222 {
223  preparePlot(scopeGadgetWidget);
224  scopeGadgetWidget->setScope(this);
225  scopeGadgetWidget->startTimer(m_refreshInterval);
226 
227  // Configure each data source
228  foreach (Plot2dCurveConfiguration *histogramDataSourceConfig, m_HistogramSourceConfigs) {
229  QRgb color = histogramDataSourceConfig->color;
230 
231  // Get and store the units
232  units = getUavObjectFieldUnits(histogramDataSourceConfig->uavObjectName,
233  histogramDataSourceConfig->uavFieldName);
234 
235  HistogramData *histogramData;
236  histogramData =
237  new HistogramData(histogramDataSourceConfig->uavObjectName,
238  histogramDataSourceConfig->uavFieldName, binWidth, maxNumberOfBins);
239 
240  histogramData->setScalePower(histogramDataSourceConfig->yScalePower);
241  histogramData->setMeanSamples(histogramDataSourceConfig->yMeanSamples);
242  histogramData->setMathFunction(histogramDataSourceConfig->mathFunction);
243 
244  // Generate the curve name
245  QString curveName =
246  (histogramData->getUavoName()) + "." + (histogramData->getUavoFieldName());
247  if (histogramData->getHaveSubFieldFlag())
248  curveName = curveName.append("." + histogramData->getUavoSubFieldName());
249 
250  // Get the uav object
251  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
252  UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
253  UAVDataObject *obj =
254  dynamic_cast<UAVDataObject *>(objManager->getObject((histogramData->getUavoName())));
255  if (!obj) {
256  qDebug() << "Object " << histogramData->getUavoName() << " is missing";
257  return;
258  }
259 
260  // Get the units
261  QString units =
262  getUavObjectFieldUnits(histogramData->getUavoName(), histogramData->getUavoFieldName());
263 
264  // Generate name with scaling factor appeneded
265  QString histogramNameScaled;
266  if (histogramDataSourceConfig->yScalePower == 0)
267  histogramNameScaled = curveName + "(" + units + ")";
268  else
269  histogramNameScaled = curveName + "(x10^"
270  + QString::number(histogramDataSourceConfig->yScalePower) + " " + units + ")";
271 
272  while (scopeGadgetWidget->getDataSources().keys().contains(histogramNameScaled))
273  histogramNameScaled = histogramNameScaled + "*";
274 
275  // Create the histogram
276  QwtPlotHistogram *plotHistogram = new QwtPlotHistogram(histogramNameScaled);
277  plotHistogram->setStyle(QwtPlotHistogram::Columns);
278  plotHistogram->setBrush(QBrush(QColor(color)));
279  plotHistogram->setData(histogramData->getIntervalSeriesData());
280 
281  plotHistogram->attach(scopeGadgetWidget);
282  histogramData->setHistogram(plotHistogram);
283 
284  // Keep the curve details for later
285  scopeGadgetWidget->insertDataSources(histogramNameScaled, histogramData);
286 
287  // Connect the UAVO
288  scopeGadgetWidget->connectUAVO(obj);
289  }
290  scopeGadgetWidget->replot();
291 }
292 
298 void HistogramScopeConfig::setGuiConfiguration(Ui::ScopeGadgetOptionsPage *options_page)
299 {
300  // Set the tab widget to 2D
301  options_page->tabWidget2d3d->setCurrentWidget(options_page->tabPlot2d);
302 
303  // Set the plot type
304  options_page->cmb2dPlotType->setCurrentIndex(options_page->cmb2dPlotType->findData(HISTOGRAM));
305 
306  // add the configured 2D curves
307  options_page->lst2dCurves->clear(); // Clear list first
308 
309  foreach (Plot2dCurveConfiguration *dataSource, m_HistogramSourceConfigs) {
310  options_page->spnMaxNumBins->setValue(maxNumberOfBins);
311  options_page->spnBinWidth->setValue(binWidth);
312 
313  QString uavObjectName = dataSource->uavObjectName;
314  QString uavFieldName = dataSource->uavFieldName;
315  int scale = dataSource->yScalePower;
316  unsigned int mean = dataSource->yMeanSamples;
317  QString mathFunction = dataSource->mathFunction;
318  QVariant varColor = dataSource->color;
319 
320  QString listItemDisplayText = uavObjectName + "." + uavFieldName; // Generate the name
321  options_page->lst2dCurves->addItem(listItemDisplayText); // Add the name to the list
322  int itemIdx =
323  options_page->lst2dCurves->count() - 1; // Get the index number for the new value
324  QListWidgetItem *listWidgetItem =
325  options_page->lst2dCurves->item(itemIdx); // Find the widget item
326 
327  bool parseOK = false;
328  QRgb rgbColor;
329 
330  if (uavObjectName != "") {
331  // Set the properties of the newly added list item
332  listItemDisplayText = uavObjectName + "." + uavFieldName;
333  rgbColor = (QRgb)varColor.toInt(&parseOK);
334  if (!parseOK)
335  rgbColor = qRgb(255, 0, 0);
336  } else {
337  listItemDisplayText = "New graph";
338  rgbColor = qRgb(255, 0, 0);
339  }
340 
341  QColor color = QColor(rgbColor);
342  listWidgetItem->setText(listItemDisplayText);
343  listWidgetItem->setTextColor(color);
344 
345  // Store some additional data for the plot curve on the list item
346  // Store some additional data for the plot curve on the list item
347  listWidgetItem->setData(Qt::UserRole + ScopeGadgetOptionsPage::UR_UAVOBJECT,
348  QVariant(uavObjectName));
349  listWidgetItem->setData(Qt::UserRole + ScopeGadgetOptionsPage::UR_UAVFIELD,
350  QVariant(uavFieldName));
351  listWidgetItem->setData(Qt::UserRole + ScopeGadgetOptionsPage::UR_SCALE, QVariant(scale));
352  listWidgetItem->setData(Qt::UserRole + ScopeGadgetOptionsPage::UR_COLOR, varColor);
353  listWidgetItem->setData(Qt::UserRole + ScopeGadgetOptionsPage::UR_MEAN, QVariant(mean));
354  listWidgetItem->setData(Qt::UserRole + ScopeGadgetOptionsPage::UR_MATHFUNCTION,
355  QVariant(mathFunction));
356 
357  // Select the row with the new name
358  options_page->lst2dCurves->setCurrentRow(itemIdx);
359  }
360 
361  // Select row 1st row in list
362  options_page->lst2dCurves->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
363 }
364 
370 {
371  scopeGadgetWidget->setMinimumSize(64, 64);
372  scopeGadgetWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
373 
374  scopeGadgetWidget->setCanvasBackground(QColor(64, 64, 64));
375 
376  scopeGadgetWidget->plotLayout()->setAlignCanvasToScales(false);
377 
378  scopeGadgetWidget->m_grid->enableX(false);
379  scopeGadgetWidget->m_grid->enableY(true);
380  scopeGadgetWidget->m_grid->enableXMin(false);
381  scopeGadgetWidget->m_grid->enableYMin(false);
382  scopeGadgetWidget->m_grid->setMajorPen(QPen(Qt::black, 0, Qt::DotLine));
383  scopeGadgetWidget->m_grid->setMinorPen(QPen(Qt::lightGray, 0, Qt::DotLine));
384  scopeGadgetWidget->m_grid->setPen(QPen(Qt::darkGray, 1, Qt::DotLine));
385  scopeGadgetWidget->m_grid->attach(scopeGadgetWidget);
386 
387  // Add the legend
388  scopeGadgetWidget->addLegend();
389 
390  // Configure axes
391  configureAxes(scopeGadgetWidget);
392 }
393 
399 {
400  // Configure axes
401  scopeGadgetWidget->setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw());
402  scopeGadgetWidget->setAxisAutoScale(QwtPlot::xBottom);
403  scopeGadgetWidget->setAxisLabelRotation(QwtPlot::xBottom, 0.0);
404  scopeGadgetWidget->setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
405  scopeGadgetWidget->axisWidget(QwtPlot::yRight)->setColorBarEnabled(false);
406  scopeGadgetWidget->enableAxis(QwtPlot::yRight, false);
407 
408  // Reduce the gap between the scope canvas and the axis scale
409  QwtScaleWidget *scaleWidget = scopeGadgetWidget->axisWidget(QwtPlot::xBottom);
410  scaleWidget->setMargin(0);
411 
412  // reduce the axis font size
413  QFont fnt(scopeGadgetWidget->axisFont(QwtPlot::xBottom));
414  fnt.setPointSize(7);
415  scopeGadgetWidget->setAxisFont(QwtPlot::xBottom, fnt); // x-axis
416  scopeGadgetWidget->setAxisFont(QwtPlot::yLeft, fnt); // y-axis
417 }
void setHistogram(QwtPlotHistogram *val)
void connectUAVO(UAVDataObject *obj)
ScopeGadgetWidget::connectUAVO Connects UAVO update signal, but only if it hasn't yet been connected...
virtual void loadConfiguration(ScopeGadgetWidget *scopeGadgetWidget)
HistogramScopeConfig::loadConfiguration loads the plot configuration into the scope gadget widget...
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
HistogramScopeConfig()
HistogramScopeConfig::HistogramScopeConfig Default constructor.
QString getUavoFieldName()
Definition: plotdata.h:68
void setScope(ScopeConfig *val)
for i
Definition: OPPlots.m:140
QString getUavoSubFieldName()
Definition: plotdata.h:69
void setMeanSamples(int val)
Definition: plotdata.h:57
void addLegend()
ScopeGadgetWidget::addLegend Add legend to plot.
virtual void saveConfiguration(QSettings *qSettings)
HistogramScopeConfig::saveConfiguration Saves configuration to XML file.
virtual ScopeConfig * cloneScope(ScopeConfig *histogramSourceConfigs)
HistogramScopeConfig::cloneScope Clones scope from existing GUI configuration.
QwtIntervalSeriesData * getIntervalSeriesData()
PlotDimensions m_plotDimensions
Definition: scopesconfig.h:75
virtual void preparePlot(ScopeGadgetWidget *)
HistogramScopeConfig::preparePlot Prepares the Qwt plot colors and axes.
The ScopeConfig class The parent class for scope configuration classes data sources.
Definition: scopesconfig.h:56
The HistogramData class The histogram plot has a variable sized buffer of data, where the data is for...
int m_refreshInterval
Definition: scopesconfig.h:73
The HistogramScopeConfig class The histogram scope configuration.
void insertDataSources(QString stringVal, PlotData *dataVal)
QString getUavObjectFieldUnits(QString uavObjectName, QString uavObjectFieldName)
Definition: scopesconfig.h:77
QMap< QString, PlotData * > getDataSources()
virtual void setGuiConfiguration(Ui::ScopeGadgetOptionsPage *options_page)
HistogramScopeConfig::setGuiConfiguration Set the GUI elements based on values from the XML settings ...
void startTimer(int)
ScopeGadgetWidget::startTimer Starts timer.
bool getHaveSubFieldFlag()
Definition: plotdata.h:70
UAVObject * getObject(const QString &name, quint32 instId=0)
void replaceHistogramDataSource(QList< Plot2dCurveConfiguration * > histogramSourceConfigs)
HistogramScopeConfig::replaceHistogramSource Replaces the list of histogram data sources.
e
Definition: OPPlots.m:99
void configureAxes(ScopeGadgetWidget *)
HistogramScopeConfig::configureAxes Configure the axes.
QwtPlotGrid * m_grid