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

Merged
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
147 changes: 147 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,20 @@
#include "MultiScriptEditor.hpp"
#include "ScriptWidget.hpp"

#include <score/tools/FileWatch.hpp>
#include <score/widgets/SetIcons.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 @@ -36,6 +45,22 @@ ScriptDialog::ScriptDialog(
lay->setStretch(1, 1);
auto bbox = new QDialogButtonBox{
QDialogButtonBox::Ok | QDialogButtonBox::Reset | QDialogButtonBox::Close, this};
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);
});
bbox->addButton(openExternalBtn, QDialogButtonBox::HelpRole);
openExternalBtn->setToolTip(
tr("Edit in the default editor set in Score Settings > User Interface"));
auto icon = makeIcons(
QStringLiteral(":/icons/undock_on.png"),
QStringLiteral(":/icons/undock_off.png"),
QStringLiteral(":/icons/undock_off.png"));
openExternalBtn->setIcon(icon);
}
bbox->button(QDialogButtonBox::Ok)->setText(tr("Compile"));
bbox->button(QDialogButtonBox::Reset)->setText(tr("Clear log"));
connect(bbox->button(QDialogButtonBox::Reset), &QPushButton::clicked, this, [this] {
Expand Down Expand Up @@ -105,6 +130,23 @@ 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);
});
bbox->addButton(openExternalBtn, QDialogButtonBox::HelpRole);
openExternalBtn->setToolTip(
tr("Edit in the default editor set in Score Settings > User Interface"));
auto icon = makeIcons(
QStringLiteral(":/icons/undock_on.png"),
QStringLiteral(":/icons/undock_off.png"),
QStringLiteral(":/icons/undock_off.png"));
openExternalBtn->setIcon(icon);
}
}

void MultiScriptDialog::addTab(
Expand Down Expand Up @@ -150,4 +192,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"));
}
}

}
samamou marked this conversation as resolved.
Show resolved Hide resolved
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 Expand Up @@ -71,7 +76,9 @@ class ProcessScriptEditDialog : public ScriptDialog

protected:
const Process_T& m_process;
void closeEvent(QCloseEvent* event) override
void closeEvent(QCloseEvent* event) override { reject(); }

void reject() override
{
const_cast<QWidget*&>(m_process.externalUI) = nullptr;
m_process.externalUIVisible(false);
Expand Down
Loading