Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* libmspub
3 : * Version: MPL 1.1 / GPLv2+ / LGPLv2+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Brennan Vincent <brennanv@email.arizona.edu>
17 : *
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
25 : * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 : * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
27 : * instead of those above.
28 : */
29 : #include "ShapeGroupElement.h"
30 :
31 0 : libmspub::ShapeGroupElement::ShapeGroupElement(ShapeGroupElement *parent) : m_shapeInfo(), m_parent(parent), m_children(), m_seqNum(0), m_transform()
32 : {
33 0 : if (m_parent)
34 : {
35 0 : m_parent->m_children.push_back(this);
36 : }
37 0 : }
38 :
39 0 : libmspub::ShapeGroupElement::~ShapeGroupElement()
40 : {
41 0 : for (unsigned i = 0; i < m_children.size(); ++i)
42 : {
43 0 : delete m_children[i];
44 : }
45 0 : }
46 :
47 0 : libmspub::ShapeGroupElement::ShapeGroupElement(ShapeGroupElement *parent, unsigned seqNum) : m_shapeInfo(), m_parent(parent), m_children(), m_seqNum(seqNum), m_transform()
48 : {
49 0 : if (m_parent)
50 : {
51 0 : m_parent->m_children.push_back(this);
52 : }
53 0 : }
54 :
55 0 : void libmspub::ShapeGroupElement::setShapeInfo(const ShapeInfo &shapeInfo)
56 : {
57 0 : m_shapeInfo = shapeInfo;
58 0 : }
59 :
60 0 : void libmspub::ShapeGroupElement::setTransform(const VectorTransformation2D &transform)
61 : {
62 0 : m_transform = transform;
63 0 : }
64 :
65 0 : void libmspub::ShapeGroupElement::setup(boost::function<void(ShapeGroupElement &self)> visitor)
66 : {
67 0 : visitor(*this);
68 0 : for (unsigned i = 0; i < m_children.size(); ++i)
69 : {
70 0 : m_children[i]->setup(visitor);
71 : }
72 0 : }
73 :
74 0 : void libmspub::ShapeGroupElement::visit(boost::function<
75 : boost::function<void(void)>
76 : (const libmspub::ShapeInfo &info, const libmspub::Coordinate &relativeTo, const libmspub::VectorTransformation2D &foldedTransform, bool isGroup, const libmspub::VectorTransformation2D &thisTransform)
77 : > visitor, const Coordinate &relativeTo, const VectorTransformation2D &parentFoldedTransform) const
78 : {
79 0 : const ShapeInfo &info = m_shapeInfo.get_value_or(ShapeInfo());
80 0 : Coordinate coord = info.m_coordinates.get_value_or(Coordinate());
81 0 : double centerX = ((double)coord.m_xs + (double)coord.m_xe) / (2 * EMUS_IN_INCH);
82 0 : double centerY = ((double)coord.m_ys + (double)coord.m_ye) / (2 * EMUS_IN_INCH);
83 0 : double relativeCenterX = ((double)relativeTo.m_xs + (double)relativeTo.m_xe) / (2 * EMUS_IN_INCH);
84 0 : double relativeCenterY = ((double)relativeTo.m_ys + (double)relativeTo.m_ye) / (2 * EMUS_IN_INCH);
85 0 : double offsetX = centerX - relativeCenterX;
86 0 : double offsetY = centerY - relativeCenterY;
87 0 : VectorTransformation2D foldedTransform = VectorTransformation2D::fromTranslate(-offsetX, -offsetY)
88 0 : * parentFoldedTransform * VectorTransformation2D::fromTranslate(offsetX, offsetY) * m_transform;
89 0 : boost::function<void(void)> afterOp = visitor(info, relativeTo, foldedTransform, isGroup(), m_transform);
90 0 : for (unsigned i = 0; i < m_children.size(); ++i)
91 : {
92 0 : m_children[i]->visit(visitor, coord, foldedTransform);
93 : }
94 0 : afterOp();
95 0 : }
96 :
97 0 : void libmspub::ShapeGroupElement::visit(boost::function<
98 : boost::function<void(void)>
99 : (const libmspub::ShapeInfo &info, const libmspub::Coordinate &relativeTo, const libmspub::VectorTransformation2D &foldedTransform, bool isGroup, const libmspub::VectorTransformation2D &thisTransform)
100 : > visitor) const
101 : {
102 0 : Coordinate origin;
103 0 : VectorTransformation2D identity;
104 0 : visit(visitor, origin, identity);
105 0 : }
106 :
107 0 : bool libmspub::ShapeGroupElement::isGroup() const
108 : {
109 0 : return !m_children.empty();
110 : }
111 :
112 0 : libmspub::ShapeGroupElement *libmspub::ShapeGroupElement::getParent()
113 : {
114 0 : return m_parent;
115 : }
116 :
117 0 : const libmspub::ShapeGroupElement *libmspub::ShapeGroupElement::getParent() const
118 : {
119 0 : return m_parent;
120 : }
121 :
122 0 : void libmspub::ShapeGroupElement::setSeqNum(unsigned seqNum)
123 : {
124 0 : m_seqNum = seqNum;
125 0 : }
126 :
127 0 : unsigned libmspub::ShapeGroupElement::getSeqNum() const
128 : {
129 0 : return m_seqNum;
130 : }
131 :
132 : /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|