Skip to content

Commit

Permalink
Visualize the array plots
Browse files Browse the repository at this point in the history
Fixes ticket:5611 avoid crashing when time is out of bounds.
Remove the DiagramWindow from PlotWindowContainer on closeEvent.
Enable/disable the visualization controls based on the active result file.
  • Loading branch information
adeas31 committed Aug 8, 2019
1 parent 88852a1 commit cb352b8
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 80 deletions.
7 changes: 1 addition & 6 deletions OMEdit/OMEdit/OMEditGUI/Animation/TimeManager.cpp
Expand Up @@ -59,12 +59,7 @@ void TimeManager::updateTick()

int TimeManager::getTimeFraction()
{
return getTimeFraction(mTimeDiscretization);
}

int TimeManager::getTimeFraction(int discretization)
{
return int(_visTime / (_endTime - _startTime)*discretization);
return int(_visTime / (_endTime - _startTime)*mTimeDiscretization);
}

double TimeManager::getEndTime() const
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEdit/OMEditGUI/Animation/TimeManager.h
Expand Up @@ -87,7 +87,6 @@ class TimeManager
/*! \brief Sets pause status to new value. */
void setPause(const bool status);
int getTimeFraction();
int getTimeFraction(int discretization);
void setSpeedUp(double value);
double getSpeedUp();
QTimer* getUpdateSceneTimer() {return mpUpdateSceneTimer;}
Expand Down
12 changes: 12 additions & 0 deletions OMEdit/OMEdit/OMEditGUI/Plotting/DiagramWindow.cpp
Expand Up @@ -34,6 +34,7 @@
#include "DiagramWindow.h"
#include "MainWindow.h"
#include "Modeling/ModelWidgetContainer.h"
#include "Plotting/PlotWindowContainer.h"
#include "Plotting/VariablesWidget.h"

/*!
Expand Down Expand Up @@ -135,3 +136,14 @@ void DiagramWindow::removeDiagram(ModelWidget *pModelWidget)
mpGraphicsView = 0;
}
}

/*!
* \brief DiagramWindow::closeEvent
* Removes DiagramWindow from PlotWindowContainer.
* \param event
*/
void DiagramWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
MainWindow::instance()->getPlotWindowContainer()->removeSubWindow(this);
}
5 changes: 2 additions & 3 deletions OMEdit/OMEdit/OMEditGUI/Plotting/DiagramWindow.h
Expand Up @@ -53,9 +53,8 @@ class DiagramWindow : public QWidget
GraphicsScene *mpGraphicsScene;
GraphicsView *mpGraphicsView;
QVBoxLayout *mpMainLayout;
signals:

public slots:
protected:
virtual void closeEvent(QCloseEvent *event);
};

