From 51359a0cdabf3e8992d3189cdec0e82ad9f938b5 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Wed, 4 Nov 2020 04:27:05 -0500 Subject: [PATCH] Allow Python scripts to use currently selected cases (#6906) * #6891 Python : Make sure python scripts can be launched from context menu * #6891 Python : Make currentScriptCaseId a general scripting feature --- .../Application/RiaApplication.cpp | 23 +++++--- ApplicationCode/Application/RiaApplication.h | 4 +- .../Application/RiaGuiApplication.cpp | 4 +- .../RicExecuteScriptForCasesFeature.cpp | 54 +++++++++++++------ .../GrpcInterface/RiaGrpcProjectService.cpp | 22 +++++--- .../RiaProjectInfoCommands.cpp | 3 +- .../SocketInterface/RiaSocketServer.cpp | 21 +------- .../SocketInterface/RiaSocketServer.h | 5 -- 8 files changed, 77 insertions(+), 59 deletions(-) diff --git a/ApplicationCode/Application/RiaApplication.cpp b/ApplicationCode/Application/RiaApplication.cpp index a78a9abea1..2066c2b950 100644 --- a/ApplicationCode/Application/RiaApplication.cpp +++ b/ApplicationCode/Application/RiaApplication.cpp @@ -155,6 +155,7 @@ RiaApplication::RiaApplication() , m_workerProcess( nullptr ) , m_preferences( nullptr ) , m_runningWorkerProcess( false ) + , m_currentScriptCaseId( -1 ) { CAF_ASSERT( s_riaApplication == nullptr ); s_riaApplication = this; @@ -309,6 +310,14 @@ RimGridView* RiaApplication::activeMainOrComparisonGridView() return viewOrComparisonView; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +int RiaApplication::currentScriptCaseId() const +{ + return m_currentScriptCaseId; +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -1099,17 +1108,17 @@ bool RiaApplication::launchProcess( const QString& program, if ( m_workerProcess == nullptr ) { // If multiple cases are present, pop the first case ID from the list and set as current case - if ( !m_currentCaseIds.empty() ) + if ( !m_scriptCaseIds.empty() ) { - int nextCaseId = m_currentCaseIds.front(); - m_currentCaseIds.pop_front(); + int nextCaseId = m_scriptCaseIds.front(); + m_scriptCaseIds.pop_front(); - m_socketServer->setCurrentCaseId( nextCaseId ); + m_currentScriptCaseId = nextCaseId; } else { // Disable current case concept - m_socketServer->setCurrentCaseId( -1 ); + m_currentScriptCaseId = -1; } m_runningWorkerProcess = true; @@ -1155,8 +1164,8 @@ bool RiaApplication::launchProcessForMultipleCases( const QString& p const std::vector& caseIds, const QProcessEnvironment& processEnvironment ) { - m_currentCaseIds.clear(); - std::copy( caseIds.begin(), caseIds.end(), std::back_inserter( m_currentCaseIds ) ); + m_scriptCaseIds.clear(); + std::copy( caseIds.begin(), caseIds.end(), std::back_inserter( m_scriptCaseIds ) ); m_currentProgram = program; m_currentArguments = arguments; diff --git a/ApplicationCode/Application/RiaApplication.h b/ApplicationCode/Application/RiaApplication.h index 1cc99bb6b4..d90a3e2193 100644 --- a/ApplicationCode/Application/RiaApplication.h +++ b/ApplicationCode/Application/RiaApplication.h @@ -115,6 +115,7 @@ public: const Rim3dView* activeReservoirView() const; RimGridView* activeGridView(); RimGridView* activeMainOrComparisonGridView(); + int currentScriptCaseId() const; RimProject* project(); @@ -250,7 +251,8 @@ protected: #endif // Execute for all settings - std::list m_currentCaseIds; + std::list m_scriptCaseIds; + int m_currentScriptCaseId; QString m_currentProgram; QStringList m_currentArguments; RiaPreferences* m_preferences; diff --git a/ApplicationCode/Application/RiaGuiApplication.cpp b/ApplicationCode/Application/RiaGuiApplication.cpp index 97f78d5db9..1b4284eeaa 100644 --- a/ApplicationCode/Application/RiaGuiApplication.cpp +++ b/ApplicationCode/Application/RiaGuiApplication.cpp @@ -1680,14 +1680,14 @@ void RiaGuiApplication::slotWorkerProcessFinished( int exitCode, QProcess::ExitS } // If multiple cases are present, invoke launchProcess() which will set next current case, and run script on this case - if ( !m_currentCaseIds.empty() ) + if ( !m_scriptCaseIds.empty() ) { launchProcess( m_currentProgram, m_currentArguments, processEnvironment ); } else { // Disable concept of current case - m_socketServer->setCurrentCaseId( -1 ); + m_currentScriptCaseId = -1; m_runningWorkerProcess = false; } } diff --git a/ApplicationCode/Commands/OctaveScriptCommands/RicExecuteScriptForCasesFeature.cpp b/ApplicationCode/Commands/OctaveScriptCommands/RicExecuteScriptForCasesFeature.cpp index 5ff3cb29cc..b0ba4b71d1 100644 --- a/ApplicationCode/Commands/OctaveScriptCommands/RicExecuteScriptForCasesFeature.cpp +++ b/ApplicationCode/Commands/OctaveScriptCommands/RicExecuteScriptForCasesFeature.cpp @@ -20,6 +20,7 @@ #include "RicExecuteScriptForCasesFeature.h" #include "RiaApplication.h" +#include "RiaLogging.h" #include "RimCalcScript.h" #include "RimCase.h" @@ -61,30 +62,49 @@ void RicExecuteScriptForCasesFeature::onActionTriggered( bool isChecked ) RiuMainWindow* mainWindow = RiuMainWindow::instance(); mainWindow->showProcessMonitorDockPanel(); - RiaApplication* app = RiaApplication::instance(); - QString octavePath = app->octavePath(); - if ( !octavePath.isEmpty() ) + RiaApplication* app = RiaApplication::instance(); + + QString pathToScriptExecutable; + QProcessEnvironment processEnvironment; + + if ( scriptAbsolutePath.endsWith( ".py" ) ) { - QStringList arguments = RimCalcScript::createCommandLineArguments( scriptAbsolutePath ); + processEnvironment = app->pythonProcessEnvironment(); + pathToScriptExecutable = app->pythonPath(); - std::vector selection; - caf::SelectionManager::instance()->objectsByType( &selection ); + if ( pathToScriptExecutable.isEmpty() ) + { + RiaLogging::warning( "Path to Python executable is empty, not able to execute script" ); + } + } + else + { + processEnvironment = app->octaveProcessEnvironment(); + pathToScriptExecutable = app->octavePath(); + if ( pathToScriptExecutable.isEmpty() ) + { + RiaLogging::warning( "Path to Octave executable is empty, not able to execute script" ); + } + } - // Get case ID from selected cases in selection model + if ( !pathToScriptExecutable.isEmpty() ) + { + QStringList arguments = RimCalcScript::createCommandLineArguments( scriptAbsolutePath ); std::vector caseIdsInSelection; - for ( size_t i = 0; i < selection.size(); i++ ) { - RimCase* casePtr = selection[i]; - caseIdsInSelection.push_back( casePtr->caseId ); + std::vector selection; + caf::SelectionManager::instance()->objectsByType( &selection ); + + for ( RimCase* rimCase : selection ) + { + caseIdsInSelection.push_back( rimCase->caseId ); + } } - if ( caseIdsInSelection.size() > 0 ) - { - RiaApplication::instance()->launchProcessForMultipleCases( octavePath, - arguments, - caseIdsInSelection, - app->octaveProcessEnvironment() ); - } + RiaApplication::instance()->launchProcessForMultipleCases( pathToScriptExecutable, + arguments, + caseIdsInSelection, + processEnvironment ); } } diff --git a/ApplicationCode/GrpcInterface/RiaGrpcProjectService.cpp b/ApplicationCode/GrpcInterface/RiaGrpcProjectService.cpp index 094664dbb0..40483bf156 100644 --- a/ApplicationCode/GrpcInterface/RiaGrpcProjectService.cpp +++ b/ApplicationCode/GrpcInterface/RiaGrpcProjectService.cpp @@ -29,16 +29,26 @@ using namespace rips; //-------------------------------------------------------------------------------------------------- Status RiaGrpcProjectService::GetCurrentCase( ServerContext* context, const rips::Empty* request, rips::CaseRequest* reply ) { - RimGridView* view = RiaApplication::instance()->activeGridView(); - if ( view ) + int scriptCaseId = RiaApplication::instance()->currentScriptCaseId(); + if ( scriptCaseId != -1 ) { - RimCase* currentCase = view->ownerCase(); - if ( currentCase ) + reply->set_id( scriptCaseId ); + return Status::OK; + } + else + { + RimGridView* view = RiaApplication::instance()->activeGridView(); + if ( view ) { - reply->set_id( currentCase->caseId() ); - return Status::OK; + RimCase* currentCase = view->ownerCase(); + if ( currentCase ) + { + reply->set_id( currentCase->caseId() ); + return Status::OK; + } } } + return Status( grpc::NOT_FOUND, "No current case found" ); } diff --git a/ApplicationCode/SocketInterface/RiaProjectInfoCommands.cpp b/ApplicationCode/SocketInterface/RiaProjectInfoCommands.cpp index 7639099db2..3320af34b5 100644 --- a/ApplicationCode/SocketInterface/RiaProjectInfoCommands.cpp +++ b/ApplicationCode/SocketInterface/RiaProjectInfoCommands.cpp @@ -20,6 +20,7 @@ #include "RiaSocketCommand.h" +#include "RiaApplication.h" #include "RiaSocketServer.h" #include "RiaSocketTools.h" @@ -78,7 +79,7 @@ public: static QString commandName() { return QString( "GetCurrentCase" ); } bool interpretCommand( RiaSocketServer* server, const QList& args, QDataStream& socketStream ) override { - qint64 caseId = server->currentCaseId(); + qint64 caseId = RiaApplication::instance()->currentScriptCaseId(); QString caseName; QString caseType; qint64 caseGroupId = -1; diff --git a/ApplicationCode/SocketInterface/RiaSocketServer.cpp b/ApplicationCode/SocketInterface/RiaSocketServer.cpp index db3f3fe572..6262996fda 100644 --- a/ApplicationCode/SocketInterface/RiaSocketServer.cpp +++ b/ApplicationCode/SocketInterface/RiaSocketServer.cpp @@ -49,10 +49,7 @@ RiaSocketServer::RiaSocketServer( QObject* parent ) , m_currentClient( nullptr ) , m_currentCommandSize( 0 ) , m_currentCommand( nullptr ) - , m_currentCaseId( -1 ) { - // TCP server setup - m_tcpServer = new QTcpServer( this ); m_nextPendingConnectionTimer = new QTimer( this ); @@ -131,7 +128,7 @@ RimEclipseCase* RiaSocketServer::findReservoir( int caseId ) int currCaseId = caseId; if ( caseId < 0 ) { - currCaseId = this->currentCaseId(); + currCaseId = RiaApplication::instance()->currentScriptCaseId(); } if ( currCaseId < 0 ) @@ -282,22 +279,6 @@ void RiaSocketServer::slotReadyRead() } } -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RiaSocketServer::setCurrentCaseId( int caseId ) -{ - m_currentCaseId = caseId; -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -int RiaSocketServer::currentCaseId() const -{ - return m_currentCaseId; -} - //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/SocketInterface/RiaSocketServer.h b/ApplicationCode/SocketInterface/RiaSocketServer.h index bde5373da1..79e6a9c948 100644 --- a/ApplicationCode/SocketInterface/RiaSocketServer.h +++ b/ApplicationCode/SocketInterface/RiaSocketServer.h @@ -57,9 +57,6 @@ public: RimEclipseCase* findReservoir( int caseId ); QTcpSocket* currentClient() { return m_currentClient; } - void setCurrentCaseId( int caseId ); - int currentCaseId() const; - void showErrorMessage( const QString& message ) const; private slots: @@ -81,6 +78,4 @@ private: RiaSocketCommand* m_currentCommand; QTimer* m_nextPendingConnectionTimer; - - int m_currentCaseId; // Set to -1 to use default server behavior };