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