#endif // DIAGRAMWINDOW_H
98 changes: 59 additions & 39 deletions OMEdit/OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp
Expand Up @@ -766,6 +766,11 @@ bool VariablesTreeModel::removeVariableTreeItem(QString variable)
{
VariablesTreeItem *pVariablesTreeItem = findVariablesTreeItem(variable, mpRootVariablesTreeItem);
if (pVariablesTreeItem) {
// if we are going to remove an active VariablesTreeItem then we should disable the visualization controls.
if (pVariablesTreeItem->isActive()) {
mpVariablesTreeView->getVariablesWidget()->enableVisualizationControls(false);
mpVariablesTreeView->getVariablesWidget()->rewindVisualization();
}
beginRemoveRows(variablesTreeItemIndex(pVariablesTreeItem), 0, pVariablesTreeItem->getChildren().size());
pVariablesTreeItem->removeChildren();
VariablesTreeItem *pParentVariablesTreeItem = pVariablesTreeItem->parent();
Expand Down Expand Up @@ -1072,7 +1077,10 @@ VariablesWidget::VariablesWidget(QWidget *pParent)
mpSimulationTimeComboBox->addItems(MainWindow::instance()->getOMCProxy()->getDerivedUnits("s"));
connect(mpSimulationTimeComboBox, SIGNAL(currentIndexChanged(QString)), SLOT(timeUnitChanged(QString)));
// simulation time slider
mSliderRange = 1000;
mpSimulationTimeSlider = new QSlider(Qt::Horizontal);
mpSimulationTimeSlider->setRange(0, mSliderRange);
mpSimulationTimeSlider->setSliderPosition(0);
connect(mpSimulationTimeSlider, SIGNAL(valueChanged(int)), SLOT(simulationTimeChanged(int)));
// toolbar
mpToolBar = new QToolBar;
Expand Down Expand Up @@ -1167,6 +1175,19 @@ VariablesWidget::VariablesWidget(QWidget *pParent)
connect(mpVariablesTreeModel, SIGNAL(variableTreeItemRemoved(QString)), MainWindow::instance()->getPlotWindowContainer(), SLOT(updatePlotWindows(QString)));
//connect(mpVariablesTreeModel, SIGNAL(clicked(QModelIndex)), this, SLOT(selectInteractivePlotWindow(QModelIndex)));
//connect(mpVariablesTreeModel, SIGNAL(itemChecked(QModelIndex)), SLOT(selectInteractivePlotWindow(QModelIndex)));
enableVisualizationControls(false);
}

/*!
* \brief VariablesWidget::enableVisualizationControls
* Enables/Disables the visualization controls.
* \param enable
*/
void VariablesWidget::enableVisualizationControls(bool enable)
{
mpSimulationTimeSlider->setEnabled(enable);
mpSimulationTimeSlider->setToolTip(enable ? "" : tr("Mark a result file active to enable the controls."));
mpToolBar->setEnabled(enable);
}

/*!
Expand Down Expand Up @@ -1265,6 +1286,13 @@ void VariablesWidget::variablesUpdated()

void VariablesWidget::updateVariablesTreeHelper(QMdiSubWindow *pSubWindow)
{
/* We always pause the visualization if we reach this function
* This function can be reached via following ways,
* When active PlotWindow is changed.
* When active PlotWindow variables are updated.
* When clearing the PlotWindow curves
*/
pauseVisualization();
if (!pSubWindow) {
return;
}
Expand Down Expand Up @@ -1485,10 +1513,8 @@ void VariablesWidget::initializeVisualization(SimulationOptions simulationOption
mpTimeManager->setPause(true);
// reset the visualization controls
mpTimeTextBox->setText("0.0");
mpSimulationTimeSlider->setRange(simulationOptions.getStartTime().toDouble(), simulationOptions.getNumberofIntervals());
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(0);
mpSimulationTimeSlider->blockSignals(state);
mpSimulationTimeSlider->setValue(mpSimulationTimeSlider->minimum());
enableVisualizationControls(true);
}

/*!
Expand Down Expand Up @@ -1957,30 +1983,34 @@ void VariablesWidget::unitChanged(const QModelIndex &index)
*/
void VariablesWidget::simulationTimeChanged(int timePercent)
{
float time = (mpTimeManager->getEndTime() - mpTimeManager->getStartTime()) * ((float)timePercent / (float)mpSimulationTimeSlider->maximum());
double time = (mpTimeManager->getEndTime() - mpTimeManager->getStartTime()) * ((double)timePercent / (double)mSliderRange);
mpTimeManager->setVisTime(time);
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));

PlotWindow *pPlotWindow = MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow();
if (pPlotWindow) {
PlotWindow::PlotType plotType = pPlotWindow->getPlotType();
if (plotType == PlotWindow::PLOTARRAY) {
QList<PlotCurve*> curves = pPlotWindow->getPlot()->getPlotCurvesList();
foreach (PlotCurve* curve, curves) {
QString varName = curve->getYVariable();
pPlotWindow->setVariablesList(QStringList(varName));
pPlotWindow->plotArray(timePercent, curve);
}
} else if (plotType == PlotWindow::PLOTARRAYPARAMETRIC) {
QList<PlotCurve*> curves = pPlotWindow->getPlot()->getPlotCurvesList();
foreach (PlotCurve* curve, curves) {
QString xVarName = curve->getXVariable();
QString yVarName = curve->getYVariable();
pPlotWindow->setVariablesList({xVarName,yVarName});
pPlotWindow->plotArrayParametric(timePercent, curve);
try {
PlotWindow::PlotType plotType = pPlotWindow->getPlotType();
if (plotType == PlotWindow::PLOTARRAY) {
QList<PlotCurve*> curves = pPlotWindow->getPlot()->getPlotCurvesList();
foreach (PlotCurve* curve, curves) {
QString varName = curve->getYVariable();
pPlotWindow->setVariablesList(QStringList(varName));
pPlotWindow->plotArray(time, curve);
}
} else if (plotType == PlotWindow::PLOTARRAYPARAMETRIC) {
QList<PlotCurve*> curves = pPlotWindow->getPlot()->getPlotCurvesList();
foreach (PlotCurve* curve, curves) {
QString xVarName = curve->getXVariable();
QString yVarName = curve->getYVariable();
pPlotWindow->setVariablesList({xVarName,yVarName});
pPlotWindow->plotArrayParametric(time, curve);
}
} else {
return;
}
} else {
return;
} catch (PlotException &e) {
MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, e.what(), Helper::scriptingKind, Helper::errorLevel));
}
} else { // if no plot window then try to update the DiagramWindow
updateVisualization();
Expand Down Expand Up @@ -2256,9 +2286,7 @@ void VariablesWidget::rewindVisualization()
mpTimeManager->setVisTime(mpTimeManager->getStartTime());
mpTimeManager->setRealTimeFactor(0.0);
mpTimeManager->setPause(true);
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(0);
mpSimulationTimeSlider->blockSignals(state);
mpSimulationTimeSlider->setValue(mpSimulationTimeSlider->minimum());
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
}

