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);
26
27 QSize sizeHint() const override { return {800, 300}; }
28 QString text() const noexcept;
29
30 void setText(const QString& str);
31 void setError(int line, const QString& str);
32 void openInExternalEditor(const QString& editorPath);
33 void stopWatchingFile();
34
35protected:
36 virtual void on_accepted() = 0;
37
38 const score::DocumentContext& m_context;
39 QTextEdit* m_textedit{};
40 QPlainTextEdit* m_error{};
41
42private:
43 QString m_watchedFile;
44 std::shared_ptr<std::function<void()>> m_fileHandle;
45};
46
47template <typename Process_T, typename Property_T, typename Spec_T>
49{
50public:
52 const Process_T& process, const score::DocumentContext& ctx, QWidget* parent)
53 : ScriptDialog{Spec_T::language, ctx, parent}
54 , m_process{process}
55 {
56 setText((m_process.*Property_T::get)());
57 con(m_process, &Process_T::errorMessage, this, &ProcessScriptEditDialog::setError);
59 &QWidget::deleteLater);
60 con(m_process, Property_T::notify, this, &ProcessScriptEditDialog::setText);
61 }
62
63 void on_accepted() override
64 {
65 this->setError(0, QString{});
66 if(this->text() != (m_process.*Property_T::get)())
67 {
68 // TODO try to see if we can make this a bit more efficient,
69 // by passing the validated / transformed data to the command maybe ?
70 if(m_process.validate(this->text()))
71 {
72 CommandDispatcher<>{m_context.commandStack}.submit(
74 m_process, this->text(), m_context});
75 }
76 }
77 }
78
79protected:
80 const Process_T& m_process;
81 void closeEvent(QCloseEvent* event) override
82 {
83 const_cast<QWidget*&>(m_process.externalUI) = nullptr;
84 m_process.externalUIVisible(false);
85 }
86};
87
88}
The CommandDispatcher class.
Definition CommandDispatcher.hpp:13
void identified_object_destroying(IdentifiedObjectAbstract *o)
To be called by subclasses.
Definition ScriptEditor.hpp:49
Definition ScriptEditor.hpp:21
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1115
Definition DocumentContext.hpp:18
Definition PropertyCommand.hpp:112