Skip to content

Commit

Permalink
Interactive plot windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrjo5 authored and adeas31 committed Oct 19, 2017
1 parent 017636b commit eb19ef4
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 5 deletions.
6 changes: 6 additions & 0 deletions OMPlot/OMPlotGUI/PlotCurve.cpp
Expand Up @@ -61,6 +61,7 @@ PlotCurve::PlotCurve(QString fileName, QString name, QString xVariableName, QStr
setLegendAttribute(QwtPlotCurve::LegendShowLine);
setLegendIconSize(QSize(30, 30));
#endif
mpPlotDirectPainter = new QwtPlotDirectPainter();
}

PlotCurve::~PlotCurve()
Expand Down Expand Up @@ -146,6 +147,11 @@ const double* PlotCurve::getXAxisVector() const
return mXAxisVector.data();
}

QPair<QVector<double>*, QVector<double>*> PlotCurve::getAxisVectors()
{
return qMakePair(&mXAxisVector, &mYAxisVector);
}

void PlotCurve::setYAxisVector(QVector<double> vector)
{
mYAxisVector = vector;
Expand Down
4 changes: 4 additions & 0 deletions OMPlot/OMPlotGUI/PlotCurve.h
Expand Up @@ -35,6 +35,7 @@
#define PLOTCURVE_H

#include "OMPlot.h"
#include <qwt_plot_directpainter.h>

namespace OMPlot
{
Expand All @@ -53,6 +54,7 @@ class PlotCurve : public QwtPlotCurve
int mStyle;

Plot *mpParentPlot;
QwtPlotDirectPainter *mpPlotDirectPainter;
public:
PlotCurve(QString fileName, QString name, QString xVariableName, QString yVariableName, QString unit, QString displayUnit, Plot *pParent);
~PlotCurve();
Expand All @@ -75,6 +77,7 @@ class PlotCurve : public QwtPlotCurve
void addXAxisValue(double value);
void updateXAxisValue(int index, double value);
const double* getXAxisVector() const;
QPair<QVector<double>*, QVector<double>*> getAxisVectors();
void clearXAxisVector() {mXAxisVector.clear();}
void setYAxisVector(QVector<double> vector);
void addYAxisValue(double value);
Expand All @@ -95,6 +98,7 @@ class PlotCurve : public QwtPlotCurve
bool hasCustomColor();
void toggleVisibility();
void setData(const double* xData, const double* yData, int size);
QwtPlotDirectPainter* getPlotDirectPainter() {return mpPlotDirectPainter;}
#if QWT_VERSION < 0x060000
virtual void updateLegend(QwtLegend *legend) const;
#endif
Expand Down
104 changes: 102 additions & 2 deletions OMPlot/OMPlotGUI/PlotWindow.cpp
Expand Up @@ -42,9 +42,15 @@

using namespace OMPlot;

PlotWindow::PlotWindow(QStringList arguments, QWidget *parent)
: QMainWindow(parent)
PlotWindow::PlotWindow(QStringList arguments, QWidget *parent, bool isInteractiveSimulation, QToolButton *pStartSimulation,
QToolButton *pPauseSimulation, QComboBox *pSimulationSpeed)
: QMainWindow(parent), mIsInteractiveSimulation(isInteractiveSimulation)
{
if (isInteractiveSimulation) {
mpStartSimulationToolButton = pStartSimulation;
mpPauseSimulationToolButton = pPauseSimulation;
mpSimulationSpeedComboBox = pSimulationSpeed;
}
/* set the widget background white. so that the plot is more useable in books and publications. */
QPalette p(palette());
p.setColor(QPalette::Background, Qt::white);
Expand Down Expand Up @@ -140,6 +146,11 @@ void PlotWindow::initializePlot(QStringList arguments)
setPlotType(PlotWindow::PLOTPARAMETRIC);
plotParametric();
}
else if (plotType.toLower().compare("plotinteractive") == 0)
{
setPlotType(PlotWindow::PLOTINTERACTIVE);
plotInteractive();
}
}

void PlotWindow::setVariablesList(QStringList variables)
Expand Down Expand Up @@ -255,6 +266,38 @@ void PlotWindow::setupToolbar()
{
QToolBar *toolBar = new QToolBar(this);
setContextMenuPolicy(Qt::NoContextMenu);
// Interactive Simulation
if (mIsInteractiveSimulation) {
//start tool button
mpStartSimulationToolButton->setText(tr("Start"));
mpStartSimulationToolButton->setIcon(QIcon(":/Resources/icons/play_animation.svg"));
mpStartSimulationToolButton->setToolTip(tr("Start"));
mpStartSimulationToolButton->setAutoRaise(true);
// start tool button
mpPauseSimulationToolButton->setEnabled(false);
mpPauseSimulationToolButton->setText(tr("Pause"));
mpPauseSimulationToolButton->setIcon(QIcon(":/Resources/icons/pause.svg"));
mpPauseSimulationToolButton->setToolTip(tr("Pause"));
mpPauseSimulationToolButton->setAutoRaise(true);
/* Speed label and combo box */
mpSimulationSpeedLabel = new QLabel(tr("Speed:"));
QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
pDoubleValidator->setBottom(0.01);
pDoubleValidator->setTop(100);
mpSimulationSpeedComboBox->setEditable(true);
mpSimulationSpeedComboBox->addItems(QStringList() << "10" << "5" << "2" << "1" << "0.5" << "0.2" << "0.1");
mpSimulationSpeedComboBox->setCurrentIndex(3);
mpSimulationSpeedComboBox->setCompleter(0);
mpSimulationSpeedComboBox->setValidator(pDoubleValidator);
// add the interactive controls
toolBar->addWidget(mpStartSimulationToolButton);
toolBar->addSeparator();
toolBar->addWidget(mpPauseSimulationToolButton);
toolBar->addSeparator();
toolBar->addWidget(mpSimulationSpeedLabel);
toolBar->addWidget(mpSimulationSpeedComboBox);
toolBar->addSeparator();
}
// Auto scale
mpAutoScaleButton = new QToolButton(toolBar);
mpAutoScaleButton->setText(tr("Auto Scale"));
Expand Down Expand Up @@ -1267,11 +1310,68 @@ void PlotWindow::plotArrayParametric(double timePercent, PlotCurve *pPlotCurve)
}
}
}

