Ignore:
Timestamp:
2020-06-12T14:35:32+02:00 (4 years ago)
Author:
Adeel Asghar <adeel.asghar@…>
Parents:
327c277
git-author:
Adeel Asghar <adeel.asghar@…> (05/29/20 14:56:19)
git-committer:
Adeel Asghar <adeel.asghar@…> (06/12/20 14:35:32)
Message:

Snapshot based redo undo functionality for OMSimulator models

Create new OMSimulator models by right clicking in the Libraries Browser
Improved the addsubmodel dialog. Let user first select the file and then propose name from filename.
Fixes ticket:5789
Fixes ticket:5984
Disable the text view if its not allowed to edit.
Tabs should be preserved when accepting text changes.
The open nodes of the tree view in the libraries browser should be preserved when accepting text changes.

Handle deletion of multiple components.
Convert dots to underscores in submodel name.
Set background-color to show the read-only ssp editor as disbaled.
Renamed OMSimulator menu item to SSP.
Use the same GUI buttons for simulation.
Separated instantiation from simulation setup.
Disable the changes once the model is in instantiated state.
Get rid of OMSSimulationOptions.h InstantiateDialog.h/.cpp
Accept text changes when user clicks in the libraries browser.
Open directly the file browser when adding a submodel.
Use just one setting for working directory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • OMEdit/OMEditLIB/OMS/OMSSimulationDialog.cpp

    rcc6e3e8 r7094b7c  
    3535#include "MainWindow.h"
    3636#include "Util/Helper.h"
     37#include "SystemSimulationInformationDialog.h"
    3738#include "Modeling/LibraryTreeWidget.h"
    3839#include "Modeling/ItemDelegate.h"
    3940#include "OMSSimulationOutputWidget.h"
    40 #include "Options/OptionsDialog.h"
     41#include "Modeling/ModelWidgetContainer.h"
    4142#include "Plotting/VariablesWidget.h"
    4243
     
    5657  : QDialog(pParent)
    5758{
    58   setWindowTitle(QString("%1 - %2").arg(Helper::applicationName, Helper::archivedSimulations));
    5959  // heading
    60   mpSimulationHeading = Utilities::getHeadingLabel(Helper::archivedSimulations);
     60  mpSimulationHeading = Utilities::getHeadingLabel(QString("%1 - %2").arg(Helper::simulationSetup, mModelCref));
    6161  mpSimulationHeading->setElideMode(Qt::ElideMiddle);
    6262  // Horizontal separator
    6363  mpHorizontalLine = Utilities::getHeadingLine();
     64  // tab widget
     65  QTabWidget *pTabWidget = new QTabWidget;
     66  // General tab
     67  QWidget *pGeneralWidget = new QWidget;
     68  // system simulation information groupbox
     69  mpSystemSimulationInformationWidget = 0;
     70  mpSystemSimulationInformationGroupBox = new QGroupBox(Helper::systemSimulationInformation);
     71  // start time
     72  mpStartTimeLabel = new Label(QString("%1:").arg(Helper::startTime));
     73  mpStartTimeTextBox = new QLineEdit;
     74  // stop time
     75  mpStopTimeLabel = new Label(QString("%1:").arg(Helper::stopTime));
     76  mpStopTimeTextBox = new QLineEdit;
     77  // result file
     78  mpResultFileLabel = new Label(tr("Result File:"));
     79  mpResultFileTextBox = new QLineEdit;
     80  // result file buffer size
     81  mpResultFileBufferSizeLabel = new Label(tr("Result File Buffer Size:"));
     82  mpResultFileBufferSizeSpinBox = new QSpinBox;
     83  mpResultFileBufferSizeSpinBox->setRange(1, INT_MAX);
     84  // logging interval
     85  mpLoggingIntervalLabel = new Label(tr("Logging Interval:"));
     86  mpLoggingIntervalTextBox = new QLineEdit("0");
     87  // signal filter
     88  mpSignalFilterLabel = new Label(tr("Signal Filter:"));
     89  mpSignalFilterTextBox = new QLineEdit;
     90  mpSignalFilterTextBox->setToolTip(tr("Leave empty to include all signals otherwise use a regex to filter."));
     91  // Add the validators
     92  QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);
     93  mpStartTimeTextBox->setValidator(pDoubleValidator);
     94  mpStopTimeTextBox->setValidator(pDoubleValidator);
     95  // General tab widget layout
     96  QGridLayout *pGeneralTabWidgetGridLayout = new QGridLayout;
     97  pGeneralTabWidgetGridLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
     98  pGeneralTabWidgetGridLayout->addWidget(mpSystemSimulationInformationGroupBox, 1, 0, 1, 2);
     99  pGeneralTabWidgetGridLayout->addWidget(mpStartTimeLabel, 2, 0);
     100  pGeneralTabWidgetGridLayout->addWidget(mpStartTimeTextBox, 2, 1);
     101  pGeneralTabWidgetGridLayout->addWidget(mpStopTimeLabel, 3, 0);
     102  pGeneralTabWidgetGridLayout->addWidget(mpStopTimeTextBox, 3, 1);
     103  pGeneralTabWidgetGridLayout->addWidget(mpResultFileLabel, 4, 0);
     104  pGeneralTabWidgetGridLayout->addWidget(mpResultFileTextBox, 4, 1);
     105  pGeneralTabWidgetGridLayout->addWidget(mpResultFileBufferSizeLabel, 5, 0);
     106  pGeneralTabWidgetGridLayout->addWidget(mpResultFileBufferSizeSpinBox, 5, 1);
     107  pGeneralTabWidgetGridLayout->addWidget(mpLoggingIntervalLabel, 6, 0);
     108  pGeneralTabWidgetGridLayout->addWidget(mpLoggingIntervalTextBox, 6, 1);
     109  pGeneralTabWidgetGridLayout->addWidget(mpSignalFilterLabel, 7, 0);
     110  pGeneralTabWidgetGridLayout->addWidget(mpSignalFilterTextBox, 7, 1);
     111  pGeneralWidget->setLayout(pGeneralTabWidgetGridLayout);
     112  pTabWidget->addTab(pGeneralWidget, Helper::general);
     113  // Archived simulation tab layout
     114  QWidget *pArchivedSimulationsTab = new QWidget;
    64115  // archived simulation tree widget
    65116  mpArchivedSimulationsTreeWidget = new QTreeWidget;
     
    72123  mpArchivedSimulationsTreeWidget->setIndentation(0);
    73124  connect(mpArchivedSimulationsTreeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), SLOT(showArchivedSimulation(QTreeWidgetItem*)));
     125  QGridLayout *pArchivedSimulationsTabGridLayout = new QGridLayout;
     126  pArchivedSimulationsTabGridLayout->setAlignment(Qt::AlignTop);
     127  pArchivedSimulationsTabGridLayout->addWidget(mpArchivedSimulationsTreeWidget, 0, 0);
     128  pArchivedSimulationsTab->setLayout(pArchivedSimulationsTabGridLayout);
     129  // add Archived simulations Tab to Simulation TabWidget
     130  pTabWidget->addTab(pArchivedSimulationsTab, Helper::archivedSimulations);
    74131  // Create the buttons
    75132  mpOkButton = new QPushButton(Helper::ok);
    76133  mpOkButton->setAutoDefault(true);
    77   connect(mpOkButton, SIGNAL(clicked()), this, SLOT(accept()));
     134  connect(mpOkButton, SIGNAL(clicked()), this, SLOT(saveSimulationSettings()));
    78135  mpCancelButton = new QPushButton(Helper::cancel);
    79136  mpCancelButton->setAutoDefault(false);
     
    84141  mpButtonBox->addButton(mpCancelButton, QDialogButtonBox::ActionRole);
    85142  // Create a layout
    86   QGridLayout *pMainLayout = new QGridLayout;
    87   pMainLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    88   pMainLayout->addWidget(mpSimulationHeading, 0, 0);
    89   pMainLayout->addWidget(mpHorizontalLine, 1, 0);
    90   pMainLayout->addWidget(mpArchivedSimulationsTreeWidget, 2, 0);
    91   pMainLayout->addWidget(mpButtonBox, 3, 0);
    92   setLayout(pMainLayout);
     143  QGridLayout *pMainGridLayout = new QGridLayout;
     144  pMainGridLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
     145  pMainGridLayout->addWidget(mpSimulationHeading, 0, 0);
     146  pMainGridLayout->addWidget(mpHorizontalLine, 1, 0);
     147  pMainGridLayout->addWidget(pTabWidget, 2, 0);
     148  pMainGridLayout->addWidget(mpButtonBox, 3, 0);
     149  setLayout(pMainGridLayout);
    93150}
    94151
     
    103160  }
    104161  mOMSSimulationOutputWidgetsList.clear();
     162}
     163
     164int OMSSimulationDialog::exec(const QString &modelCref, LibraryTreeItem *pLibraryTreeItem)
     165{
     166  mModelCref = modelCref;
     167  mpLibraryTreeItem = pLibraryTreeItem;
     168
     169  setWindowTitle(QString("%1 - %2 - %3").arg(Helper::applicationName, Helper::simulationSetup, mModelCref));
     170  mpSimulationHeading->setText(QString("%1 - %2").arg(Helper::simulationSetup, mModelCref));
     171  // initialize system simulation information
     172  if (mpSystemSimulationInformationWidget) {
     173    delete mpSystemSimulationInformationGroupBox->layout();
     174    delete mpSystemSimulationInformationWidget;
     175    mpSystemSimulationInformationWidget = 0;
     176  }
     177  LibraryTreeItem *pTopLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->getTopLevelLibraryTreeItem(mpLibraryTreeItem);
     178  LibraryTreeItem *pRootSystemLibraryTreeItem = 0;
     179  if (pTopLibraryTreeItem && pTopLibraryTreeItem->childrenSize() > 0) {
     180    pRootSystemLibraryTreeItem = pTopLibraryTreeItem->childAt(0);
     181    if (pRootSystemLibraryTreeItem) {
     182      if (!pRootSystemLibraryTreeItem->getModelWidget()) {
     183        MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->showModelWidget(pRootSystemLibraryTreeItem, false);
     184      }
     185      mpSystemSimulationInformationWidget = new SystemSimulationInformationWidget(pRootSystemLibraryTreeItem->getModelWidget());
     186      QHBoxLayout *pSystemSimulationInformationGroupBoxLayout = new QHBoxLayout;
     187      pSystemSimulationInformationGroupBoxLayout->addWidget(mpSystemSimulationInformationWidget);
     188      mpSystemSimulationInformationGroupBox->setLayout(pSystemSimulationInformationGroupBoxLayout);
     189    }
     190  }
     191  // start time
     192  double startTime;
     193  OMSProxy::instance()->getStartTime(mModelCref, &startTime);
     194  mpStartTimeTextBox->setText(QString::number(startTime));
     195  // stop time
     196  double stopTime;
     197  OMSProxy::instance()->getStopTime(mModelCref, &stopTime);
     198  mpStopTimeTextBox->setText(QString::number(stopTime));
     199  // result file
     200  char *fileName = (char*)"";
     201  int bufferSize;
     202  OMSProxy::instance()->getResultFile(mModelCref, &fileName, &bufferSize);
     203  mpResultFileTextBox->setText(QString(fileName));
     204  // result file buffer size
     205  mpResultFileBufferSizeSpinBox->setValue(bufferSize);
     206
     207  mpOkButton->setEnabled(!mpLibraryTreeItem->isSystemLibrary());
     208
     209  return QDialog::exec();
     210}
     211
     212/*!
     213 * \brief OMSSimulationDialog::simulate
     214 * Simulates the OMSimulator model.
     215 * \param pLibraryTreeItem
     216 */
     217void OMSSimulationDialog::simulate(LibraryTreeItem *pLibraryTreeItem)
     218{
     219  OMSSimulationOutputWidget *pOMSSimulationOutputWidget = new OMSSimulationOutputWidget(pLibraryTreeItem->getNameStructure());
     220  mOMSSimulationOutputWidgetsList.append(pOMSSimulationOutputWidget);
     221  int xPos = QApplication::desktop()->availableGeometry().width() - pOMSSimulationOutputWidget->frameSize().width() - 20;
     222  int yPos = QApplication::desktop()->availableGeometry().height() - pOMSSimulationOutputWidget->frameSize().height() - 20;
     223  pOMSSimulationOutputWidget->setGeometry(xPos, yPos, pOMSSimulationOutputWidget->width(), pOMSSimulationOutputWidget->height());
     224  pOMSSimulationOutputWidget->show();
    105225}
    106226
     
    109229 * Called by OMSSimulationOutputWidget when the simulation is finished.\n
    110230 * Reads the result file and plots the result.
    111  * \param omsSimulationOptions
     231 * \param resultFilePath
    112232 * \param resultFileLastModifiedDateTime
    113233 */
    114 void OMSSimulationDialog::simulationFinished(OMSSimulationOptions omsSimulationOptions, QDateTime resultFileLastModifiedDateTime)
     234void OMSSimulationDialog::simulationFinished(const QString &resultFilePath, QDateTime resultFileLastModifiedDateTime)
    115235{
    116236  // read the result file
    117   QFileInfo resultFileInfo(omsSimulationOptions.getWorkingDirectory() + "/" + omsSimulationOptions.getResultFileName());
    118   if (resultFileInfo.exists() && resultFileLastModifiedDateTime <= resultFileInfo.lastModified()) {
    119     VariablesWidget *pVariablesWidget = MainWindow::instance()->getVariablesWidget();
    120     OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
    121     QStringList list = pOMCProxy->readSimulationResultVars(resultFileInfo.absoluteFilePath());
    122     if (list.size() > 0) {
    123       MainWindow::instance()->switchToPlottingPerspectiveSlot();
    124       pVariablesWidget->insertVariablesItemsToTree(resultFileInfo.fileName(), omsSimulationOptions.getWorkingDirectory(),
    125                                                    list, SimulationOptions());
    126       MainWindow::instance()->getVariablesDockWidget()->show();
    127     }
    128   }
     237  QFileInfo resultFileInfo(resultFilePath);
     238  if (!resultFileInfo.exists() || resultFileLastModifiedDateTime > resultFileInfo.lastModified()) {
     239    MessagesWidget::instance()->addGUIMessage(MessageItem(MessageItem::Modelica, tr("Unable to find the result file <b>%1</b>.").arg(resultFileInfo.absoluteFilePath()),
     240                                                          Helper::scriptingKind, Helper::errorLevel));
     241    return;
     242  }
     243  qDebug() << resultFilePath << resultFileLastModifiedDateTime << resultFileInfo.lastModified();
     244  VariablesWidget *pVariablesWidget = MainWindow::instance()->getVariablesWidget();
     245  OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
     246  QStringList list = pOMCProxy->readSimulationResultVars(resultFileInfo.absoluteFilePath());
     247  MainWindow::instance()->switchToPlottingPerspectiveSlot();
     248  pVariablesWidget->insertVariablesItemsToTree(resultFileInfo.fileName(), resultFileInfo.absoluteDir().absolutePath(), list, SimulationOptions());
     249  MainWindow::instance()->getVariablesDockWidget()->show();
    129250}
    130251
     
    147268
    148269/*!
    149  * \brief OMSSimulationDialog::simulate
    150  * Simulates the OMSimulator model.
    151  * \param pLibraryTreeItem
    152  */
    153 void OMSSimulationDialog::simulate(LibraryTreeItem *pLibraryTreeItem)
    154 {
    155   OMSSimulationOutputWidget *pOMSSimulationOutputWidget = new OMSSimulationOutputWidget(pLibraryTreeItem->mOMSSimulationOptions);
    156   mOMSSimulationOutputWidgetsList.append(pOMSSimulationOutputWidget);
    157   int xPos = QApplication::desktop()->availableGeometry().width() - pOMSSimulationOutputWidget->frameSize().width() - 20;
    158   int yPos = QApplication::desktop()->availableGeometry().height() - pOMSSimulationOutputWidget->frameSize().height() - 20;
    159   pOMSSimulationOutputWidget->setGeometry(xPos, yPos, pOMSSimulationOutputWidget->width(), pOMSSimulationOutputWidget->height());
    160   pOMSSimulationOutputWidget->show();
    161 }
     270 * \brief OMSSimulationDialog::saveSimulationSettings
     271 * Saves the simulation settings.
     272 */
     273void OMSSimulationDialog::saveSimulationSettings()
     274{
     275  if (mpStartTimeTextBox->text().isEmpty()) {
     276    mpStartTimeTextBox->setText("0");
     277  }
     278  if (mpStopTimeTextBox->text().isEmpty()) {
     279    mpStopTimeTextBox->setText("1");
     280  }
     281  if (mpStartTimeTextBox->text().toDouble() > mpStopTimeTextBox->text().toDouble()) {
     282    QMessageBox::critical(MainWindow::instance(), QString("%1 - %2").arg(Helper::applicationName, Helper::error),
     283                          GUIMessages::getMessage(GUIMessages::SIMULATION_STARTTIME_LESSTHAN_STOPTIME), Helper::ok);
     284    return;
     285  }
     286
     287  if (mpSystemSimulationInformationWidget) {
     288    mpSystemSimulationInformationWidget->setSystemSimulationInformation(false);
     289  }
     290
     291  // set the simulation settings
     292  OMSProxy::instance()->setStartTime(mModelCref, mpStartTimeTextBox->text().toDouble());
     293  OMSProxy::instance()->setStopTime(mModelCref, mpStopTimeTextBox->text().toDouble());
     294  OMSProxy::instance()->setResultFile(mModelCref, mpResultFileTextBox->text(), mpResultFileBufferSizeSpinBox->value());
     295  OMSProxy::instance()->setLoggingInterval(mModelCref, mpLoggingIntervalTextBox->text().toDouble());
     296  if (!mpSignalFilterTextBox->text().isEmpty()) {
     297    OMSProxy::instance()->setSignalFilter(mModelCref, mpSignalFilterTextBox->text());
     298  }
     299
     300  ModelWidget *pModelWidget = mpLibraryTreeItem->getModelWidget();
     301  pModelWidget->createOMSimulatorUndoCommand(QString("Simulation setup %1").arg(mModelCref));
     302  pModelWidget->updateModelText();
     303  accept();
     304}
Note: See TracChangeset for help on using the changeset viewer.