From 479f44942f0011a5143a20353a576beaf1859e75 Mon Sep 17 00:00:00 2001 From: abn Date: Tue, 2 Sep 2014 14:50:13 +0200 Subject: [PATCH] Various fixes/improvements: - Now use Salome's embedded interpreter for execPythonCommand() - Clean behavior when importing PARAVIS module from SALOME's console --- src/PVGUI/PVGUI_Module.cxx | 119 +++++++++++++++++--------------- src/PVGUI/PVGUI_Module.h | 14 ++-- src/PV_SWIG/no_wrap/pvsimple.py | 6 +- 3 files changed, 73 insertions(+), 66 deletions(-) diff --git a/src/PVGUI/PVGUI_Module.cxx b/src/PVGUI/PVGUI_Module.cxx index f7f179e7..d906110a 100644 --- a/src/PVGUI/PVGUI_Module.cxx +++ b/src/PVGUI/PVGUI_Module.cxx @@ -63,6 +63,7 @@ #include #include #include +#include #include @@ -322,6 +323,7 @@ PVGUI_Module::PVGUI_Module() myOldMsgHandler(0), myTraceWindow(0), myStateCounter(0), + myInitTimer(0), myPushTraceTimer(0) { #ifdef HAS_PV_DOC @@ -351,10 +353,15 @@ PVGUI_Module::~PVGUI_Module() { if (myPushTraceTimer) delete myPushTraceTimer; - //MyCoreApp->prepareForQuit(); - // even if it is static: -// if (MyCoreApp) -// delete MyCoreApp; + if (myInitTimer) + delete myInitTimer; + // Disconnect from server + pqServer* server = pqActiveObjects::instance().activeServer(); + if (server && server->isRemote()) + { + MESSAGE("~PVGUI_Module(): Disconnecting from remote server ..."); + pqServerDisconnectReaction::disconnectFromServer(); + } } PARAVIS_ORB::PARAVIS_Gen_var PVGUI_Module::GetEngine() @@ -516,10 +523,17 @@ void PVGUI_Module::initialize( CAM_Application* app ) SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr(); bool isStop = aResourceMgr->booleanValue( "PARAVIS", "stop_trace", false ); - // start a timer to schedule the trace start asap: if(!isStop) { - startTimer( 0 ); + // Start a timer to schedule asap: + // - the connection to the server + // - the trace start + myInitTimer = new QTimer(aDesktop); + QObject::connect(myInitTimer, SIGNAL(timeout()), this, SLOT(onInitTimer()) ); + myInitTimer->setSingleShot(true); + myInitTimer->start(0); + + // Another timer to regularly push the trace onto the engine: myPushTraceTimer = new QTimer(aDesktop); QObject::connect(myPushTraceTimer, SIGNAL(timeout()), this, SLOT(onPushTraceTimer()) ); myPushTraceTimer->setSingleShot(false); @@ -546,9 +560,9 @@ void PVGUI_Module::initialize( CAM_Application* app ) SIGNAL(representationChanged(pqRepresentation*)), this, SLOT(onRepresentationChanged(pqRepresentation*))); -#ifndef PARAVIS_WITH_FULL_CORBA - connectToExternalPVServer(); -#endif +// MESSAGE("initialize(): Initializing PARAVIS's Python context ..."); +// execPythonCommand("import paraview.servermanager as sm; sm.fromGUI=True", false); +// MESSAGE("initialize(): Initialized."); } bool PVGUI_Module::connectToExternalPVServer() @@ -692,44 +706,55 @@ void PVGUI_Module::execPythonCommand(const QString& cmd, bool inSalomeConsole) } else { - pqPythonManager* manager = qobject_cast - ( pqApplicationCore::instance()->manager( "PYTHON_MANAGER" ) ); - if ( manager ) - { - pqPythonDialog* pyDiag = manager->pythonShellDialog(); - if ( pyDiag ) - { - pqPythonShell* shell = pyDiag->shell(); - if ( shell ) { - shell->executeScript(cmd); - } - } - } + SalomeApp_Application* app = + dynamic_cast< SalomeApp_Application* >(SUIT_Session::session()->activeApplication()); + PyConsole_Interp* pyInterp = app->pythonConsole()->getInterp(); + PyLockWrapper aGil; + pyInterp->run(cmd.toStdString().c_str()); +// pqPythonManager* manager = qobject_cast +// ( pqApplicationCore::instance()->manager( "PYTHON_MANAGER" ) ); +// if ( manager ) +// { +// pqPythonDialog* pyDiag = manager->pythonShellDialog(); +// if ( pyDiag ) +// { +// pqPythonShell* shell = pyDiag->shell(); +// if ( shell ) { +// shell->executeScript(cmd); +// } +// } +// } } } /*! - \brief Launches a tracing of current server + \brief Initialisation timer event - fired only once, after the GUI loop is ready. + See creation in initialize(). */ -void PVGUI_Module::timerEvent(QTimerEvent* te ) +void PVGUI_Module::onInitTimer() { -// connectToExternalPVServer(); +#ifndef PARAVIS_WITH_FULL_CORBA + connectToExternalPVServer(); +#endif #ifndef WNT - if ( PyInterp_Dispatcher::Get()->IsBusy() ) - { - // Reschedule for later - MESSAGE("interpreter busy -> rescheduling trace start."); - startTimer(500); - } - else - { - MESSAGE("about to start trace...."); +// if ( PyInterp_Dispatcher::Get()->IsBusy() ) +// { +// // Reschedule for later +// MESSAGE("interpreter busy -> rescheduling trace start."); +// startTimer(500); +// } +// else +// { + MESSAGE("timerEvent(): About to start trace...."); execPythonCommand("from paraview import smtrace;smtrace.start_trace()", false); - MESSAGE("trace STARTED...."); - } - killTimer( te->timerId() ); + MESSAGE("timerEvent(): Trace STARTED...."); +// } #endif + + MESSAGE("initialize(): Initializing PARAVIS's Python context ..."); + execPythonCommand("import paraview.servermanager as sm; sm.fromGUI=True", false); + MESSAGE("initialize(): Initialized."); } /*! @@ -992,9 +1017,7 @@ bool PVGUI_Module::activateModule( SUIT_Study* study ) */ bool PVGUI_Module::deactivateModule( SUIT_Study* study ) { - // Stop Python trace - MESSAGE("Stopping Python trace ...") - execPythonCommand("from paraview import smtrace;smtrace.stop_trace()", false); + MESSAGE("PARAVIS deactivation ...") QMenu* aMenu = menuMgr()->findMenu( myRecentMenuId ); if(aMenu) { @@ -1058,7 +1081,6 @@ void PVGUI_Module::onApplicationClosed( SUIT_Application* theApp ) pqApplicationCore::instance()->settings()->sync(); int aAppsNb = SUIT_Session::session()->applications().size(); if (aAppsNb == 1) { - deleteTemporaryFiles(); MyCoreApp->deleteLater(); } CAM_Module::onApplicationClosed(theApp); @@ -1251,19 +1273,6 @@ void PVGUI_Module::loadParaviewState(const char* theFileName) pqApplicationCore::instance()->loadState(theFileName, getActiveServer()); } -/*! - \brief Deletes temporary files created during import operation from VISU -*/ -void PVGUI_Module::deleteTemporaryFiles() -{ - foreach(QString aFile, myTemporaryFiles) { - if (QFile::exists(aFile)) { - QFile::remove(aFile); - } - } -} - - /*! \brief Returns current active ParaView server */ @@ -1588,7 +1597,7 @@ void PVGUI_Module::onDelete() void PVGUI_Module::onPushTraceTimer() { - //MESSAGE("Pushing trace ..."); +// MESSAGE("onPushTraceTimer(): Pushing trace to engine..."); GetEngine()->PutPythonTraceStringToEngine(getTraceString().toStdString().c_str()); } diff --git a/src/PVGUI/PVGUI_Module.h b/src/PVGUI/PVGUI_Module.h index 650872b9..5122d8df 100644 --- a/src/PVGUI/PVGUI_Module.h +++ b/src/PVGUI/PVGUI_Module.h @@ -53,8 +53,6 @@ class pqPVApplicationCore; class pqDataRepresentation; class pqRepresentation; -class PyConsole_Interp; - class PVGUI_Module : public SalomeApp_Module { Q_OBJECT @@ -204,10 +202,6 @@ private: //! Discover help project files from the resources. QString getHelpFileName(); - void deleteTemporaryFiles(); - - //QList getParaViewToolbars(); - //! Create actions for ParaViS void createActions(); @@ -278,9 +272,7 @@ public slots: protected slots: virtual void onModelOpened(); virtual void onPushTraceTimer(); - -protected: - void timerEvent(QTimerEvent *event); + virtual void onInitTimer(); private: class pqImplementation; @@ -316,6 +308,10 @@ private: static PARAVIS_ORB::PARAVIS_Gen_var myEngine; + //! Single shot timer used to connect to the PVServer, and start the trace. + QTimer * myInitTimer; + + //! Timer used to regularly push the Python trace to the engine. QTimer * myPushTraceTimer; }; diff --git a/src/PV_SWIG/no_wrap/pvsimple.py b/src/PV_SWIG/no_wrap/pvsimple.py index 4890ed1b..4694bae6 100644 --- a/src/PV_SWIG/no_wrap/pvsimple.py +++ b/src/PV_SWIG/no_wrap/pvsimple.py @@ -24,6 +24,7 @@ is provided by the PARAVIS engine. """ from paraview.simple import * +import paraview.servermanager # local import - see test on 'fromGUI' below import paravis ## Triggers the "FindOrLoadCompo(PARAVIS)" def __my_log(msg): @@ -46,5 +47,6 @@ def SalomeConnectToPVServer(): raise e __my_log("Connected to %s!" % server_url) -# Automatically connect to the right PVServer: -SalomeConnectToPVServer() +# Automatically connect to the right PVServer when not inside SALOME GUI: +if not paraview.servermanager.fromGUI: + SalomeConnectToPVServer() -- 2.39.2