Skip to content

Commit

Permalink
Implement file watching in ScriptEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
samamou committed Jan 6, 2025
1 parent 6ec1674 commit ffa0ab1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include "MultiScriptEditor.hpp"
#include "ScriptWidget.hpp"
#include "score/tools/FileWatch.hpp"

#include <QCodeEditor>
#include <QCoreApplication>
#include <QDialogButtonBox>
#include <QDir>
#include <QFile>
Expand Down Expand Up @@ -176,16 +178,8 @@ void ScriptDialog::openInExternalEditor()
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);
QString tempFile = tempDir + "/ossia_script_temp.js";

QString tempFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
+ "/ossia_script_temp.js";
QFile file(tempFile);
if(!file.open(QIODevice::WriteOnly))
{
Expand All @@ -196,12 +190,40 @@ 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)
{
m_textedit->setPlainText(updatedContent);
}
});
}
});
w.add(tempFile, m_fileHandle);

if(!QProcess::startDetached(editorPath, QStringList() << tempFile))
{
QMessageBox::warning(this, tr("Error"), tr("Failed to launch external editor"));
QMessageBox::warning(this, tr("Error"), tr("failed to launch external editor"));
}
}

void ScriptDialog::stopWatchingFile(const QString& tempFile)
{
if(tempFile.isEmpty())
{
return;
}

auto& w = score::FileWatch::instance();
w.remove(tempFile, m_fileHandle);
}
void MultiScriptDialog::openInExternalEditor()
{
QString editorPath = QSettings{}.value("Skin/DefaultEditor").toString();
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/score-lib-process/Process/Script/ScriptEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ class SCORE_LIB_PROCESS_EXPORT ScriptDialog : public QDialog
void setText(const QString& str);
void setError(int line, const QString& str);
void openInExternalEditor();
void stopWatchingFile(const QString& tempFile);

protected:
virtual void on_accepted() = 0;

const score::DocumentContext& m_context;
QTextEdit* m_textedit{};
QPlainTextEdit* m_error{};

private:
std::shared_ptr<std::function<void()>> m_fileHandle;
};

template <typename Process_T, typename Property_T, typename Spec_T>
Expand Down

0 comments on commit ffa0ab1

Please sign in to comment.