/
simulationexperimentviewpythonwrapper.cpp
164 lines (119 loc) · 6.59 KB
/
simulationexperimentviewpythonwrapper.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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// Simulation Experiment view Python wrapper
//==============================================================================
#include "centralwidget.h"
#include "corecliutils.h"
#include "filemanager.h"
#include "pythonqtsupport.h"
#include "simulation.h"
#include "simulationexperimentviewplugin.h"
#include "simulationexperimentviewpythonwrapper.h"
#include "simulationexperimentviewwidget.h"
//==============================================================================
#include <array>
//==============================================================================
namespace OpenCOR {
namespace SimulationExperimentView {
//==============================================================================
static PyObject * simulation(const QString &pFileName,
SimulationExperimentViewWidget *pSimulationExperimentViewWidget)
{
// Return the simualtion, should there be one, for the given file using the
// given Simulation Experiment view widget
SimulationSupport::Simulation *simulation = pSimulationExperimentViewWidget->simulation(pFileName);
if (simulation != nullptr) {
// Check if something is wrong with our simulation and, if so, raise a
// Python exception
// Note: there may be several issues, but only the first one needs to be
// raised since Python obviously gives control back to the user as
// soon as an exception is raised...
SimulationSupport::SimulationIssues simulationIssues = simulation->issues();
if (!simulationIssues.isEmpty()) {
auto simulationIssue = simulationIssues.first();
if ((simulationIssue.line() != 0) && (simulationIssue.column() != 0)) {
PyErr_SetString(PyExc_ValueError, qPrintable(QObject::tr("[%1:%2] %3: %4.").arg(simulationIssue.line())
.arg(simulationIssue.column())
.arg(simulationIssue.typeAsString(),
Core::formatMessage(simulationIssue.message().toHtmlEscaped()))));
} else {
PyErr_SetString(PyExc_ValueError, qPrintable(QObject::tr("%1: %2.").arg(simulationIssue.typeAsString(),
Core::formatMessage(simulationIssue.message().toHtmlEscaped()))));
}
return nullptr;
}
// Return our simulation wrapped into a Python object
return PythonQt::priv()->wrapQObject(simulation);
}
#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
//==============================================================================
static PyObject * simulation(PyObject *pSelf, PyObject *pArgs)
{
Q_UNUSED(pSelf)
Q_UNUSED(pArgs)
// Return the current simulation
SimulationExperimentViewWidget *simulationExperimentViewWidget = SimulationExperimentViewPlugin::instance()->viewWidget();
if (simulationExperimentViewWidget != nullptr) {
return simulation(Core::centralWidget()->currentFileName(), simulationExperimentViewWidget);
}
#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
//==============================================================================
static PyObject * initializeSimulation(const QString &pFileName)
{
// Initialise a simulation for the given file name
SimulationExperimentViewWidget *simulationExperimentViewWidget = SimulationExperimentViewPlugin::instance()->viewWidget();
if (simulationExperimentViewWidget != nullptr) {
simulationExperimentViewWidget->initialize(pFileName);
return simulation(pFileName, simulationExperimentViewWidget);
}
#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
//==============================================================================
#define GUI_SUPPORT
#include "opensimulation.cpp.inl"
#undef GUI_SUPPORT
//==============================================================================
#define GUI_SUPPORT
#include "closesimulation.cpp.inl"
#undef GUI_SUPPORT
//==============================================================================
SimulationExperimentViewPythonWrapper::SimulationExperimentViewPythonWrapper(void *pModule,
QObject *pParent) :
QObject(pParent)
{
// Add some Python wrappers
static std::array<PyMethodDef, 4> PythonSimulationExperimentViewMethods = {{
{ "simulation", simulation, METH_VARARGS, "The current simulation." },
{ "openSimulation", openSimulation, METH_VARARGS, "Open a simulation." },
{ "closeSimulation", closeSimulation, METH_VARARGS, "Close a simulation." },
{ nullptr, nullptr, 0, nullptr }
}};
PyModule_AddFunctions(static_cast<PyObject *>(pModule),
PythonSimulationExperimentViewMethods.data());
}
//==============================================================================
} // namespace SimulationExperimentView
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================