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