Loading...
Searching...
No Matches
MultiScriptEditor.hpp
1#pragma once
2#include <score/command/Dispatchers/CommandDispatcher.hpp>
3#include <score/command/PropertyCommand.hpp>
4#include <score/document/DocumentContext.hpp>
5#include <score/tools/Bind.hpp>
6
7#include <QDialog>
8
9#include <score_lib_process_export.h>
10
11class QPlainTextEdit;
12class QTextEdit;
13class QTabWidget;
14
15namespace Process
16{
17class ProcessModel;
18class SCORE_LIB_PROCESS_EXPORT MultiScriptDialog : public QDialog
19{
20public:
21 MultiScriptDialog(const score::DocumentContext& ctx, QWidget* parent);
22
23 QSize sizeHint() const override { return {800, 300}; }
24 std::vector<QString> text() const noexcept;
25
26 void addTab(const QString& name, const QString& text, const std::string_view language);
27 void setText(int idx, const QString& str);
28 void setError(const QString& str);
29 void clearError();
30 void openInExternalEditor(const QString& editorPath);
31
32protected:
33 virtual void on_accepted() = 0;
34
35 void hideEvent(QHideEvent* event) override;
36
37 const score::DocumentContext& m_context;
38 QTabWidget* m_tabs{};
39 struct EditorTab
40 {
41 QTextEdit* textedit{};
42 };
43 std::vector<EditorTab> m_editors;
44 QPlainTextEdit* m_error{};
45};
46
47template <typename Process_T, typename Property_T>
49{
50public:
51 using param_type = typename Property_T::param_type;
53 const Process_T& process, const score::DocumentContext& ctx, QWidget* parent)
54 : MultiScriptDialog{ctx, parent}
55 , m_process{process}
56 {
57 const auto& prop = (m_process.*Property_T::get)();
58 for(auto& [name, addr, lang] : param_type::specification)
59 {
60 addTab(name, prop.*addr, lang);
61 }
62
63 con(m_process, Property_T::notify, this, [this]() {
64 const auto& prop = (m_process.*Property_T::get)();
65 int i = 0;
66 for(auto& [name, addr, lang] : param_type::specification)
67 {
68 setText(i, prop.*addr);
69 i++;
70 }
71 });
72
73 con(m_process, &Process_T::errorMessage, this,
74 &ProcessMultiScriptEditDialog::setError);
76 &QWidget::deleteLater);
77 }
78
79 void on_accepted() override
80 {
81 this->clearError();
82 if(this->text() != (m_process.*Property_T::get)())
83 {
84 // TODO try to see if we can make this a bit more efficient,
85 // by passing the validated / transformed data to the command maybe ?
86 if(m_process.validate(this->text()))
87 {
88 CommandDispatcher<>{m_context.commandStack}.submit(
90 m_process, this->text(), m_context});
91 }
92 }
93 }
94
95protected:
96 const Process_T& m_process;
97
98 void reject() override
99 {
100 const_cast<QWidget*&>(m_process.scriptUI) = nullptr;
101 m_process.scriptUIVisible(false);
102 QDialog::reject();
103 }
104 void closeEvent(QCloseEvent* event) override
105 {
106 const_cast<QWidget*&>(m_process.scriptUI) = nullptr;
107 m_process.scriptUIVisible(false);
108 QDialog::closeEvent(event);
109 }
110};
111
112}
The CommandDispatcher class.
Definition CommandDispatcher.hpp:13
void identified_object_destroying(IdentifiedObjectAbstract *o)
To be called by subclasses.
Definition MultiScriptEditor.hpp:19
Definition MultiScriptEditor.hpp:49
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1115
Definition MultiScriptEditor.hpp:40
Definition DocumentContext.hpp:18
Definition PropertyCommand.hpp:112