/src/libetonyek/src/lib/IWORKTableRecorder.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* |
3 | | * This file is part of the libetonyek 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 | | |
10 | | #include "IWORKTableRecorder.h" |
11 | | |
12 | | #include <deque> |
13 | | |
14 | | #include <boost/variant.hpp> |
15 | | |
16 | | #include "IWORKOutputElements.h" |
17 | | #include "IWORKText.h" |
18 | | #include "IWORKTextRecorder.h" |
19 | | |
20 | | namespace libetonyek |
21 | | { |
22 | | |
23 | | using std::shared_ptr; |
24 | | |
25 | | namespace |
26 | | { |
27 | | |
28 | | struct SetComment |
29 | | { |
30 | | SetComment(const unsigned column, const unsigned row, IWORKOutputElements const &text) |
31 | 0 | : m_column(column) |
32 | 0 | , m_row(row) |
33 | 0 | , m_text(text) |
34 | 0 | { |
35 | 0 | } |
36 | | |
37 | | const unsigned m_column; |
38 | | const unsigned m_row; |
39 | | const IWORKOutputElements m_text; |
40 | | }; |
41 | | |
42 | | struct SetSize |
43 | | { |
44 | | SetSize(const unsigned columns, const unsigned rows) |
45 | 0 | : m_columns(columns) |
46 | 0 | , m_rows(rows) |
47 | 0 | { |
48 | 0 | } |
49 | | |
50 | | const unsigned m_columns; |
51 | | const unsigned m_rows; |
52 | | }; |
53 | | |
54 | | struct SetHeaders |
55 | | { |
56 | | SetHeaders(const unsigned headerColumns, const unsigned headerRows, const unsigned footerRows) |
57 | 0 | : m_headerColumns(headerColumns) |
58 | 0 | , m_headerRows(headerRows) |
59 | 0 | , m_footerRows(footerRows) |
60 | 0 | { |
61 | 0 | } |
62 | | |
63 | | const unsigned m_headerColumns; |
64 | | const unsigned m_headerRows; |
65 | | const unsigned m_footerRows; |
66 | | }; |
67 | | |
68 | | struct SetBandedRows |
69 | | { |
70 | | explicit SetBandedRows(const bool banded) |
71 | 0 | : m_banded(banded) |
72 | 0 | { |
73 | 0 | } |
74 | | |
75 | | const bool m_banded; |
76 | | }; |
77 | | |
78 | | struct SetRepeated |
79 | | { |
80 | | SetRepeated(const bool columns, const bool rows) |
81 | 0 | : m_columns(columns) |
82 | 0 | , m_rows(rows) |
83 | 0 | { |
84 | 0 | } |
85 | | |
86 | | const bool m_columns; |
87 | | const bool m_rows; |
88 | | }; |
89 | | |
90 | | struct SetOrder |
91 | | { |
92 | | explicit SetOrder(const int order) |
93 | 0 | : m_order(order) |
94 | 0 | { |
95 | 0 | } |
96 | | |
97 | | const int m_order; |
98 | | }; |
99 | | |
100 | | struct SetStyle |
101 | | { |
102 | | explicit SetStyle(const IWORKStylePtr_t &style) |
103 | 0 | : m_style(style) |
104 | 0 | { |
105 | 0 | } |
106 | | |
107 | | const IWORKStylePtr_t m_style; |
108 | | }; |
109 | | |
110 | | struct SetSizes |
111 | | { |
112 | | SetSizes(const IWORKColumnSizes_t &columnSizes, const IWORKRowSizes_t &rowSizes) |
113 | 0 | : m_columnSizes(columnSizes) |
114 | 0 | , m_rowSizes(rowSizes) |
115 | 0 | { |
116 | 0 | } |
117 | | |
118 | | const IWORKColumnSizes_t m_columnSizes; |
119 | | const IWORKRowSizes_t m_rowSizes; |
120 | | }; |
121 | | |
122 | | struct SetBorders |
123 | | { |
124 | | SetBorders(const IWORKGridLineMap_t &verticalLines, const IWORKGridLineMap_t &horizontalLines) |
125 | 0 | : m_verticalLines(verticalLines) |
126 | 0 | , m_verticalRightLines() |
127 | 0 | , m_horizontalLines(horizontalLines) |
128 | 0 | , m_horizontalBottomLines() |
129 | 0 | { |
130 | 0 | } |
131 | | SetBorders(const IWORKGridLineMap_t &verticalLeftLines, const IWORKGridLineMap_t &verticalRightLines, |
132 | | const IWORKGridLineMap_t &horizontalTopLines, const IWORKGridLineMap_t &horizontalBottomLines) |
133 | 0 | : m_verticalLines(verticalLeftLines) |
134 | 0 | , m_verticalRightLines(verticalRightLines) |
135 | 0 | , m_horizontalLines(horizontalTopLines) |
136 | 0 | , m_horizontalBottomLines(horizontalBottomLines) |
137 | 0 | { |
138 | 0 | } |
139 | | |
140 | | const IWORKGridLineMap_t m_verticalLines; |
141 | | const IWORKGridLineMap_t m_verticalRightLines; |
142 | | const IWORKGridLineMap_t m_horizontalLines; |
143 | | const IWORKGridLineMap_t m_horizontalBottomLines; |
144 | | }; |
145 | | |
146 | | struct InsertCell |
147 | | { |
148 | | InsertCell(const unsigned column, const unsigned row, const boost::optional<std::string> &value, const std::shared_ptr<IWORKText> &content, const boost::optional<IWORKDateTimeData> &dateTime, const unsigned columnSpan, const unsigned rowSpan, const IWORKFormulaPtr_t &formula, const boost::optional<unsigned> &formulaHC, const IWORKStylePtr_t &style, IWORKCellType type) |
149 | 0 | : m_column(column) |
150 | 0 | , m_row(row) |
151 | 0 | , m_value(value) |
152 | 0 | , m_content(content) |
153 | 0 | , m_dateTime(dateTime) |
154 | 0 | , m_columnSpan(columnSpan) |
155 | 0 | , m_rowSpan(rowSpan) |
156 | 0 | , m_formula(formula) |
157 | 0 | , m_formulaHC(formulaHC) |
158 | 0 | , m_style(style) |
159 | 0 | , m_type(type) |
160 | 0 | { |
161 | 0 | } |
162 | | |
163 | | const unsigned m_column; |
164 | | const unsigned m_row; |
165 | | const boost::optional<std::string> m_value; |
166 | | const std::shared_ptr<IWORKText> m_content; |
167 | | const boost::optional<IWORKDateTimeData> m_dateTime; |
168 | | const unsigned m_columnSpan; |
169 | | const unsigned m_rowSpan; |
170 | | const IWORKFormulaPtr_t m_formula; |
171 | | boost::optional<unsigned> m_formulaHC; |
172 | | const IWORKStylePtr_t m_style; |
173 | | const IWORKCellType m_type; |
174 | | }; |
175 | | |
176 | | struct InsertCoveredCell |
177 | | { |
178 | | InsertCoveredCell(const unsigned column, const unsigned row) |
179 | 0 | : m_column(column) |
180 | 0 | , m_row(row) |
181 | 0 | { |
182 | 0 | } |
183 | | |
184 | | const unsigned m_column; |
185 | | const unsigned m_row; |
186 | | }; |
187 | | |
188 | | struct SetDefaultCellStyle |
189 | | { |
190 | | SetDefaultCellStyle(IWORKTable::CellType type, const IWORKStylePtr_t &style) |
191 | 0 | : m_type(type) |
192 | 0 | , m_style(style) |
193 | 0 | { |
194 | 0 | } |
195 | | |
196 | | const IWORKTable::CellType m_type; |
197 | | const IWORKStylePtr_t m_style; |
198 | | }; |
199 | | |
200 | | struct SetDefaultLayoutStyle |
201 | | { |
202 | | SetDefaultLayoutStyle(IWORKTable::CellType type, const IWORKStylePtr_t &style) |
203 | 0 | : m_type(type) |
204 | 0 | , m_style(style) |
205 | 0 | { |
206 | 0 | } |
207 | | |
208 | | const IWORKTable::CellType m_type; |
209 | | const IWORKStylePtr_t m_style; |
210 | | }; |
211 | | |
212 | | struct SetDefaultParagraphStyle |
213 | | { |
214 | | SetDefaultParagraphStyle(IWORKTable::CellType type, const IWORKStylePtr_t &style) |
215 | 0 | : m_type(type) |
216 | 0 | , m_style(style) |
217 | 0 | { |
218 | 0 | } |
219 | | |
220 | | const IWORKTable::CellType m_type; |
221 | | const IWORKStylePtr_t m_style; |
222 | | }; |
223 | | |
224 | | typedef boost::variant |
225 | | < SetComment |
226 | | , SetSize |
227 | | , SetHeaders |
228 | | , SetBandedRows |
229 | | , SetRepeated |
230 | | , SetOrder |
231 | | , SetStyle |
232 | | , SetSizes |
233 | | , SetBorders |
234 | | , InsertCell |
235 | | , InsertCoveredCell |
236 | | , SetDefaultCellStyle |
237 | | , SetDefaultLayoutStyle |
238 | | , SetDefaultParagraphStyle |
239 | | > |
240 | | Element_t; |
241 | | |
242 | | } |
243 | | |
244 | | namespace |
245 | | { |
246 | | |
247 | | struct Sender : public boost::static_visitor<void> |
248 | | { |
249 | | explicit Sender(IWORKTable &table) |
250 | 0 | : m_table(table) |
251 | 0 | { |
252 | 0 | } |
253 | | |
254 | | void operator()(const SetComment &value) const |
255 | 0 | { |
256 | 0 | m_table.setComment(value.m_column, value.m_row, value.m_text); |
257 | 0 | } |
258 | | |
259 | | void operator()(const SetSize &value) const |
260 | 0 | { |
261 | 0 | m_table.setSize(value.m_columns, value.m_rows); |
262 | 0 | } |
263 | | |
264 | | void operator()(const SetHeaders &value) const |
265 | 0 | { |
266 | 0 | m_table.setHeaders(value.m_headerColumns, value.m_headerRows, value.m_footerRows); |
267 | 0 | } |
268 | | |
269 | | void operator()(const SetBandedRows &value) const |
270 | 0 | { |
271 | 0 | m_table.setBandedRows(value.m_banded); |
272 | 0 | } |
273 | | |
274 | | void operator()(const SetRepeated &value) const |
275 | 0 | { |
276 | 0 | m_table.setRepeated(value.m_columns, value.m_rows); |
277 | 0 | } |
278 | | |
279 | | void operator()(const SetStyle &value) const |
280 | 0 | { |
281 | 0 | m_table.setStyle(value.m_style); |
282 | 0 | } |
283 | | |
284 | | void operator()(const SetOrder &value) const |
285 | 0 | { |
286 | 0 | m_table.setOrder(value.m_order); |
287 | 0 | } |
288 | | |
289 | | void operator()(const SetSizes &value) const |
290 | 0 | { |
291 | 0 | m_table.setSizes(value.m_columnSizes, value.m_rowSizes); |
292 | 0 | } |
293 | | |
294 | | void operator()(const SetBorders &value) const |
295 | 0 | { |
296 | 0 | m_table.setBorders(value.m_verticalLines,value.m_verticalRightLines, |
297 | 0 | value.m_horizontalLines, value.m_horizontalBottomLines); |
298 | 0 | } |
299 | | |
300 | | void operator()(const InsertCell &value) const |
301 | 0 | { |
302 | 0 | const shared_ptr<IWORKTextRecorder> recorder(value.m_content->getRecorder()); |
303 | 0 | value.m_content->setRecorder(shared_ptr<IWORKTextRecorder>()); |
304 | 0 | if (bool(recorder)) |
305 | 0 | recorder->replay(*value.m_content); |
306 | 0 | m_table.insertCell(value.m_column, value.m_row, value.m_value, value.m_content, value.m_dateTime, value.m_columnSpan, value.m_rowSpan, value.m_formula, value.m_formulaHC, value.m_style, value.m_type); |
307 | 0 | } |
308 | | |
309 | | void operator()(const InsertCoveredCell &value) const |
310 | 0 | { |
311 | 0 | m_table.insertCoveredCell(value.m_column, value.m_row); |
312 | 0 | } |
313 | | |
314 | | void operator()(const SetDefaultCellStyle &value) const |
315 | 0 | { |
316 | 0 | m_table.setDefaultCellStyle(value.m_type, value.m_style); |
317 | 0 | } |
318 | | |
319 | | void operator()(const SetDefaultLayoutStyle &value) const |
320 | 0 | { |
321 | 0 | m_table.setDefaultLayoutStyle(value.m_type, value.m_style); |
322 | 0 | } |
323 | | |
324 | | void operator()(const SetDefaultParagraphStyle &value) const |
325 | 0 | { |
326 | 0 | m_table.setDefaultParagraphStyle(value.m_type, value.m_style); |
327 | 0 | } |
328 | | |
329 | | private: |
330 | | IWORKTable &m_table; |
331 | | }; |
332 | | |
333 | | } |
334 | | |
335 | | struct IWORKTableRecorder::Impl |
336 | | { |
337 | | Impl(); |
338 | | |
339 | | std::deque<Element_t> m_elements; |
340 | | }; |
341 | | |
342 | | IWORKTableRecorder::Impl::Impl() |
343 | 0 | : m_elements() |
344 | 0 | { |
345 | 0 | } |
346 | | |
347 | | IWORKTableRecorder::IWORKTableRecorder() |
348 | 0 | : m_impl(new Impl()) |
349 | 0 | { |
350 | 0 | } |
351 | | |
352 | | void IWORKTableRecorder::replay(IWORKTable &table) const |
353 | 0 | { |
354 | 0 | Sender sender(table); |
355 | 0 | for (std::deque<Element_t>::const_iterator it = m_impl->m_elements.begin(); it != m_impl->m_elements.end(); ++it) |
356 | 0 | boost::apply_visitor(sender, *it); |
357 | 0 | } |
358 | | |
359 | | void IWORKTableRecorder::setComment(unsigned column, unsigned row, IWORKOutputElements const &text) |
360 | 0 | { |
361 | 0 | m_impl->m_elements.push_back(SetComment(column, row, text)); |
362 | 0 | } |
363 | | |
364 | | void IWORKTableRecorder::setSize(unsigned columns, unsigned rows) |
365 | 0 | { |
366 | 0 | m_impl->m_elements.push_back(SetSize(columns, rows)); |
367 | 0 | } |
368 | | |
369 | | void IWORKTableRecorder::setHeaders(unsigned headerColumns, unsigned headerRows, const unsigned footerRows) |
370 | 0 | { |
371 | 0 | m_impl->m_elements.push_back(SetHeaders(headerColumns, headerRows, footerRows)); |
372 | 0 | } |
373 | | |
374 | | void IWORKTableRecorder::setBandedRows(bool banded) |
375 | 0 | { |
376 | 0 | m_impl->m_elements.push_back(SetBandedRows(banded)); |
377 | 0 | } |
378 | | |
379 | | void IWORKTableRecorder::setRepeated(bool columns, bool rows) |
380 | 0 | { |
381 | 0 | m_impl->m_elements.push_back(SetRepeated(columns, rows)); |
382 | 0 | } |
383 | | |
384 | | void IWORKTableRecorder::setOrder(int order) |
385 | 0 | { |
386 | 0 | m_impl->m_elements.push_back(SetOrder(order)); |
387 | 0 | } |
388 | | |
389 | | void IWORKTableRecorder::setStyle(const IWORKStylePtr_t &style) |
390 | 0 | { |
391 | 0 | m_impl->m_elements.push_back(SetStyle(style)); |
392 | 0 | } |
393 | | |
394 | | void IWORKTableRecorder::setSizes(const IWORKColumnSizes_t &columnSizes, const IWORKRowSizes_t &rowSizes) |
395 | 0 | { |
396 | 0 | m_impl->m_elements.push_back(SetSizes(columnSizes, rowSizes)); |
397 | 0 | } |
398 | | |
399 | | void IWORKTableRecorder::setBorders(const IWORKGridLineMap_t &verticalLines, const IWORKGridLineMap_t &horizontalLines) |
400 | 0 | { |
401 | 0 | m_impl->m_elements.push_back(SetBorders(verticalLines, horizontalLines)); |
402 | 0 | } |
403 | | |
404 | | void IWORKTableRecorder::setBorders(const IWORKGridLineMap_t &verticalLeftLines, const IWORKGridLineMap_t &verticalRightLines, |
405 | | const IWORKGridLineMap_t &horizontalTopLines, const IWORKGridLineMap_t &horizontalBottomLines) |
406 | 0 | { |
407 | 0 | m_impl->m_elements.push_back(SetBorders(verticalLeftLines, verticalRightLines, |
408 | 0 | horizontalTopLines, horizontalBottomLines)); |
409 | 0 | } |
410 | | |
411 | | void IWORKTableRecorder::insertCell(const unsigned column, const unsigned row, const boost::optional<std::string> &value, const std::shared_ptr<IWORKText> &content, const boost::optional<IWORKDateTimeData> &dateTime, const unsigned columnSpan, const unsigned rowSpan, const IWORKFormulaPtr_t &formula, const boost::optional<unsigned> &formulaHC, const IWORKStylePtr_t &style, const IWORKCellType type) |
412 | 0 | { |
413 | 0 | m_impl->m_elements.push_back(InsertCell(column, row, value, content, dateTime, columnSpan, rowSpan, formula, formulaHC, style, type)); |
414 | 0 | } |
415 | | |
416 | | void IWORKTableRecorder::insertCoveredCell(const unsigned column, const unsigned row) |
417 | 0 | { |
418 | 0 | m_impl->m_elements.push_back(InsertCoveredCell(column, row)); |
419 | 0 | } |
420 | | |
421 | | void IWORKTableRecorder::setDefaultCellStyle(const IWORKTable::CellType type, const IWORKStylePtr_t &style) |
422 | 0 | { |
423 | 0 | m_impl->m_elements.push_back(SetDefaultCellStyle(type, style)); |
424 | 0 | } |
425 | | |
426 | | void IWORKTableRecorder::setDefaultLayoutStyle(const IWORKTable::CellType type, const IWORKStylePtr_t &style) |
427 | 0 | { |
428 | 0 | m_impl->m_elements.push_back(SetDefaultLayoutStyle(type, style)); |
429 | 0 | } |
430 | | |
431 | | void IWORKTableRecorder::setDefaultParagraphStyle(const IWORKTable::CellType type, const IWORKStylePtr_t &style) |
432 | 0 | { |
433 | 0 | m_impl->m_elements.push_back(SetDefaultParagraphStyle(type, style)); |
434 | 0 | } |
435 | | |
436 | | } |
437 | | |
438 | | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |