Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open code in external editor #1647

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(const QString& editorPath);

protected:
virtual void on_accepted() = 0;
Expand Down
133 changes: 133 additions & 0 deletions src/plugins/score-lib-process/Process/Script/ScriptEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
#include "MultiScriptEditor.hpp"
#include "ScriptWidget.hpp"

#include <score/tools/FileWatch.hpp>

#include <QCodeEditor>
#include <QCoreApplication>
#include <QDialogButtonBox>
#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QProcess>
#include <QPushButton>
#include <QSettings>
#include <QStandardPaths>
#include <QTabWidget>
#include <QVBoxLayout>

Expand Down Expand Up @@ -50,6 +58,16 @@ ScriptDialog::ScriptDialog(
connect(
bbox->button(QDialogButtonBox::Close), &QPushButton::clicked, this,
&QDialog::close);

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 @@ -105,6 +123,16 @@ MultiScriptDialog::MultiScriptDialog(const score::DocumentContext& ctx, QWidget*
connect(
bbox->button(QDialogButtonBox::Close), &QPushButton::clicked, this,
&QDialog::close);

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 @@ -150,4 +178,109 @@ void MultiScriptDialog::clearError()
m_error->clear();
}

void ScriptDialog::openInExternalEditor(const QString& editorPath)
{

if(editorPath.isEmpty())
samamou marked this conversation as resolved.
Show resolved Hide resolved
{
QMessageBox::warning(
this, tr("Error"), tr("no 'Default editor' configured in score settings"));
return;
}

const QString tempFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation)
+ "/ossia_script_temp.js";
QFile file(tempFile);
if(!file.open(QIODevice::WriteOnly))
{
QMessageBox::warning(this, tr("Error"), tr("failed to create temporary file."));
return;
}

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

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());

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"));
}
}

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

auto& w = score::FileWatch::instance();
w.remove(tempFile, m_fileHandle);
samamou marked this conversation as resolved.
Show resolved Hide resolved
m_fileHandle.reset();
}
void MultiScriptDialog::openInExternalEditor(const QString& editorPath)
{
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());
}

openedFiles.append(tempFile);
}

if(!openedFiles.isEmpty() && !QProcess::startDetached(editorPath, openedFiles))
{
QMessageBox::warning(this, tr("Error"), tr("Failed to launch external editor"));
}
}

}
5 changes: 5 additions & 0 deletions src/plugins/score-lib-process/Process/Script/ScriptEditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ class SCORE_LIB_PROCESS_EXPORT ScriptDialog : public QDialog

void setText(const QString& str);
void setError(int line, const QString& str);
void openInExternalEditor(const QString& editorPath);
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
Loading