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 const score::DocumentContext& m_context;
36 QTabWidget* m_tabs{};
37 struct EditorTab
38 {
39 QTextEdit* textedit{};
40 };
41 std::vector<EditorTab> m_editors;
42 QPlainTextEdit* m_error{};
43};
44
45template <typename Process_T, typename Property_T>
47{
48public:
49 using param_type = typename Property_T::param_type;
51 const Process_T& process, const score::DocumentContext& ctx, QWidget* parent)
52 : MultiScriptDialog{ctx, parent}
53 , m_process{process}
54 {
55 const auto& prop = (m_process.*Property_T::get)();
56 for(auto& [name, addr, lang] : param_type::specification)
57 {
58 addTab(name, prop.*addr, lang);
59 }
60
61 con(m_process, Property_T::notify, this, [this]() {
62 const auto& prop = (m_process.*Property_T::get)();
63 int i = 0;
64 for(auto& [name, addr, lang] : param_type::specification)
65 {
66 setText(i, prop.*addr);
67 i++;
68 }
69 });
70
71 con(m_process, &Process_T::errorMessage, this,
72 &ProcessMultiScriptEditDialog::setError);
74 &QWidget::deleteLater);
75 }
76
77 void on_accepted() override
78 {
79 this->clearError();
80 if(this->text() != (m_process.*Property_T::get)())
81 {
82 // TODO try to see if we can make this a bit more efficient,
83 // by passing the validated / transformed data to the command maybe ?
84 if(m_process.validate(this->text()))
85 {
86 CommandDispatcher<>{m_context.commandStack}.submit(
88 m_process, this->text(), m_context});
89 }
90 }
91 }
92
93protected:
94 const Process_T& m_process;
95 void closeEvent(QCloseEvent* event) override
96 {
97 const_cast<QWidget*&>(m_process.externalUI) = nullptr;
98 m_process.externalUIVisible(false);
99 }
100};
101
102}
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:47
Base classes and tools to implement processes and layers.
Definition JSONVisitor.hpp:1324
Definition MultiScriptEditor.hpp:38
Definition DocumentContext.hpp:18
Definition PropertyCommand.hpp:112