Skip to content

Commit

Permalink
Fix time-related plots and diagram visualization issues (#11069)
Browse files Browse the repository at this point in the history
* Clean up time manager & browser time slot functions

* Update diagram visualization once it is shown

* Update diagram visualization even if plot shown

* Update all plots and not only current plot window

* Separate update of plots and diagram visualization

* Init time slider and time label using time manager

* Avoid duplicate computations when time changed

* Fix visualization time within start/end range

* Check time value within start/end bounds

* Set final value to time slider

* Set final value to time label

* Set final value to speed box

* Extract common time update

* Fix browser update by timer

* Standardize action tips
  • Loading branch information
anotheruserofgithub committed Aug 23, 2023
1 parent 2ecebf0 commit f0e7add
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 84 deletions.
1 change: 1 addition & 0 deletions OMEdit/OMEditLIB/Plotting/DiagramWindow.cpp
Expand Up @@ -68,6 +68,7 @@ void DiagramWindow::showVisualizationDiagram(ModelWidget *pModelWidget)
mpModelWidget = pModelWidget;
mpModelWidget->getDiagramGraphicsView()->setIsVisualizationView(true);
connect(MainWindow::instance()->getVariablesWidget(), SIGNAL(updateDynamicSelect(double)), mpModelWidget->getDiagramGraphicsView(), SIGNAL(updateDynamicSelect(double)));
MainWindow::instance()->getVariablesWidget()->updateVisualization();
mpModelWidget->getDiagramGraphicsView()->show();
mpMainLayout->addWidget(mpModelWidget->getDiagramGraphicsView());
} else {
Expand Down
175 changes: 112 additions & 63 deletions OMEdit/OMEditLIB/Plotting/VariablesWidget.cpp
Expand Up @@ -1442,7 +1442,7 @@ VariablesWidget::VariablesWidget(QWidget *pParent)
mpToolBar->setIconSize(QSize(toolbarIconSize, toolbarIconSize));
// rewind action
mpRewindAction = new QAction(QIcon(":/Resources/icons/initialize.svg"), tr("Rewind"), this);
mpRewindAction->setStatusTip(tr("Rewinds the visualization to the start"));
mpRewindAction->setStatusTip(tr("Rewind the visualization to the start"));
connect(mpRewindAction, SIGNAL(triggered()), SLOT(rewindVisualization()));
// play action
mpPlayAction = new QAction(QIcon(":/Resources/icons/play_animation.svg"), Helper::animationPlay, this);
Expand All @@ -1460,7 +1460,7 @@ VariablesWidget::VariablesWidget(QWidget *pParent)
mpTimeTextBox = new QLineEdit("0.0");
mpTimeTextBox->setMaximumHeight(toolbarIconSize);
mpTimeTextBox->setValidator(pDoubleValidator);
connect(mpTimeTextBox, SIGNAL(returnPressed()), SLOT(visulizationTimeChanged()));
connect(mpTimeTextBox, SIGNAL(returnPressed()), SLOT(visualizationTimeChanged()));
// speed
mpSpeedLabel = new Label;
mpSpeedLabel->setText(Helper::speed);
Expand Down Expand Up @@ -1864,8 +1864,8 @@ void VariablesWidget::initializeVisualization()
mpTimeManager->setVisTime(mpTimeManager->getStartTime());
mpTimeManager->setPause(true);
// reset the visualization controls
mpTimeTextBox->setText("0.0");
mpSimulationTimeSlider->setValue(mpSimulationTimeSlider->minimum());
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction());
enableVisualizationControls(true);
}

Expand Down Expand Up @@ -2373,43 +2373,86 @@ void VariablesWidget::unitChanged(const QModelIndex &index)
/*!
* \brief VariablesWidget::simulationTimeChanged
* SLOT activated when mpSimulationTimeSlider valueChanged SIGNAL is raised.
* \param time
* \param value
*/
void VariablesWidget::simulationTimeChanged(int value)
{
if (value >= 0) {
double start = mpTimeManager->getStartTime();
double end = mpTimeManager->getEndTime();
double time = (end - start) * (value / (double)mSliderRange) + start;
updateBrowserTime(time);
} else {
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction());
mpSimulationTimeSlider->blockSignals(state);
}
}

/*!
* \brief VariablesWidget::updateBrowserTime
* Updates the browser to the provided point of time
* \param time The new point of time
*/
void VariablesWidget::simulationTimeChanged(int timePercent)
void VariablesWidget::updateBrowserTime(double time)
{
double time = (mpTimeManager->getEndTime() - mpTimeManager->getStartTime()) * ((double)timePercent / (double)mSliderRange);
double start = mpTimeManager->getStartTime();
double end = mpTimeManager->getEndTime();
if (time < start) {
time = start;
} else if (time > end) {
time = end;
}
mpTimeManager->setVisTime(time);
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction());
mpSimulationTimeSlider->blockSignals(state);
updateVisualization();
updatePlotWindows();
}

