Skip to content

Commit

Permalink
Some minor cleaning up.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Nov 13, 2019
1 parent 439b155 commit 727b394
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 95 deletions.
Expand Up @@ -671,5 +671,9 @@
<source>unable to get the simulation&apos;s runtime</source>
<translation>incapable d&apos;obtenir le modèle d&apos;exécution de la simulation</translation>
</message>
<message>
<source>unable to close the simulation</source>
<translation>incapable de fermer la simulation</translation>
</message>
</context>
</TS>
Expand Up @@ -22,13 +22,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

#include "centralwidget.h"
#include "coreguiutils.h"
#include "file.h"
#include "pythonqtsupport.h"
#include "simulation.h"
#include "simulationexperimentviewplugin.h"
#include "simulationexperimentviewpythonwrapper.h"
#include "simulationexperimentviewsimulationwidget.h"
#include "simulationexperimentviewwidget.h"

//==============================================================================
Expand All @@ -38,42 +35,40 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

namespace OpenCOR {

//==============================================================================

namespace SimulationSupport {
class Simulation;
} // namespace SimulationSupport

//==============================================================================

namespace SimulationExperimentView {

//==============================================================================

static PyObject *getSimulation(const QString &pFileName,
SimulationExperimentViewWidget *pSimulationExperimentViewWidget)
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) {
if (simulation->runtime() == nullptr) {
// The simulation is missing a runtime so raise a Python exception
// The simulation is missing a runtime, so raise a Python exception

PyErr_SetString(PyExc_ValueError, qPrintable(QObject::tr("unable to get the simulation's runtime")));

return nullptr;
}
// Let the simulation's widget know when we start running
// Note: connect is also in the PythonQt namespace

// Let the Simulation Experiment view widget know when we start running
// a simulation

QObject::connect(simulation, &SimulationSupport::Simulation::runStarting,
pSimulationExperimentViewWidget, &SimulationExperimentViewWidget::startingRun);

// Use the simulation's widget to clear our data
// Let the Simulation Experiment view widget know when we want to clear
// simulation results

QObject::connect(simulation, &SimulationSupport::Simulation::clearResults,
pSimulationExperimentViewWidget, &SimulationExperimentViewWidget::clearResults);
pSimulationExperimentViewWidget, &SimulationExperimentViewWidget::clearSimulationResults);

// Return our simulation wrapped into a Python object

return PythonQt::priv()->wrapQObject(simulation);
}
Expand All @@ -85,14 +80,36 @@ static PyObject *getSimulation(const QString &pFileName,

//==============================================================================

static PyObject *initializeSimulation(const QString &pFileName)
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 getSimulation(pFileName, simulationExperimentViewWidget);
return simulation(pFileName, simulationExperimentViewWidget);
}

#include "pythonbegin.h"
Expand All @@ -102,20 +119,27 @@ static PyObject *initializeSimulation(const QString &pFileName)

//==============================================================================

