-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdocumentmanager.cpp
More file actions
411 lines (336 loc) · 11.3 KB
/
Copy pathdocumentmanager.cpp
File metadata and controls
411 lines (336 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright (C) 2024 Nick Egorrov, nicegorov@yandex.ru
*
* This file is part of GCodeWorkShop.
*
* GCodeWorkShop is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QList> // for QList, QList<>::const_iterator, QList<>::iterator
#include <QMap> // for QMap, QMap<>::const_iterator
#include <QMdiArea> // for QMdiArea, QMdiArea::StackingOrder
#include <QObject> // for emit, SIGNAL, SLOT, QObject
#include <QPointer> // for QPointer
#include <QString> // for QString, operator==
#include <Qt> // for ConnectionType, DirectConnection
#include <QtDebug> // for QDebug
#include <QtGlobal> // for qAsConst, qWarning
// As long as qAsConst is used.
// IWYU pragma: no_include "type_traits"
// IWYU pragma: no_include <type_traits>
#if (QT_VERSION < QT_VERSION_CHECK(5, 7, 0))
#define qAsConst(s) (s)
#endif
#include <document.h> // for Document
#include <documentinfo.h> // for DocumentInfo
#include <documentmanager.h> // IWYU pragma: associated
#include <documentproducer.h> // for DocumentProducer
#include <documentstyle.h> // for DocumentStyle
#include <documentwidgetproperties.h> // for DocumentWidgetProperties
DocumentManager::DocumentManager(QObject* parent) :
QObject(parent),
m_producers(),
m_docList(),
m_area(nullptr)
{
}
DocumentManager::~DocumentManager()
{
for (DocumentProducer* prod : qAsConst(m_producers)) {
delete prod;
}
}
Document* DocumentManager::activeDocument() const
{
if (m_area.isNull() || m_area->subWindowList().isEmpty()) {
return nullptr;
}
// WARNING: This is very strange, but opening a single document when the application starts
// causes the activeSubWindow function to return nullptr. This is probably caused by the fact
// that the main window is not yet visible when the document is opened.
QMdiSubWindow* subWindow = m_area->subWindowList(QMdiArea::StackingOrder).last();
for (const DocumentListItem& item : qAsConst(m_docList)) {
if (item.subWindow == subWindow) {
return item.document;
}
}
return nullptr;
}
bool DocumentManager::isActiveDocument(Document* document)
{
return activeDocument() == document;
}
bool DocumentManager::setActiveDocument(Document* doc)
{
if (m_area.isNull()) {
return false;
}
for (const DocumentListItem& item : qAsConst(m_docList)) {
if (item.document == doc) {
m_area->setActiveSubWindow(item.subWindow);
return true;
}
}
return false;
}
bool DocumentManager::setActiveDocument(const QString& filePath)
{
return setActiveDocument(findDocumentByFilePath(filePath));
}
Document* DocumentManager::findDocumentById(const QString& id) const
{
for (const DocumentListItem& item : qAsConst(m_docList)) {
if (item.id == id) {
return item.document;
}
}
return nullptr;
}
Document* DocumentManager::findDocumentByFilePath(const QString& filePath) const
{
for (const DocumentListItem& item : qAsConst(m_docList)) {
if (item.document->filePath() == filePath) {
return item.document;
}
}
return nullptr;
}
QList<Document*> DocumentManager::documentList() const
{
QList<Document*> list;
for (auto doc : m_docList) {
list.append(doc.document);
}
return list;
}
DocumentStyle::Ptr DocumentManager::documentStyle(const QString& type) const
{
DocumentProducer* producer = documentProducer(type);
if (!producer) {
qWarning() << "DocumentManager::documentStyle() : The" << type << "type is not registered." ;
return DocumentStyle::Ptr(new DocumentStyle());
}
return producer->documentStyle();
}
void DocumentManager::setDocumentStyle(const DocumentStyle::Ptr& style)
{
if (!style) {
qWarning() << "DocumentManager::setDocumentWidgetProperties() : The style is nullptr." ;
return;
}
const QString& type = style->documentType();
DocumentProducer* producer = documentProducer(type);
if (!producer) {
qWarning() << "DocumentManager::setDocumentStyle() : The" << type << "type is not registered." ;
return;
}
producer->setDocumentStyle(style);
}
DocumentWidgetProperties::Ptr DocumentManager::documentWidgetProperties(const QString& type) const
{
DocumentProducer* producer = documentProducer(type);
if (!producer) {
qWarning() << "DocumentManager::documentWidgetProperties() : The" << type << "type is not registered." ;
return DocumentWidgetProperties::Ptr(new DocumentWidgetProperties());
}
return producer->documentWidgetProperties();
}
void DocumentManager::setDocumentWidgetProperties(const DocumentWidgetProperties::Ptr& property)
{
if (!property) {
qWarning() << "DocumentManager::setDocumentWidgetProperties() : The property is nullptr." ;
return;
}
const QString& type = property->documentType();
DocumentProducer* producer = documentProducer(type);
if (!producer) {
qWarning() << "DocumentManager::setDocumentWidgetProperties() : The" << type << "type is not registered." ;
return;
}
producer->setDocumentWidgetProperties(property);
}
void DocumentManager::updateDocuments(const QString& type)
{
DocumentProducer* producer = documentProducer(type);
if (!producer) {
qWarning() << "DocumentManager::updateDocuments() : The" << type << "type is not registered." ;
return;
}
for (const DocumentListItem& item : qAsConst(m_docList)) {
if (item.document->type() == type) {
producer->updateDocument(item.document);
}
}
}
Document* DocumentManager::createDocument(const QString& type, const QString& id)
{
DocumentProducer* producer = documentProducer(type);
if (!producer) {
qWarning() << "DocumentManager::createDocument() : The" << type << "type is not registered." ;
return nullptr;
}
Document* doc = producer->createDocument();
if (!doc) {
return nullptr;
}
addDocument(doc, id);
return doc;
}
DocumentInfo::Ptr DocumentManager::createDocumentInfo(const QString& type)
{
DocumentProducer* producer = documentProducer(type);
DocumentInfo* info = nullptr;
if (producer) {
info = producer->createDocumentInfo();
} else {
qWarning() << "DocumentManager::createDocumentInfo() : The" << type << "type is not registered." ;
}
return DocumentInfo::Ptr(info);
}
DocumentStyle::Ptr DocumentManager::createDocumentStyle(const QString& type)
{
DocumentProducer* producer = documentProducer(type);
DocumentStyle* style = nullptr;
if (producer) {
style = producer->createDocumentStyle();
} else {
qWarning() << "DocumentManager::createDocumentStyle() : The" << type << "type is not registered." ;
}
return DocumentStyle::Ptr(style);
}
DocumentWidgetProperties::Ptr DocumentManager::createDocumentWidgetProperties(const QString& type)
{
DocumentProducer* producer = documentProducer(type);
DocumentWidgetProperties* properties = nullptr;
if (producer) {
properties = producer->createDocumentWidgetProperties();
} else {
qWarning() << "DocumentManager::createDocumentWidgetProperties() : The" << type << "type is not registered." ;
}
return DocumentWidgetProperties::Ptr(properties);
}
void DocumentManager::addDocument(Document* document, const QString& id)
{
document->setParent(this);
connect(document, SIGNAL(closeRequested(Document*)), this, SLOT(documentCloseRequest(Document*)),
Qt::ConnectionType::DirectConnection);
connect(document, SIGNAL(closed(Document*)), this, SLOT(removeDocument(Document*)));
connect(document, SIGNAL(closed(Document*)), this, SLOT(documentClosed(Document*)));
connect(document, SIGNAL(modificationChanged(Document*, bool)), this, SLOT(documentModificationChanged(Document*,
bool)));
connect(document, SIGNAL(cursorPositionChanged(Document*)), this, SLOT(documentCursorPositionChanged(Document*)));
connect(document, SIGNAL(selectionChanged(Document*)), this, SLOT(documentSelectionChanged(Document*)));
connect(document, SIGNAL(briefChanged(Document*)), this, SLOT(documentBriefChanged(Document*)));
connect(document, SIGNAL(redoAvailable(Document*, bool)), this, SLOT(documentRedoAvailable(Document*, bool)));
connect(document, SIGNAL(undoAvailable(Document*, bool)), this, SLOT(documentUndoAvailable(Document*, bool)));
connect(document, SIGNAL(customContextMenuRequested(Document*, const QPoint&)), this,
SLOT(customContextMenuRequest(Document*, const QPoint&)));
connect(document, SIGNAL(fileWatchRequested(const QString&, bool)), this, SLOT(documentFileWatchRequest(const QString&,
bool)));
DocumentListItem item;
item.document = document;
item.id = id;
item.subWindow = m_area->addSubWindow(document->widget());
m_docList.append(item);
emit documentListChanged();
}
void DocumentManager::removeDocument(Document* document)
{
for (int i = 0; i < m_docList.size(); i++) {
const DocumentListItem& item = m_docList.at(i);
if (item.document == document) {
m_docList.removeAt(i);
emit documentListChanged();
if (m_docList.isEmpty()) {
emit activeDocumentChanged(nullptr);
}
return;
}
}
}
DocumentProducer* DocumentManager::documentProducer(const QString& type) const
{
return m_producers.value(type, nullptr);
}
void DocumentManager::registerDocumentProducer(DocumentProducer* producer)
{
const QString& type = producer->documentType();
if (m_producers.contains(type)) {
qWarning() << "DocumentManager::registerProducer() : The" << type << "type always registered." ;
return;
}
m_producers.insert(type, producer);
}
void DocumentManager::setMdiArea(QMdiArea* mdi)
{
m_area = mdi;
connect(m_area, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(activeWindowChanged(QMdiSubWindow*)));
}
void DocumentManager::activeWindowChanged(QMdiSubWindow* window)
{
for (const DocumentListItem& item : qAsConst(m_docList)) {
if (item.subWindow == window) {
emit activeDocumentChanged(item.document);
}
}
}
void DocumentManager::documentModificationChanged(Document* doc, bool mod)
{
if (isActiveDocument(doc)) {
emit modificationChanged(mod);
}
}
void DocumentManager::documentCursorPositionChanged(Document* doc)
{
if (isActiveDocument(doc)) {
emit cursorPositionChanged();
}
}
void DocumentManager::documentSelectionChanged(Document* doc)
{
if (isActiveDocument(doc)) {
emit selectionChanged();
}
}
void DocumentManager::documentBriefChanged(Document* doc)
{
emit briefChanged(doc);
}
void DocumentManager::documentRedoAvailable(Document* doc, bool available)
{
if (isActiveDocument(doc)) {
emit redoAvailable(available);
}
}
void DocumentManager::documentUndoAvailable(Document* doc, bool available)
{
if (isActiveDocument(doc)) {
emit undoAvailable(available);
}
}
bool DocumentManager::documentCloseRequest(Document* doc)
{
return emit closeRequested(doc);
}
void DocumentManager::documentClosed(Document* doc)
{
emit closed(doc);
}
void DocumentManager::customContextMenuRequest(Document* doc, const QPoint& point)
{
emit customContextMenuRequested(doc, point);
}
void DocumentManager::documentFileWatchRequest(const QString& filePath, bool watch)
{
emit fileWatchRequest(filePath, watch);
}