PlotWindow *pPlotWindow = MainWindow::instance()->getPlotWindowContainer()->getCurrentWindow();
if (pPlotWindow) {
/*!
* \brief VariablesWidget::updatePlotWindows
* Updates the plot windows.
*/
void VariablesWidget::updatePlotWindows()
{
double time = mpTimeManager->getVisTime();
foreach (QMdiSubWindow *pSubWindow, MainWindow::instance()->getPlotWindowContainer()->subWindowList(QMdiArea::StackingOrder)) {
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);
if (MainWindow::instance()->getPlotWindowContainer()->isPlotWindow(pSubWindow->widget())) {
PlotWindow *pPlotWindow = qobject_cast<PlotWindow*>(pSubWindow->widget());
PlotWindow::PlotType plotType = pPlotWindow->getPlotType();
if (plotType == PlotWindow::PLOTARRAY || plotType == PlotWindow::PLOTARRAYPARAMETRIC) {
QList<PlotCurve*> curves = pPlotWindow->getPlot()->getPlotCurvesList();
if (curves.isEmpty()) {
if (!pPlotWindow->getFooter().isEmpty()) {
pPlotWindow->setTime(time);
pPlotWindow->updateTimeText();
}
} else if (plotType == PlotWindow::PLOTARRAY) {
foreach (PlotCurve* curve, curves) {
QString varName = curve->getYVariable();
pPlotWindow->setVariablesList(QStringList(varName));
pPlotWindow->plotArray(time, curve);
}
} else {
foreach (PlotCurve* curve, curves) {
QString xVarName = curve->getXVariable();
QString yVarName = curve->getYVariable();
pPlotWindow->setVariablesList({xVarName, yVarName});
pPlotWindow->plotArrayParametric(time, curve);
}
}
}
} 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();
}
}

/*!
* \brief VariablesWidget::valueEntered
* Handles the case when a new value is entered in VariablesTreeView.\n
Expand Down Expand Up @@ -2651,7 +2694,7 @@ void VariablesWidget::timeUnitChanged(QString unit)
if (pPlotWindow->getPlotType() == PlotWindow::PLOTARRAY ||
pPlotWindow->getPlotType() == PlotWindow::PLOTARRAYPARAMETRIC) {
pPlotWindow->setTimeUnit(unit);
pPlotWindow->updateTimeText(unit);
pPlotWindow->updateTimeText();
} else if (pPlotWindow->getPlotType() == PlotWindow::PLOT ||
pPlotWindow->getPlotType() == PlotWindow::PLOTINTERACTIVE) {
OMCInterface::convertUnits_res convertUnit = MainWindow::instance()->getOMCProxy()->convertUnits(pPlotWindow->getTimeUnit(), unit);
Expand Down Expand Up @@ -2806,10 +2849,14 @@ void VariablesWidget::showReSimulateSetup()
*/
void VariablesWidget::rewindVisualization()
{
mpTimeManager->setVisTime(mpTimeManager->getStartTime());
mpTimeManager->setRealTimeFactor(0.0);
mpTimeManager->setPause(true);
mpSimulationTimeSlider->setValue(mpSimulationTimeSlider->minimum());
mpTimeManager->setRealTimeFactor(0.0);
mpTimeManager->setVisTime(mpTimeManager->getStartTime());
updateVisualization();
updatePlotWindows();
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction());
mpSimulationTimeSlider->blockSignals(state);
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
}

Expand All @@ -2832,39 +2879,33 @@ void VariablesWidget::pauseVisualization()
}

/*!
* \brief VariablesWidget::visulizationTimeChanged
* \brief VariablesWidget::visualizationTimeChanged
* Slot activated when mpTimeTextBox returnPressed SIGNAL is raised.
*/
void VariablesWidget::visulizationTimeChanged()
void VariablesWidget::visualizationTimeChanged()
{
QString time = mpTimeTextBox->text();
bool isDouble = true;
double start = mpTimeManager->getStartTime();
double end = mpTimeManager->getEndTime();
double value = time.toDouble(&isDouble);
if (isDouble && value >= 0.0) {
if (value < start) {
value = start;
} else if (value > end) {
value = end;
}
mpTimeManager->setVisTime(value);
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction());

bool isDouble = false;
double time = mpTimeTextBox->text().toDouble(&isDouble);
if (isDouble && time >= 0.0) {
updateBrowserTime(time);
} else {
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
}
}

