ResInsight/ApplicationCode/Commands/OctaveScriptCommands/RicNewPythonScriptFeature.cpp

161 lines
5.7 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 "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
#include <QMessageBox>
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()
2015-08-20 03:31:36 -05:00
{
std::vector<RimCalcScript*> calcScripts = RicScriptFeatureImpl::selectedScripts();
std::vector<RimScriptCollection*> calcScriptCollections = RicScriptFeatureImpl::selectedScriptCollections();
if (calcScripts.size() == 1u && calcScripts.front()->scriptType() == RimCalcScript::PYTHON)
{
return true;
}
else if (calcScriptCollections.size() == 1u && !calcScriptCollections.front()->directory().isEmpty())
{
return true;
}
return false;
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();
}
else if (scriptColl)
{
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;
2018-02-18 11:56:43 -06: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
{
QFile file(fullPathFilenameNewScript);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(
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 instance\nresInsight = rips.Instance.find()\n# Example code\nprint(\"ResInsight version:\" + resInsight.app.versionString())\n";
}
RicRefreshScriptsFeature::refreshScriptFolders();
2015-08-20 03:31:36 -05:00
if (calcScript)
{
Riu3DMainWindowTools::selectAsCurrentItem(calcScript);
}
2015-08-20 03:31:36 -05:00
RiaApplication* app = RiaApplication::instance();
QString scriptEditor = app->scriptEditorPath();
if (!scriptEditor.isEmpty())
2015-08-20 03:31:36 -05:00
{
QStringList arguments;
arguments << fullPathFilenameNewScript;
QProcess* myProcess = new QProcess(this);
myProcess->start(scriptEditor, arguments);
if (!myProcess->waitForStarted(1000))
{
QMessageBox::warning(Riu3DMainWindowTools::mainWindowWidget(),
"Script editor",
"Failed to start script editor executable\n" + scriptEditor);
}
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
}