Document.hpp
1 #pragma once
2 #include <score/document/DocumentContext.hpp>
3 #include <score/locking/ObjectLocker.hpp>
4 #include <score/selection/FocusManager.hpp>
5 #include <score/selection/SelectionStack.hpp>
6 
7 #include <core/command/CommandStack.hpp>
8 #include <core/document/DocumentBackups.hpp>
9 #include <core/document/DocumentMetadata.hpp>
10 
11 #include <ossia/detail/json.hpp>
12 
13 #include <QByteArray>
14 #include <QString>
15 #include <QTimer>
16 #include <QVariant>
17 
18 #include <verdigris>
19 
20 class QObject;
21 class QWidget;
22 namespace score
23 {
24 class DocumentBackupManager;
25 } // namespace score
26 #include <score/model/Identifier.hpp>
27 
28 #include <score_lib_base_export.h>
29 
30 namespace score
31 {
32 using JsonWriter = ossia::json_writer;
33 
34 class DocumentDelegateFactory;
35 class DocumentModel;
36 class DocumentPresenter;
37 class DocumentView;
38 
50 class SCORE_LIB_BASE_EXPORT Document final : public QObject
51 {
52  W_OBJECT(Document)
53  friend class DocumentBuilder;
54  friend struct DocumentContext;
55 
56 public:
57  ~Document();
58 
59  void blockAllSignals();
60 
61  const DocumentMetadata& metadata() const noexcept { return m_metadata; }
62  DocumentMetadata& metadata() noexcept { return m_metadata; }
63 
64  const Id<DocumentModel>& id() const noexcept;
65 
66  CommandStack& commandStack() noexcept { return m_commandStack; }
67 
68  SelectionStack& selectionStack() noexcept { return m_selectionStack; }
69 
70  FocusManager& focusManager() noexcept { return m_focus; }
71 
72  ObjectLocker& locker() noexcept { return m_objectLocker; }
73 
74  const DocumentContext& context() const noexcept { return m_context; }
75 
76  DocumentModel& model() const noexcept { return *m_model; }
77 
78  DocumentPresenter* presenter() const noexcept { return m_presenter; }
79 
80  DocumentView* view() const noexcept { return m_view; }
81 
82  DocumentBackupManager* backupManager() const noexcept { return m_backupMgr; }
83 
84  void saveDocumentModelAsJson(JSONObject::Serializer& writer);
85  QByteArray saveDocumentModelAsByteArray();
86 
87  void saveAsJson(JSONObject::Serializer& writer);
88  QByteArray saveAsByteArray();
89 
92  bool virgin() const
93  {
94  return m_virgin && !m_commandStack.canUndo() && !m_commandStack.canRedo();
95  }
96 
97  // Load without creating presenter and view
98  Document(const QString& name, DocumentDelegateFactory& type, QObject* parent);
99 
100  // Called once all the plug-ins, etc... of the document have been loaded
101  void ready();
102 
103  // Update the various internal timers rate after settings changes
104  void updateTimers();
105 
106 private:
107  // These are to be constructed by DocumentBuilder.
108  Document(
109  const QString& name, const Id<DocumentModel>& id, DocumentDelegateFactory& type,
110  QWidget* parentview, QObject* parent);
111 
112  // Load
113  Document(
114  const QString& name, DocumentDelegateFactory& type, QWidget* parentview,
115  QObject* parent);
116 
117  Document(
118  const QString& name, const QByteArray& data, SerializationIdentifier format,
119  DocumentDelegateFactory& type, QWidget* parentview, QObject* parent);
120 
121  // Restore
122  Document(
124  QWidget* parentview, QObject* parent);
125 
126  void loadModel(const QString& fileName, DocumentDelegateFactory& factory);
127  void loadModel(
128  const QString& fileName, const QByteArray& data, SerializationIdentifier format,
129  DocumentDelegateFactory& factory);
130  void restoreModel(const QByteArray& data, DocumentDelegateFactory& factory);
131  void init();
132 
133  DocumentMetadata m_metadata;
134  CommandStack m_commandStack;
135 
136  SelectionStack m_selectionStack;
137  ObjectLocker m_objectLocker;
138  FocusManager m_focus;
139  QTimer m_documentCoarseUpdateTimer;
140  QTimer m_execTimer;
141  CommandStackFacade m_facade{m_commandStack};
142  OngoingCommandDispatcher m_disp{m_facade};
143 
144  DocumentModel* m_model{};
145  DocumentView* m_view{};
146  DocumentPresenter* m_presenter{};
147 
148  DocumentBackupManager* m_backupMgr{};
149 
150  DocumentContext m_context;
151 
152  std::optional<score::RestorableDocument> m_initialData{};
153  bool m_virgin{false}; // Used to check if we can safely close it
154  // if we want to load a document instead upon opening score.
155 };
156 }
157 
158 Q_DECLARE_METATYPE(score::Document*)
159 Q_DECLARE_METATYPE(Id<score::DocumentModel>)
160 Q_DECLARE_METATYPE(rapidjson::Value*)
161 W_REGISTER_ARGTYPE(score::Document*)
162 W_REGISTER_ARGTYPE(Id<score::DocumentModel>)
Definition: JSONVisitor.hpp:52
The OngoingCommandDispatcher class.
Definition: OngoingCommandDispatcher.hpp:27
The id_base_t class.
Definition: Identifier.hpp:57
A small abstraction layer over the score::CommandStack.
Definition: CommandStackFacade.hpp:20
Definition: CommandStack.hpp:26
Handles document backup to allow restoring if there is a crash.
Definition: DocumentBackupManager.hpp:36
Methods to set-up documents.
Definition: DocumentBuilder.hpp:27
Used to provide custom document types.
Definition: DocumentDelegateFactory.hpp:26
The Document class is the central part of the software.
Definition: Document.hpp:51
bool virgin() const
Definition: Document.hpp:92
Model part of a document.
Definition: DocumentModel.hpp:29
Interface between the DocumentModel and the DocumentView.
Definition: DocumentPresenter.hpp:20
The DocumentView class shows a document.
Definition: DocumentView.hpp:19
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
Document-wide metadata.
Definition: DocumentMetadata.hpp:16
Definition: FocusManager.hpp:9
Definition: DocumentBackups.hpp:14