/*!
* \brief VariablesWidget::visualizationSpeedChanged
* Slot activated when mpSpeedComboBox currentIndexChanged SIGNAL is raised.
* Slot activated when mpSpeedComboBox currentIndexChanged SIGNAL is raised,
* as well as when mpSpeedComboBox->lineEdit() textChanged SIGNAL is raised.
*/
void VariablesWidget::visualizationSpeedChanged()
{
QString speed = mpSpeedComboBox->lineEdit()->text();
bool isDouble = true;
double value = speed.toDouble(&isDouble);
if (isDouble && value > 0.0) {
mpTimeManager->setSpeedUp(value);
bool isDouble = false;
double speed = mpSpeedComboBox->lineEdit()->text().toDouble(&isDouble);
if (isDouble && speed > 0.0) {
mpTimeManager->setSpeedUp(speed);
} else {
mpSpeedComboBox->lineEdit()->setText(QString::number(mpTimeManager->getSpeedUp()));
}
}

Expand All @@ -2874,24 +2915,32 @@ void VariablesWidget::visualizationSpeedChanged()
*/
void VariablesWidget::incrementVisualization()
{
//measure realtime
// measure real time
mpTimeManager->updateTick();
//update scene and set next time step
// set next time step
if (!mpTimeManager->isPaused()) {
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
// set time slider
int time = mpTimeManager->getTimeFraction();
mpSimulationTimeSlider->setValue(time);
//finish animation with pause when endtime is reached
// finish animation with pause when end time is reached
if (mpTimeManager->getVisTime() >= mpTimeManager->getEndTime()) {
pauseVisualization();
} else { // get the new visualization time
double newTime = mpTimeManager->getVisTime() + (mpTimeManager->getHVisual()*mpTimeManager->getSpeedUp());
} else {
// set new visualization time
double newTime = mpTimeManager->getVisTime() + (mpTimeManager->getHVisual() * mpTimeManager->getSpeedUp());
if (newTime <= mpTimeManager->getEndTime()) {
mpTimeManager->setVisTime(newTime);
} else {
mpTimeManager->setVisTime(mpTimeManager->getEndTime());
}
}
// update browser
updateVisualization();
updatePlotWindows();
if (!mpTimeManager->isPaused()) {
// set time label
mpTimeTextBox->setText(QString::number(mpTimeManager->getVisTime()));
// set time slider
bool state = mpSimulationTimeSlider->blockSignals(true);
mpSimulationTimeSlider->setValue(mpTimeManager->getTimeFraction());
mpSimulationTimeSlider->blockSignals(state);
}
}
}
8 changes: 5 additions & 3 deletions OMEdit/OMEditLIB/Plotting/VariablesWidget.h
Expand Up @@ -235,6 +235,9 @@ class VariablesWidget : public QWidget
void interactiveReSimulation(QString modelName);
void updateInitXmlFile(SimulationOptions simulationOptions);
void initializeVisualization();
void updateVisualization();
void updatePlotWindows();
void updateBrowserTime(double time);
double readVariableValue(QString variable, double time);
void closeResultFile();
private:
Expand Down Expand Up @@ -263,14 +266,13 @@ class VariablesWidget : public QWidget
QFile mPlotFileReader;
void selectInteractivePlotWindow(VariablesTreeItem *pVariablesTreeItem);
void openResultFile(double &startTime, double &stopTime);
void updateVisualization();
void checkVariable(const QModelIndex &index, bool checkState);
void unCheckVariableAndErrorMessage(const QModelIndex &index, const QString &errorMessage);
void unCheckCurveVariable(const QString &variable);
public slots:
void plotVariables(const QModelIndex &index, qreal curveThickness, int curveStyle, bool shiftKey, OMPlot::PlotCurve *pPlotCurve = 0, OMPlot::PlotWindow *pPlotWindow = 0);
void unitChanged(const QModelIndex &index);
void simulationTimeChanged(int timePercent);
void simulationTimeChanged(int value);
void valueEntered(const QModelIndex &index);
void timeUnitChanged(QString unit);
void updateVariablesTree(QMdiSubWindow *pSubWindow);
Expand All @@ -282,7 +284,7 @@ public slots:
private slots:
void playVisualization();
void pauseVisualization();
void visulizationTimeChanged();
void visualizationTimeChanged();
void visualizationSpeedChanged();
void incrementVisualization();
signals:
Expand Down

0 comments on commit f0e7add

Please sign in to comment.