ResInsight/ApplicationLibCode/Commands/OctaveScriptCommands/RicNewPythonScriptFeature.cpp

140 lines
5.3 KiB
C++
Raw Normal View History

2015-08-20 03:31:36 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- Equinor ASA
2015-08-20 03:31:36 -05:00
// Copyright (C) 2015- Ceetron Solutions AS
//
2015-08-20 03:31:36 -05:00
// 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.
//
2015-08-20 03:31:36 -05:00
// 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>
2015-08-20 03:31:36 -05:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RicNewPythonScriptFeature.h"
2015-08-20 03:31:36 -05:00
2016-07-18 08:38:33 -05:00
#include "RiaApplication.h"
#include "RiaLogging.h"
#include "RiaPreferences.h"
2016-07-18 08:38:33 -05:00
2023-03-13 08:46:41 -05:00
#include "ApplicationCommands/RicOpenInTextEditorFeature.h"
2016-07-18 08:38:33 -05:00
#include "RicRefreshScriptsFeature.h"
#include "RicScriptFeatureImpl.h"
2015-08-20 03:31:36 -05:00
#include "RimCalcScript.h"
#include "RimScriptCollection.h"
2016-07-18 08:38:33 -05:00
#include "Riu3DMainWindowTools.h"
#include "RiuTools.h"
2016-07-18 08:38:33 -05:00
#include "cafUtils.h"
2015-08-20 03:31:36 -05:00
#include <QAction>
#include <QFileInfo>
#include <QInputDialog>
#include <QLineEdit>
2015-08-20 03:31:36 -05:00
CAF_CMD_SOURCE_INIT( RicNewPythonScriptFeature, "RicNewPythonScriptFeature" );
2015-08-20 03:31:36 -05:00
//--------------------------------------------------------------------------------------------------
///
2015-08-20 03:31:36 -05:00
//--------------------------------------------------------------------------------------------------
bool RicNewPythonScriptFeature::isCommandEnabled() const
2015-08-20 03:31:36 -05:00
{
std::vector<RimScriptCollection*> calcScriptCollections = RicScriptFeatureImpl::selectedScriptCollections();
if ( calcScriptCollections.empty() ) return false;
return !calcScriptCollections.front()->directory().isEmpty();
2015-08-20 03:31:36 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2015-08-20 03:31:36 -05:00
//--------------------------------------------------------------------------------------------------
void RicNewPythonScriptFeature::onActionTriggered( bool isChecked )
2015-08-20 03:31:36 -05:00
{
std::vector<RimCalcScript*> calcScripts = RicScriptFeatureImpl::selectedScripts();
std::vector<RimScriptCollection*> calcScriptCollections = RicScriptFeatureImpl::selectedScriptCollections();
2015-08-20 03:31:36 -05:00
RimCalcScript* calcScript = calcScripts.size() > 0 ? calcScripts[0] : nullptr;
2018-10-10 03:17:07 -05:00
RimScriptCollection* scriptColl = calcScriptCollections.size() > 0 ? calcScriptCollections[0] : nullptr;
2015-08-20 03:31:36 -05:00
QString fullPathNewScript;
if ( calcScript )
2015-08-20 03:31:36 -05:00
{
QFileInfo existingScriptFileInfo( calcScript->absoluteFileName() );
2015-08-20 03:31:36 -05:00
fullPathNewScript = existingScriptFileInfo.absolutePath();
scriptColl = calcScript->firstAncestorOrThisOfTypeAsserted<RimScriptCollection>();
2015-08-20 03:31:36 -05:00
}
else if ( scriptColl )
2015-08-20 03:31:36 -05:00
{
fullPathNewScript = scriptColl->directory();
}
else
{
return;
}
QString fullPathFilenameNewScript;
fullPathFilenameNewScript = fullPathNewScript + "/untitled.py";
int num = 1;
while ( caf::Utils::fileExists( fullPathFilenameNewScript ) )
2015-08-20 03:31:36 -05:00
{
fullPathFilenameNewScript = fullPathNewScript + "/untitled" + QString::number( num ) + ".py";
2015-08-20 03:31:36 -05:00
num++;
}
bool ok;
fullPathFilenameNewScript = QInputDialog::getText( nullptr,
"Specify new script file",
"File name",
QLineEdit::Normal,
fullPathFilenameNewScript,
&ok,
RiuTools::defaultDialogFlags() );
if ( ok && !fullPathFilenameNewScript.isEmpty() )
2015-08-20 03:31:36 -05:00
{
QFile file( fullPathFilenameNewScript );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
RiaLogging::errorInMessageBox( Riu3DMainWindowTools::mainWindowWidget(),
"Script editor",
"Failed to create file\n" + fullPathFilenameNewScript );
return;
}
else
{
QTextStream stream( &file );
stream << "# Load ResInsight Processing Server Client Library\nimport rips\n# Connect to ResInsight "
2019-10-15 09:03:42 -05:00
"instance\nresinsight = rips.Instance.find()\n# Example code\nprint(\"ResInsight version: \" + "
"resinsight.version_string())\n";
}
scriptColl->readContentFromDisc( RiaPreferences::current()->maxScriptFoldersDepth() );
scriptColl->updateConnectedEditors();
2015-08-20 03:31:36 -05:00
if ( calcScript )
{
Riu3DMainWindowTools::selectAsCurrentItem( calcScript );
}
2015-08-20 03:31:36 -05:00
2023-03-13 08:46:41 -05:00
RicOpenInTextEditorFeature::openFileInTextEditor( fullPathFilenameNewScript, this );
2015-08-20 03:31:36 -05:00
}
}
//--------------------------------------------------------------------------------------------------
///
2015-08-20 03:31:36 -05:00
//--------------------------------------------------------------------------------------------------
void RicNewPythonScriptFeature::setupActionLook( QAction* actionToSetup )
2015-08-20 03:31:36 -05:00
{
actionToSetup->setText( "New Python Script" );
2015-08-20 03:31:36 -05:00
}