#7289 Script : Add execute of last used script

This commit is contained in:
Magne Sjaastad 2021-01-26 10:23:17 +01:00
parent d5b24f180e
commit 4866794f2b
7 changed files with 172 additions and 9 deletions

View File

@ -4,6 +4,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicAddScriptPathFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicDeleteScriptPathFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicEditScriptFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicExecuteScriptFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicExecuteLastUsedScriptFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicExecuteScriptForCasesFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicNewOctaveScriptFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicNewPythonScriptFeature.h
@ -15,6 +16,7 @@ set (SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicAddScriptPathFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicDeleteScriptPathFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicEditScriptFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExecuteLastUsedScriptFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExecuteScriptFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicExecuteScriptForCasesFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicNewOctaveScriptFeature.cpp

View File

@ -0,0 +1,98 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021- Equinor ASA
//
// ResInsight 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.
//
// ResInsight 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 at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RicExecuteLastUsedScriptFeature.h"
#include "RiaApplication.h"
#include "RicExecuteScriptFeature.h"
#include "RimCalcScript.h"
#include "RimProject.h"
#include "RimScriptCollection.h"
#include "cafCmdFeatureManager.h"
#include <QAction>
#include <QFileInfo>
#include <QSettings>
CAF_CMD_SOURCE_INIT( RicExecuteLastUsedScriptFeature, "RicExecuteLastUsedScriptFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RicExecuteLastUsedScriptFeature::lastUsedScriptPathKey()
{
return "lastUsedScriptPath";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicExecuteLastUsedScriptFeature::isCommandEnabled()
{
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExecuteLastUsedScriptFeature::onActionTriggered( bool isChecked )
{
QSettings settings;
QString lastUsedScript = settings.value( lastUsedScriptPathKey() ).toString();
if ( !lastUsedScript.isEmpty() )
{
RimScriptCollection* rootScriptCollection = RiaApplication::instance()->project()->scriptCollection();
std::vector<RimCalcScript*> scripts;
rootScriptCollection->descendantsIncludingThisOfType( scripts );
for ( auto c : scripts )
{
if ( c->absoluteFileName() == lastUsedScript )
{
RicExecuteScriptFeature::executeScript( c );
return;
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExecuteLastUsedScriptFeature::setupActionLook( QAction* actionToSetup )
{
QString actionText = "Execute Last Used Script";
QSettings settings;
QString lastUsedScript = settings.value( lastUsedScriptPathKey() ).toString();
if ( !lastUsedScript.isEmpty() )
{
QFileInfo fi( lastUsedScript );
QString fileName = fi.fileName();
if ( !fileName.isEmpty() )
{
actionText = "Execute " + fileName;
}
}
actionToSetup->setText( actionText );
}

View File

@ -0,0 +1,38 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021- Equinor ASA
//
// ResInsight 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.
//
// ResInsight 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 at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafCmdFeature.h"
#include <vector>
//==================================================================================================
///
//==================================================================================================
class RicExecuteLastUsedScriptFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;
static QString lastUsedScriptPathKey();
protected:
bool isCommandEnabled() override;
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
};

View File

@ -19,6 +19,7 @@
#include "RicExecuteScriptFeature.h"
#include "RicExecuteLastUsedScriptFeature.h"
#include "RicScriptFeatureImpl.h"
#include "RiaApplication.h"
@ -33,7 +34,9 @@
#include <QAction>
#include <QFileInfo>
#include <QSettings>
#include "cafCmdFeatureManager.h"
#include <iostream>
CAF_CMD_SOURCE_INIT( RicExecuteScriptFeature, "RicExecuteScriptFeature" );
@ -55,11 +58,25 @@ void RicExecuteScriptFeature::onActionTriggered( bool isChecked )
std::vector<RimCalcScript*> selection = RicScriptFeatureImpl::selectedScripts();
CVF_ASSERT( selection.size() > 0 );
executeScript( selection[0] );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExecuteScriptFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Execute" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExecuteScriptFeature::executeScript( RimCalcScript* calcScript )
{
RiuMainWindow* mainWindow = RiuMainWindow::instance();
mainWindow->showProcessMonitorDockPanel();
RimCalcScript* calcScript = selection[0];
RiaApplication* app = RiaApplication::instance();
if ( calcScript->scriptType() == RimCalcScript::OCTAVE )
{
@ -107,12 +124,13 @@ void RicExecuteScriptFeature::onActionTriggered( bool isChecked )
RiaApplication::instance()->launchProcess( pythonPath, arguments, penv );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExecuteScriptFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Execute" );
if ( !calcScript->absoluteFileName().isEmpty() )
{
QSettings settings;
settings.setValue( RicExecuteLastUsedScriptFeature::lastUsedScriptPathKey(), calcScript->absoluteFileName() );
auto cmdFeature = caf::CmdFeatureManager::instance()->getCommandFeature( "RicExecuteLastUsedScriptFeature" );
cmdFeature->action(); // Retrieve the action to update the looks
}
}

View File

@ -23,6 +23,8 @@
#include <vector>
class RimCalcScript;
//==================================================================================================
///
//==================================================================================================
@ -30,6 +32,8 @@ class RicExecuteScriptFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;
static void executeScript( RimCalcScript* calcScript );
protected:
// Overrides
bool isCommandEnabled() override;

View File

@ -490,6 +490,7 @@ caf::CmdFeatureMenuBuilder RimContextCommandBuilder::commandsFromSelection()
menuBuilder << "Separator";
menuBuilder << "RicAddScriptPathFeature";
menuBuilder << "RicRefreshScriptsFeature";
menuBuilder << "RicExecuteLastUsedScriptFeature";
menuBuilder << "Separator";
menuBuilder << "RicDeleteScriptPathFeature";
}

View File

@ -531,6 +531,7 @@ void RiuMainWindow::createMenus()
testMenu->addAction( cmdFeatureMgr->action( "RicRunCommandFileFeature" ) );
testMenu->addAction( cmdFeatureMgr->action( "RicExportObjectAndFieldKeywordsFeature" ) );
testMenu->addAction( cmdFeatureMgr->action( "RicSaveProjectNoGlobalPathsFeature" ) );
testMenu->addAction( cmdFeatureMgr->action( "RicExecuteLastUsedScriptFeature" ) );
testMenu->addSeparator();
@ -674,6 +675,7 @@ void RiuMainWindow::createToolBars()
toolbar->addAction( cmdFeatureMgr->action( "RicLaunchUnitTestsFeature" ) );
toolbar->addAction( cmdFeatureMgr->action( "RicLaunchRegressionTestsFeature" ) );
toolbar->addAction( cmdFeatureMgr->action( "RicRunCommandFileFeature" ) );
toolbar->addAction( cmdFeatureMgr->action( "RicExecuteLastUsedScriptFeature" ) );
}
// Create animation toolbar