-
Notifications
You must be signed in to change notification settings - Fork 298
/
OMSSimulationOutputWidget.cpp
196 lines (189 loc) · 8.21 KB
/
OMSSimulationOutputWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
/*
* @author Adeel Asghar <adeel.asghar@liu.se>
*/
#include "OMSSimulationOutputWidget.h"
#include "Util/Helper.h"
#include "MainWindow.h"
#include "OMSSimulationDialog.h"
#include "OMSProxy.h"
#include "Modeling/LibraryTreeWidget.h"
#include "Options/OptionsDialog.h"
#include <QGridLayout>
#include <QtCore/qmath.h>
OMSSimulationOutputWidget::OMSSimulationOutputWidget(const QString &cref, QWidget *pParent)
: QWidget(pParent), mCref(cref)
{
setWindowTitle(QString("%1 - %2 - %3").arg(Helper::applicationName, mCref, Helper::simulationOutput));
resize(640, 120);
// simulation widget heading
mpSimulationHeading = Utilities::getHeadingLabel(QString("%1 - %2").arg(tr("OMSimulator Simulation"), mCref));
mpSimulationHeading->setElideMode(Qt::ElideMiddle);
// Horizontal separator
mpHorizontalLine = Utilities::getHeadingLine();
// progress label
mpProgressLabel = new Label(tr("Running simulation of <b>%1</b>. Please wait for a while.").arg(mCref));
mpProgressLabel->setTextFormat(Qt::RichText);
mpCancelSimulationButton = new QPushButton(Helper::cancelSimulation);
mpCancelSimulationButton->setEnabled(false);
connect(mpCancelSimulationButton, SIGNAL(clicked()), SLOT(cancelSimulation()));
mpProgressBar = new QProgressBar;
mpProgressBar->setAlignment(Qt::AlignHCenter);
mpProgressBar->setRange(0, 100);
mpProgressBar->setTextVisible(true);
// layout
QGridLayout *pMainLayout = new QGridLayout;
pMainLayout->setContentsMargins(5, 5, 5, 5);
pMainLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
pMainLayout->addWidget(mpSimulationHeading, 0, 0, 1, 2);
pMainLayout->addWidget(mpHorizontalLine, 1, 0, 1, 2);
pMainLayout->addWidget(mpProgressLabel, 2, 0, 1, 2);
pMainLayout->addWidget(mpProgressBar, 3, 0);
pMainLayout->addWidget(mpCancelSimulationButton, 3, 1);
setLayout(pMainLayout);
// save the model start time
OMSProxy::instance()->getStartTime(mCref, &mStartTime);
// save the model stop time
OMSProxy::instance()->getStopTime(mCref, &mStopTime);
// create the ArchivedSimulationItem
mpArchivedOMSSimulationItem = new ArchivedOMSSimulationItem(mCref, mStartTime, mStopTime, this);
MainWindow::instance()->getOMSSimulationDialog()->getArchivedSimulationsTreeWidget()->addTopLevelItem(mpArchivedOMSSimulationItem);
// save the last modified datetime of result file.
char *fileName = (char*)"";
int bufferSize;
OMSProxy::instance()->getResultFile(mCref, &fileName, &bufferSize);
mResultFilePath = QString("%1/%2").arg(OptionsDialog::instance()->getGeneralSettingsPage()->getWorkingDirectory(), QString(fileName));
QFileInfo resultFileInfo(mResultFilePath);
if (resultFileInfo.exists()) {
mResultFileLastModifiedDateTime = resultFileInfo.lastModified();
} else {
mResultFileLastModifiedDateTime = QDateTime::currentDateTime();
}
mIsSimulationRunning = false;
// initialize the model
if (OMSProxy::instance()->initialize(mCref)) {
// start the asynchronous simulation
qRegisterMetaType<oms_status_enu_t>("oms_status_enu_t");
connect(this, SIGNAL(sendSimulationProgress(QString,double,oms_status_enu_t)), SLOT(simulationProgress(QString,double,oms_status_enu_t)));
if (OMSProxy::instance()->simulate_asynchronous(mCref)) {
mIsSimulationRunning = true;
mpCancelSimulationButton->setEnabled(true);
} else {
mpProgressLabel->setText(tr("Simulation using the <b>%1</b> model is failed. %2").arg(mCref).arg(GUIMessages::getMessage(GUIMessages::CHECK_MESSAGES_BROWSER)));
mpProgressBar->setValue(mpProgressBar->maximum());
mpArchivedOMSSimulationItem->setStatus(tr("Simulation failed!"));
}
} else {
LibraryTreeItem *pLibraryTreeItem;
pLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItemOneLevel(mCref);
if (pLibraryTreeItem) {
MainWindow::instance()->instantiateOMSModel(pLibraryTreeItem, false);
}
mpProgressLabel->setText(tr("Initialization using the <b>%1</b> model is failed. %2").arg(mCref).arg(GUIMessages::getMessage(GUIMessages::CHECK_MESSAGES_BROWSER)));
mpProgressBar->setValue(mpProgressBar->maximum());
mpArchivedOMSSimulationItem->setStatus(tr("Initialization failed!"));
}
}
/*!
* \brief OMSSimulationOutputWidget::simulateCallback
* This function is called by simulateCallback function from OMSProxy.\n
* Emits the SIGNAL sendSimulationProgress so that we can update the GUI elements in the GUI thread.
* \param ident
* \param time
* \param status
*/
void OMSSimulationOutputWidget::simulateCallback(const char* ident, double time, oms_status_enu_t status)
{
emit sendSimulationProgress(QString(ident), time, status);
}
/*!
* \brief OMSSimulationOutputWidget::cancelSimulation
* Slot activated when mpCancelSimulationButton clicked SIGNAL is raised.\n
* Cancels the running simulation.
*/
void OMSSimulationOutputWidget::cancelSimulation()
{
// cancel the simulation
if (OMSProxy::instance()->cancelSimulation_asynchronous(mCref)) {
LibraryTreeItem *pLibraryTreeItem;
pLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItemOneLevel(mCref);
if (pLibraryTreeItem) {
MainWindow::instance()->instantiateOMSModel(pLibraryTreeItem, false);
}
mpProgressLabel->setText(tr("Simulation using the <b>%1</b> model is cancelled.").arg(mCref));
mpProgressBar->setValue(mpProgressBar->maximum());
mIsSimulationRunning = false;
mpCancelSimulationButton->setEnabled(false);
}
}
/*!
* \brief OMSSimulationOutputWidget::simulationProgress
* Slot activated when sendSimulationProgress SIGNAL is raised.\n
* Updates the simulation progress.
* \param ident
* \param time
* \param status
*/
void OMSSimulationOutputWidget::simulationProgress(QString ident, double time, oms_status_enu_t status)
{
if (status < oms_status_warning) {
int progress = (time * 100) / mStopTime;
mpProgressBar->setValue(progress);
if (time >= mStopTime) {
mpProgressLabel->setText(tr("Simulation using the <b>%1</b> model is finished.").arg(mCref));
mpProgressBar->setValue(mpProgressBar->maximum());
mIsSimulationRunning = false;
mpCancelSimulationButton->setEnabled(false);
mpArchivedOMSSimulationItem->setStatus(Helper::finished);
// terminate the model after the simulation is finished successfully.
LibraryTreeItem *pLibraryTreeItem;
pLibraryTreeItem = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel()->findLibraryTreeItem(ident);
if (pLibraryTreeItem) {
MainWindow::instance()->instantiateOMSModel(pLibraryTreeItem, false);
}
// simulation finished show the results
MainWindow::instance()->getOMSSimulationDialog()->simulationFinished(mResultFilePath, mResultFileLastModifiedDateTime);
}
}
}
/*!
* \brief OMSSimulationOutputWidget::keyPressEvent
* Closes the widget when Esc key is pressed.
* \param event
*/
void OMSSimulationOutputWidget::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
close();
return;
}
QWidget::keyPressEvent(event);
}