Skip to content

Commit

Permalink
Add function for opening multiple scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
samamou committed Dec 31, 2024
1 parent 705f297 commit 6ec1674
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
64 changes: 61 additions & 3 deletions src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QCodeEditor>
#include <QDialogButtonBox>
#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QPlainTextEdit>
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -187,14 +193,66 @@ 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))
{
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<QTextEdit*>(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"));
}
}

}

0 comments on commit 6ec1674

Please sign in to comment.