QPair<QVector<double>*, QVector<double>*> PlotWindow::plotInteractive(PlotCurve *pPlotCurve)
{
if (mVariablesList.isEmpty() && getPlotType() == PlotWindow::PLOTINTERACTIVE) {
throw NoVariableException(QString(tr("No variables specified!")).toStdString().c_str());
} else if (mVariablesList.size() != 1) {
throw NoVariableException(QString(tr("Could not determine the variable name!")).toStdString().c_str());
}
QString variableName = mVariablesList.at(0);
pPlotCurve = new PlotCurve(mInteractiveModelName, variableName, "time", variableName, getUnit(), getDisplayUnit(), mpPlot);
// clear previous curve data
pPlotCurve->clearXAxisVector();
pPlotCurve->clearYAxisVector();
pPlotCurve->setSamples(mpInteractiveData);
mpPlot->addPlotCurve(pPlotCurve);
pPlotCurve->attach(mpPlot);
mpPlot->replot();
return pPlotCurve->getAxisVectors();
}

void PlotWindow::setInteractiveOwner(const QString &interactiveTreeItemOwner)
{
mInteractiveTreeItemOwner = interactiveTreeItemOwner;
}

void PlotWindow::setInteractivePort(const int port)
{
mInteractivePort = port;
}

void PlotWindow::setInteractivePlotData(QwtSeriesData<QPointF>* pInteractiveData)
{
mpInteractiveData = pInteractiveData;
}

void PlotWindow::setInteractiveModelName(const QString &modelName)
{
mInteractiveModelName = modelName;
}

void PlotWindow::setTitle(QString title)
{
mpPlot->setTitle(title);
}

void PlotWindow::updateCurves()
{
for (auto & p : mpPlot->getPlotCurvesList()) {
// append the last point to the plotting curve
p->getPlotDirectPainter()->drawSeries(p, p->getSize() - 2, -1);
}
}

