DocumentContext.hpp
1 #pragma once
2 #include <score/command/CommandStackFacade.hpp>
3 #include <score/command/Dispatchers/OngoingCommandDispatcher.hpp>
4 #include <score/document/DocumentInterface.hpp>
5 #include <score/selection/FocusManager.hpp>
7 class QTimer;
8 namespace score
9 {
10 struct ApplicationContext;
11 struct GUIApplicationContext;
12 class Document;
13 class CommandStack;
14 class SelectionStack;
15 class ObjectLocker;
16 class DocumentPlugin;
17 struct SCORE_LIB_BASE_EXPORT DocumentContext
18 {
19  friend class score::Document;
20 
22  score::Document& document;
23  const score::CommandStackFacade commandStack;
24  score::SelectionStack& selectionStack;
25  score::ObjectLocker& objectLocker;
26  const score::FocusFacade focus;
27  QTimer& coarseUpdateTimer;
28  QTimer& execTimer;
29 
30  OngoingCommandDispatcher& dispatcher;
31 
32  const std::vector<DocumentPlugin*>& pluginModels() const;
33 
34  template <typename T>
35  T& model() const
36  {
37  return IDocument::modelDelegate<T>(document);
38  }
39 
40  template <typename T>
41  T& plugin() const
42  {
43  using namespace std;
44  const auto& pms = this->pluginModels();
45  auto it = find_if(
46  begin(pms), end(pms), [&](DocumentPlugin* pm) { return dynamic_cast<T*>(pm); });
47 
48  SCORE_ASSERT(it != end(pms));
49  return *safe_cast<T*>(*it);
50  }
51 
52  template <typename T>
53  T* findPlugin() const
54  {
55  using namespace std;
56  const auto& pms = this->pluginModels();
57  auto it = find_if(
58  begin(pms), end(pms), [&](DocumentPlugin* pm) { return dynamic_cast<T*>(pm); });
59 
60  if(it != end(pms))
61  return safe_cast<T*>(*it);
62  return nullptr;
63  }
64 
65 protected:
67  DocumentContext(const DocumentContext&) = default;
68  DocumentContext(DocumentContext&&) = delete;
69  DocumentContext& operator=(const DocumentContext&) = delete;
70  DocumentContext& operator=(DocumentContext&&) = delete;
71 };
72 
74 }
75 
76 W_REGISTER_ARGTYPE(score::DocumentContext*)
77 W_REGISTER_ARGTYPE(const score::DocumentContext*)
Base class for IdentifiedObject.
Definition: IdentifiedObjectAbstract.hpp:16
The OngoingCommandDispatcher class.
Definition: OngoingCommandDispatcher.hpp:27
A small abstraction layer over the score::CommandStack.
Definition: CommandStackFacade.hpp:20
The Document class is the central part of the software.
Definition: Document.hpp:51
Extend a document with custom data and systems.
Definition: DocumentPluginBase.hpp:24
The ObjectLocker class.
Definition: ObjectLocker.hpp:21
The SelectionStack class.
Definition: SelectionStack.hpp:24
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: DocumentContext.hpp:18
Definition: FocusManager.hpp:34
Specializes ApplicationContext with the QMainWindow.
Definition: GUIApplicationContext.hpp:15