ACF $AcfVersion:0$
TMultiPageDocumentWrap.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QList>
7
8// ACF includes
10#include <istd/TSmartPtr.h>
11#include <iser/IArchive.h>
12#include <iser/CArchiveTag.h>
13#include <iprm/IOptionsList.h>
17
18
19namespace idoc
20{
21
22
26template <class Base>
28 virtual public Base,
30 virtual public iprm::IOptionsList
31{
32public:
33 typedef Base BaseClass;
35
36 // pseudo-reimplemented (idoc::IMultiPageDocument)
37 virtual int GetPagesCount() const override;
38 virtual const istd::IChangeable& GetDocumentPage(int pageIndex) const override;
39 virtual const idoc::IDocumentMetaInfo* GetPageMetaInfo(int pageIndex) const override;
40 virtual void ResetPages() override;
41 virtual bool RemovePage(int pageIndex) override;
42 virtual const IDocumentMetaInfo& GetDocumentMetaInfo() const override;
43
44 // reimplemented (iprm::IOptionsList)
45 virtual int GetOptionsFlags() const override;
46 virtual int GetOptionsCount() const override;
47 virtual QString GetOptionName(int index) const override;
48 virtual QString GetOptionDescription(int index) const override;
49 virtual QByteArray GetOptionId(int index) const override;
50 virtual bool IsOptionEnabled(int index) const override;
51
52 // reimplemented (iser::ISerializable)
53 virtual bool Serialize(iser::IArchive& archive) override;
54
55 // reimplemented (istd::IChangeable)
56 virtual bool CopyFrom(const istd::IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS) override;
57 virtual bool ResetData(CompatibilityMode mode) override;
58
59protected:
65
66protected:
70 virtual bool SerializePage(iser::IArchive& archive, Page& pageItem);
71
72protected:
73 typedef QList<Page> Pages;
74
76};
77
78
79// public methods
80
81// pseudo-reimplemented (idoc::IMultiPageDocument)
82
83template <class Base>
85{
86 return m_documentPages.count();
87}
88
89
90template <class Base>
92{
93 Q_ASSERT(pageIndex < m_documentPages.count());
94 Q_ASSERT(pageIndex >= 0);
95 Q_ASSERT(m_documentPages.at(pageIndex).pagePtr.IsValid());
96
97 return *m_documentPages.at(pageIndex).pagePtr.GetPtr();
98}
99
100
101template <class Base>
103{
104 Q_ASSERT(pageIndex < m_documentPages.count());
105 Q_ASSERT(pageIndex >= 0);
106
107 return &m_documentPages.at(pageIndex).pageMetaInfo;
108}
109
110
111template <class Base>
113{
114 istd::CChangeNotifier changePtr(this);
115
116 m_documentPages.clear();
117}
118
119
120template <class Base>
122{
123 Q_ASSERT(pageIndex < m_documentPages.count());
124 Q_ASSERT(pageIndex >= 0);
125
126 istd::CChangeNotifier changePtr(this);
127
128 m_documentPages.removeAt(pageIndex);
129
130 return true;
131}
132
133
134template <class Base>
139
140
141// reimplemented (iprm::IOptionsList)
142
143template <class Base>
145{
146 return SCF_NONE;
147}
148
149
150template <class Base>
152{
153 return GetPagesCount();
154}
155
156
157template <class Base>
159{
160 Q_ASSERT(index < m_documentPages.count());
161 Q_ASSERT(index>= 0);
162
163 return m_documentPages[index].pageMetaInfo.GetMetaInfo(idoc::IDocumentMetaInfo::MIT_TITLE).toString();
164}
165
166
167template <class Base>
169{
170 return QString();
171}
172
173
174template <class Base>
176{
177 return GetOptionName(index).toUtf8();
178}
179
180
181template <class Base>
183{
184 return true;
185}
186
187
188// reimplemented (iser::ISerializable)
189
190template <class Base>
192{
193 static iser::CArchiveTag metaInfoTag("MetaInfo", "Meta information about the document", iser::CArchiveTag::TT_GROUP);
194 static iser::CArchiveTag pagesTag("Pages", "Container of the document pages", iser::CArchiveTag::TT_MULTIPLE);
195 static iser::CArchiveTag pageTag("Page", "Single document page", iser::CArchiveTag::TT_GROUP, &pagesTag);
196
197 // Serialize meta info:
198 bool retVal = archive.BeginTag(metaInfoTag);
199 retVal = retVal && BaseClass2::Serialize(archive);
200 retVal = retVal && archive.EndTag(metaInfoTag);
201
202 // Serialize document pages:
203 int pagesCount = m_documentPages.count();
204
205 retVal = retVal && archive.BeginMultiTag(pagesTag, pageTag, pagesCount);
206
207 if (archive.IsStoring()){
208 for (int pageIndex = 0; pageIndex < pagesCount; ++pageIndex){
209 retVal = retVal && archive.BeginTag(pageTag);
210 retVal = retVal && SerializePage(archive, m_documentPages[pageIndex]);
211 retVal = retVal && archive.EndTag(pageTag);
212 }
213 }
214 else{
215 istd::CChangeNotifier changePtr(this);
216
217 m_documentPages.clear();
218
219 for (int pageIndex = 0; pageIndex < pagesCount; ++pageIndex){
220 this->InsertPage(NULL, NULL, pageIndex);
221
222 retVal = retVal && archive.BeginTag(pageTag);
223 retVal = retVal && SerializePage(archive, m_documentPages[pageIndex]);
224 retVal = retVal && archive.EndTag(pageTag);
225 }
226 }
227
228 retVal = retVal && archive.EndTag(pagesTag);
229
230 return retVal;
231}
232
233
234// reimplemented (istd::IChangeable)
235
236template <class Base>
238{
239 // native copy
240 const TMultiPageDocumentWrap<Base>* sourcePtr = dynamic_cast<const TMultiPageDocumentWrap<Base>*>(&object);
241 if (sourcePtr != NULL){
242 istd::CChangeNotifier changePtr(this);
243
244 m_documentPages.clear();
245
246 for (int pageIndex = 0; pageIndex < sourcePtr->m_documentPages.count(); ++pageIndex){
247 istd::IChangeable* pagePtr = this->InsertPage();
248 if (pagePtr == NULL){
249 return false;
250 }
251
252 const istd::IChangeable* sourcePagePtr = sourcePtr->m_documentPages.at(pageIndex).pagePtr.GetPtr();
253 Q_ASSERT(sourcePagePtr != NULL);
254
255 if (!pagePtr->CopyFrom(*sourcePagePtr)){
256 return false;
257 }
258
259 if (!m_documentPages[pageIndex].pageMetaInfo.CopyFrom(sourcePtr->m_documentPages.at(pageIndex).pageMetaInfo)){
260 return false;
261 }
262 }
263
264 if (!BaseClass2::CopyFrom(object, mode)){
265 return false;
266 }
267
268 return true;
269 }
270
271 // copy via idoc::IMultiPageDocument
272 const idoc::IMultiPageDocument* docPtr = dynamic_cast<const idoc::IMultiPageDocument*>(&object);
273 if (docPtr != NULL){
274 istd::CChangeNotifier changePtr(this);
275
276 m_documentPages.clear();
277
278 int pagesCount = docPtr->GetPagesCount();
279 for (int pageIndex = 0; pageIndex < pagesCount; ++pageIndex){
280 istd::IChangeable* pagePtr = this->InsertPage();
281 if (pagePtr == NULL){
282 return false;
283 }
284
285 const istd::IChangeable& sourcePage = docPtr->GetDocumentPage(pageIndex);
286 if (!pagePtr->CopyFrom(sourcePage)){
287 return false;
288 }
289
290 const idoc::IDocumentMetaInfo* metaInfoPtr = docPtr->GetPageMetaInfo(pageIndex);
291 if (metaInfoPtr != NULL){
292 if (!m_documentPages[pageIndex].pageMetaInfo.CopyFrom(*metaInfoPtr)){
293 return false;
294 }
295 }
296 }
297
298 if (!BaseClass2::CopyFrom(object, mode)){
299 return false;
300 }
301
302 return true;
303 }
304
305 // copy via idoc::IMultiPageDocumentProvider
306 const idoc::IMultiPageDocumentProvider* docProviderPtr = dynamic_cast<const idoc::IMultiPageDocumentProvider*>(&object);
307 if (docProviderPtr != NULL){
308 const idoc::IMultiPageDocument* sourceDocPtr = docProviderPtr->GetDocument();
309 if (sourceDocPtr != NULL){
310 // recursive call
311 return CopyFrom(*sourceDocPtr, mode);
312 }
313 }
314
315 return false;
316}
317
318
319template <class Base>
321{
322 ResetPages();
323
324 return true;
325}
326
327
328// protected methods
329
330template <class Base>
332{
333 static iser::CArchiveTag pageTitleTag("PageTitle", "Title of the page", iser::CArchiveTag::TT_GROUP);
334 static iser::CArchiveTag pageContentTag("PageContent", "The contents of the page", iser::CArchiveTag::TT_GROUP);
335
336 bool retVal = archive.BeginTag(pageTitleTag);
337 retVal = retVal && pageItem.pageMetaInfo.Serialize(archive);
338 retVal = retVal && archive.EndTag(pageTitleTag);
339
340 iser::ISerializable* serializablePagePtr = dynamic_cast<iser::ISerializable*>(pageItem.pagePtr.GetPtr());
341 if (serializablePagePtr != NULL){
342 retVal = retVal && archive.BeginTag(pageContentTag);
343 retVal = serializablePagePtr->Serialize(archive);
344 retVal = retVal && archive.EndTag(pageContentTag);
345 }
346 else{
347 qWarning("Page object doesn't support serialization");
348
349 return false;
350 }
351
352 return retVal;
353}
354
355
357
358
359} // namespace idoc
360
361
362
363
Implementation of the basic document's meta information.
virtual bool Serialize(iser::IArchive &archive) override
Load or store state of this object as a archive stream.
Interface for document meta-information.
@ MIT_TITLE
Title of the document.
Simple interface for a structured document.
virtual const istd::IChangeable & GetDocumentPage(int pageIndex) const =0
Get the document page.
virtual const idoc::IDocumentMetaInfo * GetPageMetaInfo(int pageIndex) const =0
Get meta info of the given page if exists.
virtual int GetPagesCount() const =0
Get number of pages in the document.
Common interface for a provider of a multi-page document.
virtual const IMultiPageDocument * GetDocument() const =0
Get multi-page document instance.
Generic implementation of IMultiPageDocument interface.
virtual bool IsOptionEnabled(int index) const override
Return true if the option is enabled and can be selected.
virtual bool ResetData(CompatibilityMode mode) override
Reset data to its default state.
virtual bool CopyFrom(const istd::IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS) override
Copy this object from another one.
virtual int GetPagesCount() const override
virtual bool SerializePage(iser::IArchive &archive, Page &pageItem)
Serialize a single page item into or from the archive.
virtual bool Serialize(iser::IArchive &archive) override
Load or store state of this object as a archive stream.
virtual bool RemovePage(int pageIndex) override
virtual int GetOptionsFlags() const override
Get constraints flags.
virtual QString GetOptionDescription(int index) const override
Get human-readable description for an option.
virtual QByteArray GetOptionId(int index) const override
Get option ID.
virtual const idoc::IDocumentMetaInfo * GetPageMetaInfo(int pageIndex) const override
virtual const istd::IChangeable & GetDocumentPage(int pageIndex) const override
virtual QString GetOptionName(int index) const override
Get name of specified option.
virtual int GetOptionsCount() const override
Get number of managed options.
CStandardDocumentMetaInfo BaseClass2
virtual const IDocumentMetaInfo & GetDocumentMetaInfo() const override
Constraints of selection from set of possibilities.
Process tag used to group data in archive stream.
Definition CArchiveTag.h:22
@ TT_GROUP
Normal tag used for grouping of tags or processed elements.
Definition CArchiveTag.h:37
@ TT_MULTIPLE
Multiple tag containing variable number of child tags.
Definition CArchiveTag.h:42
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
virtual bool EndTag(const CArchiveTag &tag)=0
Ends a tagged section in the archive.
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
Begins a tagged section containing multiple elements of the same type.
virtual bool IsStoring() const =0
Checks if this archive is in storing (writing) or loading (reading) mode.
virtual bool BeginTag(const CArchiveTag &tag)=0
Begins a tagged section in the archive.
Common class for all classes which objects can be archived or restored from archive.
virtual bool Serialize(IArchive &archive)=0
Load or store state of this object as a archive stream.
Help class which provides the automatic update mechanism of the model.
Common interface for data model objects, which can be changed.
Definition IChangeable.h:28
CompatibilityMode
Control how relationship betweeen objects are interpreted.
@ CM_WITHOUT_REFS
External references are simple ignored.
virtual bool CopyFrom(const IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS)
Copy this object from another one.
Interface * GetPtr() noexcept
#define NULL
Definition istd.h:74
Contains the system independent basic implementations of Document/View design pattern.
idoc::TMultiPageDocumentWrap< idoc::IMultiPageDocument > CMultiPageDocumentBase
idoc::CStandardDocumentMetaInfo pageMetaInfo