QXPContentCollector.h Source File documentation

QXPContentCollector.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/*
3 * This file is part of the libqxp 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#ifndef QXPCONTENTCOLLECTOR_H_INCLUDED
11#define QXPCONTENTCOLLECTOR_H_INCLUDED
12
13#include "QXPCollector.h"
14#include <vector>
15#include <unordered_map>
16#include <memory>
17#include <functional>
18#include <type_traits>
19
20#include "QXPTypes.h"
21
22namespace libqxp
23{
24
25 class QXPContentCollector : public QXPCollector
26{
27 // disable copying
28 QXPContentCollector(const QXPContentCollector &other) = delete;
29 QXPContentCollector &operator=(const QXPContentCollector &other) = delete;
30
31public:
32 QXPContentCollector(librevenge::RVNGDrawingInterface *painter);
33 ~QXPContentCollector();
34
35 void startDocument() override;
36 void endDocument() override;
37
38 void startPage(const Page &page) override;
39 void endPage() override;
40
41 void collectDocumentProperties(const QXPDocumentProperties &props) override;
42
43 void collectLine(const std::shared_ptr<Line> &line) override;
44 void collectBox(const std::shared_ptr<Box> &box) override;
45 void collectTextBox(const std::shared_ptr<TextBox> &box) override;
46 void collectTextPath(const std::shared_ptr<TextPath> &textPath) override;
47 void collectGroup(const std::shared_ptr<Group> &group) override;
48
49 void collectText(const std::shared_ptr<Text> &text, const unsigned linkId) override;
50
51private:
52 struct CollectedPage;
53
54 template<typename T>
55 using ObjectHandler = std::function<void(const std::shared_ptr<T> &obj, const CollectedPage &)>;
56
57 template<typename T, typename TThis>
58 using ObjectHandlerMember = std::function<void(TThis *, const std::shared_ptr<T> &obj, const CollectedPage &)>;
59
60 class CollectedObjectInterface
61 {
62 public:
63 virtual ~CollectedObjectInterface() = default;
64
65 virtual void draw(const CollectedPage &page) = 0;
66
67 virtual unsigned zIndex() const = 0;
68 virtual void setZIndex(unsigned value) = 0;
69 };
70
71 template<typename T>
72 class CollectedObject : public CollectedObjectInterface
73 {
74 static_assert(std::is_base_of<Object, T>::value, "T is not Object");
75 public:
76 const std::shared_ptr<T> object;
77
78 CollectedObject(const std::shared_ptr<T> &obj, const ObjectHandler<T> &handler)
79 : object(obj), m_handler(handler), m_isProcessed(false)
80 { }
81
82 void draw(const CollectedPage &page) override
83 {
84 if (!m_isProcessed)
85 {
86 m_handler(object, page);
87 }
88 m_isProcessed = true;
89 }
90
91 unsigned zIndex() const override
92 {
93 return object->zIndex;
94 }
95
96 void setZIndex(unsigned value) override
97 {
98 object->zIndex = value;
99 }
100
101 private:
102 const ObjectHandler<T> m_handler;
103 bool m_isProcessed;
104 };
105
106 struct CollectedPage
107 {
108 const PageSettings settings;
109 std::vector<std::shared_ptr<CollectedObject<Group>>> groups;
110 std::vector<std::shared_ptr<TextObject>> linkedTextObjects;
111 std::vector<std::shared_ptr<CollectedObjectInterface>> objects;
112
113 CollectedPage(const PageSettings &pageSettings)
114 : settings(pageSettings), groups(), linkedTextObjects(), objects()
115 { }
116
117 double getX(const double x) const;
118 double getX(const std::shared_ptr<Object> &obj) const;
119 double getY(const double y) const;
120 double getY(const std::shared_ptr<Object> &obj) const;
121 Point getPoint(const Point &p) const;
122 };
123
124 librevenge::RVNGDrawingInterface *m_painter;
125
126 bool m_isDocumentStarted;
127 bool m_isCollectingFacingPage;
128
129 std::vector<CollectedPage> m_unprocessedPages;
130
131 std::unordered_map<unsigned, std::shared_ptr<Text>> m_linkTextMap;
132 std::unordered_map<unsigned, std::unordered_map<unsigned, std::shared_ptr<TextObject>>> m_linkIndexedTextObjectsMap;
133
134 QXPDocumentProperties m_docProps;
135
136 CollectedPage &getInsertionPage(const std::shared_ptr<Object> &obj);
137
138 template<typename T>
139 std::shared_ptr<CollectedObject<T>> addObject(const std::shared_ptr<T> &obj, const ObjectHandlerMember<T, QXPContentCollector> &handler)
140 {
141 auto collectedObj = std::make_shared<CollectedObject<T>>(obj, std::bind(handler, this, std::placeholders::_1, std::placeholders::_2));
142 getInsertionPage(obj).objects.push_back(collectedObj);
143 return collectedObj;
144 }
145
146 void draw(bool force = false);
147
148 void collectTextObject(const std::shared_ptr<TextObject> &textObj, CollectedPage &page);
149 void updateLinkedTexts();
150 bool hasUnfinishedLinkedTexts();
151
152 void drawLine(const std::shared_ptr<Line> &line, const CollectedPage &page);
153 void drawBox(const std::shared_ptr<Box> &box, const CollectedPage &page);
154 void drawRectangle(const std::shared_ptr<Box> &box, const CollectedPage &page);
155 void drawOval(const std::shared_ptr<Box> &oval, const CollectedPage &page);
156 void drawPolygon(const std::shared_ptr<Box> &polygon, const CollectedPage &page);
157 void drawBezierBox(const std::shared_ptr<Box> &box, const CollectedPage &page);
158 void drawTextBox(const std::shared_ptr<TextBox> &textbox, const CollectedPage &page);
159 void drawTextPath(const std::shared_ptr<TextPath> &textPath, const CollectedPage &page);
160 void drawText(const std::shared_ptr<Text> &text, const LinkedTextSettings &linkSettings);
161 void drawGroup(const std::shared_ptr<Group> &group, const CollectedPage &page);
162
163 void writeFill(librevenge::RVNGPropertyList &propList, const boost::optional<Fill> &fill);
164 void writeFrame(librevenge::RVNGPropertyList &propList, const Frame &frame, const bool runaround, const bool allowHairline = false);
165};
166
167}
168
169#endif // QXPCONTENTCOLLECTOR_H_INCLUDED
170
171/* vim:set shiftwidth=2 softtabstop=2 expandtab: */
QXPCollector.h
QXPTypes.h
libqxp::QXPCollector::QXPCollector
QXPCollector(const QXPCollector &other)=delete
libqxp::QXPContentCollector::CollectedObjectInterface
Definition QXPContentCollector.h:61
libqxp::QXPContentCollector::CollectedObjectInterface::zIndex
virtual unsigned zIndex() const =0
libqxp::QXPContentCollector::CollectedObjectInterface::setZIndex
virtual void setZIndex(unsigned value)=0
libqxp::QXPContentCollector::CollectedObjectInterface::~CollectedObjectInterface
virtual ~CollectedObjectInterface()=default
libqxp::QXPContentCollector::CollectedObjectInterface::draw
virtual void draw(const CollectedPage &page)=0
libqxp::QXPContentCollector::CollectedObject::m_isProcessed
bool m_isProcessed
Definition QXPContentCollector.h:103
libqxp::QXPContentCollector::CollectedObject::draw
void draw(const CollectedPage &page) override
Definition QXPContentCollector.h:82
libqxp::QXPContentCollector::CollectedObject::setZIndex
void setZIndex(unsigned value) override
Definition QXPContentCollector.h:96
libqxp::QXPContentCollector::CollectedObject::zIndex
unsigned zIndex() const override
Definition QXPContentCollector.h:91
libqxp::QXPContentCollector::CollectedObject::CollectedObject
CollectedObject(const std::shared_ptr< T > &obj, const ObjectHandler< T > &handler)
Definition QXPContentCollector.h:78
libqxp::QXPContentCollector::CollectedObject::object
const std::shared_ptr< T > object
Definition QXPContentCollector.h:76
libqxp::QXPContentCollector::CollectedObject::m_handler
const ObjectHandler< T > m_handler
Definition QXPContentCollector.h:102
libqxp::QXPContentCollector::endDocument
void endDocument() override
Definition QXPContentCollector.cpp:319
libqxp::QXPContentCollector::m_isCollectingFacingPage
bool m_isCollectingFacingPage
Definition QXPContentCollector.h:127
libqxp::QXPContentCollector::m_unprocessedPages
std::vector< CollectedPage > m_unprocessedPages
Definition QXPContentCollector.h:129
libqxp::QXPContentCollector::m_painter
librevenge::RVNGDrawingInterface * m_painter
Definition QXPContentCollector.h:124
libqxp::QXPContentCollector::drawOval
void drawOval(const std::shared_ptr< Box > &oval, const CollectedPage &page)
Definition QXPContentCollector.cpp:626
libqxp::QXPContentCollector::collectTextObject
void collectTextObject(const std::shared_ptr< TextObject > &textObj, CollectedPage &page)
Definition QXPContentCollector.cpp:472
libqxp::QXPContentCollector::drawBezierBox
void drawBezierBox(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition QXPContentCollector.cpp:676
libqxp::QXPContentCollector::drawText
void drawText(const std::shared_ptr< Text > &text, const LinkedTextSettings &linkSettings)
Definition QXPContentCollector.cpp:825
libqxp::QXPContentCollector::startPage
void startPage(const Page &page) override
Definition QXPContentCollector.cpp:335
libqxp::QXPContentCollector::ObjectHandler
std::function< void(const std::shared_ptr< T > &obj, const CollectedPage &)> ObjectHandler
Definition QXPContentCollector.h:55
libqxp::QXPContentCollector::~QXPContentCollector
~QXPContentCollector()
Definition QXPContentCollector.cpp:301
libqxp::QXPContentCollector::m_docProps
QXPDocumentProperties m_docProps
Definition QXPContentCollector.h:134
libqxp::QXPContentCollector::drawRectangle
void drawRectangle(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition QXPContentCollector.cpp:591
libqxp::QXPContentCollector::getInsertionPage
CollectedPage & getInsertionPage(const std::shared_ptr< Object > &obj)
Definition QXPContentCollector.cpp:414
libqxp::QXPContentCollector::ObjectHandlerMember
std::function< void(TThis *, const std::shared_ptr< T > &obj, const CollectedPage &)> ObjectHandlerMember
Definition QXPContentCollector.h:58
libqxp::QXPContentCollector::drawGroup
void drawGroup(const std::shared_ptr< Group > &group, const CollectedPage &page)
Definition QXPContentCollector.cpp:1041
libqxp::QXPContentCollector::updateLinkedTexts
void updateLinkedTexts()
Definition QXPContentCollector.cpp:494
libqxp::QXPContentCollector::drawLine
void drawLine(const std::shared_ptr< Line > &line, const CollectedPage &page)
Definition QXPContentCollector.cpp:529
libqxp::QXPContentCollector::writeFill
void writeFill(librevenge::RVNGPropertyList &propList, const boost::optional< Fill > &fill)
Definition QXPContentCollector.cpp:1072
libqxp::QXPContentCollector::writeFrame
void writeFrame(librevenge::RVNGPropertyList &propList, const Frame &frame, const bool runaround, const bool allowHairline=false)
Definition QXPContentCollector.cpp:1083
libqxp::QXPContentCollector::addObject
std::shared_ptr< CollectedObject< T > > addObject(const std::shared_ptr< T > &obj, const ObjectHandlerMember< T, QXPContentCollector > &handler)
Definition QXPContentCollector.h:139
libqxp::QXPContentCollector::drawTextPath
void drawTextPath(const std::shared_ptr< TextPath > &textPath, const CollectedPage &page)
Definition QXPContentCollector.cpp:756
libqxp::QXPContentCollector::collectText
void collectText(const std::shared_ptr< Text > &text, const unsigned linkId) override
Definition QXPContentCollector.cpp:397
libqxp::QXPContentCollector::operator=
QXPContentCollector & operator=(const QXPContentCollector &other)=delete
libqxp::QXPContentCollector::drawBox
void drawBox(const std::shared_ptr< Box > &box, const CollectedPage &page)
Definition QXPContentCollector.cpp:571
libqxp::QXPContentCollector::collectLine
void collectLine(const std::shared_ptr< Line > &line) override
Definition QXPContentCollector.cpp:356
libqxp::QXPContentCollector::draw
void draw(bool force=false)
Definition QXPContentCollector.cpp:423
libqxp::QXPContentCollector::drawTextBox
void drawTextBox(const std::shared_ptr< TextBox > &textbox, const CollectedPage &page)
Definition QXPContentCollector.cpp:706
libqxp::QXPContentCollector::m_linkTextMap
std::unordered_map< unsigned, std::shared_ptr< Text > > m_linkTextMap
Definition QXPContentCollector.h:131
libqxp::QXPContentCollector::hasUnfinishedLinkedTexts
bool hasUnfinishedLinkedTexts()
Definition QXPContentCollector.cpp:513
libqxp::QXPContentCollector::collectGroup
void collectGroup(const std::shared_ptr< Group > &group) override
Definition QXPContentCollector.cpp:390
libqxp::QXPContentCollector::drawPolygon
void drawPolygon(const std::shared_ptr< Box > &polygon, const CollectedPage &page)
Definition QXPContentCollector.cpp:650
libqxp::QXPContentCollector::m_isDocumentStarted
bool m_isDocumentStarted
Definition QXPContentCollector.h:126
libqxp::QXPContentCollector::collectTextPath
void collectTextPath(const std::shared_ptr< TextPath > &textPath) override
Definition QXPContentCollector.cpp:378
libqxp::QXPContentCollector::collectDocumentProperties
void collectDocumentProperties(const QXPDocumentProperties &props) override
Definition QXPContentCollector.cpp:351
libqxp::QXPContentCollector::m_linkIndexedTextObjectsMap
std::unordered_map< unsigned, std::unordered_map< unsigned, std::shared_ptr< TextObject > > > m_linkIndexedTextObjectsMap
Definition QXPContentCollector.h:132
libqxp::QXPContentCollector::endPage
void endPage() override
Definition QXPContentCollector.cpp:345
libqxp::QXPContentCollector::QXPContentCollector
QXPContentCollector(const QXPContentCollector &other)=delete
libqxp::QXPContentCollector::startDocument
void startDocument() override
Definition QXPContentCollector.cpp:309
libqxp::QXPContentCollector::collectTextBox
void collectTextBox(const std::shared_ptr< TextBox > &box) override
Definition QXPContentCollector.cpp:366
libqxp::QXPContentCollector::collectBox
void collectBox(const std::shared_ptr< Box > &box) override
Definition QXPContentCollector.cpp:361
libqxp
Definition libqxp_utils.cpp:25
libqxp::Frame
Definition QXPTypes.h:345
libqxp::LinkedTextSettings
Definition QXPTypes.h:362
libqxp::PageSettings
Definition QXPTypes.h:537
libqxp::Page
Definition QXPTypes.h:546
libqxp::Point
Definition QXPTypes.h:25
libqxp::QXPContentCollector::CollectedPage
Definition QXPContentCollector.h:107
libqxp::QXPContentCollector::CollectedPage::getX
double getX(const double x) const
Definition QXPContentCollector.cpp:1156
libqxp::QXPContentCollector::CollectedPage::settings
const PageSettings settings
Definition QXPContentCollector.h:108
libqxp::QXPContentCollector::CollectedPage::objects
std::vector< std::shared_ptr< CollectedObjectInterface > > objects
Definition QXPContentCollector.h:111
libqxp::QXPContentCollector::CollectedPage::getY
double getY(const double y) const
Definition QXPContentCollector.cpp:1166
libqxp::QXPContentCollector::CollectedPage::groups
std::vector< std::shared_ptr< CollectedObject< Group > > > groups
Definition QXPContentCollector.h:109
libqxp::QXPContentCollector::CollectedPage::getPoint
Point getPoint(const Point &p) const
Definition QXPContentCollector.cpp:1176
libqxp::QXPContentCollector::CollectedPage::CollectedPage
CollectedPage(const PageSettings &pageSettings)
Definition QXPContentCollector.h:113
libqxp::QXPContentCollector::CollectedPage::linkedTextObjects
std::vector< std::shared_ptr< TextObject > > linkedTextObjects
Definition QXPContentCollector.h:110
libqxp::QXPDocumentProperties
Definition QXPTypes.h:561