ResInsight/ApplicationLibCode/Commands/ApplicationCommands/RicOpenInTextEditorFeature.cpp

99 lines
3.5 KiB
C++
Raw Normal View History

2023-03-13 08:46:41 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
2023-03-13 08:46:41 -05:00
// 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
// 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.
//
/////////////////////////////////////////////////////////////////////////////////
2023-03-13 08:46:41 -05:00
#include "RicOpenInTextEditorFeature.h"
2023-03-13 08:46:41 -05:00
#include "RiaGuiApplication.h"
#include "RiaLogging.h"
#include "PlotTemplates/RimPlotTemplateFileItem.h"
2023-03-13 08:46:41 -05:00
#include "RimPressureDepthData.h"
2023-03-13 08:46:41 -05:00
#include "Riu3DMainWindowTools.h"
#include "cafSelectionManager.h"
#include <QAction>
2023-03-13 08:46:41 -05:00
CAF_CMD_SOURCE_INIT( RicOpenInTextEditorFeature, "RicOpenInTextEditorFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-03-13 08:46:41 -05:00
void RicOpenInTextEditorFeature::openFileInTextEditor( const QString& filePath, QObject* parent )
{
2023-03-13 08:46:41 -05:00
RiaApplication* app = RiaApplication::instance();
QString textEditorExecutablePath = app->scriptEditorPath();
if ( !textEditorExecutablePath.isEmpty() )
{
QStringList arguments;
arguments << filePath;
auto* myProcess = new QProcess( parent );
myProcess->start( textEditorExecutablePath, arguments );
if ( !myProcess->waitForStarted( 1000 ) )
{
RiaLogging::errorInMessageBox( Riu3DMainWindowTools::mainWindowWidget(),
"Text editor",
"Failed to start text editor\n" + textEditorExecutablePath );
}
}
}
2023-03-13 08:46:41 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicOpenInTextEditorFeature::isCommandEnabled()
{
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-03-13 08:46:41 -05:00
void RicOpenInTextEditorFeature::onActionTriggered( bool isChecked )
{
std::vector<caf::PdmUiItem*> uiItems;
caf::SelectionManager::instance()->selectedItems( uiItems );
2023-03-13 08:46:41 -05:00
QString filePath;
2023-03-13 08:46:41 -05:00
if ( !uiItems.empty() )
{
2023-03-13 08:46:41 -05:00
if ( auto templateFileItem = dynamic_cast<RimPlotTemplateFileItem*>( uiItems.front() ) )
{
2023-03-13 08:46:41 -05:00
filePath = templateFileItem->absoluteFilePath();
}
}
2023-03-13 08:46:41 -05:00
if ( !filePath.isEmpty() )
{
RicOpenInTextEditorFeature::openFileInTextEditor( filePath, this );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-03-13 08:46:41 -05:00
void RicOpenInTextEditorFeature::setupActionLook( QAction* actionToSetup )
{
2023-03-13 08:46:41 -05:00
actionToSetup->setText( "Open in Text Editor" );
actionToSetup->setIcon( QIcon( ":/open-text-editor.svg" ) );
}