Skip to content

Commit

Permalink
Undo/redo changed simulation time settings
Browse files Browse the repository at this point in the history
  • Loading branch information
robbr48 committed May 23, 2024
1 parent 2fa2c93 commit 39a2962
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
33 changes: 33 additions & 0 deletions HopsanGUI/UndoStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "GUIObjects/GUIWidgets.h"
#include "MessageHandler.h"
#include "Widgets/UndoWidget.h"
#include "Widgets/ModelWidget.h"


//! @class UndoStack
Expand Down Expand Up @@ -387,6 +388,13 @@ void UndoStack::undoOneStep()
return;
}
}
else if(stuffElement.attribute("what") == UNDO_SIMULATIONTIMECHANGED)
{
QString oldStartTime = stuffElement.attribute("oldStartTime");
QString oldTimeStep = stuffElement.attribute("oldTimeStep");
QString oldStopTime = stuffElement.attribute("oldStopTime");
mpParentSystemObject->mpModelWidget->setTopLevelSimulationTime(oldStartTime, oldTimeStep, oldStopTime, NoUndo);
}
else if(stuffElement.attribute("what") == UNDO_REMOVEDALIASES)
{
QDomElement xmlAlias = stuffElement.firstChildElement(hmf::alias);
Expand Down Expand Up @@ -731,6 +739,13 @@ void UndoStack::redoOneStep()
}
mpParentSystemObject->mWidgetMap.find(index).value()->setPos(x_new, y_new);
}
else if(stuffElement.attribute("what") == UNDO_SIMULATIONTIMECHANGED)
{
QString newStartTime = stuffElement.attribute("newStartTime");
QString newTimeStep = stuffElement.attribute("newTimeStep");
QString newStopTime = stuffElement.attribute("newStopTime");
mpParentSystemObject->mpModelWidget->setTopLevelSimulationTime(newStartTime, newTimeStep, newStopTime, NoUndo);
}
stuffElement = stuffElement.nextSiblingElement("stuff");
}

Expand Down Expand Up @@ -1142,6 +1157,24 @@ void UndoStack::registerModifiedWidget(Widget *pItem)
}
}

void UndoStack::registerSimulationTimeChanged(QString oldStartTime, QString oldTimeStep, QString oldStopTime, QString newStartTime, QString newTimeStep, QString newStopTime)
{
if(mEnabled) {
QDomElement currentPostElement = getCurrentPost();
QDomElement stuffElement = appendDomElement(currentPostElement, "stuff");
stuffElement.setAttribute("what", UNDO_SIMULATIONTIMECHANGED);

stuffElement.setAttribute("oldStartTime", oldStartTime);
stuffElement.setAttribute("oldTimeStep", oldTimeStep);
stuffElement.setAttribute("oldStopTime", oldStopTime);
stuffElement.setAttribute("newStartTime", newStartTime);
stuffElement.setAttribute("newTimeStep", newTimeStep);
stuffElement.setAttribute("newStopTime", newStopTime);

gpUndoWidget->refreshList();
}
}

void UndoStack::setEnabled(bool enabled)
{
mEnabled = enabled;
Expand Down
3 changes: 3 additions & 0 deletions HopsanGUI/UndoStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#define UNDO_DELETEDTEXTBOXWIDGET "deletedtextboxwidget"
#define UNDO_RESIZEDTEXTBOXWIDGET "resizedtextboxwidget"
#define UNDO_MODIFIEDTEXTBOXWIDGET "modifiedtextboxwidget"
#define UNDO_SIMULATIONTIMECHANGED "simulationtimechanged"

//Forward Declarations
class ModelObject;
Expand Down Expand Up @@ -133,6 +134,8 @@ friend class UndoWidget;
void registerResizedTextBoxWidget(const int index, const double w_old, const double h_old, const double w_new, const double h_new, const QPointF oldPos, const QPointF newPos);
void registerModifiedWidget(Widget *pItem);

void registerSimulationTimeChanged(QString oldStartTime, QString oldTimeStep, QString oldStopTime, QString startTime, QString timeStep, QString stopTime);

private:
SystemObject *mpParentSystemObject;
int mCurrentStackPosition;
Expand Down
10 changes: 8 additions & 2 deletions HopsanGUI/Widgets/ModelWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include "CoreAccess.h"
#include "GeneratorUtils.h"
#include "LibraryHandler.h"
#include "UndoStack.h"

//! @class ModelWidget
//! @brief The ModelWidget class is a Widget to contain a simulation model
Expand Down Expand Up @@ -190,8 +191,13 @@ void ModelWidget::setMessageHandler(GUIMessageHandler *pMessageHandler)
connect(this, SIGNAL(checkMessages()), mpMessageHandler, SLOT(collectHopsanCoreMessages()), Qt::UniqueConnection);
}

void ModelWidget::setTopLevelSimulationTime(const QString startTime, const QString timeStep, const QString stopTime)
void ModelWidget::setTopLevelSimulationTime(const QString startTime, const QString timeStep, const QString stopTime, UndoStatusEnumT undoSettings)
{
if(undoSettings == Undo) {
mpToplevelSystem->getUndoStackPtr()->newPost();
mpToplevelSystem->getUndoStackPtr()->registerSimulationTimeChanged(mStartTime, QString::number(mpToplevelSystem->getTimeStep()), mStopTime, startTime, timeStep, stopTime);
}

mStartTime = startTime;
mStopTime = stopTime;

Expand All @@ -208,7 +214,7 @@ void ModelWidget::setTopLevelSimulationTime(const QString startTime, const QStri
}
else
{
getTopLevelSystemContainer()->setTimeStep(timeStep.toDouble());
mpToplevelSystem->setTimeStep(timeStep.toDouble());
}

// Notify about any changes made (this will go back up to the main window simtime edit
Expand Down
2 changes: 1 addition & 1 deletion HopsanGUI/Widgets/ModelWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ModelWidget : public QWidget
AnimationWidget *mpAnimationWidget;

public slots:
void setTopLevelSimulationTime(const QString startTime, const QString timeStep, const QString stopTime);
void setTopLevelSimulationTime(const QString startTime, const QString timeStep, const QString stopTime, UndoStatusEnumT undoSettings=Undo);
bool simulate_nonblocking();
bool simulate_blocking();
bool startRealtimeSimulation(const double realtimeFactor);
Expand Down
1 change: 1 addition & 0 deletions HopsanGUI/Widgets/UndoWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ QString UndoWidget::translateTag(QString tag)
tagMap.insert(UNDO_DELETEDTEXTBOXWIDGET, "Deleted Text Box Widget");
tagMap.insert(UNDO_RESIZEDTEXTBOXWIDGET, "Resized Text Box Widget");
tagMap.insert(UNDO_MODIFIEDTEXTBOXWIDGET, "Modified Text Box Widget");
tagMap.insert(UNDO_SIMULATIONTIMECHANGED, "Simulation Time Settings Changed");

if(tagMap.contains(tag))
return tagMap.find(tag).value();
Expand Down

0 comments on commit 39a2962

Please sign in to comment.