From 76227e393954947c56ebbe8fa94ab98e9a965cc9 Mon Sep 17 00:00:00 2001 From: Adeel Asghar Date: Fri, 12 Jun 2015 14:44:30 +0200 Subject: [PATCH] #3352 - Allow creating new result file when doing re-simulation. --- OMEdit/OMEditGUI/Editors/TextEditor.cpp | 2 +- OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp | 27 ++++----- .../OMEditGUI/Simulation/SimulationDialog.cpp | 55 ++++++++++++++----- .../OMEditGUI/Simulation/SimulationDialog.h | 5 ++ .../OMEditGUI/Simulation/SimulationOptions.h | 5 +- OMEdit/OMEditGUI/Util/Helper.cpp | 2 +- 6 files changed, 67 insertions(+), 29 deletions(-) diff --git a/OMEdit/OMEditGUI/Editors/TextEditor.cpp b/OMEdit/OMEditGUI/Editors/TextEditor.cpp index 202e09cd598..21e47830c24 100644 --- a/OMEdit/OMEditGUI/Editors/TextEditor.cpp +++ b/OMEdit/OMEditGUI/Editors/TextEditor.cpp @@ -91,7 +91,7 @@ void TextEditor::showContextMenu(QPoint point) void TextEditor::contentsHasChanged(int position, int charsRemoved, int charsAdded) { Q_UNUSED(position); - if (mpModelWidget->isVisible()) { + if (mpModelWidget && mpModelWidget->isVisible()) { if (charsRemoved == 0 && charsAdded == 0) { return; } diff --git a/OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp b/OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp index 694bb304926..58272b6774b 100644 --- a/OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp +++ b/OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp @@ -435,17 +435,19 @@ void VariablesTreeModel::insertVariablesItems(QString fileName, QString filePath mpRootVariablesTreeItem->insertChild(row, pTopVariablesTreeItem); endInsertRows(); /* open the model_init.xml file for reading */ - QString initFileName = QString(fileName).replace(resultTypeRegExp, "_init.xml"); - QFile initFile(QString(filePath).append(QDir::separator()).append(initFileName)); - if (initFile.open(QIODevice::ReadOnly)) { - QXmlStreamReader initXmlReader(&initFile); - parseInitXml(initXmlReader); - initFile.close(); - } else { - MessagesWidget *pMessagesWidget = mpVariablesTreeView->getVariablesWidget()->getMainWindow()->getMessagesWidget(); - pMessagesWidget->addGUIMessage(MessageItem(MessageItem::Modelica, "", false, 0, 0, 0, 0, - GUIMessages::getMessage(GUIMessages::ERROR_OPENING_FILE).arg(initFile.fileName()) - .arg(initFile.errorString()),Helper::scriptingKind, Helper::warningLevel)); + if (simulationOptions.isValid()) { + QString initFileName = QString(simulationOptions.getOutputFileName()).append("_init.xml"); + QFile initFile(QString(filePath).append(QDir::separator()).append(initFileName)); + if (initFile.open(QIODevice::ReadOnly)) { + QXmlStreamReader initXmlReader(&initFile); + parseInitXml(initXmlReader); + initFile.close(); + } else { + MessagesWidget *pMessagesWidget = mpVariablesTreeView->getVariablesWidget()->getMainWindow()->getMessagesWidget(); + pMessagesWidget->addGUIMessage(MessageItem(MessageItem::Modelica, "", false, 0, 0, 0, 0, + GUIMessages::getMessage(GUIMessages::ERROR_OPENING_FILE).arg(initFile.fileName()) + .arg(initFile.errorString()), Helper::scriptingKind, Helper::errorLevel)); + } } /* open the .mat file */ ModelicaMatReader matReader; @@ -1021,9 +1023,8 @@ void VariablesWidget::reSimulate(bool showSetup) void VariablesWidget::updateInitXmlFile(SimulationOptions simulationOptions) { /* Update the _init.xml file with new values. */ - QRegExp resultTypeRegExp("(_res.mat|_res.plt|_res.csv)"); /* open the model_init.xml file for writing */ - QString initFileName = QString(simulationOptions.getResultFileName()).replace(resultTypeRegExp, "_init.xml"); + QString initFileName = QString(simulationOptions.getOutputFileName()).append("_init.xml"); QFile initFile(QString(simulationOptions.getWorkingDirectory()).append(QDir::separator()).append(initFileName)); QDomDocument initXmlDocument; if (initFile.open(QIODevice::ReadOnly)) { diff --git a/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp b/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp index bb42fde193e..dc380562734 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp +++ b/OMEdit/OMEditGUI/Simulation/SimulationDialog.cpp @@ -273,10 +273,15 @@ void SimulationDialog::setUpForm() mpOutputFormatComboBox = new QComboBox; mpOutputFormatComboBox->addItems(Helper::ModelicaSimulationOutputFormats.toLower().split(",")); // Output filename - mpFileNameLabel = new Label(tr("File Name (Optional):")); + mpFileNameLabel = new Label(tr("File Name Prefix (Optional):")); mpFileNameTextBox = new QLineEdit; mpFileNameTextBox->setToolTip(tr("The name is used as a prefix for the output files. This is just a name not the path.\n" "If you want to change the output path then update the working directory in Options/Preferences.")); + mpResultFileNameLabel = new Label(tr("Result File (Optional):")); + mpResultFileNameTextBox = new QLineEdit; + mpResultFileName = new Label; + connect(mpResultFileNameTextBox, SIGNAL(textEdited(QString)), SLOT(resultFileNameChanged(QString))); + connect(mpOutputFormatComboBox, SIGNAL(currentIndexChanged(QString)), SLOT(resultFileNameChanged(QString))); // Variable filter mpVariableFilterLabel = new Label(tr("Variable Filter (Optional):")); mpVariableFilterTextBox = new QLineEdit(".*"); @@ -294,17 +299,20 @@ void SimulationDialog::setUpForm() QGridLayout *pOutputTabLayout = new QGridLayout; pOutputTabLayout->setAlignment(Qt::AlignTop); pOutputTabLayout->addWidget(mpNumberofIntervalLabel, 0, 0); - pOutputTabLayout->addWidget(mpNumberofIntervalsSpinBox, 0, 1); + pOutputTabLayout->addWidget(mpNumberofIntervalsSpinBox, 0, 1, 1, 2); pOutputTabLayout->addWidget(mpOutputFormatLabel, 1, 0); - pOutputTabLayout->addWidget(mpOutputFormatComboBox, 1, 1); + pOutputTabLayout->addWidget(mpOutputFormatComboBox, 1, 1, 1, 2); pOutputTabLayout->addWidget(mpFileNameLabel, 2, 0); - pOutputTabLayout->addWidget(mpFileNameTextBox, 2, 1); - pOutputTabLayout->addWidget(mpVariableFilterLabel, 3, 0); - pOutputTabLayout->addWidget(mpVariableFilterTextBox, 3, 1); - pOutputTabLayout->addWidget(mpProtectedVariablesCheckBox, 4, 0, 1, 2); - pOutputTabLayout->addWidget(mpEquidistantTimeGridCheckBox, 5, 0, 1, 2); - pOutputTabLayout->addWidget(mpStoreVariablesAtEventsCheckBox, 6, 0, 1, 2); - pOutputTabLayout->addWidget(mpShowGeneratedFilesCheckBox, 7, 0, 1, 2); + pOutputTabLayout->addWidget(mpFileNameTextBox, 2, 1, 1, 2); + pOutputTabLayout->addWidget(mpResultFileNameLabel, 3, 0); + pOutputTabLayout->addWidget(mpResultFileNameTextBox, 3, 1); + pOutputTabLayout->addWidget(mpResultFileName, 3, 2); + pOutputTabLayout->addWidget(mpVariableFilterLabel, 4, 0); + pOutputTabLayout->addWidget(mpVariableFilterTextBox, 4, 1, 1, 2); + pOutputTabLayout->addWidget(mpProtectedVariablesCheckBox, 5, 0, 1, 3); + pOutputTabLayout->addWidget(mpEquidistantTimeGridCheckBox, 6, 0, 1, 3); + pOutputTabLayout->addWidget(mpStoreVariablesAtEventsCheckBox, 7, 0, 1, 3); + pOutputTabLayout->addWidget(mpShowGeneratedFilesCheckBox, 8, 0, 1, 3); mpOutputTab->setLayout(pOutputTabLayout); // add Output Tab to Simulation TabWidget mpSimulationTabWidget->addTab(mpOutputTab, Helper::output); @@ -556,7 +564,6 @@ void SimulationDialog::initializeFields(bool isReSimulate, SimulationOptions sim } mpCflagsTextBox->setEnabled(true); mpNumberofIntervalsSpinBox->setEnabled(true); - mpOutputFormatComboBox->setEnabled(true); mpFileNameTextBox->setEnabled(true); mpSaveSimulationCheckbox->setEnabled(true); mpSimulateButton->setText(Helper::simulate); @@ -603,8 +610,6 @@ void SimulationDialog::initializeFields(bool isReSimulate, SimulationOptions sim mpBuildOnlyCheckBox->setChecked(simulationOptions.getBuildOnly()); // Output Interval mpNumberofIntervalsSpinBox->setValue(simulationOptions.getNumberofIntervals()); - // Output Format - mpOutputFormatComboBox->setDisabled(true); // Output filename mpFileNameTextBox->setDisabled(true); // Variable filter @@ -740,6 +745,7 @@ SimulationOptions SimulationDialog::createSimulationOptions() } else if (mClassName.contains('\'')) { simulationOptions.setFileNamePrefix("_omcQuot_" + QByteArray(mClassName.toStdString().c_str()).toHex()); } + simulationOptions.setResultFileName(mpResultFileName->text()); simulationOptions.setVariableFilter(mpVariableFilterTextBox->text()); simulationOptions.setProtectedVariables(mpProtectedVariablesCheckBox->isChecked()); simulationOptions.setEquidistantTimeGrid(mpEquidistantTimeGridCheckBox->isChecked()); @@ -786,6 +792,7 @@ SimulationOptions SimulationDialog::createSimulationOptions() .arg("solver").arg(simulationOptions.getMethod()) .arg("outputFormat").arg(simulationOptions.getOutputFormat()) .arg("variableFilter").arg(simulationOptions.getVariableFilter())); + simulationFlags.append(QString("-r=").append(simulationOptions.getResultFileName())); // dassl options if (mpDasslOptionsGroupBox->isEnabled()) { // dassl jacobian @@ -1173,3 +1180,25 @@ void SimulationDialog::simulate() } } } + +/*! + * \brief SimulationDialog::resultFileNameChanged + * \param text + * Slot activated when mpResultFileNameTextBox textEdited OR mpOutputFormatComboBox currentIndexChanged signal is raised.\n + * Sets the result file name label. + */ +void SimulationDialog::resultFileNameChanged(QString text) +{ + QLineEdit *pLineEditSender = qobject_cast(sender()); + QComboBox *pComboBoxSender = qobject_cast(sender()); + + if (pLineEditSender) { + if (text.isEmpty()) { + mpResultFileName->clear(); + } else { + mpResultFileName->setText(QString("%1_res.%2").arg(text).arg(mpOutputFormatComboBox->currentText())); + } + } else if (pComboBoxSender && !mpResultFileNameTextBox->text().isEmpty()) { + mpResultFileName->setText(QString("%1_res.%2").arg(mpResultFileNameTextBox->text()).arg(mpOutputFormatComboBox->currentText())); + } +} diff --git a/OMEdit/OMEditGUI/Simulation/SimulationDialog.h b/OMEdit/OMEditGUI/Simulation/SimulationDialog.h index 31149dbc336..7c6376dd58e 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationDialog.h +++ b/OMEdit/OMEditGUI/Simulation/SimulationDialog.h @@ -125,6 +125,9 @@ class SimulationDialog : public QDialog QComboBox *mpOutputFormatComboBox; Label *mpFileNameLabel; QLineEdit *mpFileNameTextBox; + Label *mpResultFileNameLabel; + QLineEdit *mpResultFileNameTextBox; + Label *mpResultFileName; Label *mpVariableFilterLabel; QLineEdit *mpVariableFilterTextBox; QCheckBox *mpProtectedVariablesCheckBox; @@ -214,6 +217,8 @@ public slots: void browseEquationSystemInitializationFile(); void showArchivedSimulation(QTreeWidgetItem *pTreeWidgetItem); void simulate(); +private slots: + void resultFileNameChanged(QString text); }; #endif // SIMULATIONDIALOG_H diff --git a/OMEdit/OMEditGUI/Simulation/SimulationOptions.h b/OMEdit/OMEditGUI/Simulation/SimulationOptions.h index 8ce4b57b0d1..5e199dfc685 100644 --- a/OMEdit/OMEditGUI/Simulation/SimulationOptions.h +++ b/OMEdit/OMEditGUI/Simulation/SimulationOptions.h @@ -66,6 +66,7 @@ class SimulationOptions setStepSize(0.002); setOutputFormat("mat"); setFileNamePrefix(""); + setResultFileName(""); setVariableFilter(""); setProtectedVariables(false); setEquidistantTimeGrid(true); @@ -149,7 +150,8 @@ class SimulationOptions void setFileNamePrefix(QString fileNamePrefix) {mFileNamePrefix = fileNamePrefix;} QString getFileNamePrefix() {return mFileNamePrefix;} QString getOutputFileName() const {return mFileNamePrefix.isEmpty() ? mClassName : mFileNamePrefix;} - QString getResultFileName() {return getOutputFileName() + "_res." + mOutputFormat;} + void setResultFileName(QString resultFileName) {mResultFileName = resultFileName;} + QString getResultFileName() {return mResultFileName.isEmpty() ? getOutputFileName() + "_res." + mOutputFormat : mResultFileName;} void setVariableFilter(QString variableFilter) {mVariableFilter = variableFilter;} QString getVariableFilter() {return mVariableFilter.isEmpty() ? ".*" : mVariableFilter;} void setProtectedVariables(bool protectedVariables) {mProtectedVariables = protectedVariables;} @@ -254,6 +256,7 @@ class SimulationOptions qreal mStepSize; QString mOutputFormat; QString mFileNamePrefix; + QString mResultFileName; QString mVariableFilter; bool mProtectedVariables; bool mEquidistantTimeGrid; diff --git a/OMEdit/OMEditGUI/Util/Helper.cpp b/OMEdit/OMEditGUI/Util/Helper.cpp index 1081638d117..35275de62a5 100644 --- a/OMEdit/OMEditGUI/Util/Helper.cpp +++ b/OMEdit/OMEditGUI/Util/Helper.cpp @@ -75,7 +75,7 @@ QString Helper::modelicaFileFormat = "text/uri-list"; qreal Helper::shapesStrokeWidth = 2.0; int Helper::headingFontSize = 18; QString Helper::ModelicaInitializationMethods = ",none,symbolic"; -QString Helper::ModelicaSimulationOutputFormats = "mat,plt,csv,empty"; +QString Helper::ModelicaSimulationOutputFormats = "mat,plt,csv"; QString Helper::clockOptions = ",RT,CYC,CPU"; QString Helper::linearSolvers = ",lapack"; QString Helper::nonLinearSolvers = ",hybrid,kinsol,newton";