/src/libetonyek/src/lib/IWORKRecorder.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 "IWORKRecorder.h" |
11 | | |
12 | | #include <deque> |
13 | | |
14 | | #include <boost/variant.hpp> |
15 | | |
16 | | #include "IWORKCollector.h" |
17 | | #include "IWORKTableRecorder.h" |
18 | | #include "IWORKText.h" |
19 | | #include "IWORKTextRecorder.h" |
20 | | |
21 | | namespace libetonyek |
22 | | { |
23 | | |
24 | | using std::shared_ptr; |
25 | | |
26 | | namespace |
27 | | { |
28 | | |
29 | | struct CollectStyle |
30 | | { |
31 | | explicit CollectStyle(const IWORKStylePtr_t &style) |
32 | 0 | : m_style(style) |
33 | 0 | { |
34 | 0 | } |
35 | | |
36 | | const IWORKStylePtr_t m_style; |
37 | | }; |
38 | | |
39 | | struct SetGraphicStyle |
40 | | { |
41 | | explicit SetGraphicStyle(const IWORKStylePtr_t &style) |
42 | 0 | : m_style(style) |
43 | 0 | { |
44 | 0 | } |
45 | | |
46 | | const IWORKStylePtr_t m_style; |
47 | | }; |
48 | | |
49 | | struct CollectGeometry |
50 | | { |
51 | | explicit CollectGeometry(const IWORKGeometryPtr_t &geometry) |
52 | 0 | : m_geometry(geometry) |
53 | 0 | { |
54 | 0 | } |
55 | | |
56 | | const IWORKGeometryPtr_t m_geometry; |
57 | | }; |
58 | | |
59 | | struct CollectPath |
60 | | { |
61 | | explicit CollectPath(const IWORKPathPtr_t &path) |
62 | 0 | : m_path(path) |
63 | 0 | { |
64 | 0 | } |
65 | | |
66 | | const IWORKPathPtr_t m_path; |
67 | | }; |
68 | | |
69 | | struct CollectImage |
70 | | { |
71 | | CollectImage(const IWORKMediaContentPtr_t &image, const IWORKGeometryPtr_t &cropGeometry, const boost::optional<int> &order, bool locked) |
72 | 0 | : m_image(image) |
73 | 0 | , m_cropGeometry(cropGeometry) |
74 | 0 | , m_order(order) |
75 | 0 | , m_locked(locked) |
76 | 0 | { |
77 | 0 | } |
78 | | |
79 | | const IWORKMediaContentPtr_t m_image; |
80 | | const IWORKGeometryPtr_t m_cropGeometry; |
81 | | const boost::optional<int> m_order; |
82 | | const bool m_locked; |
83 | | }; |
84 | | |
85 | | struct CollectLine |
86 | | { |
87 | | explicit CollectLine(const IWORKLinePtr_t &line) |
88 | 0 | : m_line(line) |
89 | 0 | { |
90 | 0 | } |
91 | | |
92 | | const IWORKLinePtr_t m_line; |
93 | | }; |
94 | | |
95 | | struct CollectShape |
96 | | { |
97 | | CollectShape(const boost::optional<int> &order, const boost::optional<unsigned> &resizeFlags, bool locked) |
98 | 0 | : m_order(order) |
99 | 0 | , m_resizeFlags(resizeFlags) |
100 | 0 | , m_locked(locked) |
101 | 0 | { |
102 | 0 | } |
103 | | const boost::optional<int> m_order; |
104 | | const boost::optional<unsigned> m_resizeFlags; |
105 | | bool m_locked; |
106 | | }; |
107 | | |
108 | | struct CollectMedia |
109 | | { |
110 | | CollectMedia(const IWORKMediaContentPtr_t &content, const IWORKGeometryPtr_t &cropGeometry, const boost::optional<int> &order) |
111 | 0 | : m_content(content) |
112 | 0 | , m_cropGeometry(cropGeometry) |
113 | 0 | , m_order(order) |
114 | 0 | { |
115 | 0 | } |
116 | | |
117 | | const IWORKMediaContentPtr_t m_content; |
118 | | const IWORKGeometryPtr_t m_cropGeometry; |
119 | | const boost::optional<int> m_order; |
120 | | }; |
121 | | |
122 | | struct CollectStylesheet |
123 | | { |
124 | | explicit CollectStylesheet(const IWORKStylesheetPtr_t &stylesheet) |
125 | 0 | : m_stylesheet(stylesheet) |
126 | 0 | { |
127 | 0 | } |
128 | | |
129 | | const IWORKStylesheetPtr_t m_stylesheet; |
130 | | }; |
131 | | |
132 | | struct CollectTable |
133 | | { |
134 | | explicit CollectTable(const std::shared_ptr<IWORKTable> &table) |
135 | 0 | : m_table(table) |
136 | 0 | { |
137 | 0 | } |
138 | | |
139 | | const std::shared_ptr<IWORKTable> m_table; |
140 | | }; |
141 | | |
142 | | struct CollectText |
143 | | { |
144 | | explicit CollectText(const std::shared_ptr<IWORKText> &text) |
145 | 0 | : m_text(text) |
146 | 0 | { |
147 | 0 | } |
148 | | |
149 | | const std::shared_ptr<IWORKText> m_text; |
150 | | }; |
151 | | |
152 | | enum OperationType { Attachment, Attachments, Group, GroupSymbol, Level }; |
153 | | struct StartOp |
154 | | { |
155 | | explicit StartOp(const OperationType &type) |
156 | 0 | : m_type(type) |
157 | 0 | { |
158 | 0 | } |
159 | | |
160 | | OperationType m_type; |
161 | | }; |
162 | | |
163 | | struct EndOp |
164 | | { |
165 | | explicit EndOp(OperationType type) |
166 | 0 | : m_type(type) |
167 | 0 | { |
168 | 0 | } |
169 | | |
170 | | OperationType m_type; |
171 | | }; |
172 | | |
173 | | struct PushStylesheet |
174 | | { |
175 | | explicit PushStylesheet(const IWORKStylesheetPtr_t &stylesheet) |
176 | 0 | : m_stylesheet(stylesheet) |
177 | 0 | { |
178 | 0 | } |
179 | | |
180 | | const IWORKStylesheetPtr_t m_stylesheet; |
181 | | }; |
182 | | |
183 | | struct PopStylesheet |
184 | | { |
185 | | }; |
186 | | |
187 | | typedef boost::variant |
188 | | < CollectStyle |
189 | | , SetGraphicStyle |
190 | | , CollectGeometry |
191 | | , CollectPath |
192 | | , CollectImage |
193 | | , CollectLine |
194 | | , CollectShape |
195 | | , CollectMedia |
196 | | , CollectStylesheet |
197 | | , CollectTable |
198 | | , CollectText |
199 | | , EndOp |
200 | | , StartOp |
201 | | , PushStylesheet |
202 | | , PopStylesheet |
203 | | > |
204 | | Element_t; |
205 | | |
206 | | } |
207 | | |
208 | | namespace |
209 | | { |
210 | | |
211 | | struct Sender : public boost::static_visitor<void> |
212 | | { |
213 | | explicit Sender(IWORKCollector &collector) |
214 | 0 | : m_collector(collector) |
215 | 0 | { |
216 | 0 | } |
217 | | |
218 | | void operator()(const CollectStyle &value) const |
219 | 0 | { |
220 | 0 | m_collector.collectStyle(value.m_style); |
221 | 0 | } |
222 | | |
223 | | void operator()(const SetGraphicStyle &value) const |
224 | 0 | { |
225 | 0 | m_collector.setGraphicStyle(value.m_style); |
226 | 0 | } |
227 | | |
228 | | void operator()(const CollectGeometry &value) const |
229 | 0 | { |
230 | 0 | m_collector.collectGeometry(value.m_geometry); |
231 | 0 | } |
232 | | |
233 | | void operator()(const CollectPath &value) const |
234 | 0 | { |
235 | 0 | m_collector.collectBezier(value.m_path); |
236 | 0 | } |
237 | | |
238 | | void operator()(const CollectImage &value) const |
239 | 0 | { |
240 | 0 | m_collector.collectImage(value.m_image, value.m_cropGeometry, value.m_order, value.m_locked); |
241 | 0 | } |
242 | | |
243 | | void operator()(const CollectLine &value) const |
244 | 0 | { |
245 | 0 | m_collector.collectLine(value.m_line); |
246 | 0 | } |
247 | | |
248 | | void operator()(const CollectShape &value) const |
249 | 0 | { |
250 | 0 | m_collector.collectShape(value.m_order, value.m_resizeFlags, value.m_locked); |
251 | 0 | } |
252 | | |
253 | | void operator()(const CollectMedia &value) const |
254 | 0 | { |
255 | 0 | m_collector.collectMedia(value.m_content, value.m_cropGeometry, value.m_order); |
256 | 0 | } |
257 | | |
258 | | void operator()(const CollectStylesheet &value) const |
259 | 0 | { |
260 | 0 | m_collector.collectStylesheet(value.m_stylesheet); |
261 | 0 | } |
262 | | |
263 | | void operator()(const CollectTable &value) const |
264 | 0 | { |
265 | 0 | const shared_ptr<IWORKTableRecorder> recorder(value.m_table->getRecorder()); |
266 | 0 | value.m_table->setRecorder(shared_ptr<IWORKTableRecorder>()); |
267 | 0 | if (bool(recorder)) |
268 | 0 | recorder->replay(*value.m_table); |
269 | 0 | m_collector.collectTable(value.m_table); |
270 | 0 | } |
271 | | |
272 | | void operator()(const CollectText &value) const |
273 | 0 | { |
274 | 0 | const shared_ptr<IWORKTextRecorder> recorder(value.m_text->getRecorder()); |
275 | 0 | value.m_text->setRecorder(shared_ptr<IWORKTextRecorder>()); |
276 | 0 | if (bool(recorder)) |
277 | 0 | recorder->replay(*value.m_text); |
278 | 0 | m_collector.collectText(value.m_text); |
279 | 0 | } |
280 | | |
281 | | void operator()(const EndOp &value) const |
282 | 0 | { |
283 | 0 | switch (value.m_type) |
284 | 0 | { |
285 | 0 | case OperationType::Attachment: |
286 | 0 | m_collector.endAttachment(); |
287 | 0 | break; |
288 | 0 | case OperationType::Attachments: |
289 | 0 | m_collector.endAttachments(); |
290 | 0 | break; |
291 | 0 | case OperationType::Group: |
292 | 0 | m_collector.endGroup(); |
293 | 0 | break; |
294 | 0 | case OperationType::GroupSymbol: |
295 | 0 | m_collector.closeGroup(); |
296 | 0 | break; |
297 | 0 | case OperationType::Level: |
298 | 0 | m_collector.endLevel(); |
299 | 0 | break; |
300 | 0 | default: |
301 | 0 | ETONYEK_DEBUG_MSG(("Sender::operator(EndOp)[IWORKRecorder.cpp]: unexpected type\n")); |
302 | 0 | break; |
303 | 0 | } |
304 | 0 | } |
305 | | |
306 | | void operator()(const StartOp &value) const |
307 | 0 | { |
308 | 0 | switch (value.m_type) |
309 | 0 | { |
310 | 0 | case OperationType::Attachment: |
311 | 0 | m_collector.startAttachment(); |
312 | 0 | break; |
313 | 0 | case OperationType::Attachments: |
314 | 0 | m_collector.startAttachments(); |
315 | 0 | break; |
316 | 0 | case OperationType::Group: |
317 | 0 | m_collector.startGroup(); |
318 | 0 | break; |
319 | 0 | case OperationType::GroupSymbol: |
320 | 0 | m_collector.openGroup(); |
321 | 0 | break; |
322 | 0 | case OperationType::Level: |
323 | 0 | m_collector.startLevel(); |
324 | 0 | break; |
325 | 0 | default: |
326 | 0 | ETONYEK_DEBUG_MSG(("Sender::operator(StartOp)[IWORKRecorder.cpp]: unexpected type\n")); |
327 | 0 | break; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | | void operator()(const PushStylesheet &value) const |
332 | 0 | { |
333 | 0 | m_collector.pushStylesheet(value.m_stylesheet); |
334 | 0 | } |
335 | | |
336 | | void operator()(const PopStylesheet &) const |
337 | 0 | { |
338 | 0 | m_collector.popStylesheet(); |
339 | 0 | } |
340 | | |
341 | | private: |
342 | | IWORKCollector &m_collector; |
343 | | }; |
344 | | |
345 | | } |
346 | | |
347 | | struct IWORKRecorder::Impl |
348 | | { |
349 | | Impl(); |
350 | | |
351 | | std::deque<Element_t> m_elements; |
352 | | }; |
353 | | |
354 | | IWORKRecorder::Impl::Impl() |
355 | 0 | : m_elements() |
356 | 0 | { |
357 | 0 | } |
358 | | |
359 | | IWORKRecorder::IWORKRecorder() |
360 | 0 | : m_impl(new Impl()) |
361 | 0 | { |
362 | 0 | } |
363 | | |
364 | | void IWORKRecorder::replay(IWORKCollector &collector) const |
365 | 0 | { |
366 | 0 | Sender sender(collector); |
367 | 0 | for (std::deque<Element_t>::const_iterator it = m_impl->m_elements.begin(); it != m_impl->m_elements.end(); ++it) |
368 | 0 | boost::apply_visitor(sender, *it); |
369 | 0 | } |
370 | | |
371 | | void IWORKRecorder::collectStyle(const IWORKStylePtr_t &style) |
372 | 0 | { |
373 | 0 | m_impl->m_elements.push_back(CollectStyle(style)); |
374 | 0 | } |
375 | | |
376 | | void IWORKRecorder::setGraphicStyle(const IWORKStylePtr_t &style) |
377 | 0 | { |
378 | 0 | m_impl->m_elements.push_back(SetGraphicStyle(style)); |
379 | 0 | } |
380 | | |
381 | | void IWORKRecorder::collectGeometry(const IWORKGeometryPtr_t &geometry) |
382 | 0 | { |
383 | 0 | m_impl->m_elements.push_back(CollectGeometry(geometry)); |
384 | 0 | } |
385 | | |
386 | | void IWORKRecorder::collectPath(const IWORKPathPtr_t &path) |
387 | 0 | { |
388 | 0 | m_impl->m_elements.push_back(CollectPath(path)); |
389 | 0 | } |
390 | | |
391 | | void IWORKRecorder::collectImage(const IWORKMediaContentPtr_t &image, const IWORKGeometryPtr_t &cropGeometry, const boost::optional<int> &order, bool locked) |
392 | 0 | { |
393 | 0 | m_impl->m_elements.push_back(CollectImage(image, cropGeometry, order, locked)); |
394 | 0 | } |
395 | | |
396 | | void IWORKRecorder::collectLine(const IWORKLinePtr_t &line) |
397 | 0 | { |
398 | 0 | m_impl->m_elements.push_back(CollectLine(line)); |
399 | 0 | } |
400 | | |
401 | | void IWORKRecorder::collectShape(const boost::optional<int> &order, const boost::optional<unsigned> &resizeFlags, bool locked) |
402 | 0 | { |
403 | 0 | m_impl->m_elements.push_back(CollectShape(order,resizeFlags,locked)); |
404 | 0 | } |
405 | | |
406 | | void IWORKRecorder::collectMedia(const IWORKMediaContentPtr_t &content, const IWORKGeometryPtr_t &cropGeometry, const boost::optional<int> &order) |
407 | 0 | { |
408 | 0 | m_impl->m_elements.push_back(CollectMedia(content, cropGeometry, order)); |
409 | 0 | } |
410 | | |
411 | | void IWORKRecorder::collectStylesheet(const IWORKStylesheetPtr_t &stylesheet) |
412 | 0 | { |
413 | 0 | m_impl->m_elements.push_back(CollectStylesheet(stylesheet)); |
414 | 0 | } |
415 | | |
416 | | void IWORKRecorder::collectTable(const std::shared_ptr<IWORKTable> &table) |
417 | 0 | { |
418 | 0 | m_impl->m_elements.push_back(CollectTable(table)); |
419 | 0 | } |
420 | | |
421 | | void IWORKRecorder::collectText(const std::shared_ptr<IWORKText> &text) |
422 | 0 | { |
423 | 0 | m_impl->m_elements.push_back(CollectText(text)); |
424 | 0 | } |
425 | | |
426 | | |
427 | | void IWORKRecorder::startAttachment() |
428 | 0 | { |
429 | 0 | m_impl->m_elements.push_back(StartOp(OperationType::Attachment)); |
430 | 0 | } |
431 | | |
432 | | void IWORKRecorder::endAttachment() |
433 | 0 | { |
434 | 0 | m_impl->m_elements.push_back(EndOp(OperationType::Attachment)); |
435 | 0 | } |
436 | | |
437 | | void IWORKRecorder::startAttachments() |
438 | 0 | { |
439 | 0 | m_impl->m_elements.push_back(StartOp(OperationType::Attachments)); |
440 | 0 | } |
441 | | |
442 | | void IWORKRecorder::endAttachments() |
443 | 0 | { |
444 | 0 | m_impl->m_elements.push_back(EndOp(OperationType::Attachments)); |
445 | 0 | } |
446 | | |
447 | | void IWORKRecorder::openGroup() |
448 | 0 | { |
449 | 0 | m_impl->m_elements.push_back(StartOp(OperationType::GroupSymbol)); |
450 | 0 | } |
451 | | |
452 | | void IWORKRecorder::closeGroup() |
453 | 0 | { |
454 | 0 | m_impl->m_elements.push_back(EndOp(OperationType::GroupSymbol)); |
455 | 0 | } |
456 | | |
457 | | void IWORKRecorder::startGroup() |
458 | 0 | { |
459 | 0 | m_impl->m_elements.push_back(StartOp(OperationType::Group)); |
460 | 0 | } |
461 | | |
462 | | void IWORKRecorder::endGroup() |
463 | 0 | { |
464 | 0 | m_impl->m_elements.push_back(EndOp(OperationType::Group)); |
465 | 0 | } |
466 | | |
467 | | void IWORKRecorder::startLevel() |
468 | 0 | { |
469 | 0 | m_impl->m_elements.push_back(StartOp(OperationType::Level)); |
470 | 0 | } |
471 | | |
472 | | void IWORKRecorder::endLevel() |
473 | 0 | { |
474 | 0 | m_impl->m_elements.push_back(EndOp(OperationType::Level)); |
475 | 0 | } |
476 | | |
477 | | void IWORKRecorder::pushStylesheet(const IWORKStylesheetPtr_t &stylesheet) |
478 | 0 | { |
479 | 0 | m_impl->m_elements.push_back(PushStylesheet(stylesheet)); |
480 | 0 | } |
481 | | |
482 | | void IWORKRecorder::popStylesheet() |
483 | 0 | { |
484 | 0 | m_impl->m_elements.push_back(PopStylesheet()); |
485 | 0 | } |
486 | | |
487 | | } // namespace libetonyek |
488 | | |
489 | | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |