Debian Codegraph @

utils/fgqcanvas/fgcanvasgroup.cpp

raw symbols SCIP
1 #include "fgcanvasgroup.h"
2
3 #include <QDebug>
4
5 #include "canvasitem.h"
6 #include "localprop.h"
7 #include "fgcanvaspaintcontext.h"
8 #include "fgcanvaspath.h"
9 #include "fgcanvastext.h"
10 #include "fgqcanvasmap.h"
11 #include "fgqcanvasimage.h"
12
13 class ChildOrderingFunction
14 {
15 public:
16 bool operator()(const FGCanvasElement* a, FGCanvasElement* b)
17 {
18 if (a->zIndex() == b->zIndex()) {
19 // use prop node positions in the parent
20 return a->property()->position() < b->property()->position();
21 }
22
23 return a->zIndex() < b->zIndex();
24 }
25 };
26
27 FGCanvasGroup::FGCanvasGroup(FGCanvasGroup* pr, LocalProp* prop) :
28 FGCanvasElement(pr, prop)
29 {
30 }
31
32 const FGCanvasElementVec &FGCanvasGroup::children() const
33 {
34 return _children;
35 }
36
37 void FGCanvasGroup::markChildZIndicesDirty() const
38 {
39 _zIndicesDirty = true;
40 }
41
42 bool FGCanvasGroup::hasChilden() const
43 {
44 return !_children.empty();
45 }
46
47 unsigned int FGCanvasGroup::childCount() const
48 {
49 return _children.size();
50 }
51
52 FGCanvasElement *FGCanvasGroup::childAt(unsigned int index) const
53 {
54 return _children.at(index);
55 }
56
57 unsigned int FGCanvasGroup::indexOfChild(const FGCanvasElement *e) const
58 {
59 auto it = std::find(_children.begin(), _children.end(), e);
60 if (it == _children.end()) {
61 qWarning() << Q_FUNC_INFO << "not found";
62 return 0;
63 }
64
65 return std::distance(_children.begin(), it);
66 }
67
68 CanvasItem *FGCanvasGroup::createQuickItem(QQuickItem *parent)
69 {
70 _quick = new CanvasItem(parent);
71
72 for (auto e : _children) {
73 e->createQuickItem(_quick);
74 }
75
76 return _quick;
77 }
78
79 void FGCanvasGroup::doPaint(FGCanvasPaintContext *context) const
80 {
81 for (FGCanvasElement* element : _children) {
82 element->paint(context);
83 }
84 }
85
86 void FGCanvasGroup::doPolish()
87 {
88 if (_cachedSymbolDirty) {
89 qDebug() << _propertyRoot->path() << "should use symbol cache:" << _propertyRoot->value("symbol-type", QVariant()).toByteArray();
90 _cachedSymbolDirty = false;
91 }
92
93 if (_zIndicesDirty) {
94 std::sort(_children.begin(), _children.end(), ChildOrderingFunction());
95 _zIndicesDirty = false;
96 resetChildQuickItemZValues();
97 }
98
99 for (FGCanvasElement* element : _children) {
100 element->polish();
101 }
102 }
103
104 void FGCanvasGroup::resetChildQuickItemZValues()
105 {
106 int counter = 0;
107 for (auto e : _children) {
108 auto qq = e->quickItem();
109 if (qq) {
110 qq->setZ(counter++);
111 }
112 }
113 }
114
115 bool FGCanvasGroup::onChildAdded(LocalProp *prop)
116 {
117 const bool isRootGroup = (_parent == nullptr);
118 int newChildCount = 0;
119
120 if (FGCanvasElement::onChildAdded(prop)) {
121 return true;
122 }
123
124 const QByteArray nm = prop->name();
125 if (nm == "group") {
126 _children.push_back(new FGCanvasGroup(this, prop));
127 newChildCount++;
128 } else if (nm == "path") {
129 _children.push_back(new FGCanvasPath(this, prop));
130 newChildCount++;
131 } else if (nm == "text") {
132 _children.push_back(new FGCanvasText(this, prop));
133 newChildCount++;
134 } else if (nm == "image") {
135 _children.push_back(new FGQCanvasImage(this, prop));
136 newChildCount++;
137 } else if (nm == "map") {
138 _children.push_back(new FGQCanvasMap(this, prop));
139 newChildCount++;
140 } else if (nm == "symbol-type") {
141 connect(prop, &LocalProp::valueChanged, this, &FGCanvasGroup::markCachedSymbolDirty);
142 return true;
143 }
144
145 if (isRootGroup) {
146 // ignore all of these, handled by the enclosing canvas view
147 if (nm == "size") {
148 connect(prop, &LocalProp::valueChanged, this, &FGCanvasGroup::canvasSizeChanged);
149 return true;
150 }
151
152 if ((nm == "view") || nm.startsWith("status") || (nm == "name") || (nm == "mipmapping") || (nm == "placement")) {
153 return true;
154 }
155 }
156
157 if (newChildCount > 0) {
158 markChildZIndicesDirty();
159
160 if (_quick) {
161 _children.back()->createQuickItem(_quick);
162 }
163
164 emit childAdded();
165 return true;
166 }
167
168 qDebug() << "saw unknown group child" << prop->name();
169 return false;
170 }
171
172 bool FGCanvasGroup::onChildRemoved(LocalProp *prop)
173 {
174 if (FGCanvasElement::onChildRemoved(prop)) {
175 return true;
176 }
177
178 const QByteArray nm = prop->name();
179 if ((nm == "group") || (nm == "image") || (nm == "path") || (nm == "text") || (nm == "map")) {
180 int removedChildIndex = indexOfChildWithProp(prop);
181 if (removedChildIndex >= 0) {
182 auto it = _children.begin() + removedChildIndex;
183 delete *it;
184 _children.erase(it);
185 emit childRemoved(removedChildIndex);
186 }
187 return true;
188 }
189
190 return false;
191 }
192
193 void FGCanvasGroup::removeChild(FGCanvasElement *child)
194 {
195 auto it = std::find(_children.begin(), _children.end(), child);
196 if (it != _children.end()) {
197 int index = std::distance(_children.begin(), it);
198 _children.erase(it);
199 emit childRemoved(index);
200 }
201 }
202
203 void FGCanvasGroup::dumpElement()
204 {
205 qDebug() << "Group at" << _propertyRoot->path();
206 for (auto c : _children) {
207 c->dumpElement();
208 }
209 qDebug() << "End-group at" << _propertyRoot->path();
210 }
211
212 int FGCanvasGroup::indexOfChildWithProp(LocalProp* prop) const
213 {
214 auto it = std::find_if(_children.begin(), _children.end(), [prop](FGCanvasElement* child)
215 {
216 return (child->property() == prop);
217 });
218
219 if (it == _children.end()) {
220 return -1;
221 }
222
223 return std::distance(_children.begin(), it);
224 }
225
226 void FGCanvasGroup::markStyleDirty()
227 {
228 for (FGCanvasElement* element : _children) {
229 element->markStyleDirty();
230 }
231 }
232
233 void FGCanvasGroup::doDestroy()
234 {
235 delete _quick;
236
237 FGCanvasElementVec children = std::move(_children);
238 _children.clear();
239 for (auto c : children) {
240 delete c;
241 }
242 }
243
244 void FGCanvasGroup::markCachedSymbolDirty()
245 {
246 _cachedSymbolDirty = true;
247 }