Loading...
Searching...
No Matches
ScriptEditor.hpp
1#pragma once
2
3#include <score/command/Dispatchers/CommandDispatcher.hpp>
4#include <score/command/PropertyCommand.hpp>
5#include <score/document/DocumentContext.hpp>
6#include <score/tools/Bind.hpp>
7
8#include <QDialog>
9
10#include <score_lib_process_export.h>
11
12#include <string_view>
13
14class QPlainTextEdit;
15class QTextEdit;
16class QTabWidget;
17namespace Process
18{
19class ProcessModel;
20class SCORE_LIB_PROCESS_EXPORT ScriptDialog : public QDialog
21{
22public:
24 const std::string_view lang, const score::DocumentContext& ctx, QWidget* parent);
25
26 QSize sizeHint() const override { return {800, 300}; }
27 QString text() const noexcept;
28
29 void setText(const QString& str);
30 void setError(int line, const QString& str);
31 void openInExternalEditor(const QString& editorPath);
32 void stopWatchingFile(const QString& tempFile);
33
34protected:
35 virtual void on_accepted() = 0;
36
37 const score::DocumentContext& m_context;
38 QTextEdit* m_textedit{};
39 QPlainTextEdit* m_error{};
40
41private:
42 std::shared_ptr<std::function<void()>> m_fileHandle;
43};
44
45template <typename Process_T, typename Property_T, typename Spec_T>
47{
48public:
50 const Process_T& process, const score::DocumentContext& ctx, QWidget* parent)
51 : ScriptDialog{Spec_T::language, ctx, parent}
52 , m_process{process}
53 {
54 setText((m_process.*Property_T::get)());
55 con(m_process, &Process_T::errorMessage, this, &ProcessScriptEditDialog::setError);
57 &QWidget::deleteLater);
58 con(m_process, Property_T::notify, this, &ProcessScriptEditDialog::setText);
59 }
60
61 void on_accepted() override
62 {
63 this->setError(0, QString{});
64 if(this->text() != (m_process.*Property_T::get)())
65 {
66 // TODO try to see if we can make this a bit more efficient,
67 // by passing the validated / transformed data to the command maybe ?
68 if(m_process.validate(this->text()))
69 {
70 CommandDispatcher<>{m_context.commandStack}.submit(
72 m_process, this->text(), m_context});
73 }
74 }
75 }
76
77protected:
78 const Process_T& m_process;
79 void closeEvent(QCloseEvent* event) override
80 {
81 const_cast<QWidget*&>(m_process.externalUI) = nullptr;
82 m_process.externalUIVisible(false);
83 }
84};
85
86}
The CommandDispatcher class.
Definition CommandDispatcher.hpp:13
void identified_object_destroying(IdentifiedObjectAbstract *o)
To be called by subclasses.
Definition ScriptEditor.hpp:47
Definition ScriptEditor.hpp:21
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1324
Definition DocumentContext.hpp:18
Definition PropertyCommand.hpp:112