Expand Down Expand Up @@ -2298,10 +2326,8 @@ void VariablesWidget::visulizationTimeChanged()
value = end;
}
mpTimeManager->setVisTime(value);
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction(mpSimulationTimeSlider->maximum()));
mpSimulationTimeSlider->blockSignals(state);
updateVisualization();
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction());

}
}

Expand All @@ -2325,20 +2351,14 @@ void VariablesWidget::visualizationSpeedChanged()
*/
void VariablesWidget::incrementVisualization()
{
if (!mpTimeManager->isPaused()) {
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
// set time slider
int time = mpTimeManager->getTimeFraction(mpSimulationTimeSlider->maximum());
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(time);
mpSimulationTimeSlider->blockSignals(state);
}

//measure realtime
mpTimeManager->updateTick();
//update scene and set next time step
if (!mpTimeManager->isPaused()) {
updateVisualization();
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
// set time slider
int time = mpTimeManager->getTimeFraction();
mpSimulationTimeSlider->setValue(time);
//finish animation with pause when endtime is reached
if (mpTimeManager->getVisTime() >= mpTimeManager->getEndTime()) {
pauseVisualization();
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEdit/OMEditGUI/Plotting/VariablesWidget.h
Expand Up @@ -187,6 +187,7 @@ class VariablesWidget : public QWidget
VariableTreeProxyModel* getVariableTreeProxyModel() {return mpVariableTreeProxyModel;}
VariablesTreeModel* getVariablesTreeModel() {return mpVariablesTreeModel;}
VariablesTreeView* getVariablesTreeView() {return mpVariablesTreeView;}
void enableVisualizationControls(bool enable);
void insertVariablesItemsToTree(QString fileName, QString filePath, QStringList variablesList, SimulationOptions simulationOptions);
void addSelectedInteractiveVariables(const QString &modelName, const QList<QString> &selectedVariables);
void variablesUpdated();
Expand All @@ -204,6 +205,7 @@ class VariablesWidget : public QWidget
Label *mpSimulationTimeLabel;
QComboBox *mpSimulationTimeComboBox;
QSlider *mpSimulationTimeSlider;
int mSliderRange;
QToolBar *mpToolBar;
QAction *mpRewindAction;
QAction *mpPlayAction;
Expand Down
33 changes: 4 additions & 29 deletions OMPlot/OMPlot/OMPlotGUI/PlotWindow.cpp
Expand Up @@ -906,11 +906,11 @@ void PlotWindow::updateTimeText(QString unit)
mpPlot->replot();
}

void PlotWindow::plotArray(double timePercent, PlotCurve *pPlotCurve)
void PlotWindow::plotArray(double time, PlotCurve *pPlotCurve)
{
double *res;
QString currentLine;
double time;
setTime(time);
double timeUnitFactor = getTimeUnitFactor(getTimeUnit());
if (mVariablesList.isEmpty() and getPlotType() == PlotWindow::PLOTARRAY)
throw NoVariableException(QString("No variables specified!").toStdString().c_str());
Expand Down Expand Up @@ -942,11 +942,6 @@ void PlotWindow::plotArray(double timePercent, PlotCurve *pPlotCurve)
//Read in timevector
double timeVals[intervalSize];
readPLTDataset(mpTextStream, "time", intervalSize, timeVals);
//calculate time
double startTime = timeVals[0];
double stopTime = timeVals[intervalSize-1];
time = startTime + (stopTime - startTime)*timePercent/100.0;
setTime(time);
//Find indexes and alpha to interpolate data in particular time
double alpha;
int it = setupInterp(timeVals, time, intervalSize, alpha);
Expand Down Expand Up @@ -996,11 +991,6 @@ void PlotWindow::plotArray(double timePercent, PlotCurve *pPlotCurve)
omc_free_csv_reader(csvReader);
throw NoVariableException(tr("Variable doesnt exist: %1").arg("time").toStdString().c_str());
}
//calculate time
double startTime = timeVals[0];
double stopTime = timeVals[csvReader->numsteps-1];
time = startTime + (stopTime - startTime)*timePercent/100.0;
setTime(time);
double alpha;
int it = setupInterp(timeVals, time, csvReader->numsteps, alpha);
if (it < 0) {
Expand Down Expand Up @@ -1062,8 +1052,6 @@ void PlotWindow::plotArray(double timePercent, PlotCurve *pPlotCurve)
//calculate time
double startTime = omc_matlab4_startTime(&reader);
double stopTime = omc_matlab4_stopTime(&reader);
time = startTime + (stopTime - startTime)*timePercent/100.0;
setTime(time);
if (reader.nvar < 1) {
omc_free_matlab4_reader(&reader);
throw NoVariableException("Variable doesnt exist: time");
Expand Down Expand Up @@ -1114,11 +1102,11 @@ void PlotWindow::plotArray(double timePercent, PlotCurve *pPlotCurve)
}
}

void PlotWindow::plotArrayParametric(double timePercent, PlotCurve *pPlotCurve)
void PlotWindow::plotArrayParametric(double time, PlotCurve *pPlotCurve)
{
QString xVariable, yVariable, xTitle, yTitle;
int pair = 0;
double time;
setTime(time);
double timeUnitFactor = getTimeUnitFactor(getTimeUnit());
if (mVariablesList.isEmpty())
throw NoVariableException(QString("No variables specified!").toStdString().c_str());
Expand All @@ -1129,7 +1117,6 @@ void PlotWindow::plotArrayParametric(double timePercent, PlotCurve *pPlotCurve)

for (pair = 0; pair < mVariablesList.size(); pair += 2)
{
QStringList varPair;
xVariable = mVariablesList.at(pair);
yVariable = mVariablesList.at(pair+1);
// if (!editCase)
Expand Down Expand Up @@ -1175,11 +1162,6 @@ void PlotWindow::plotArrayParametric(double timePercent, PlotCurve *pPlotCurve)
//Read in timevector
double timeVals[intervalSize];
readPLTDataset(mpTextStream, "time", intervalSize, timeVals);
//calculate time
double startTime = timeVals[0];
double stopTime = timeVals[intervalSize-1];
time = startTime + (stopTime - startTime)*timePercent/100.0;
setTime(time);
//Find indexes and alpha to interpolate data in particular time
double alpha;
int it = setupInterp(timeVals, time, intervalSize, alpha);
Expand Down Expand Up @@ -1230,11 +1212,6 @@ void PlotWindow::plotArrayParametric(double timePercent, PlotCurve *pPlotCurve)
omc_free_csv_reader(csvReader);
throw NoVariableException(tr("Variable doesnt exist: %1").arg("time").toStdString().c_str());
}
//calculate time
double startTime = timeVals[0];
double stopTime = timeVals[csvReader->numsteps-1];
time = startTime + (stopTime - startTime)*timePercent/100.0;
setTime(time);
double alpha;
int it = setupInterp(timeVals, time, csvReader->numsteps, alpha);
if (it < 0) {
Expand Down Expand Up @@ -1311,8 +1288,6 @@ void PlotWindow::plotArrayParametric(double timePercent, PlotCurve *pPlotCurve)
//calculate time
double startTime = omc_matlab4_startTime(&reader);
double stopTime = omc_matlab4_stopTime(&reader);
time = startTime + (stopTime - startTime)*timePercent/100.0;
setTime(time);
if (reader.nvar < 1) {
omc_free_matlab4_reader(&reader);
throw NoVariableException("Variable doesnt exist: time");
Expand Down
4 changes: 2 additions & 2 deletions OMPlot/OMPlot/OMPlotGUI/PlotWindow.h
Expand Up @@ -110,8 +110,8 @@ class PlotWindow : public QMainWindow
void setupToolbar();
void plot(PlotCurve *pPlotCurve = 0);
void plotParametric(PlotCurve *pPlotCurve = 0);
void plotArray(double timePercent, PlotCurve *pPlotCurve = 0);
void plotArrayParametric(double timePercent, PlotCurve *pPlotCurve = 0);
void plotArray(double time, PlotCurve *pPlotCurve = 0);
void plotArrayParametric(double time, PlotCurve *pPlotCurve = 0);
QPair<QVector<double>*, QVector<double>*> plotInteractive(PlotCurve *pPlotCurve = 0);
void setInteractiveOwner(const QString &interactiveTreeItemOwner);
void setInteractivePort(const int port);
Expand Down

0 comments on commit cb352b8

Please sign in to comment.