TMultiPageDocumentWrap.h
Go to the documentation of this file.
1 #ifndef idoc_TMultiPageDocumentWrap_included
2 #define idoc_TMultiPageDocumentWrap_included
3 
4 
5 // Qt includes
6 #include <QtCore/QList>
7 
8 // ACF includes
9 #include <istd/CChangeNotifier.h>
10 #include <istd/TSmartPtr.h>
11 #include <iser/IArchive.h>
12 #include <iser/CArchiveTag.h>
13 #include <iprm/IOptionsList.h>
17 
18 
19 namespace idoc
20 {
21 
22 
26 template <class Base>
28  virtual public Base,
30  virtual public iprm::IOptionsList
31 {
32 public:
33  typedef Base BaseClass;
35 
36  // pseudo-reimplemented (idoc::IMultiPageDocument)
37  virtual int GetPagesCount() const;
38  virtual const istd::IChangeable& GetDocumentPage(int pageIndex) const;
39  virtual const idoc::IDocumentMetaInfo* GetPageMetaInfo(int pageIndex) const;
40  virtual void ResetPages();
41  virtual bool RemovePage(int pageIndex);
42  virtual const IDocumentMetaInfo& GetDocumentMetaInfo() const;
43 
44  // reimplemented (iprm::IOptionsList)
45  virtual int GetOptionsFlags() const;
46  virtual int GetOptionsCount() const;
47  virtual QString GetOptionName(int index) const;
48  virtual QString GetOptionDescription(int index) const;
49  virtual QByteArray GetOptionId(int index) const;
50  virtual bool IsOptionEnabled(int index) const;
51 
52  // reimplemented (iser::ISerializable)
53  virtual bool Serialize(iser::IArchive& archive);
54 
55  // reimplemented (istd::IChangeable)
56  virtual bool CopyFrom(const istd::IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS);
57  virtual bool ResetData(CompatibilityMode mode);
58 
59 protected:
60  struct Page
61  {
64  };
65 
66 protected:
70  virtual bool SerializePage(iser::IArchive& archive, Page& pageItem);
71 
72 protected:
73  typedef QList<Page> Pages;
74 
76 };
77 
78 
79 // public methods
80 
81 // pseudo-reimplemented (idoc::IMultiPageDocument)
82 
83 template <class Base>
85 {
86  return m_documentPages.count();
87 }
88 
89 
90 template <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 
101 template <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 
111 template <class Base>
113 {
114  istd::CChangeNotifier changePtr(this);
115 
116  m_documentPages.clear();
117 }
118 
119 
120 template <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 
134 template <class Base>
136 {
137  return *this;
138 }
139 
140 
141 // reimplemented (iprm::IOptionsList)
142 
143 template <class Base>
145 {
146  return SCF_NONE;
147 }
148 
149 
150 template <class Base>
152 {
153  return GetPagesCount();
154 }
155 
156 
157 template <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 
167 template <class Base>
169 {
170  return QString();
171 }
172 
173 
174 template <class Base>
175 QByteArray TMultiPageDocumentWrap<Base>::GetOptionId(int index) const
176 {
177  return GetOptionName(index).toUtf8();
178 }
179 
180 
181 template <class Base>
183 {
184  return true;
185 }
186 
187 
188 // reimplemented (iser::ISerializable)
189 
190 template <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 
236 template <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 
319 template <class Base>
321 {
322  ResetPages();
323 
324  return true;
325 }
326 
327 
328 // protected methods
329 
330 template <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 #endif // !idoc_TMultiPageDocumentWrap_included
363 
364 
Common interface for a provider of a multi-page document.
External references are simple ignored.
Definition: IChangeable.h:197
CompatibilityMode
Control how relationship betweeen objects are interpreted.
Definition: IChangeable.h:186
virtual bool SerializePage(iser::IArchive &archive, Page &pageItem)
Serialize a single page item into or from the archive.
virtual const istd::IChangeable & GetDocumentPage(int pageIndex) const
virtual bool CopyFrom(const istd::IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS)
Copy this object from another one.
virtual bool IsStoring() const =0
Check if this archive is loading or storing.
Interface for document meta-information.
Constraints of selection from set of possibilities.
Definition: IOptionsList.h:22
virtual bool RemovePage(int pageIndex)
virtual bool CopyFrom(const IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS)
Copy this object from another one.
Definition: IChangeable.h:325
Common class for all classes which objects can be archived or restored from archive.
Definition: ISerializable.h:23
Implementation of the basic document's meta information.
Simple interface for a structured document.
virtual QString GetOptionName(int index) const
Get name of specified option.
virtual bool Serialize(IArchive &archive)=0
Load or store state of this object as a archive stream.
virtual bool BeginTag(const CArchiveTag &tag)=0
Begin of archive tag.
virtual QByteArray GetOptionId(int index) const
Get option ID.
Multiple tag containing variable number of child tags.
Definition: CArchiveTag.h:42
Normal tag used for grouping of tags or processed elements.
Definition: CArchiveTag.h:37
Generic implementation of IMultiPageDocument interface.
virtual bool BeginMultiTag(const CArchiveTag &tag, const CArchiveTag &subTag, int &count)=0
Begin of archive tag containing set of subelements of the same type.
istd::TSmartPtr< istd::IChangeable > pagePtr
Represent input/output persistence archive.
Definition: IArchive.h:30
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
virtual int GetPagesCount() const =0
Get number of pages in the document.
idoc::CStandardDocumentMetaInfo pageMetaInfo
CStandardDocumentMetaInfo BaseClass2
virtual bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.
virtual const idoc::IDocumentMetaInfo * GetPageMetaInfo(int pageIndex) const =0
Get meta info of the given page if exists.
Help class which provides the automatic update mechanism of the model.
virtual bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.
#define NULL
Definition: istd.h:64
virtual const IMultiPageDocument * GetDocument() const =0
Get multi-page document instance.
virtual QString GetOptionDescription(int index) const
Get human readable description for a option with the index index.
virtual bool IsOptionEnabled(int index) const
Return true if the option is enabled and can be selected.
virtual bool ResetData(CompatibilityMode mode)
Reset data to its default state.
Process tag used to group data in archive stream.
Definition: CArchiveTag.h:21
idoc::TMultiPageDocumentWrap< idoc::IMultiPageDocument > CMultiPageDocumentBase
virtual bool EndTag(const CArchiveTag &tag)=0
End of archive tag.
const Type * GetPtr() const
Get access to pointed object.
Definition: TTransPtr.h:138
virtual const idoc::IDocumentMetaInfo * GetPageMetaInfo(int pageIndex) const
virtual int GetOptionsCount() const
Get number of managed options.
virtual const IDocumentMetaInfo & GetDocumentMetaInfo() const
virtual const istd::IChangeable & GetDocumentPage(int pageIndex) const =0
Get the document page.
virtual int GetOptionsFlags() const
Get constraints flags.

© 2007-2017 Witold Gantzke and Kirill Lepskiy