diff --git a/src/plugins/score-lib-process/Process/Script/MultiScriptEditor.hpp b/src/plugins/score-lib-process/Process/Script/MultiScriptEditor.hpp index 52aac03382..9523d02f49 100644 --- a/src/plugins/score-lib-process/Process/Script/MultiScriptEditor.hpp +++ b/src/plugins/score-lib-process/Process/Script/MultiScriptEditor.hpp @@ -27,6 +27,7 @@ class SCORE_LIB_PROCESS_EXPORT MultiScriptDialog : public QDialog void setText(int idx, const QString& str); void setError(const QString& str); void clearError(); + void openInExternalEditor(); protected: virtual void on_accepted() = 0; diff --git a/src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp b/src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp index f36c01b992..c05a75f091 100644 --- a/src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp +++ b/src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -114,6 +115,11 @@ MultiScriptDialog::MultiScriptDialog(const score::DocumentContext& ctx, QWidget* connect( bbox->button(QDialogButtonBox::Close), &QPushButton::clicked, this, &QDialog::close); + + auto openExternalBtn = new QPushButton{tr("Open in external editor.."), this}; + connect( + openExternalBtn, &QPushButton::clicked, this, [this] { openInExternalEditor(); }); + lay->addWidget(openExternalBtn); } void MultiScriptDialog::addTab( @@ -187,9 +193,7 @@ void ScriptDialog::openInExternalEditor() return; } - QTextStream stream(&file); - QString scriptContent = this->text(); - stream << scriptContent; + file.write(this->text().toUtf8()); file.close(); if(!QProcess::startDetached(editorPath, QStringList() << tempFile)) @@ -197,4 +201,58 @@ void ScriptDialog::openInExternalEditor() QMessageBox::warning(this, tr("Error"), tr("Failed to launch external editor")); } } + +void MultiScriptDialog::openInExternalEditor() +{ + QString editorPath = QSettings{}.value("Skin/DefaultEditor").toString(); + + if(editorPath.isEmpty()) + { + QMessageBox::warning( + this, tr("Error"), tr("no 'Default editor' configured in score settings")); + return; + } + + if(!QFile::exists(editorPath)) + { + QMessageBox::warning( + this, tr("Error"), tr("the configured external editor does not exist.")); + return; + } + + QString tempDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation); + QDir dir(tempDir); + QStringList openedFiles; + + for(int i = 0; i < m_tabs->count(); ++i) + { + QString tabName = m_tabs->tabText(i); + QString tempFile = tempDir + "/" + tabName + ".js"; + + QFile file(tempFile); + if(!file.open(QIODevice::WriteOnly)) + { + QMessageBox::warning(this, tr("Error"), tr("failed to create temporary files.")); + return; + } + + QWidget* widget = m_tabs->widget(i); + QTextEdit* textedit = qobject_cast(widget); + + if(textedit) + { + QString content = textedit->document()->toPlainText(); + file.write(content.toUtf8()); + } + file.close(); + + openedFiles.append(tempFile); + } + + if(!openedFiles.isEmpty() && !QProcess::startDetached(editorPath, openedFiles)) + { + QMessageBox::warning(this, tr("Error"), tr("Failed to launch external editor")); + } +} + }