DocumentManager.hpp
1 #pragma once
2 #include <score/tools/Version.hpp>
3 
4 #include <core/document/DocumentBuilder.hpp>
5 
6 #include <ossia/detail/json_fwd.hpp>
7 
8 #include <QObject>
9 #include <QString>
10 
11 #include <score_lib_base_export.h>
12 
13 #include <vector>
14 #include <verdigris>
15 class QRecentFilesMenu;
16 namespace score
17 {
18 class Document;
19 struct ApplicationContext;
20 class View;
21 struct RestorableDocument;
22 } // namespace score
23 
24 namespace score
25 {
26 
28 {
29 public:
30  const std::vector<Document*>& documents() const { return m_documents; }
31 
32  std::vector<Document*>& documents() { return m_documents; }
33 
34  Document* currentDocument() const { return m_currentDocument; }
35 
36  void setCurrentDocument(Document* d) { m_currentDocument = d; }
37 
38 protected:
39  std::vector<Document*> m_documents;
40  Document* m_currentDocument{};
41 };
42 
46 class SCORE_LIB_BASE_EXPORT DocumentManager
47  : public QObject
48  , public DocumentList
49 {
50  W_OBJECT(DocumentManager)
51 public:
52  DocumentManager(score::View* view, QObject* parentPresenter);
53 
54  void init(const score::GUIApplicationContext& ctx);
55 
56  ~DocumentManager();
57 
58  auto recentFiles() const { return m_recentFiles; }
59 
60  // Document management
61  Document* setupDocument(const score::GUIApplicationContext& ctx, score::Document* doc);
62 
63  template <typename... Args>
64  Document* newDocument(const score::GUIApplicationContext& ctx, Args&&... args)
65  {
66  prepareNewDocument(ctx);
67  return setupDocument(ctx, m_builder.newDocument(ctx, std::forward<Args>(args)...));
68  }
69 
70  template <typename... Args>
71  Document* loadDocument(const score::GUIApplicationContext& ctx, Args&&... args)
72  {
73  closeVirginDocument(ctx);
74  prepareNewDocument(ctx);
75  return setupDocument(ctx, m_builder.loadDocument(ctx, std::forward<Args>(args)...));
76  }
77 
78  template <typename... Args>
79  void restoreDocument(const score::GUIApplicationContext& ctx, Args&&... args)
80  {
81  prepareNewDocument(ctx);
82  setupDocument(ctx, m_builder.restoreDocument(ctx, std::forward<Args>(args)...));
83  }
84 
85  // Restore documents after a crash
86  void restoreDocuments(const score::GUIApplicationContext& ctx);
87 
88  void setCurrentDocument(const score::GUIApplicationContext& ctx, Document* doc);
89 
90  // Returns true if the document was closed.
91  bool closeDocument(const score::GUIApplicationContext& ctx, Document&);
92  void forceCloseDocument(const score::GUIApplicationContext& ctx, Document&);
93 
94  // Methods to save and load
95  bool saveDocument(Document&);
96  bool saveDocumentAs(Document&);
97 
98  bool saveStack();
99  Document* loadStack(const score::GUIApplicationContext& ctx);
100  Document* loadStack(const score::GUIApplicationContext& ctx, const QString&);
101 
102  Document* loadFile(const score::GUIApplicationContext& ctx);
103  Document* loadFile(const score::GUIApplicationContext& ctx, const QString& filename);
104 
105  bool closeAllDocuments(const score::GUIApplicationContext& ctx);
106 
107  bool preparingNewDocument() const;
108 
113  static bool
114  checkAndUpdateJson(rapidjson::Value&, const score::GUIApplicationContext& ctx);
115 
116 public:
117  void documentChanged(score::Document* arg_1)
118  E_SIGNAL(SCORE_LIB_BASE_EXPORT, documentChanged, arg_1)
119 
120 private:
121  void closeVirginDocument(const score::GUIApplicationContext& ctx);
122  void prepareNewDocument(const score::GUIApplicationContext& ctx);
123 
124  static bool updateJson(
125  rapidjson::Value& object, score::Version json_ver, score::Version score_ver);
126 
127  void saveRecentFilesState();
128 
129  score::View* m_view{};
130 
131  DocumentBuilder m_builder;
132 
133  QPointer<QRecentFilesMenu> m_recentFiles{};
134 
135  bool m_preparingNewDocument{};
136 };
137 
138 SCORE_LIB_BASE_EXPORT
139 Id<score::DocumentModel> getStrongId(const std::vector<score::Document*>& v);
140 
141 }
The id_base_t class.
Definition: Identifier.hpp:57
Methods to set-up documents.
Definition: DocumentBuilder.hpp:27
The Document class is the central part of the software.
Definition: Document.hpp:51
Owns the documents.
Definition: DocumentManager.hpp:49
Represents the version of a plug-in.
Definition: Version.hpp:13
The main display of the application.
Definition: lib/core/view/Window.hpp:41
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: DocumentManager.hpp:28
Specializes ApplicationContext with the QMainWindow.
Definition: GUIApplicationContext.hpp:15