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_WRITERFILTER_SOURCE_DMAPPER_TABLEDATA_HXX
21 : #define INCLUDED_WRITERFILTER_SOURCE_DMAPPER_TABLEDATA_HXX
22 :
23 : #include <dmapper/resourcemodel.hxx>
24 :
25 : #include <vector>
26 : #include <memory>
27 :
28 : namespace writerfilter
29 : {
30 : namespace dmapper
31 : {
32 :
33 : /**
34 : Class containing the data to describe a table cell.
35 : */
36 : class CellData
37 : {
38 : /**
39 : Handle to start of cell.
40 : */
41 : css::uno::Reference<css::text::XTextRange> mStart;
42 :
43 : /**
44 : Handle to end of cell.
45 : */
46 : css::uno::Reference<css::text::XTextRange> mEnd;
47 :
48 : /**
49 : Pointer to properties of cell.
50 : */
51 : TablePropertyMapPtr mpProps;
52 :
53 : bool mbOpen;
54 :
55 : public:
56 : typedef std::shared_ptr<CellData> Pointer_t;
57 :
58 8791 : CellData(css::uno::Reference<css::text::XTextRange> start, TablePropertyMapPtr pProps)
59 8791 : : mStart(start), mEnd(start), mpProps(pProps), mbOpen(true)
60 : {
61 8791 : }
62 :
63 17582 : virtual ~CellData() {}
64 :
65 : /**
66 : Set the start handle of the cell.
67 :
68 : @param start the start handle of the cell
69 : */
70 : void setStart(css::uno::Reference<css::text::XTextRange> start) { mStart = start; }
71 :
72 : /**
73 : Set the end handle of a cell.
74 :
75 : @param end the end handle of the cell
76 : */
77 8351 : void setEnd(css::uno::Reference<css::text::XTextRange> end) { mEnd = end; mbOpen = false; }
78 :
79 : /**
80 : Set the properties of the cell.
81 :
82 : @param pProps the properties of the cell
83 : */
84 : void setProperties(TablePropertyMapPtr pProps) { mpProps = pProps; }
85 :
86 : /**
87 : Adds properties to the cell.
88 :
89 : @param pProps the properties to add
90 : */
91 1318 : void insertProperties(TablePropertyMapPtr pProps)
92 : {
93 1318 : if( mpProps.get() )
94 347 : mpProps->InsertProps(pProps);
95 : else
96 971 : mpProps = pProps;
97 1318 : }
98 :
99 : /**
100 : Return start handle of the cell.
101 : */
102 8351 : const css::uno::Reference<css::text::XTextRange>& getStart() { return mStart; }
103 :
104 : /**
105 : Return end handle of the cell.
106 : */
107 8349 : const css::uno::Reference<css::text::XTextRange>& getEnd() { return mEnd; }
108 :
109 : /**
110 : Return properties of the cell.
111 : */
112 8351 : TablePropertyMapPtr getProperties() { return mpProps; }
113 :
114 6686 : bool isOpen() const { return mbOpen; }
115 : };
116 :
117 : /**
118 : Class to handle data of a table row.
119 : */
120 : class RowData
121 : {
122 : typedef ::std::vector<CellData::Pointer_t> Cells;
123 :
124 : /**
125 : the cell data of the row
126 : */
127 : Cells mCells;
128 :
129 : /**
130 : the properties of the row
131 : */
132 : mutable TablePropertyMapPtr mpProperties;
133 :
134 : public:
135 : typedef std::shared_ptr<RowData> Pointer_t;
136 :
137 8449 : RowData() {}
138 :
139 : RowData(const RowData& rRowData)
140 : : mCells(rRowData.mCells), mpProperties(rRowData.mpProperties)
141 : {
142 : }
143 :
144 16898 : virtual ~RowData() {}
145 :
146 : /**
147 : Add a cell to the row.
148 :
149 : @param start the start handle of the cell
150 : @param end the end handle of the cell
151 : @param pProps the properties of the cell
152 : */
153 8791 : void addCell(const css::uno::Reference<css::text::XTextRange>& start, TablePropertyMapPtr pProps)
154 : {
155 8791 : CellData::Pointer_t pCellData(new CellData(start, pProps));
156 8791 : mCells.push_back(pCellData);
157 8791 : }
158 :
159 8351 : void endCell(const css::uno::Reference<css::text::XTextRange>& end)
160 : {
161 8351 : if (mCells.size() > 0)
162 8351 : mCells.back()->setEnd(end);
163 8351 : }
164 :
165 10101 : bool isCellOpen() const
166 : {
167 10101 : return mCells.size() > 0 && mCells.back()->isOpen();
168 : }
169 :
170 : /**
171 : Add properties to the row.
172 :
173 : @param pProperties the properties to set
174 : */
175 2978 : void insertProperties(TablePropertyMapPtr pProperties)
176 : {
177 2978 : if( pProperties.get() )
178 : {
179 2941 : if( !mpProperties.get() )
180 2941 : mpProperties = pProperties;
181 : else
182 0 : mpProperties->InsertProps(pProperties);
183 : }
184 2978 : }
185 :
186 : /**
187 : Add properties to a cell of the row.
188 :
189 : @param i index of the cell
190 : @param pProps the properties to add
191 : */
192 0 : void insertCellProperties(unsigned int i, TablePropertyMapPtr pProps)
193 : {
194 0 : mCells[i]->insertProperties(pProps);
195 0 : }
196 :
197 : /**
198 : Add properties to the last cell of the row.
199 : */
200 1318 : void insertCellProperties(TablePropertyMapPtr pProps)
201 : {
202 1318 : if (!mCells.empty())
203 1318 : mCells.back()->insertProperties(pProps);
204 1318 : }
205 :
206 : /**
207 : Return number of cells in the row.
208 : */
209 2991 : unsigned int getCellCount()
210 : {
211 2991 : return mCells.size();
212 : }
213 :
214 : /**
215 : Return start handle of a cell in the row.
216 :
217 : @param i index of the cell
218 : */
219 8351 : const css::uno::Reference<css::text::XTextRange>& getCellStart(unsigned int i) const
220 : {
221 8351 : return mCells[i]->getStart();
222 : }
223 :
224 : /**
225 : Return end handle of a cell in the row.
226 :
227 : @param i index of the cell
228 : */
229 8349 : const css::uno::Reference<css::text::XTextRange>& getCellEnd(unsigned int i) const
230 : {
231 8349 : return mCells[i]->getEnd();
232 : }
233 :
234 : /**
235 : Return the properties of a cell in the row.
236 :
237 : @param i index of the cell
238 : */
239 8351 : TablePropertyMapPtr getCellProperties(unsigned int i) const
240 : {
241 8351 : return mCells[i]->getProperties();
242 : }
243 :
244 : /**
245 : Return properties of the row.
246 : */
247 2978 : TablePropertyMapPtr getProperties()
248 : {
249 2978 : return mpProperties;
250 : }
251 :
252 : /**
253 : Clear the row data.
254 : */
255 : void clear()
256 : {
257 : mCells.clear();
258 : mpProperties.reset();
259 : }
260 : };
261 :
262 : /**
263 : Class that holds the data of a table.
264 : */
265 : class TableData
266 : {
267 : typedef RowData::Pointer_t RowPointer_t;
268 : typedef ::std::vector<RowPointer_t> Rows;
269 :
270 : /**
271 : the table properties
272 : */
273 : TablePropertyMapPtr mpTableProps;
274 :
275 : /**
276 : the data of the rows of the table
277 : */
278 : Rows mRows;
279 :
280 : /**
281 : pointer to the data of the current row (while building up the table data).
282 : */
283 : RowPointer_t mpRow;
284 :
285 : /**
286 : depth of the current table in a hierarchy of tables
287 : */
288 : unsigned int mnDepth;
289 :
290 : /**
291 : initialize mpRow
292 : */
293 8449 : void newRow() { mpRow = RowPointer_t(new RowData()); }
294 :
295 : public:
296 : typedef std::shared_ptr<TableData> Pointer_t;
297 :
298 5471 : TableData(unsigned int nDepth) : mnDepth(nDepth) { newRow(); }
299 5471 : ~TableData() {}
300 :
301 : /**
302 : End the current row.
303 :
304 : Sets properties of the current row and pushes the row to the
305 : back of the rows currently contained in the table.
306 :
307 : @param pProperties properties of the row to be ended
308 : */
309 2978 : void endRow(TablePropertyMapPtr pProperties)
310 : {
311 2978 : mpRow->insertProperties(pProperties);
312 2978 : mRows.push_back(mpRow);
313 2978 : newRow();
314 2978 : }
315 :
316 : /**
317 : Add a cell to the current row.
318 :
319 : @param start start handle of the cell
320 : @param end end handle of the cell
321 : @param pProps properties of the cell
322 : */
323 8791 : void addCell(const css::uno::Reference<css::text::XTextRange>& start, TablePropertyMapPtr pProps)
324 : {
325 8791 : mpRow->addCell(start, pProps);
326 8791 : }
327 :
328 : /**
329 : End the current cell of the current row.
330 :
331 : @parm end end handle of the cell
332 : */
333 8351 : void endCell(const css::uno::Reference<css::text::XTextRange>& end)
334 : {
335 8351 : mpRow->endCell(end);
336 8351 : }
337 :
338 : /**
339 : Return if the current cell of the current row is open.
340 : */
341 10101 : bool isCellOpen() const
342 : {
343 10101 : return mpRow->isCellOpen();
344 : }
345 :
346 : /**
347 : Insert properties to the current cell of the current row.
348 :
349 : @param pProps the properties to add
350 : */
351 1318 : void insertCellProperties(TablePropertyMapPtr pProps)
352 : {
353 1318 : mpRow->insertCellProperties(pProps);
354 1318 : }
355 :
356 : /**
357 : Add properties to a cell of the current row.
358 :
359 : @param i index of the cell
360 : @param pProps properties to add
361 : */
362 0 : void insertCellProperties(unsigned int i, TablePropertyMapPtr pProps)
363 : {
364 0 : mpRow->insertCellProperties(i, pProps);
365 0 : }
366 :
367 : /**
368 : Return the table properties.
369 : */
370 : TablePropertyMapPtr getTableProperties()
371 : {
372 : return mpTableProps;
373 : }
374 :
375 : /**
376 : Return number of rows in the table.
377 : */
378 5439 : unsigned int getRowCount()
379 : {
380 5439 : return mRows.size();
381 : }
382 :
383 : /**
384 : Return depth of table in surrounding table hierarchy.
385 : */
386 5554 : unsigned int getDepth()
387 : {
388 5554 : return mnDepth;
389 : }
390 :
391 : /**
392 : Return row data of a certain row.
393 :
394 : @param i index of the row
395 : */
396 2978 : const RowPointer_t getRow(unsigned int i) const
397 : {
398 2978 : return mRows[i];
399 : }
400 :
401 5 : const RowPointer_t getCurrentRow() const
402 : {
403 5 : return mpRow;
404 : }
405 : };
406 :
407 : }
408 : }
409 :
410 : #endif // INCLUDED_WRITERFILTER_SOURCE_DMAPPER_RESOURCEMODEL_TABLEDATA_HXX
411 :
412 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|