2010-08-10 07:42:39 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file scopegadgetwidget.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup ScopePlugin Scope Gadget Plugin
|
|
|
|
* @{
|
|
|
|
* @brief The scope Gadget, graphically plots the states of UAVObjects
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "scopegadgetwidget.h"
|
2010-08-26 21:54:23 +02:00
|
|
|
#include "utils/stylehelper.h"
|
2010-08-10 07:42:39 +02:00
|
|
|
|
2011-01-20 23:30:49 +01:00
|
|
|
#include "extensionsystem/pluginmanager.h"
|
2011-01-22 18:38:22 +01:00
|
|
|
#include "uavobjectmanager.h"
|
|
|
|
#include "uavobject.h"
|
2011-01-20 23:30:49 +01:00
|
|
|
#include "coreplugin/icore.h"
|
|
|
|
#include "coreplugin/connectionmanager.h"
|
|
|
|
|
2010-08-10 07:42:39 +02:00
|
|
|
#include "qwt/src/qwt_plot_curve.h"
|
|
|
|
#include "qwt/src/qwt_plot_grid.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <math.h>
|
|
|
|
#include <QDebug>
|
2014-01-26 16:54:24 +01:00
|
|
|
#include <QDir>
|
2010-08-10 07:42:39 +02:00
|
|
|
#include <QColor>
|
|
|
|
#include <QStringList>
|
2013-09-15 23:06:25 +02:00
|
|
|
#include <QWidget>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QPushButton>
|
2011-02-14 11:19:18 +01:00
|
|
|
#include <QMutexLocker>
|
2012-05-24 23:48:58 +02:00
|
|
|
#include <QWheelEvent>
|
2010-08-10 07:42:39 +02:00
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
#include <qwt/src/qwt_legend_label.h>
|
|
|
|
#include <qwt/src/qwt_plot_canvas.h>
|
|
|
|
#include <qwt/src/qwt_plot_layout.h>
|
|
|
|
|
|
|
|
ScopeGadgetWidget::ScopeGadgetWidget(QWidget *parent) : QwtPlot(parent),
|
|
|
|
m_csvLoggingStarted(false), m_csvLoggingEnabled(false),
|
|
|
|
m_csvLoggingHeaderSaved(false), m_csvLoggingDataSaved(false),
|
|
|
|
m_csvLoggingNameSet(false), m_csvLoggingDataValid(false),
|
|
|
|
m_csvLoggingDataUpdated(false), m_csvLoggingConnected(false),
|
|
|
|
m_csvLoggingNewFileOnConnect(false),
|
|
|
|
m_csvLoggingStartTime(QDateTime::currentDateTime()),
|
|
|
|
m_csvLoggingPath("./csvlogging/"),
|
|
|
|
m_plotLegend(NULL)
|
2010-08-10 07:42:39 +02:00
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
setMouseTracking(true);
|
2014-10-30 01:12:43 +01:00
|
|
|
//plotLayout()->setCanvasMargin(20, QwtPlot::yRight);
|
|
|
|
QwtPlotCanvas * plotCanvas = dynamic_cast<QwtPlotCanvas *>(canvas());
|
|
|
|
if (plotCanvas) {
|
|
|
|
plotCanvas->setFrameStyle(QFrame::NoFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
axisWidget(QwtPlot::yLeft)->setMargin(2);
|
|
|
|
axisWidget(QwtPlot::xBottom)->setMargin(2);
|
2011-02-14 11:19:18 +01:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
// Setup the timer that replots data
|
2010-08-14 22:25:55 +02:00
|
|
|
replotTimer = new QTimer(this);
|
|
|
|
connect(replotTimer, SIGNAL(timeout()), this, SLOT(replotNewData()));
|
2011-01-20 23:30:49 +01:00
|
|
|
|
2012-05-24 23:48:58 +02:00
|
|
|
// Listen to telemetry connection/disconnection events, no point in
|
|
|
|
// running the scopes if we are not connected and not replaying logs.
|
2011-01-20 23:30:49 +01:00
|
|
|
// Also listen to disconnect actions from the user
|
|
|
|
Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
|
2011-03-25 11:59:24 +01:00
|
|
|
connect(cm, SIGNAL(deviceAboutToDisconnect()), this, SLOT(stopPlotting()));
|
2013-05-13 21:24:19 +02:00
|
|
|
connect(cm, SIGNAL(deviceConnected(QIODevice *)), this, SLOT(startPlotting()));
|
|
|
|
|
|
|
|
// Listen to autopilot connection events
|
2011-03-25 11:59:24 +01:00
|
|
|
connect(cm, SIGNAL(deviceAboutToDisconnect()), this, SLOT(csvLoggingDisconnect()));
|
2013-05-13 21:24:19 +02:00
|
|
|
connect(cm, SIGNAL(deviceConnected(QIODevice *)), this, SLOT(csvLoggingConnect()));
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ScopeGadgetWidget::~ScopeGadgetWidget()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
if (replotTimer) {
|
|
|
|
replotTimer->stop();
|
|
|
|
delete replotTimer;
|
|
|
|
replotTimer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the object to de-monitor
|
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
|
|
|
|
foreach(QString uavObjName, m_connectedUAVObjects) {
|
|
|
|
UAVDataObject *obj = dynamic_cast<UAVDataObject *>(objManager->getObject(uavObjName));
|
|
|
|
disconnect(obj, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(uavObjectReceived(UAVObject *)));
|
|
|
|
}
|
|
|
|
|
|
|
|
clearCurvePlots();
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
QwtPlot::mousePressEvent(e);
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
QwtPlot::mouseReleaseEvent(e);
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::mouseDoubleClickEvent(QMouseEvent *e)
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
// On double-click, toggle legend
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.lock();
|
2013-05-13 21:24:19 +02:00
|
|
|
if (legend()) {
|
2012-05-24 23:48:58 +02:00
|
|
|
deleteLegend();
|
2013-05-13 21:24:19 +02:00
|
|
|
} else {
|
2012-05-24 23:48:58 +02:00
|
|
|
addLegend();
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.unlock();
|
2012-05-24 23:48:58 +02:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
// On double-click, reset plot zoom
|
2012-05-24 23:48:58 +02:00
|
|
|
setAxisAutoScale(QwtPlot::yLeft, true);
|
|
|
|
|
|
|
|
update();
|
2011-02-14 11:19:18 +01:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
QwtPlot::mouseDoubleClickEvent(e);
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::mouseMoveEvent(QMouseEvent *e)
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
QwtPlot::mouseMoveEvent(e);
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
2011-01-31 03:09:44 +01:00
|
|
|
|
2011-02-14 11:19:18 +01:00
|
|
|
void ScopeGadgetWidget::wheelEvent(QWheelEvent *e)
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
// Change zoom on scroll wheel event
|
|
|
|
QwtInterval yInterval = axisInterval(QwtPlot::yLeft);
|
|
|
|
|
|
|
|
if (yInterval.minValue() != yInterval.maxValue()) { // Make sure that the two values are never the same. Sometimes axisInterval returns (0,0)
|
|
|
|
// Determine what y value to zoom about. NOTE, this approach has a bug that the in that
|
|
|
|
// the value returned by Qt includes the legend, whereas the value transformed by Qwt
|
|
|
|
// does *not*. Thus, when zooming with a legend, there will always be a small bias error.
|
|
|
|
// In practice, this seems not to be a UI problem.
|
|
|
|
QPoint mouse_pos = e->pos(); // Get the mouse coordinate in the frame
|
|
|
|
double zoomLine = invTransform(QwtPlot::yLeft, mouse_pos.y()); // Transform the y mouse coordinate into a frame value.
|
|
|
|
|
|
|
|
double zoomScale = 1.1; // THIS IS AN ARBITRARY CONSTANT, AND PERHAPS SHOULD BE IN A DEFINE INSTEAD OF BURIED HERE
|
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.lock(); // DOES THIS mutex.lock NEED TO BE HERE? I DON'T KNOW, I JUST COPIED IT FROM THE ABOVE CODE
|
2012-05-24 23:48:58 +02:00
|
|
|
// Set the scale
|
2013-05-13 21:24:19 +02:00
|
|
|
if (e->delta() < 0) {
|
2012-05-24 23:48:58 +02:00
|
|
|
setAxisScale(QwtPlot::yLeft,
|
2013-05-13 21:24:19 +02:00
|
|
|
(yInterval.minValue() - zoomLine) * zoomScale + zoomLine,
|
|
|
|
(yInterval.maxValue() - zoomLine) * zoomScale + zoomLine);
|
|
|
|
} else {
|
2012-05-24 23:48:58 +02:00
|
|
|
setAxisScale(QwtPlot::yLeft,
|
2013-05-13 21:24:19 +02:00
|
|
|
(yInterval.minValue() - zoomLine) / zoomScale + zoomLine,
|
|
|
|
(yInterval.maxValue() - zoomLine) / zoomScale + zoomLine);
|
2012-05-24 23:48:58 +02:00
|
|
|
}
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.unlock();
|
2012-05-24 23:48:58 +02:00
|
|
|
}
|
2012-05-22 11:34:01 +02:00
|
|
|
QwtPlot::wheelEvent(e);
|
2011-01-20 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
void ScopeGadgetWidget::showEvent(QShowEvent *e)
|
|
|
|
{
|
|
|
|
replotNewData();
|
|
|
|
QwtPlot::showEvent(e);
|
|
|
|
}
|
|
|
|
|
2011-01-20 23:30:49 +01:00
|
|
|
/**
|
|
|
|
* Starts/stops telemetry
|
|
|
|
*/
|
2011-01-26 23:21:54 +01:00
|
|
|
void ScopeGadgetWidget::startPlotting()
|
2011-01-20 23:30:49 +01:00
|
|
|
{
|
2014-10-28 18:12:04 +01:00
|
|
|
if (replotTimer && !replotTimer->isActive()) {
|
2011-01-20 23:30:49 +01:00
|
|
|
replotTimer->start(m_refreshInterval);
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2011-01-20 23:30:49 +01:00
|
|
|
}
|
|
|
|
|
2011-01-26 23:21:54 +01:00
|
|
|
void ScopeGadgetWidget::stopPlotting()
|
2011-01-20 23:30:49 +01:00
|
|
|
{
|
2014-10-28 18:12:04 +01:00
|
|
|
if (replotTimer) {
|
|
|
|
replotTimer->stop();
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
2011-02-14 11:19:18 +01:00
|
|
|
void ScopeGadgetWidget::deleteLegend()
|
|
|
|
{
|
2014-10-30 01:12:43 +01:00
|
|
|
if (m_plotLegend) {
|
2014-10-28 18:12:04 +01:00
|
|
|
insertLegend(NULL, QwtPlot::TopLegend);
|
2014-10-30 01:12:43 +01:00
|
|
|
m_plotLegend = NULL;
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::addLegend()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
if (legend()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show a legend at the top
|
2014-10-30 01:12:43 +01:00
|
|
|
m_plotLegend = new QwtLegend(this);
|
|
|
|
m_plotLegend->setDefaultItemMode(QwtLegendData::Checkable);
|
|
|
|
m_plotLegend->setFrameStyle(QFrame::NoFrame);
|
|
|
|
m_plotLegend->setToolTip(tr("Click legend to show/hide scope trace.\nDouble click legend or plot to show/hide legend."));
|
2013-05-13 21:24:19 +02:00
|
|
|
|
2014-01-26 16:54:24 +01:00
|
|
|
// set colors
|
2014-10-30 01:12:43 +01:00
|
|
|
QPalette pal = m_plotLegend->palette();
|
|
|
|
pal.setColor(m_plotLegend->backgroundRole(), QColor(100, 100, 100));
|
2014-01-26 16:54:24 +01:00
|
|
|
pal.setColor(QPalette::Text, QColor(0, 0, 0));
|
2014-10-30 01:12:43 +01:00
|
|
|
m_plotLegend->setPalette(pal);
|
2013-05-13 21:24:19 +02:00
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
insertLegend(m_plotLegend, QwtPlot::TopLegend);
|
2011-02-14 11:19:18 +01:00
|
|
|
|
2012-05-24 23:48:58 +02:00
|
|
|
// Update the checked/unchecked state of the legend items
|
|
|
|
// -> this is necessary when hiding a legend where some plots are
|
2014-01-26 16:54:24 +01:00
|
|
|
// not visible, and then un-hiding it.
|
2014-10-30 01:12:43 +01:00
|
|
|
foreach(QwtPlotItem * plotItem, itemList()) {
|
|
|
|
QWidget *legendWidget = m_plotLegend->legendWidget(QwtPlot::itemToInfo(plotItem));
|
|
|
|
if (legendWidget && legendWidget->inherits("QwtLegendLabel")) {
|
|
|
|
((QwtLegendLabel *)legendWidget)->setChecked(!plotItem->isVisible());
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2012-05-24 23:48:58 +02:00
|
|
|
}
|
2011-03-13 15:14:52 +01:00
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
connect(m_plotLegend, SIGNAL(checked(QVariant, bool, int)), this, SLOT(showCurve(QVariant,bool,int)));
|
2011-02-14 11:19:18 +01:00
|
|
|
}
|
|
|
|
|
2010-08-10 07:42:39 +02:00
|
|
|
void ScopeGadgetWidget::preparePlot(PlotType plotType)
|
|
|
|
{
|
|
|
|
m_plotType = plotType;
|
|
|
|
|
|
|
|
clearCurvePlots();
|
|
|
|
|
|
|
|
setMinimumSize(64, 64);
|
|
|
|
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
|
2011-02-13 22:04:31 +01:00
|
|
|
|
2012-05-24 23:48:58 +02:00
|
|
|
setCanvasBackground(QColor(64, 64, 64));
|
2010-08-14 22:25:55 +02:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
// Add grid lines
|
2010-08-14 22:25:55 +02:00
|
|
|
QwtPlotGrid *grid = new QwtPlotGrid;
|
2014-10-30 01:12:43 +01:00
|
|
|
grid->setMajorPen(QPen(Qt::gray, 0, Qt::DashLine));
|
|
|
|
grid->setMinorPen(QPen(Qt::lightGray, 0, Qt::DotLine));
|
2012-05-24 23:48:58 +02:00
|
|
|
grid->setPen(QPen(Qt::darkGray, 1, Qt::DotLine));
|
|
|
|
grid->attach(this);
|
2010-08-10 07:42:39 +02:00
|
|
|
|
2011-01-20 23:30:49 +01:00
|
|
|
// Only start the timer if we are already connected
|
|
|
|
Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
|
2014-07-14 22:09:53 +02:00
|
|
|
if (cm->isConnected() && replotTimer) {
|
2013-05-13 21:24:19 +02:00
|
|
|
if (!replotTimer->isActive()) {
|
2012-05-24 23:48:58 +02:00
|
|
|
replotTimer->start(m_refreshInterval);
|
2013-05-13 21:24:19 +02:00
|
|
|
} else {
|
2012-05-24 23:48:58 +02:00
|
|
|
replotTimer->setInterval(m_refreshInterval);
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2012-05-24 23:48:58 +02:00
|
|
|
}
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
void ScopeGadgetWidget::showCurve(QVariant itemInfo, bool visible, int index)
|
2010-08-10 07:42:39 +02:00
|
|
|
{
|
2014-10-30 01:12:43 +01:00
|
|
|
Q_UNUSED(index);
|
|
|
|
QwtPlotItem *item = infoToItem(itemInfo);
|
|
|
|
item->setVisible(!visible);
|
|
|
|
if (m_plotLegend) {
|
|
|
|
QWidget *legendItem = legend()->find((WId)item);
|
|
|
|
if (legendItem && legendItem->inherits("QwtLegendLabel")) {
|
|
|
|
((QwtLegendLabel *)legendItem)->setChecked(visible);
|
2014-01-26 16:54:24 +01:00
|
|
|
}
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2010-08-10 07:42:39 +02:00
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.lock();
|
2013-05-13 21:24:19 +02:00
|
|
|
replot();
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.unlock();
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 23:34:29 +02:00
|
|
|
void ScopeGadgetWidget::setupSequentialPlot()
|
2010-08-10 07:42:39 +02:00
|
|
|
{
|
2012-05-21 23:34:29 +02:00
|
|
|
preparePlot(SequentialPlot);
|
2010-08-10 07:42:39 +02:00
|
|
|
|
|
|
|
setAxisScaleDraw(QwtPlot::xBottom, new QwtScaleDraw());
|
2014-10-22 00:52:39 +02:00
|
|
|
setAxisScale(QwtPlot::xBottom, 0, m_plotDataSize);
|
2010-08-10 07:42:39 +02:00
|
|
|
setAxisLabelRotation(QwtPlot::xBottom, 0.0);
|
|
|
|
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
|
2011-02-14 01:44:33 +01:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
// reduce the axis font size
|
|
|
|
QFont fnt(axisFont(QwtPlot::xBottom));
|
|
|
|
fnt.setPointSize(7);
|
|
|
|
setAxisFont(QwtPlot::xBottom, fnt); // x-axis
|
|
|
|
setAxisFont(QwtPlot::yLeft, fnt); // y-axis
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::setupChronoPlot()
|
|
|
|
{
|
|
|
|
preparePlot(ChronoPlot);
|
|
|
|
|
|
|
|
setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw());
|
|
|
|
uint NOW = QDateTime::currentDateTime().toTime_t();
|
2014-10-22 00:52:39 +02:00
|
|
|
setAxisScale(QwtPlot::xBottom, NOW - m_plotDataSize / 1000, NOW);
|
2013-05-13 21:24:19 +02:00
|
|
|
setAxisLabelRotation(QwtPlot::xBottom, 0.0);
|
|
|
|
setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom);
|
|
|
|
|
|
|
|
// reduce the axis font size
|
|
|
|
QFont fnt(axisFont(QwtPlot::xBottom));
|
|
|
|
fnt.setPointSize(7);
|
|
|
|
setAxisFont(QwtPlot::xBottom, fnt); // x-axis
|
|
|
|
setAxisFont(QwtPlot::yLeft, fnt); // y-axis
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
2014-10-28 13:33:21 +01:00
|
|
|
void ScopeGadgetWidget::addCurvePlot(QString objectName, QString fieldPlusSubField, int scaleFactor,
|
|
|
|
int meanSamples, QString mathFunction, QPen pen, bool antialiased)
|
2010-08-10 07:42:39 +02:00
|
|
|
{
|
2014-10-22 00:52:39 +02:00
|
|
|
QString fieldName = fieldPlusSubField;
|
|
|
|
QString elementName;
|
2014-10-28 13:33:21 +01:00
|
|
|
int element = 0;
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
if (fieldPlusSubField.contains("-")) {
|
|
|
|
QStringList fieldSubfield = fieldName.split("-", QString::SkipEmptyParts);
|
|
|
|
fieldName = fieldSubfield.at(0);
|
|
|
|
elementName = fieldSubfield.at(1);
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2010-08-10 07:42:39 +02:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
// Get the uav object
|
2010-09-01 23:29:24 +02:00
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
|
2014-10-28 13:33:21 +01:00
|
|
|
UAVDataObject *object = dynamic_cast<UAVDataObject *>(objManager->getObject(objectName));
|
|
|
|
if (!object) {
|
|
|
|
qDebug() << "Object" << objectName << "is missing";
|
2011-08-29 18:37:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-10-28 13:33:21 +01:00
|
|
|
|
|
|
|
UAVObjectField *field = object->getField(fieldName);
|
2013-05-13 21:24:19 +02:00
|
|
|
if (!field) {
|
2014-10-22 00:52:39 +02:00
|
|
|
qDebug() << "In scope gadget, in fields loaded from GCS config file, field" <<
|
2014-10-28 13:33:21 +01:00
|
|
|
fieldName << "of object" << objectName << "is missing";
|
2011-08-29 18:37:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-10-28 13:33:21 +01:00
|
|
|
|
|
|
|
if (!elementName.isEmpty()) {
|
|
|
|
element = field->getElementNames().indexOf(QRegExp(elementName, Qt::CaseSensitive, QRegExp::FixedString));
|
|
|
|
if (element < 0) {
|
|
|
|
qDebug() << "In scope gadget, in fields loaded from GCS config file, field" <<
|
|
|
|
fieldName << "of object" << objectName << "element name" << elementName << "is missing";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 00:52:39 +02:00
|
|
|
PlotData *plotData;
|
|
|
|
|
|
|
|
if (m_plotType == SequentialPlot) {
|
2014-10-28 22:42:49 +01:00
|
|
|
plotData = new SequentialPlotData(object, field, element, scaleFactor,
|
|
|
|
meanSamples, mathFunction, m_plotDataSize,
|
|
|
|
pen, antialiased);
|
2014-10-22 00:52:39 +02:00
|
|
|
} else if (m_plotType == ChronoPlot) {
|
2014-10-28 22:42:49 +01:00
|
|
|
plotData = new ChronoPlotData(object, field, element, scaleFactor,
|
|
|
|
meanSamples, mathFunction, m_plotDataSize,
|
|
|
|
pen, antialiased);
|
2014-10-22 00:52:39 +02:00
|
|
|
}
|
|
|
|
|
2014-10-28 22:42:49 +01:00
|
|
|
plotData->attach(this);
|
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
// Keep the curve details for later
|
2014-10-29 00:05:53 +01:00
|
|
|
m_curvesData.insert(plotData->plotName(), plotData);
|
2010-08-10 07:42:39 +02:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
// Link to the new signal data only if this UAVObject has not been connected yet
|
2014-10-28 13:33:21 +01:00
|
|
|
if (!m_connectedUAVObjects.contains(object->getName())) {
|
|
|
|
m_connectedUAVObjects.append(object->getName());
|
|
|
|
connect(object, SIGNAL(objectUpdated(UAVObject *)), this, SLOT(uavObjectReceived(UAVObject *)));
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.lock();
|
2013-05-13 21:24:19 +02:00
|
|
|
replot();
|
2014-10-30 01:12:43 +01:00
|
|
|
m_mutex.unlock();
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
void ScopeGadgetWidget::uavObjectReceived(UAVObject *obj)
|
2010-08-10 07:42:39 +02:00
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
foreach(PlotData * plotData, m_curvesData.values()) {
|
|
|
|
if (plotData->append(obj)) {
|
|
|
|
m_csvLoggingDataUpdated = 1;
|
|
|
|
}
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
2011-11-26 22:51:20 +01:00
|
|
|
csvLoggingAddData();
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::replotNewData()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
if (!isVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-14 11:19:18 +01:00
|
|
|
|
2014-10-30 01:12:43 +01:00
|
|
|
QMutexLocker locker(&m_mutex);
|
2013-05-13 21:24:19 +02:00
|
|
|
foreach(PlotData * plotData, m_curvesData.values()) {
|
2010-08-14 22:25:55 +02:00
|
|
|
plotData->removeStaleData();
|
2014-10-29 00:05:53 +01:00
|
|
|
plotData->updatePlotData();
|
2010-08-14 22:25:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QDateTime NOW = QDateTime::currentDateTime();
|
|
|
|
double toTime = NOW.toTime_t();
|
|
|
|
toTime += NOW.time().msec() / 1000.0;
|
2013-05-13 21:24:19 +02:00
|
|
|
if (m_plotType == ChronoPlot) {
|
2014-10-22 00:52:39 +02:00
|
|
|
setAxisScale(QwtPlot::xBottom, toTime - m_plotDataSize, toTime);
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2011-02-14 11:19:18 +01:00
|
|
|
|
2011-01-31 03:09:44 +01:00
|
|
|
csvLoggingInsertData();
|
2010-08-10 07:42:39 +02:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
replot();
|
2010-08-10 07:42:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::clearCurvePlots()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
foreach(PlotData * plotData, m_curvesData.values()) {
|
2010-08-10 07:42:39 +02:00
|
|
|
delete plotData;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_curvesData.clear();
|
|
|
|
}
|
|
|
|
|
2014-01-26 16:54:24 +01:00
|
|
|
void ScopeGadgetWidget::saveState(QSettings *qSettings)
|
|
|
|
{
|
|
|
|
// plot state
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
foreach(PlotData * plotData, m_curvesData.values()) {
|
2014-10-22 00:52:39 +02:00
|
|
|
bool plotVisible = plotData->isVisible();
|
2014-03-20 01:46:38 +01:00
|
|
|
|
2014-01-30 21:54:05 +01:00
|
|
|
if (!plotVisible) {
|
|
|
|
qSettings->setValue(QString("plot%1").arg(i), plotVisible);
|
|
|
|
}
|
2014-01-26 16:54:24 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
// legend state
|
|
|
|
qSettings->setValue("legendVisible", legend() != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::restoreState(QSettings *qSettings)
|
|
|
|
{
|
|
|
|
// plot state
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
foreach(PlotData * plotData, m_curvesData.values()) {
|
2014-10-29 00:05:53 +01:00
|
|
|
plotData->setVisible(qSettings->value(QString("plot%1").arg(i), true).toBool());
|
2014-01-26 16:54:24 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
// legend state
|
|
|
|
bool legendVisible = qSettings->value("legendVisible", true).toBool();
|
|
|
|
if (legendVisible) {
|
|
|
|
addLegend();
|
|
|
|
} else {
|
|
|
|
deleteLegend();
|
|
|
|
}
|
|
|
|
}
|
2010-08-14 22:25:55 +02:00
|
|
|
|
2011-01-31 03:09:44 +01:00
|
|
|
/*
|
2013-05-13 21:24:19 +02:00
|
|
|
int csvLoggingEnable;
|
|
|
|
int csvLoggingHeaderSaved;
|
|
|
|
int csvLoggingDataSaved;
|
|
|
|
QString csvLoggingPath;
|
|
|
|
QFile csvLoggingFile;
|
|
|
|
*/
|
2011-01-31 03:09:44 +01:00
|
|
|
int ScopeGadgetWidget::csvLoggingStart()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
if (!m_csvLoggingStarted) {
|
|
|
|
if (m_csvLoggingEnabled) {
|
|
|
|
if ((!m_csvLoggingNewFileOnConnect) || (m_csvLoggingNewFileOnConnect && m_csvLoggingConnected)) {
|
|
|
|
QDateTime NOW = QDateTime::currentDateTime();
|
|
|
|
m_csvLoggingStartTime = NOW;
|
|
|
|
m_csvLoggingHeaderSaved = 0;
|
|
|
|
m_csvLoggingDataSaved = 0;
|
|
|
|
m_csvLoggingBuffer.clear();
|
|
|
|
QDir PathCheck(m_csvLoggingPath);
|
|
|
|
if (!PathCheck.exists()) {
|
|
|
|
PathCheck.mkpath("./");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_csvLoggingNameSet) {
|
|
|
|
m_csvLoggingFile.setFileName(QString("%1/%2_%3_%4.csv").arg(m_csvLoggingPath).arg(m_csvLoggingName).arg(NOW.toString("yyyy-MM-dd")).arg(NOW.toString("hh-mm-ss")));
|
|
|
|
} else {
|
|
|
|
m_csvLoggingFile.setFileName(QString("%1/Log_%2_%3.csv").arg(m_csvLoggingPath).arg(NOW.toString("yyyy-MM-dd")).arg(NOW.toString("hh-mm-ss")));
|
|
|
|
}
|
|
|
|
QDir FileCheck(m_csvLoggingFile.fileName());
|
|
|
|
if (FileCheck.exists()) {
|
|
|
|
m_csvLoggingFile.setFileName("");
|
|
|
|
} else {
|
|
|
|
m_csvLoggingStarted = 1;
|
|
|
|
csvLoggingInsertHeader();
|
|
|
|
}
|
|
|
|
}
|
2011-01-31 03:09:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ScopeGadgetWidget::csvLoggingStop()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
m_csvLoggingStarted = 0;
|
2011-01-31 03:09:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ScopeGadgetWidget::csvLoggingInsertHeader()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
if (!m_csvLoggingStarted) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (m_csvLoggingHeaderSaved) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (m_csvLoggingDataSaved) {
|
|
|
|
return -3;
|
|
|
|
}
|
2011-01-31 03:09:44 +01:00
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
m_csvLoggingHeaderSaved = 1;
|
2013-09-15 23:06:25 +02:00
|
|
|
if (m_csvLoggingFile.open(QIODevice::WriteOnly | QIODevice::Append) == false) {
|
2011-01-31 03:09:44 +01:00
|
|
|
qDebug() << "Unable to open " << m_csvLoggingFile.fileName() << " for csv logging Header";
|
2013-05-13 21:24:19 +02:00
|
|
|
} else {
|
|
|
|
QTextStream ts(&m_csvLoggingFile);
|
|
|
|
ts << "date" << ", " << "Time" << ", " << "Sec since start" << ", " << "Connected" << ", " << "Data changed";
|
|
|
|
|
|
|
|
foreach(PlotData * plotData2, m_curvesData.values()) {
|
|
|
|
ts << ", ";
|
2014-10-22 00:52:39 +02:00
|
|
|
ts << plotData2->objectName();
|
2014-10-28 13:33:21 +01:00
|
|
|
ts << "." << plotData2->field()->getName();
|
2014-10-22 00:52:39 +02:00
|
|
|
if (!plotData2->elementName().isEmpty()) {
|
|
|
|
ts << "." << plotData2->elementName();
|
2013-05-13 21:24:19 +02:00
|
|
|
}
|
2011-01-31 03:09:44 +01:00
|
|
|
}
|
|
|
|
ts << endl;
|
|
|
|
m_csvLoggingFile.close();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-26 22:51:20 +01:00
|
|
|
int ScopeGadgetWidget::csvLoggingAddData()
|
2011-01-31 03:09:44 +01:00
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
if (!m_csvLoggingStarted) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-28 12:19:54 +01:00
|
|
|
m_csvLoggingDataValid = false;
|
2011-01-31 03:09:44 +01:00
|
|
|
QDateTime NOW = QDateTime::currentDateTime();
|
|
|
|
QString tempString;
|
|
|
|
|
2013-05-13 21:24:19 +02:00
|
|
|
QTextStream ss(&tempString);
|
|
|
|
ss << NOW.toString("yyyy-MM-dd") << ", " << NOW.toString("hh:mm:ss.z") << ", ";
|
2011-02-01 18:43:30 +01:00
|
|
|
|
2011-02-01 15:57:39 +01:00
|
|
|
#if QT_VERSION >= 0x040700
|
2013-05-13 21:24:19 +02:00
|
|
|
ss << (NOW.toMSecsSinceEpoch() - m_csvLoggingStartTime.toMSecsSinceEpoch()) / 1000.00;
|
2011-02-01 15:57:39 +01:00
|
|
|
#else
|
2013-05-13 21:24:19 +02:00
|
|
|
ss << (NOW.toTime_t() - m_csvLoggingStartTime.toTime_t());
|
2011-02-01 15:57:39 +01:00
|
|
|
#endif
|
2011-11-26 22:51:20 +01:00
|
|
|
ss << ", " << m_csvLoggingConnected << ", " << m_csvLoggingDataUpdated;
|
2014-10-28 12:19:54 +01:00
|
|
|
m_csvLoggingDataUpdated = false;
|
2013-05-13 21:24:19 +02:00
|
|
|
|
|
|
|
foreach(PlotData * plotData2, m_curvesData.values()) {
|
|
|
|
ss << ", ";
|
2014-10-28 12:19:54 +01:00
|
|
|
if (plotData2->hasData()) {
|
|
|
|
ss << QString().sprintf("%3.10g", plotData2->lastData());
|
|
|
|
m_csvLoggingDataValid = true;
|
2011-01-31 03:09:44 +01:00
|
|
|
}
|
|
|
|
}
|
2011-11-26 22:51:20 +01:00
|
|
|
ss << endl;
|
2013-05-13 21:24:19 +02:00
|
|
|
if (m_csvLoggingDataValid) {
|
|
|
|
QTextStream ts(&m_csvLoggingBuffer);
|
2011-11-26 22:51:20 +01:00
|
|
|
ts << tempString;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ScopeGadgetWidget::csvLoggingInsertData()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
if (!m_csvLoggingStarted) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
m_csvLoggingDataSaved = 1;
|
2011-01-31 03:09:44 +01:00
|
|
|
|
2013-09-15 23:06:25 +02:00
|
|
|
if (m_csvLoggingFile.open(QIODevice::WriteOnly | QIODevice::Append) == false) {
|
2011-11-26 22:51:20 +01:00
|
|
|
qDebug() << "Unable to open " << m_csvLoggingFile.fileName() << " for csv logging Data";
|
2013-05-13 21:24:19 +02:00
|
|
|
} else {
|
|
|
|
QTextStream ts(&m_csvLoggingFile);
|
2011-11-26 22:51:20 +01:00
|
|
|
ts << m_csvLoggingBuffer;
|
|
|
|
m_csvLoggingFile.close();
|
|
|
|
}
|
|
|
|
m_csvLoggingBuffer.clear();
|
2011-01-31 03:09:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-02-14 11:19:18 +01:00
|
|
|
|
2011-01-31 03:09:44 +01:00
|
|
|
void ScopeGadgetWidget::csvLoggingSetName(QString newName)
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
m_csvLoggingName = newName;
|
|
|
|
m_csvLoggingNameSet = 1;
|
2011-01-31 03:09:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopeGadgetWidget::csvLoggingConnect()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
m_csvLoggingConnected = 1;
|
|
|
|
if (m_csvLoggingNewFileOnConnect) {
|
|
|
|
csvLoggingStart();
|
|
|
|
}
|
2011-01-31 03:09:44 +01:00
|
|
|
}
|
2014-01-26 16:54:24 +01:00
|
|
|
|
2011-01-31 03:09:44 +01:00
|
|
|
void ScopeGadgetWidget::csvLoggingDisconnect()
|
|
|
|
{
|
2013-05-13 21:24:19 +02:00
|
|
|
m_csvLoggingHeaderSaved = 0;
|
|
|
|
m_csvLoggingConnected = 0;
|
|
|
|
if (m_csvLoggingNewFileOnConnect) {
|
|
|
|
csvLoggingStop();
|
|
|
|
}
|
2011-01-31 03:09:44 +01:00
|
|
|
}
|