static PyObject *openSimulation(PyObject *self, PyObject *args)
static PyObject * openSimulation(PyObject *pSelf, PyObject *pArgs)
{
Q_UNUSED(self)
Q_UNUSED(pSelf)

// Open a simulation

PyObject *bytes;
char *name;
Py_ssize_t len;
if (PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &bytes) == 0) { // NOLINT(cppcoreguidelines-pro-type-vararg)

if (PyArg_ParseTuple(pArgs, "O&", PyUnicode_FSConverter, &bytes) == 0) { // NOLINT(cppcoreguidelines-pro-type-vararg)
#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

char *name;
Py_ssize_t len;

PyBytes_AsStringAndSize(bytes, &name, &len);

QString fileName = QString::fromUtf8(name, int(len));

#include "pythonbegin.h"
Py_DECREF(bytes);
#include "pythonend.h"
Expand All @@ -135,20 +159,27 @@ static PyObject *openSimulation(PyObject *self, PyObject *args)

//==============================================================================

static PyObject *openRemoteSimulation(PyObject *self, PyObject *args)
static PyObject * openRemoteSimulation(PyObject *pSelf, PyObject *pArgs)
{
Q_UNUSED(self)
Q_UNUSED(pSelf)

// Open a remote simulation

PyObject *bytes;
char *name;
Py_ssize_t len;
if (PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &bytes) == 0) { // NOLINT(cppcoreguidelines-pro-type-vararg)

if (PyArg_ParseTuple(pArgs, "O&", PyUnicode_FSConverter, &bytes) == 0) { // NOLINT(cppcoreguidelines-pro-type-vararg)
#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

char *name;
Py_ssize_t len;

PyBytes_AsStringAndSize(bytes, &name, &len);

QString url = QString::fromUtf8(name, int(len));

#include "pythonbegin.h"
Py_DECREF(bytes);
#include "pythonend.h"
Expand All @@ -166,23 +197,25 @@ static PyObject *openRemoteSimulation(PyObject *self, PyObject *args)

//==============================================================================

static PyObject *closeSimulation(PyObject *self, PyObject *args)
static PyObject * closeSimulation(PyObject *pSelf, PyObject *pArgs)
{
Q_UNUSED(self)
Q_UNUSED(pSelf)

if (PyTuple_Size(args) > 0) {
// Close a simulation

if (PyTuple_Size(pArgs) > 0) {
#include "pythonbegin.h"
PythonQtInstanceWrapper *wrappedSimulation = PythonQtSupport::getInstanceWrapper(PyTuple_GET_ITEM(args, 0)); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
PythonQtInstanceWrapper *wrappedSimulation = PythonQtSupport::getInstanceWrapper(PyTuple_GET_ITEM(pArgs, 0)); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
#include "pythonend.h"

if (wrappedSimulation != nullptr) {
auto simulation = static_cast<SimulationSupport::Simulation *>(wrappedSimulation->_objPointerCopy);

// Close the simulation by closing its file, raising an exception if we
// are unable to do so
// Close the simulation by closing its file, raising an exception if
// we were unable to do so

if (!Core::centralWidget()->closeFile(simulation->fileName())) {
PyErr_SetString(PyExc_IOError, "unable to close file");
PyErr_SetString(PyExc_IOError, qPrintable(QObject::tr("unable to close the simulation")));

return nullptr;
}
Expand All @@ -196,30 +229,14 @@ static PyObject *closeSimulation(PyObject *self, PyObject *args)

//==============================================================================

static PyObject *OpenCOR_simulation(PyObject *self, PyObject *args)
{
Q_UNUSED(self)
Q_UNUSED(args)

SimulationExperimentViewWidget *simulationExperimentViewWidget = SimulationExperimentViewPlugin::instance()->viewWidget();

if (simulationExperimentViewWidget != nullptr) {
return getSimulation(Core::centralWidget()->currentFileName(), simulationExperimentViewWidget);
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

//==============================================================================

SimulationExperimentViewPythonWrapper::SimulationExperimentViewPythonWrapper(PyObject *pModule, QObject *pParent) : QObject(pParent)
SimulationExperimentViewPythonWrapper::SimulationExperimentViewPythonWrapper(PyObject *pModule,
QObject *pParent) :
QObject(pParent)
{
// Add some Python wrappers

static std::array<PyMethodDef, 5> PythonSimulationExperimentViewMethods = {{
{ "simulation", OpenCOR_simulation, METH_VARARGS, "Current simulation." },
{ "simulation", simulation, METH_VARARGS, "Current simulation." },
{ "openSimulation", openSimulation, METH_VARARGS, "Open a simulation." },
{ "openRemoteSimulation", openRemoteSimulation, METH_VARARGS, "Open a remote simulation." },
{ "closeSimulation", closeSimulation, METH_VARARGS, "Close a simulation." },
Expand Down
Expand Up @@ -36,9 +36,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

namespace OpenCOR {

//==============================================================================

namespace SimulationExperimentView {

//==============================================================================
Expand All @@ -49,7 +46,7 @@ class SimulationExperimentViewPythonWrapper : public QObject

public:
explicit SimulationExperimentViewPythonWrapper(PyObject *pModule,
QObject *pParent = nullptr);
QObject *pParent);
};

//==============================================================================
Expand Down
Expand Up @@ -1396,14 +1396,9 @@ void SimulationExperimentViewSimulationWidget::runPauseResumeSimulation()
mSimulation->resume();
} else {
// Try to allocate all the memory we need by adding a run to our
// simulation
// simulation and, if successful, run our simulation

bool runSimulation = mSimulation->addRun();

// Run our simulation (after having added a run to our graphs), in
// case we were able to allocate all the memory we need

if (runSimulation) {
if (mSimulation->addRun()) {
mViewWidget->checkSimulationResults(mSimulation->fileName(), Task::AddRun);

mSimulation->run();
Expand Down
Expand Up @@ -495,21 +495,6 @@ QWidget * SimulationExperimentViewWidget::widget(const QString &pFileName)

//==============================================================================

void SimulationExperimentViewWidget::clearResults(const QString &pFileName)
{
// Clear our simulation data

SimulationExperimentViewSimulationWidget *simulationWidget = mSimulationWidgets.value(pFileName);

if (simulationWidget != nullptr) {
return;
}

simulationWidget->clearSimulationResults();
}

//==============================================================================

quint64 SimulationExperimentViewWidget::simulationResultsSize(const QString &pFileName) const
{
// Return the results size for the given file name
Expand Down Expand Up @@ -592,11 +577,29 @@ void SimulationExperimentViewWidget::checkSimulationResults(const QString &pFile

void SimulationExperimentViewWidget::startingRun(const QString &pFileName)
{
// A run is starting for the given file name (after having added a run), so
// check its simulation results

checkSimulationResults(pFileName, SimulationExperimentViewSimulationWidget::Task::AddRun);
}

//==============================================================================

void SimulationExperimentViewWidget::clearSimulationResults(const QString &pFileName)
{
// Clear the simulation results for the given file name

SimulationExperimentViewSimulationWidget *simulationWidget = mSimulationWidgets.value(pFileName);

if (simulationWidget != nullptr) {
return;
}

simulationWidget->clearSimulationResults();
}

//==============================================================================

void SimulationExperimentViewWidget::simulationWidgetSplitterMoved(const QIntList &pSizes)
{
// The splitter of our simulation widget has moved, so keep track of its new
Expand Down
Expand Up @@ -132,8 +132,8 @@ class SimulationExperimentViewWidget : public Core::ViewWidget
void updateContentsInformationGui(SimulationExperimentViewSimulationWidget *pSimulationWidget);

public slots:
void clearResults(const QString &pFileName);
void startingRun(const QString &pFileName);
void clearSimulationResults(const QString &pFileName);

private slots:
void simulationWidgetSplitterMoved(const QIntList &pSizes);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/support/SimulationSupport/src/simulation.h
Expand Up @@ -405,8 +405,8 @@ class SIMULATIONSUPPORT_EXPORT Simulation : public QObject
bool simulationSettingsOk(bool pEmitSignal = true);

signals:
void clearResults(const QString &pFileName);
void runStarting(const QString &pFileName);
void clearResults(const QString &pFileName);

void running(bool pIsResuming);
void paused();
Expand Down
Expand Up @@ -441,20 +441,15 @@ bool SimulationSupportPythonWrapper::run(Simulation *pSimulation)

mErrorMessage = QString();

// Try to allocate all the memory we need by adding a run to our
// simulation
// Try to allocate all the memory we need by adding a run to our simulation
// and, if successful, run our simulation

bool runSimulation = pSimulation->addRun();

// Run our simulation (after having added a run to our graphs), in
// case we were able to allocate all the memory we need

if (runSimulation) {
if (pSimulation->addRun()) {
// Save the keyboard focus, which will be to our IPython console

focusWidget = QApplication::focusWidget();

// Let the simulation widget know we are starting
// Let people know that we are starting our run

emit pSimulation->runStarting(pSimulation->fileName());

Expand Down

0 comments on commit 727b394

Please sign in to comment.