Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_TABLE_DATA
21 : #define INCLUDED_TABLE_DATA
22 :
23 : #include <resourcemodel/WW8ResourceModel.hxx>
24 :
25 : #include <vector>
26 : #include <boost/shared_ptr.hpp>
27 :
28 : namespace writerfilter
29 : {
30 :
31 : template <typename T, typename PropertiesPointer>
32 : /**
33 : Class containing the data to describe a table cell.
34 : */
35 : class CellData
36 : {
37 : /**
38 : Handle to start of cell.
39 : */
40 : T mStart;
41 :
42 : /**
43 : Handle to end of cell.
44 : */
45 : T mEnd;
46 :
47 : /**
48 : Pointer to properties of cell.
49 : */
50 : PropertiesPointer mpProps;
51 :
52 : bool mbOpen;
53 :
54 : public:
55 : typedef boost::shared_ptr<CellData> Pointer_t;
56 :
57 782 : CellData(T start, PropertiesPointer pProps)
58 782 : : mStart(start), mEnd(start), mpProps(pProps), mbOpen(true)
59 : {
60 782 : }
61 :
62 1564 : virtual ~CellData() {}
63 :
64 : /**
65 : Set the start handle of the cell.
66 :
67 : @param start the start handle of the cell
68 : */
69 : void setStart(T start) { mStart = start; }
70 :
71 : /**
72 : Set the end handle of a cell.
73 :
74 : @param end the end handle of the cell
75 : */
76 727 : void setEnd(T end) { mEnd = end; mbOpen = false; }
77 :
78 : /**
79 : Set the properties of the cell.
80 :
81 : @param pProps the properties of the cell
82 : */
83 : void setProperties(PropertiesPointer pProps) { mpProps = pProps; }
84 :
85 : /**
86 : Adds properties to the cell.
87 :
88 : @param pProps the properties to add
89 : */
90 59 : void insertProperties(PropertiesPointer pProps)
91 : {
92 59 : if( mpProps.get() )
93 0 : mpProps->InsertProps(pProps);
94 : else
95 59 : mpProps = pProps;
96 59 : }
97 :
98 : /**
99 : Return start handle of the cell.
100 : */
101 727 : const T & getStart() { return mStart; }
102 :
103 : /**
104 : Return end handle of the cell.
105 : */
106 727 : const T & getEnd() { return mEnd; }
107 :
108 : /**
109 : Return properties of the cell.
110 : */
111 727 : PropertiesPointer getProperties() { return mpProps; }
112 :
113 586 : bool isOpen() const { return mbOpen; }
114 : };
115 :
116 : template <typename T, typename PropertiesPointer>
117 : /**
118 : Class to handle data of a table row.
119 : */
120 : class RowData
121 : {
122 : typedef typename CellData<T, PropertiesPointer>::Pointer_t
123 : CellDataPointer_t;
124 : typedef ::std::vector<CellDataPointer_t> Cells;
125 :
126 : /**
127 : the cell data of the row
128 : */
129 : Cells mCells;
130 :
131 : /**
132 : the properties of the row
133 : */
134 : mutable PropertiesPointer mpProperties;
135 :
136 : public:
137 : typedef boost::shared_ptr<RowData <T, PropertiesPointer> > Pointer_t;
138 :
139 843 : RowData() {}
140 :
141 : RowData(const RowData<T, PropertiesPointer> & rRowData)
142 : : mCells(rRowData.mCells), mpProperties(rRowData.mpProperties)
143 : {
144 : }
145 :
146 1686 : virtual ~RowData() {}
147 :
148 : /**
149 : Add a cell to the row.
150 :
151 : @param start the start handle of the cell
152 : @param end the end handle of the cell
153 : @param pProps the properties of the cell
154 : */
155 782 : void addCell(const T & start, PropertiesPointer pProps)
156 : {
157 : CellDataPointer_t pCellData
158 782 : (new CellData<T, PropertiesPointer>(start, pProps));
159 782 : mCells.push_back(pCellData);
160 782 : }
161 :
162 727 : void endCell(const T & end)
163 : {
164 727 : if (mCells.size() > 0)
165 727 : mCells.back()->setEnd(end);
166 727 : }
167 :
168 839 : bool isCellOpen() const
169 : {
170 839 : return mCells.size() > 0 && mCells.back()->isOpen();
171 : }
172 :
173 : /**
174 : Add properties to the row.
175 :
176 : @param pProperties the properties to set
177 : */
178 205 : void insertProperties(PropertiesPointer pProperties)
179 : {
180 205 : if( pProperties.get() )
181 : {
182 191 : if( !mpProperties.get() )
183 191 : mpProperties = pProperties;
184 : else
185 0 : mpProperties->InsertProps(pProperties);
186 : }
187 205 : }
188 :
189 : /**
190 : Add properties to a cell of the row.
191 :
192 : @param i index of the cell
193 : @param pProps the properties to add
194 : */
195 0 : void insertCellProperties(unsigned int i, PropertiesPointer pProps)
196 : {
197 0 : mCells[i]->insertProperties(pProps);
198 0 : }
199 :
200 : /**
201 : Add properties to the last cell of the row.
202 : */
203 59 : void insertCellProperties(PropertiesPointer pProps)
204 : {
205 59 : if (! mCells.empty())
206 59 : mCells.back()->insertProperties(pProps);
207 59 : }
208 :
209 : /**
210 : Return number of cells in the row.
211 : */
212 208 : unsigned int getCellCount()
213 : {
214 208 : return mCells.size();
215 : }
216 :
217 : /**
218 : Return start handle of a cell in the row.
219 :
220 : @param i index of the cell
221 : */
222 727 : const T & getCellStart(unsigned int i) const
223 : {
224 727 : return mCells[i]->getStart();
225 : }
226 :
227 : /**
228 : Return end handle of a cell in the row.
229 :
230 : @param i index of the cell
231 : */
232 727 : const T & getCellEnd(unsigned int i) const
233 : {
234 727 : return mCells[i]->getEnd();
235 : }
236 :
237 : /**
238 : Return the properties of a cell in the row.
239 :
240 : @param i index of the cell
241 : */
242 727 : PropertiesPointer getCellProperties(unsigned int i) const
243 : {
244 727 : return mCells[i]->getProperties();
245 : }
246 :
247 : /**
248 : Return properties of the row.
249 : */
250 205 : PropertiesPointer getProperties()
251 : {
252 205 : return mpProperties;
253 : }
254 :
255 : /**
256 : Clear the row data.
257 : */
258 : void clear()
259 : {
260 : mCells.clear();
261 : mpProperties.reset();
262 : }
263 : };
264 :
265 : template <typename T, typename PropertiesPointer>
266 : /**
267 : Class that holds the data of a table.
268 : */
269 : class TableData
270 : {
271 : typedef typename RowData<T, PropertiesPointer>::Pointer_t RowPointer_t;
272 : typedef ::std::vector<RowPointer_t> Rows;
273 :
274 : /**
275 : the table properties
276 : */
277 : PropertiesPointer mpTableProps;
278 :
279 : /**
280 : the data of the rows of the table
281 : */
282 : Rows mRows;
283 :
284 : /**
285 : pointer to the data of the current row (while building up the table data).
286 : */
287 : RowPointer_t mpRow;
288 :
289 : /**
290 : depth of the current table in a hierarchy of tables
291 : */
292 : unsigned int mnDepth;
293 :
294 : /**
295 : initialize mpRow
296 : */
297 843 : void newRow() { mpRow = RowPointer_t(new RowData<T, PropertiesPointer>()); }
298 :
299 : public:
300 : typedef boost::shared_ptr<TableData <T, PropertiesPointer> > Pointer_t;
301 :
302 638 : TableData(unsigned int nDepth) : mnDepth(nDepth) { newRow(); }
303 638 : ~TableData() {}
304 :
305 : /**
306 : End the current row.
307 :
308 : Sets properties of the current row and pushes the row to the
309 : back of the rows currently contained in the table.
310 :
311 : @param pProperties properties of the row to be ended
312 : */
313 205 : void endRow(PropertiesPointer pProperties)
314 : {
315 205 : mpRow->insertProperties(pProperties);
316 205 : mRows.push_back(mpRow);
317 205 : newRow();
318 205 : }
319 :
320 : /**
321 : Add a cell to the current row.
322 :
323 : @param start start handle of the cell
324 : @param end end handle of the cell
325 : @param pProps properties of the cell
326 : */
327 782 : void addCell(const T & start, PropertiesPointer pProps)
328 : {
329 782 : mpRow->addCell(start, pProps);
330 782 : }
331 :
332 : /**
333 : End the current cell of the current row.
334 :
335 : @parm end end handle of the cell
336 : */
337 727 : void endCell(const T & end)
338 : {
339 727 : mpRow->endCell(end);
340 727 : }
341 :
342 : /**
343 : Return if the current cell of the current row is open.
344 : */
345 839 : bool isCellOpen() const
346 : {
347 839 : return mpRow->isCellOpen();
348 : }
349 :
350 : /**
351 : Insert properties to the current cell of the current row.
352 :
353 : @param pProps the properties to add
354 : */
355 59 : void insertCellProperties(PropertiesPointer pProps)
356 : {
357 59 : mpRow->insertCellProperties(pProps);
358 59 : }
359 :
360 : /**
361 : Add properties to a cell of the current row.
362 :
363 : @param i index of the cell
364 : @param pProps properties to add
365 : */
366 0 : void insertCellProperties(unsigned int i, PropertiesPointer pProps)
367 : {
368 0 : mpRow->insertCellProperties(i, pProps);
369 0 : }
370 :
371 : void insertTableProperties( PropertiesPointer pProps )
372 : {
373 : if ( mpTableProps.get( ) )
374 : mpTableProps->insert( pProps );
375 : else
376 : mpTableProps = pProps;
377 : }
378 :
379 : /**
380 : Return the table properties.
381 : */
382 : PropertiesPointer getTableProperties( )
383 : {
384 : return mpTableProps;
385 : }
386 :
387 : /**
388 : Return number of rows in the table.
389 : */
390 637 : unsigned int getRowCount()
391 : {
392 637 : return mRows.size();
393 : }
394 :
395 : /**
396 : Return depth of table in surrounding table hierarchy.
397 : */
398 653 : unsigned int getDepth()
399 : {
400 653 : return mnDepth;
401 : }
402 :
403 : /**
404 : Return row data of a certain row.
405 :
406 : @param i index of the row
407 : */
408 205 : const RowPointer_t getRow(unsigned int i) const
409 : {
410 205 : return mRows[i];
411 : }
412 :
413 1 : const RowPointer_t getCurrentRow() const
414 : {
415 1 : return mpRow;
416 : }
417 : };
418 :
419 : }
420 :
421 : #endif // INCLUDED_TABLE_DATA
422 :
423 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|