Debian Codegraph @

plugins/script/interfaces/LayerInterface.cpp

raw
1 #include "LayerInterface.h"
2
3 #include <pybind11/pybind11.h>
4
5 namespace script
6 {
7
8 namespace
9 {
10 inline scene::ILayerManager& getMapLayerManager()
11 {
12 if (!GlobalMapModule().getRoot())
13 {
14 throw std::runtime_error("No map loaded.");
15 }
16
17 return GlobalMapModule().getRoot()->getLayerManager();
18 }
19
20 }
21
22 int LayerInterface::createLayer(const std::string &name)
23 {
24 try
25 {
26 return getMapLayerManager().createLayer(name);
27 }
28 catch (const std::runtime_error& ex)
29 {
30 rError() << ex.what() << std::endl;
31 return -1;
32 }
33 }
34
35 int LayerInterface::createLayer(const std::string &name, int layerID)
36 {
37 try
38 {
39 return getMapLayerManager().createLayer(name, layerID);
40 }
41 catch (const std::runtime_error& ex)
42 {
43 rError() << ex.what() << std::endl;
44 return -1;
45 }
46 }
47
48 void LayerInterface::deleteLayer(const std::string &name)
49 {
50 try
51 {
52 getMapLayerManager().deleteLayer(name);
53 }
54 catch (const std::runtime_error& ex)
55 {
56 rError() << ex.what() << std::endl;
57 }
58 }
59
60 void LayerInterface::foreachLayer(LayerVisitor &visitor)
61 {
62 try
63 {
64 getMapLayerManager().foreachLayer([&](int layerId, const std::string& layerName)
65 {
66 visitor.visit(layerId, layerName);
67 });
68 }
69 catch (const std::runtime_error& ex)
70 {
71 rError() << ex.what() << std::endl;
72 }
73 }
74
75 int LayerInterface::getLayerID(const std::string& name)
76 {
77 try
78 {
79 return getMapLayerManager().getLayerID(name);
80 }
81 catch (const std::runtime_error& ex)
82 {
83 rError() << ex.what() << std::endl;
84 return -1;
85 }
86 }
87
88 std::string LayerInterface::getLayerName(int layerID)
89 {
90 try
91 {
92 return getMapLayerManager().getLayerName(layerID);
93 }
94 catch (const std::runtime_error& ex)
95 {
96 rError() << ex.what() << std::endl;
97 return "";
98 }
99 }
100
101 bool LayerInterface::layerExists(int layerID)
102 {
103 try
104 {
105 return getMapLayerManager().layerExists(layerID);
106 }
107 catch (const std::runtime_error& ex)
108 {
109 rError() << ex.what() << std::endl;
110 return false;
111 }
112 }
113
114 bool LayerInterface::renameLayer(int layerID, const std::string &newLayerName)
115 {
116 try
117 {
118 return getMapLayerManager().renameLayer(layerID, newLayerName);
119 }
120 catch (const std::runtime_error& ex)
121 {
122 rError() << ex.what() << std::endl;
123 return false;
124 }
125 }
126
127 int LayerInterface::getFirstVisibleLayer()
128 {
129 try
130 {
131 return getMapLayerManager().getFirstVisibleLayer();
132 }
133 catch (const std::runtime_error& ex)
134 {
135 rError() << ex.what() << std::endl;
136 return -1;
137 }
138 }
139
140 int LayerInterface::getActiveLayer()
141 {
142 try
143 {
144 return getMapLayerManager().getActiveLayer();
145 }
146 catch (const std::runtime_error& ex)
147 {
148 rError() << ex.what() << std::endl;
149 return -1;
150 }
151 }
152
153 void LayerInterface::setActiveLayer(int layerID)
154 {
155 try
156 {
157 getMapLayerManager().setActiveLayer(layerID);
158 }
159 catch (const std::runtime_error& ex)
160 {
161 rError() << ex.what() << std::endl;
162 }
163 }
164
165 bool LayerInterface::layerIsVisible(const std::string &layerName)
166 {
167 try
168 {
169 return getMapLayerManager().layerIsVisible(getLayerID(layerName));
170 }
171 catch (const std::runtime_error& ex)
172 {
173 rError() << ex.what() << std::endl;
174 return false;
175 }
176 }
177
178 bool LayerInterface::layerIsVisible(int layerID)
179 {
180 try
181 {
182 return getMapLayerManager().layerIsVisible(layerID);
183 }
184 catch (const std::runtime_error& ex)
185 {
186 rError() << ex.what() << std::endl;
187 return false;
188 }
189 }
190
191 void LayerInterface::setLayerVisibility(const std::string &layerName, bool visible)
192 {
193 try
194 {
195 getMapLayerManager().setLayerVisibility(getLayerID(layerName), visible);
196 }
197 catch (const std::runtime_error& ex)
198 {
199 rError() << ex.what() << std::endl;
200 }
201 }
202
203 void LayerInterface::setLayerVisibility(int layerID, bool visible)
204 {
205 try
206 {
207 getMapLayerManager().setLayerVisibility(layerID, visible);
208 }
209 catch (const std::runtime_error& ex)
210 {
211 rError() << ex.what() << std::endl;
212 }
213 }
214
215 void LayerInterface::addSelectionToLayer(const std::string &layerName)
216 {
217 try
218 {
219 getMapLayerManager().addSelectionToLayer(getLayerID(layerName));
220 }
221 catch (const std::runtime_error& ex)
222 {
223 rError() << ex.what() << std::endl;
224 }
225 }
226
227 void LayerInterface::addSelectionToLayer(int layerID)
228 {
229 try
230 {
231 getMapLayerManager().addSelectionToLayer(layerID);
232 }
233 catch (const std::runtime_error& ex)
234 {
235 rError() << ex.what() << std::endl;
236 }
237 }
238
239 void LayerInterface::moveSelectionToLayer(const std::string &layerName)
240 {
241 try
242 {
243 getMapLayerManager().moveSelectionToLayer(getLayerID(layerName));
244 }
245 catch (const std::runtime_error& ex)
246 {
247 rError() << ex.what() << std::endl;
248 }
249 }
250
251 void LayerInterface::moveSelectionToLayer(int layerID)
252 {
253 try
254 {
255 getMapLayerManager().moveSelectionToLayer(layerID);
256 }
257 catch (const std::runtime_error& ex)
258 {
259 rError() << ex.what() << std::endl;
260 }
261 }
262
263 void LayerInterface::removeSelectionFromLayer(const std::string &layerName)
264 {
265 try
266 {
267 getMapLayerManager().removeSelectionFromLayer(getLayerID(layerName));
268 }
269 catch (const std::runtime_error& ex)
270 {
271 rError() << ex.what() << std::endl;
272 }
273 }
274
275 void LayerInterface::removeSelectionFromLayer(int layerID)
276 {
277 try
278 {
279 getMapLayerManager().removeSelectionFromLayer(layerID);
280 }
281 catch (const std::runtime_error& ex)
282 {
283 rError() << ex.what() << std::endl;
284 }
285 }
286
287 void LayerInterface::setSelected(int layerID, bool selected)
288 {
289 try
290 {
291 getMapLayerManager().setSelected(layerID, selected);
292 }
293 catch (const std::runtime_error& ex)
294 {
295 rError() << ex.what() << std::endl;
296 }
297 }
298
299 int LayerInterface::getParentLayer(int layerId)
300 {
301 try
302 {
303 return getMapLayerManager().getParentLayer(layerId);
304 }
305 catch (const std::runtime_error& ex)
306 {
307 rError() << ex.what() << std::endl;
308 return -1;
309 }
310 }
311
312 void LayerInterface::setParentLayer(int childlayerId, int parentLayerId)
313 {
314 try
315 {
316 getMapLayerManager().setParentLayer(childlayerId, parentLayerId);
317 }
318 catch (const std::runtime_error& ex)
319 {
320 rError() << ex.what() << std::endl;
321 }
322 }
323
324 void LayerInterface::registerInterface(py::module& scope, py::dict& globals)
325 {
326 // Expose the LayerVisitor interface
327 py::class_<LayerVisitor, LayerVisitorWrapper> visitor(scope, "LayerVisitor");
328
329 visitor.def(py::init<>());
330 visitor.def("visit", &LayerVisitor::visit);
331
332 // Add the module declaration to the given python namespace
333 py::class_<LayerInterface> layerManager(scope, "LayerManager");
334
335 layerManager.def("createLayer", py::overload_cast<const std::string&>(&LayerInterface::createLayer));
336 layerManager.def("createLayer", py::overload_cast<const std::string&, int>(&LayerInterface::createLayer));
337 layerManager.def("deleteLayer", &LayerInterface::deleteLayer);
338 layerManager.def("foreachLayer", &LayerInterface::foreachLayer);
339 layerManager.def("getLayerID", &LayerInterface::getLayerID);
340 layerManager.def("getLayerName", &LayerInterface::getLayerName);
341 layerManager.def("layerExists", &LayerInterface::layerExists);
342 layerManager.def("renameLayer", &LayerInterface::renameLayer);
343 layerManager.def("getFirstVisibleLayer", &LayerInterface::getFirstVisibleLayer);
344 layerManager.def("getActiveLayer", &LayerInterface::getActiveLayer);
345 layerManager.def("setActiveLayer", &LayerInterface::setActiveLayer);
346 layerManager.def("layerIsVisible", py::overload_cast<const std::string&>(&LayerInterface::layerIsVisible));
347 layerManager.def("layerIsVisible", py::overload_cast<int>(&LayerInterface::layerIsVisible));
348 layerManager.def("setLayerVisibility", py::overload_cast<const std::string&, bool>(&LayerInterface::setLayerVisibility));
349 layerManager.def("setLayerVisibility", py::overload_cast<int, bool>(&LayerInterface::setLayerVisibility));
350 layerManager.def("addSelectionToLayer", py::overload_cast<const std::string&>(&LayerInterface::addSelectionToLayer));
351 layerManager.def("addSelectionToLayer", py::overload_cast<int>(&LayerInterface::addSelectionToLayer));
352 layerManager.def("moveSelectionToLayer", py::overload_cast<const std::string&>(&LayerInterface::moveSelectionToLayer));
353 layerManager.def("moveSelectionToLayer", py::overload_cast<int>(&LayerInterface::moveSelectionToLayer));
354 layerManager.def("removeSelectionFromLayer", py::overload_cast<const std::string&>(&LayerInterface::removeSelectionFromLayer));
355 layerManager.def("removeSelectionFromLayer", py::overload_cast<int>(&LayerInterface::removeSelectionFromLayer));
356 layerManager.def("setSelected", &LayerInterface::setSelected);
357 layerManager.def("getParentLayer", &LayerInterface::getParentLayer);
358 layerManager.def("setParentLayer", &LayerInterface::setParentLayer);
359
360 // Now point the Python variable "GlobalLayerManager" to this instance
361 globals["GlobalLayerManager"] = this;
362 }
363
364
365 }