Skip to content

Commit

Permalink
Show button only if default editor is set in settings + small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
samamou committed Jan 8, 2025
1 parent ffa0ab1 commit d4a13e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +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();
void openInExternalEditor(const QString& editorPath);

protected:
virtual void on_accepted() = 0;
Expand Down
42 changes: 23 additions & 19 deletions src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ ScriptDialog::ScriptDialog(
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);
if(auto editorPath = QSettings{}.value("Skin/DefaultEditor").toString();
!editorPath.isEmpty())
{
auto openExternalBtn = new QPushButton{tr("Edit in default editor"), this};
connect(openExternalBtn, &QPushButton::clicked, this, [this, editorPath] {
openInExternalEditor(editorPath);
});
lay->addWidget(openExternalBtn);
}
}

QString ScriptDialog::text() const noexcept
Expand Down Expand Up @@ -118,10 +123,15 @@ MultiScriptDialog::MultiScriptDialog(const score::DocumentContext& ctx, QWidget*
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);
if(auto editorPath = QSettings{}.value("Skin/DefaultEditor").toString();
!editorPath.isEmpty())
{
auto openExternalBtn = new QPushButton{tr("Edit in default editor"), this};
connect(openExternalBtn, &QPushButton::clicked, this, [this, editorPath] {
openInExternalEditor(editorPath);
});
lay->addWidget(openExternalBtn);
}
}

void MultiScriptDialog::addTab(
Expand Down Expand Up @@ -167,9 +177,8 @@ void MultiScriptDialog::clearError()
m_error->clear();
}

void ScriptDialog::openInExternalEditor()
void ScriptDialog::openInExternalEditor(const QString& editorPath)
{
QString editorPath = QSettings{}.value("Skin/DefaultEditor").toString();

if(editorPath.isEmpty())
{
Expand All @@ -178,8 +187,8 @@ void ScriptDialog::openInExternalEditor()
return;
}

QString tempFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
+ "/ossia_script_temp.js";
const QString tempFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
+ "/ossia_script_temp.js";
QFile file(tempFile);
if(!file.open(QIODevice::WriteOnly))
{
Expand All @@ -188,15 +197,13 @@ void ScriptDialog::openInExternalEditor()
}

file.write(this->text().toUtf8());
file.close();

auto& w = score::FileWatch::instance();
m_fileHandle = std::make_shared<std::function<void()>>([this, tempFile]() {
QFile file(tempFile);
if(file.open(QIODevice::ReadOnly))
{
QString updatedContent = QString::fromUtf8(file.readAll());
file.close();

QMetaObject::invokeMethod(m_textedit, [this, updatedContent]() {
if(m_textedit)
Expand All @@ -208,7 +215,7 @@ void ScriptDialog::openInExternalEditor()
});
w.add(tempFile, m_fileHandle);

if(!QProcess::startDetached(editorPath, QStringList() << tempFile))
if(!QProcess::startDetached(editorPath, QStringList{tempFile}))
{
QMessageBox::warning(this, tr("Error"), tr("failed to launch external editor"));
}
Expand All @@ -224,10 +231,8 @@ void ScriptDialog::stopWatchingFile(const QString& tempFile)
auto& w = score::FileWatch::instance();
w.remove(tempFile, m_fileHandle);
}
void MultiScriptDialog::openInExternalEditor()
void MultiScriptDialog::openInExternalEditor(const QString& editorPath)
{
QString editorPath = QSettings{}.value("Skin/DefaultEditor").toString();

if(editorPath.isEmpty())
{
QMessageBox::warning(
Expand Down Expand Up @@ -266,7 +271,6 @@ void MultiScriptDialog::openInExternalEditor()
QString content = textedit->document()->toPlainText();
file.write(content.toUtf8());
}
file.close();

openedFiles.append(tempFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SCORE_LIB_PROCESS_EXPORT ScriptDialog : public QDialog

void setText(const QString& str);
void setError(int line, const QString& str);
void openInExternalEditor();
void openInExternalEditor(const QString& editorPath);
void stopWatchingFile(const QString& tempFile);

protected:
Expand Down

0 comments on commit d4a13e3

Please sign in to comment.