Skip to content

Commit

Permalink
#649 New script feature : Create empty file before opening text editor
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Jul 18, 2016
1 parent 5ec9b9c commit 6e29bfe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@

#include <QAction>
#include <QFileInfo>
#include <QInputDialog>
#include <QLineEdit>
#include <QMessageBox>
#include "RicRefreshScriptsFeature.h"

CAF_CMD_SOURCE_INIT(RicNewScriptFeature, "RicNewScriptFeature");

Expand All @@ -38,8 +41,7 @@ CAF_CMD_SOURCE_INIT(RicNewScriptFeature, "RicNewScriptFeature");
//--------------------------------------------------------------------------------------------------
bool RicNewScriptFeature::isCommandEnabled()
{
std::vector<RimCalcScript*> selection = RicScriptFeatureImpl::selectedScripts();
return selection.size() > 0;
return true;
}

//--------------------------------------------------------------------------------------------------
Expand All @@ -55,7 +57,7 @@ void RicNewScriptFeature::onActionTriggered(bool isChecked)

QString fullPathNewScript;

if (calcScript )
if (calcScript)
{
QFileInfo existingScriptFileInfo(calcScript->absolutePath());
fullPathNewScript = existingScriptFileInfo.absolutePath();
Expand All @@ -79,19 +81,40 @@ void RicNewScriptFeature::onActionTriggered(bool isChecked)
num++;
}

RiaApplication* app = RiaApplication::instance();
QString scriptEditor = app->scriptEditorPath();
if (!scriptEditor.isEmpty())
bool ok;
fullPathFilenameNewScript = QInputDialog::getText(NULL, "Specify new script file", "File name", QLineEdit::Normal, fullPathFilenameNewScript, &ok);

if (ok && !fullPathFilenameNewScript.isEmpty())
{
QStringList arguments;
arguments << fullPathFilenameNewScript;
QFile file(fullPathFilenameNewScript);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(RiuMainWindow::instance(), "Script editor", "Failed to create file\n" + fullPathFilenameNewScript);

return;
}

RicRefreshScriptsFeature::refreshScriptFolders();

QProcess* myProcess = new QProcess(this);
myProcess->start(scriptEditor, arguments);
if (calcScript)
{
RiuMainWindow::instance()->selectAsCurrentItem(calcScript);
}

if (!myProcess->waitForStarted(1000))
RiaApplication* app = RiaApplication::instance();
QString scriptEditor = app->scriptEditorPath();
if (!scriptEditor.isEmpty())
{
QMessageBox::warning(RiuMainWindow::instance(), "Script editor", "Failed to start script editor executable\n" + scriptEditor);
QStringList arguments;
arguments << fullPathFilenameNewScript;

QProcess* myProcess = new QProcess(this);
myProcess->start(scriptEditor, arguments);

if (!myProcess->waitForStarted(1000))
{
QMessageBox::warning(RiuMainWindow::instance(), "Script editor", "Failed to start script editor executable\n" + scriptEditor);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ QStringList RimContextCommandBuilder::commandsFromSelection()
{
commandIds << "RicAddScriptPathFeature";
commandIds << "RicDeleteScriptPathFeature";
commandIds << "RicNewScriptFeature";
commandIds << "Separator";
commandIds << "RicRefreshScriptsFeature";
}
Expand Down

0 comments on commit 6e29bfe

Please sign in to comment.