Add open in text editor feature

This commit is contained in:
Magne Sjaastad
2023-03-13 14:46:41 +01:00
parent 40c329f3ac
commit ab44f9fa17
12 changed files with 76 additions and 105 deletions

View File

@@ -19,6 +19,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicSaveProjectNoGlobalPathsFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicShowClassNamesFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicShowPlotDataCtxFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicOpenInTextEditorFeature.h
)
set(SOURCE_GROUP_SOURCE_FILES
@@ -42,6 +43,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicSaveProjectNoGlobalPathsFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicShowClassNamesFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicShowPlotDataCtxFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicOpenInTextEditorFeature.cpp
)
list(APPEND COMMAND_CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})

View File

@@ -1,6 +1,6 @@
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022 Equinor ASA
// Copyright (C) 2023- 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
@@ -16,68 +16,42 @@
//
/////////////////////////////////////////////////////////////////////////////////
#include "RicEditPlotTemplateFeature.h"
#include "RicOpenInTextEditorFeature.h"
#include "RiaApplication.h"
#include "RiaGuiApplication.h"
#include "RiaLogging.h"
#include "PlotTemplates/RimPlotTemplateFileItem.h"
#include "RimProject.h"
#include "RimPressureDepthData.h"
#include "RiuPlotMainWindow.h"
#include "Riu3DMainWindowTools.h"
#include "cafSelectionManager.h"
#include <QAction>
#include <QFile>
#include <QFileInfo>
#include <QInputDialog>
#include <QMessageBox>
#include <QString>
CAF_CMD_SOURCE_INIT( RicEditPlotTemplateFeature, "RicEditPlotTemplateFeature" );
CAF_CMD_SOURCE_INIT( RicOpenInTextEditorFeature, "RicOpenInTextEditorFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicEditPlotTemplateFeature::isCommandEnabled()
void RicOpenInTextEditorFeature::openFileInTextEditor( const QString& filePath, QObject* parent )
{
std::vector<caf::PdmUiItem*> uiItems;
caf::SelectionManager::instance()->selectedItems( uiItems );
if ( uiItems.size() != 1 ) return false;
RimPlotTemplateFileItem* file = dynamic_cast<RimPlotTemplateFileItem*>( uiItems[0] );
return ( file != nullptr );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicEditPlotTemplateFeature::onActionTriggered( bool isChecked )
{
std::vector<caf::PdmUiItem*> uiItems;
caf::SelectionManager::instance()->selectedItems( uiItems );
if ( uiItems.size() != 1 ) return;
RimPlotTemplateFileItem* file = dynamic_cast<RimPlotTemplateFileItem*>( uiItems[0] );
if ( file == nullptr ) return;
RiaApplication* app = RiaApplication::instance();
QString scriptEditor = app->scriptEditorPath();
if ( !scriptEditor.isEmpty() )
RiaApplication* app = RiaApplication::instance();
QString textEditorExecutablePath = app->scriptEditorPath();
if ( !textEditorExecutablePath.isEmpty() )
{
QStringList arguments;
arguments << file->absoluteFilePath();
arguments << filePath;
QProcess* myProcess = new QProcess( this );
myProcess->start( scriptEditor, arguments );
auto* myProcess = new QProcess( parent );
myProcess->start( textEditorExecutablePath, arguments );
if ( !myProcess->waitForStarted( 1000 ) )
{
RiaLogging::errorInMessageBox( RiuPlotMainWindow::instance(),
RiaLogging::errorInMessageBox( Riu3DMainWindowTools::mainWindowWidget(),
"Text editor",
"Failed to start text editor executable\n" + scriptEditor );
"Failed to start text editor\n" + textEditorExecutablePath );
}
}
}
@@ -85,7 +59,40 @@ void RicEditPlotTemplateFeature::onActionTriggered( bool isChecked )
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicEditPlotTemplateFeature::setupActionLook( QAction* actionToSetup )
bool RicOpenInTextEditorFeature::isCommandEnabled()
{
actionToSetup->setText( "Edit" );
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicOpenInTextEditorFeature::onActionTriggered( bool isChecked )
{
std::vector<caf::PdmUiItem*> uiItems;
caf::SelectionManager::instance()->selectedItems( uiItems );
QString filePath;
if ( !uiItems.empty() )
{
if ( auto templateFileItem = dynamic_cast<RimPlotTemplateFileItem*>( uiItems.front() ) )
{
filePath = templateFileItem->absoluteFilePath();
}
}
if ( !filePath.isEmpty() )
{
RicOpenInTextEditorFeature::openFileInTextEditor( filePath, this );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicOpenInTextEditorFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Open in Text Editor" );
actionToSetup->setIcon( QIcon( ":/open-text-editor.svg" ) );
}

View File

@@ -1,6 +1,6 @@
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022 Equinor ASA
// Copyright (C) 2023- 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
@@ -23,10 +23,12 @@
//==================================================================================================
///
//==================================================================================================
class RicEditPlotTemplateFeature : public caf::CmdFeature
class RicOpenInTextEditorFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;
static void openFileInTextEditor( const QString& filePath, QObject* parent );
protected:
bool isCommandEnabled() override;
void onActionTriggered( bool isChecked ) override;

View File

@@ -19,6 +19,7 @@
#include "RicEditScriptFeature.h"
#include "ApplicationCommands/RicOpenInTextEditorFeature.h"
#include "RicScriptFeatureImpl.h"
#include "RiaApplication.h"
@@ -40,7 +41,7 @@ CAF_CMD_SOURCE_INIT( RicEditScriptFeature, "RicEditScriptFeature" );
bool RicEditScriptFeature::isCommandEnabled()
{
std::vector<RimCalcScript*> selection = RicScriptFeatureImpl::selectedScripts();
return selection.size() > 0;
return !selection.empty();
}
//--------------------------------------------------------------------------------------------------
@@ -49,28 +50,11 @@ bool RicEditScriptFeature::isCommandEnabled()
void RicEditScriptFeature::onActionTriggered( bool isChecked )
{
std::vector<RimCalcScript*> selection = RicScriptFeatureImpl::selectedScripts();
CVF_ASSERT( selection.size() > 0 );
CVF_ASSERT( !selection.empty() );
RimCalcScript* calcScript = selection[0];
RiaApplication* app = RiaApplication::instance();
QString scriptEditor = app->scriptEditorPath();
if ( !scriptEditor.isEmpty() )
{
QStringList arguments;
arguments << calcScript->absoluteFileName;
QProcess* myProcess = new QProcess( this );
myProcess->setProcessEnvironment( app->pythonProcessEnvironment() );
myProcess->start( scriptEditor, arguments );
if ( !myProcess->waitForStarted( 1000 ) )
{
RiaLogging::errorInMessageBox( Riu3DMainWindowTools::mainWindowWidget(),
"Script editor",
"Failed to start script editor executable\n" + scriptEditor );
}
}
RicOpenInTextEditorFeature::openFileInTextEditor( calcScript->absoluteFileName, this );
}
//--------------------------------------------------------------------------------------------------

View File

@@ -22,6 +22,7 @@
#include "RiaApplication.h"
#include "RiaLogging.h"
#include "ApplicationCommands/RicOpenInTextEditorFeature.h"
#include "RicRefreshScriptsFeature.h"
#include "RicScriptFeatureImpl.h"
@@ -115,23 +116,7 @@ void RicNewOctaveScriptFeature::onActionTriggered( bool isChecked )
Riu3DMainWindowTools::selectAsCurrentItem( calcScript );
}
RiaApplication* app = RiaApplication::instance();
QString scriptEditor = app->scriptEditorPath();
if ( !scriptEditor.isEmpty() )
{
QStringList arguments;
arguments << fullPathFilenameNewScript;
QProcess* myProcess = new QProcess( this );
myProcess->start( scriptEditor, arguments );
if ( !myProcess->waitForStarted( 1000 ) )
{
RiaLogging::errorInMessageBox( Riu3DMainWindowTools::mainWindowWidget(),
"Script editor",
"Failed to start script editor executable\n" + scriptEditor );
}
}
RicOpenInTextEditorFeature::openFileInTextEditor( fullPathFilenameNewScript, this );
}
}

View File

@@ -23,6 +23,7 @@
#include "RiaLogging.h"
#include "RiaPreferences.h"
#include "ApplicationCommands/RicOpenInTextEditorFeature.h"
#include "RicRefreshScriptsFeature.h"
#include "RicScriptFeatureImpl.h"
@@ -125,23 +126,7 @@ void RicNewPythonScriptFeature::onActionTriggered( bool isChecked )
Riu3DMainWindowTools::selectAsCurrentItem( calcScript );
}
RiaApplication* app = RiaApplication::instance();
QString scriptEditor = app->scriptEditorPath();
if ( !scriptEditor.isEmpty() )
{
QStringList arguments;
arguments << fullPathFilenameNewScript;
QProcess* myProcess = new QProcess( this );
myProcess->start( scriptEditor, arguments );
if ( !myProcess->waitForStarted( 1000 ) )
{
RiaLogging::errorInMessageBox( Riu3DMainWindowTools::mainWindowWidget(),
"Script editor",
"Failed to start script editor executable\n" + scriptEditor );
}
}
RicOpenInTextEditorFeature::openFileInTextEditor( fullPathFilenameNewScript, this );
}
}

View File

@@ -7,7 +7,6 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicSaveMultiPlotTemplateFeatureSettings.h
${CMAKE_CURRENT_LIST_DIR}/RicCreateMultiPlotFromSelectionFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicRenamePlotTemplateFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicEditPlotTemplateFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicDeletePlotTemplateFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicSetAsDefaultTemplateFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicCreateNewPlotFromTemplateFeature.h
@@ -23,7 +22,6 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicSaveMultiPlotTemplateFeatureSettings.cpp
${CMAKE_CURRENT_LIST_DIR}/RicCreateMultiPlotFromSelectionFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicRenamePlotTemplateFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicEditPlotTemplateFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicDeletePlotTemplateFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicSetAsDefaultTemplateFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicCreateNewPlotFromTemplateFeature.cpp