2015-08-20 03:31:36 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2019-07-30 04:19:26 -05:00
|
|
|
// Copyright (C) 2019- Equinor ASA
|
2015-08-20 03:31:36 -05:00
|
|
|
// Copyright (C) 2015- Ceetron Solutions AS
|
2019-07-30 04:19:26 -05:00
|
|
|
//
|
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.
|
2019-07-30 04:19:26 -05:00
|
|
|
//
|
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.
|
2019-07-30 04:19:26 -05:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-07-30 04:19:26 -05:00
|
|
|
#include "RicNewPythonScriptFeature.h"
|
2015-08-20 03:31:36 -05:00
|
|
|
|
2016-07-18 08:38:33 -05:00
|
|
|
#include "RiaApplication.h"
|
2020-10-13 04:19:51 -05:00
|
|
|
#include "RiaLogging.h"
|
2022-06-01 23:53:05 -05:00
|
|
|
#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"
|
2015-08-20 04:59:07 -05:00
|
|
|
#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
|
|
|
|
2018-02-27 09:37:06 -06:00
|
|
|
#include "Riu3DMainWindowTools.h"
|
2017-11-29 01:50:48 -06:00
|
|
|
#include "RiuTools.h"
|
2016-07-18 08:38:33 -05:00
|
|
|
|
2017-05-11 02:23:13 -05:00
|
|
|
#include "cafUtils.h"
|
|
|
|
|
2015-08-20 03:31:36 -05:00
|
|
|
#include <QAction>
|
|
|
|
#include <QFileInfo>
|
2016-07-18 08:36:53 -05:00
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QLineEdit>
|
2015-08-20 03:31:36 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
CAF_CMD_SOURCE_INIT( RicNewPythonScriptFeature, "RicNewPythonScriptFeature" );
|
2015-08-20 03:31:36 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-07-30 04:19:26 -05:00
|
|
|
///
|
2015-08-20 03:31:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2023-06-26 07:28:46 -05:00
|
|
|
bool RicNewPythonScriptFeature::isCommandEnabled() const
|
2015-08-20 03:31:36 -05:00
|
|
|
{
|
2019-07-30 04:19:26 -05:00
|
|
|
std::vector<RimScriptCollection*> calcScriptCollections = RicScriptFeatureImpl::selectedScriptCollections();
|
2022-06-07 05:00:57 -05:00
|
|
|
if ( calcScriptCollections.empty() ) return false;
|
|
|
|
return !calcScriptCollections.front()->directory().isEmpty();
|
2015-08-20 03:31:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-07-30 04:19:26 -05:00
|
|
|
///
|
2015-08-20 03:31:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicNewPythonScriptFeature::onActionTriggered( bool isChecked )
|
2015-08-20 03:31:36 -05:00
|
|
|
{
|
2019-07-30 04:19:26 -05:00
|
|
|
std::vector<RimCalcScript*> calcScripts = RicScriptFeatureImpl::selectedScripts();
|
2015-08-20 04:59:07 -05:00
|
|
|
std::vector<RimScriptCollection*> calcScriptCollections = RicScriptFeatureImpl::selectedScriptCollections();
|
2015-08-20 03:31:36 -05:00
|
|
|
|
2019-07-30 04:19:26 -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;
|
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( calcScript )
|
2015-08-20 03:31:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QFileInfo existingScriptFileInfo( calcScript->absoluteFileName() );
|
2015-08-20 03:31:36 -05:00
|
|
|
fullPathNewScript = existingScriptFileInfo.absolutePath();
|
2023-05-12 14:41:34 -05:00
|
|
|
scriptColl = calcScript->firstAncestorOrThisOfTypeAsserted<RimScriptCollection>();
|
2015-08-20 03:31:36 -05:00
|
|
|
}
|
2019-09-06 03:40:57 -05:00
|
|
|
else if ( scriptColl )
|
2015-08-20 03:31:36 -05:00
|
|
|
{
|
|
|
|
fullPathNewScript = scriptColl->directory();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString fullPathFilenameNewScript;
|
|
|
|
|
2019-07-30 04:19:26 -05:00
|
|
|
fullPathFilenameNewScript = fullPathNewScript + "/untitled.py";
|
|
|
|
int num = 1;
|
2019-09-06 03:40:57 -05:00
|
|
|
while ( caf::Utils::fileExists( fullPathFilenameNewScript ) )
|
2015-08-20 03:31:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
fullPathFilenameNewScript = fullPathNewScript + "/untitled" + QString::number( num ) + ".py";
|
2015-08-20 03:31:36 -05:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
2016-07-18 08:36:53 -05:00
|
|
|
bool ok;
|
2019-09-06 03:40:57 -05:00
|
|
|
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
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
QFile file( fullPathFilenameNewScript );
|
|
|
|
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
|
2016-07-18 08:36:53 -05:00
|
|
|
{
|
2020-10-13 04:19:51 -05:00
|
|
|
RiaLogging::errorInMessageBox( Riu3DMainWindowTools::mainWindowWidget(),
|
|
|
|
"Script editor",
|
|
|
|
"Failed to create file\n" + fullPathFilenameNewScript );
|
2016-07-18 08:36:53 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2019-07-30 06:12:19 -05:00
|
|
|
else
|
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
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";
|
2019-07-30 06:12:19 -05:00
|
|
|
}
|
2019-07-30 04:19:26 -05:00
|
|
|
|
2022-06-01 23:53:05 -05:00
|
|
|
scriptColl->readContentFromDisc( RiaPreferences::current()->maxScriptFoldersDepth() );
|
2019-07-30 06:44:47 -05:00
|
|
|
scriptColl->updateConnectedEditors();
|
2015-08-20 03:31:36 -05:00
|
|
|
|
2019-09-06 03:40:57 -05:00
|
|
|
if ( calcScript )
|
2016-07-18 08:36:53 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
Riu3DMainWindowTools::selectAsCurrentItem( calcScript );
|
2016-07-18 08:36:53 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-07-30 04:19:26 -05:00
|
|
|
///
|
2015-08-20 03:31:36 -05:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
2019-09-06 03:40:57 -05:00
|
|
|
void RicNewPythonScriptFeature::setupActionLook( QAction* actionToSetup )
|
2015-08-20 03:31:36 -05:00
|
|
|
{
|
2019-09-06 03:40:57 -05:00
|
|
|
actionToSetup->setText( "New Python Script" );
|
2015-08-20 03:31:36 -05:00
|
|
|
}
|