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 void hideEvent(QHideEvent* event) override;
39
40 const score::DocumentContext& m_context;
41 QTextEdit* m_textedit{};
42 QPlainTextEdit* m_error{};
43
44private:
45 QString m_watchedFile;
46 std::shared_ptr<std::function<void()>> m_fileHandle;
47};
48
49template <typename Process_T, typename Property_T, typename Spec_T>
51{
52public:
54 const Process_T& process, const score::DocumentContext& ctx, QWidget* parent)
55 : ScriptDialog{Spec_T::language, ctx, parent}
56 , m_process{process}
57 {
58 setText((m_process.*Property_T::get)());
59 con(m_process, &Process_T::errorMessage, this, &ProcessScriptEditDialog::setError);
61 &QWidget::deleteLater);
62 con(m_process, Property_T::notify, this, &ProcessScriptEditDialog::setText);
63 }
64
65 void on_accepted() override
66 {
67 this->setError(0, QString{});
68 if(this->text() != (m_process.*Property_T::get)())
69 {
70 // TODO try to see if we can make this a bit more efficient,
71 // by passing the validated / transformed data to the command maybe ?
72 if(m_process.validate(this->text()))
73 {
74 CommandDispatcher<>{m_context.commandStack}.submit(
76 m_process, this->text(), m_context});
77 }
78 }
79 }
80
81protected:
82 const Process_T& m_process;
83
84 void reject() override
85 {
86 const_cast<QWidget*&>(m_process.scriptUI) = nullptr;
87 m_process.scriptUIVisible(false);
88 QDialog::reject();
89 }
90
91 void closeEvent(QCloseEvent* event) override
92 {
93 const_cast<QWidget*&>(m_process.scriptUI) = nullptr;
94 m_process.scriptUIVisible(false);
95 QDialog::closeEvent(event);
96 }
97};
98
99}
The CommandDispatcher class.
Definition CommandDispatcher.hpp:13
void identified_object_destroying(IdentifiedObjectAbstract *o)
To be called by subclasses.
Definition ScriptEditor.hpp:51
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