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
This commit is contained in:
Magne Sjaastad 2020-11-04 04:27:05 -05:00 committed by GitHub
parent cfccd6e943
commit 51359a0cda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 77 additions and 59 deletions

View File

@ -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<int>& 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;

View File

@ -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<int> m_currentCaseIds;
std::list<int> m_scriptCaseIds;
int m_currentScriptCaseId;
QString m_currentProgram;
QStringList m_currentArguments;
RiaPreferences* m_preferences;

View File

@ -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;
}
}

View File

@ -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<RimCase*> 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<int> caseIdsInSelection;
for ( size_t i = 0; i < selection.size(); i++ )
{
RimCase* casePtr = selection[i];
caseIdsInSelection.push_back( casePtr->caseId );
std::vector<RimCase*> 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 );
}
}

View File

@ -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" );
}

View File

@ -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<QByteArray>& args, QDataStream& socketStream ) override
{
qint64 caseId = server->currentCaseId();
qint64 caseId = RiaApplication::instance()->currentScriptCaseId();
QString caseName;
QString caseType;
qint64 caseGroupId = -1;

View File

@ -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;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -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
};