void PlotWindow::updateYAxis(QPair<double, double> minMaxValues)
{
// replot if a value is out of bounds
if (minMaxValues.first < mpPlot->axisScaleDiv(QwtPlot::yLeft).lowerBound() || minMaxValues.second > mpPlot->axisScaleDiv(QwtPlot::yLeft).upperBound()) {
mpPlot->replot();
}
}


void PlotWindow::setGrid(QString grid)
{
if (grid.toLower().compare("simple") == 0)
Expand Down
26 changes: 24 additions & 2 deletions OMPlot/OMPlotGUI/PlotWindow.h
Expand Up @@ -60,7 +60,7 @@ class PlotWindow : public QMainWindow
{
Q_OBJECT
public:
enum PlotType {PLOT, PLOTALL, PLOTPARAMETRIC, PLOTARRAY, PLOTARRAYPARAMETRIC};
enum PlotType {PLOT, PLOTALL, PLOTPARAMETRIC, PLOTINTERACTIVE, PLOTARRAY, PLOTARRAYPARAMETRIC};
private:
Plot *mpPlot;
QCheckBox *mpLogXCheckBox;
Expand All @@ -70,6 +70,10 @@ class PlotWindow : public QMainWindow
QToolButton *mpNoGridButton;
QToolButton *mpAutoScaleButton;
QToolButton *mpSetupButton;
QToolButton *mpStartSimulationToolButton;
QToolButton *mpPauseSimulationToolButton;
QLabel *mpSimulationSpeedLabel;
QComboBox *mpSimulationSpeedComboBox;
QTextStream *mpTextStream;
QFile mFile;
QStringList mVariablesList;
Expand All @@ -85,8 +89,15 @@ class PlotWindow : public QMainWindow
double mCurveWidth;
int mCurveStyle;
double mTime;
bool mIsInteractiveSimulation;
QString mInteractiveTreeItemOwner;
int mInteractivePort;
QwtSeriesData<QPointF>* mpInteractiveData;
QString mInteractiveModelName;
QMdiSubWindow *mpSubWindow;
public:
PlotWindow(QStringList arguments = QStringList(), QWidget *parent = 0);
PlotWindow(QStringList arguments = QStringList(), QWidget *parent = 0, bool isInteractiveSimulation = false,
QToolButton *pStartSimulation = 0, QToolButton *pPauseSimulation = 0, QComboBox *pSimulationSpeed = 0);
~PlotWindow();

void setUpWidget();
Expand All @@ -101,6 +112,15 @@ class PlotWindow : public QMainWindow
void plotParametric(PlotCurve *pPlotCurve = 0);
void plotArray(double timePercent, PlotCurve *pPlotCurve = 0);
void plotArrayParametric(double timePercent, PlotCurve *pPlotCurve = 0);
QPair<QVector<double>*, QVector<double>*> plotInteractive(PlotCurve *pPlotCurve = 0);
void setInteractiveOwner(const QString &interactiveTreeItemOwner);
void setInteractivePort(const int port);
void setInteractivePlotData(QwtSeriesData<QPointF>* pInteractiveData);
void setSubWindow(QMdiSubWindow *pSubWindow) {mpSubWindow = pSubWindow;}
QMdiSubWindow* getSubWindow() {return mpSubWindow;}
void setInteractiveModelName(const QString &modelName);
QString getInteractiveOwner() {return mInteractiveTreeItemOwner;}
int getInteractivePort() {return mInteractivePort;}
void setTitle(QString title);
void setGrid(QString grid);
QString getGrid();
Expand Down Expand Up @@ -136,6 +156,8 @@ class PlotWindow : public QMainWindow
void setTime(double time){mTime = time;}
double getTime() {return mTime;}
void updateTimeText(QString unit);
void updateCurves();
void updateYAxis(QPair<double, double> minMaxValues);
signals:
void closingDown();
public slots:
Expand Down
73 changes: 73 additions & 0 deletions OMPlot/OMPlotGUI/Resources/icons/initialize.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions OMPlot/OMPlotGUI/Resources/icons/pause.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eb19ef4

Please sign in to comment.