]> Shamusworld >> Repos - architektonas/blob - src/base/rs_layerlist.cpp
Fixed thumbnail rendering in LibraryWidget and DXF detection.
[architektonas] / src / base / rs_layerlist.cpp
1 // rs_layerlist.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/01/2010  Added this text. :-)
15 //
16
17 #include "rs_layerlist.h"
18
19 //#include "rs_layerlistlistener.h"
20 #include "rs_debug.h"
21 #include "rs_entity.h"
22 #include "rs_layer.h"
23
24 /**
25  * Default constructor.
26  */
27 RS_LayerList::RS_LayerList()
28 {
29 // Good news, we don't have to screw with this shit
30 //      layers.setAutoDelete(false);
31 //      layerListListeners.setAutoDelete(false);
32         activeLayer = NULL;
33         setModified(false);
34 }
35
36 /*virtual*/ RS_LayerList::~RS_LayerList()
37 {
38 }
39
40 /**
41  * Removes all layers in the layerlist.
42  */
43 void RS_LayerList::clear()
44 {
45         layers.clear();
46         setModified(true);
47 }
48
49 /**
50  * @return Number of layers in the list.
51  */
52 uint RS_LayerList::count() const
53 {
54         return layers.count();
55 }
56
57 /**
58  * @return Layer at given position or NULL if i is out of range.
59  */
60 RS_Layer * RS_LayerList::at(uint i)
61 {
62         return layers.at(i);
63 }
64
65 /**
66  * Activates the given layer.
67  *
68  * @param notify Notify listeners.
69  */
70 void RS_LayerList::activate(const QString & name, bool notify)
71 {
72         RS_DEBUG->print("RS_LayerList::activate: %s, notify: %d begin", name.toLatin1().data(), notify);
73
74         activate(find(name), notify);
75         /*
76         if (activeLayer==NULL) {
77                 RS_DEBUG->print("activeLayer is NULL");
78 } else {
79                 RS_DEBUG->print("activeLayer is %s", activeLayer->getName().latin1());
80 }
81         */
82
83         RS_DEBUG->print("RS_LayerList::activate: %s end", name.toLatin1().data());
84 }
85
86 /**
87  * Activates the given layer.
88  *
89  * @param notify Notify listeners.
90  */
91 void RS_LayerList::activate(RS_Layer * layer, bool notify)
92 {
93         RS_DEBUG->print("RS_LayerList::activate notify: %d begin", notify);
94
95         /*if (layer!=NULL) {
96                 RS_DEBUG->print("RS_LayerList::activate: %s",
97                                                 layer->getName().latin1());
98 } else {
99                 RS_DEBUG->print("RS_LayerList::activate: NULL");
100 }*/
101
102         activeLayer = layer;
103
104 #if 0
105         if (notify)
106         {
107                 for(int i=0; i<layerListListeners.count(); ++i)
108                 {
109                         RS_LayerListListener * l = layerListListeners.at(i);
110                         l->layerActivated(activeLayer);
111                         RS_DEBUG->print("RS_LayerList::activate listener notified");
112                 }
113         }
114 #endif
115
116         RS_DEBUG->print("RS_LayerList::activate end");
117 }
118
119 //! @return The active layer of NULL if no layer is activated.
120 RS_Layer * RS_LayerList::getActive()
121 {
122         return activeLayer;
123 }
124
125 /**
126  * Adds a layer to the layer list.
127  * If there is already a layer with the same name, no layer is
128  * added. In that case the layer passed to the methode will be deleted!
129  * If no layer was active so far, the new layer becomes the active one.
130  *
131  * Listeners are notified.
132  */
133 void RS_LayerList::add(RS_Layer * layer)
134 {
135         RS_DEBUG->print("RS_LayerList::addLayer()");
136
137         if (layer == NULL)
138                 return;
139
140         // check if layer already exists:
141         RS_Layer * l = find(layer->getName());
142
143         if (l == NULL)
144         {
145                 layers.append(layer);
146
147 #if 0
148                 // notify listeners
149                 for(int i=0; i<layerListListeners.count(); ++i)
150                 {
151                         RS_LayerListListener * l = layerListListeners.at(i);
152                         l->layerAdded(layer);
153                 }
154 #endif
155
156                 setModified(true);
157
158                 // if there was no active layer so far, activate this one.
159                 if (activeLayer == NULL)
160                         activate(layer);
161         }
162         else
163         {
164                 // if there was no active layer so far, activate this one.
165                 if (activeLayer == NULL)
166                         activate(l);
167
168                 l->setPen(layer->getPen());
169
170                 delete layer;
171                 layer = NULL;
172         }
173 }
174
175 /**
176  * Removes a layer from the list.
177  * Listeners are notified after the layer was removed from
178  * the list but before it gets deleted.
179  */
180 void RS_LayerList::remove(RS_Layer * layer)
181 {
182         RS_DEBUG->print("RS_LayerList::removeLayer()");
183
184         if (layer == NULL)
185                 return;
186
187         // here the layer is removed from the list but not deleted
188 //      layers.remove(layer);
189         int idx = layers.indexOf(layer);
190
191         if (idx != -1)
192                 layers.takeAt(idx);
193
194 #if 0
195         for(int i=0; i<layerListListeners.count(); ++i)
196         {
197                 RS_LayerListListener * l = layerListListeners.at(i);
198                 l->layerRemoved(layer);
199         }
200 #endif
201
202         setModified(true);
203
204         // activate an other layer if necessary:
205         if (activeLayer == layer)
206                 activate(layers.first());
207
208         // now it's save to delete the layer
209         delete layer;
210 }
211
212 /**
213  * Changes a layer's attributes. The attributes of layer 'layer'
214  * are copied from layer 'source'.
215  * Listeners are notified.
216  */
217 void RS_LayerList::edit(RS_Layer * layer, const RS_Layer & source)
218 {
219         if (!layer)
220                 return;
221
222         *layer = source;
223
224 #if 0
225         for(int i=0; i<layerListListeners.count(); ++i)
226         {
227                 RS_LayerListListener * l = layerListListeners.at(i);
228                 l->layerEdited(layer);
229         }
230 #endif
231
232         setModified(true);
233 }
234
235 /**
236  * @return Pointer to the layer with the given name or
237  * \p NULL if no such layer was found.
238  */
239 RS_Layer * RS_LayerList::find(const QString & name)
240 {
241         //RS_DEBUG->print("RS_LayerList::find begin");
242
243         RS_Layer * ret = NULL;
244
245         for(int i=0; i<layers.size(); i++)
246         {
247                 RS_Layer * l = layers[i];
248
249                 if (l->getName() == name)
250                         ret = l;
251         }
252
253         //RS_DEBUG->print("RS_LayerList::find end");
254
255         return ret;
256 }
257
258 /**
259  * @return Index of the given layer in the layer list or -1 if the layer
260  * was not found.
261  */
262 int RS_LayerList::getIndex(const QString & name)
263 {
264 #if 0
265         //RS_DEBUG->print("RS_LayerList::find begin");
266
267         int ret = -1;
268         int i = 0;
269
270         for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
271         {
272                 if (l->getName() == name)
273                 {
274                         ret = i;
275                         break;
276                 }
277
278                 i++;
279         }
280
281         //RS_DEBUG->print("RS_LayerList::find end");
282
283         return ret;
284 #else
285         for(int i=0; i<layers.size(); i++)
286         {
287                 RS_Layer * l = layers[i];
288
289                 if (l->getName() == name)
290                         return i;
291         }
292
293         return -1;
294 #endif
295 }
296
297 /**
298  * @return Index of the given layer in the layer list or -1 if the layer
299  * was not found.
300  */
301 int RS_LayerList::getIndex(RS_Layer * layer)
302 {
303 #if 0
304         //RS_DEBUG->print("RS_LayerList::find begin");
305
306         int ret = -1;
307         int i = 0;
308
309         for(RS_Layer* l=layers.first(); l!=NULL; l=layers.next())
310         {
311                 if (l == layer)
312                 {
313                         ret = i;
314                         break;
315                 }
316
317                 i++;
318         }
319
320         //RS_DEBUG->print("RS_LayerList::find end");
321
322         return ret;
323 #else
324         return layers.indexOf(layer);
325 #endif
326 }
327
328 /**
329  * Switches on / off the given layer.
330  * Listeners are notified.
331  */
332 void RS_LayerList::toggle(const QString & name)
333 {
334         toggle(find(name));
335 }
336
337 /**
338  * Switches on / off the given layer.
339  * Listeners are notified.
340  */
341 void RS_LayerList::toggle(RS_Layer * layer)
342 {
343         if (!layer)
344                 return;
345
346         layer->toggle();
347
348 #if 0
349         // Notify listeners:
350         for(int i=0; i < layerListListeners.count(); ++i)
351         {
352                 RS_LayerListListener * l = layerListListeners.at(i);
353                 l->layerToggled(layer);
354         }
355 #endif
356 }
357
358 /**
359  * Locks or unlocks the given layer.
360  * Listeners are notified.
361  */
362 void RS_LayerList::toggleLock(RS_Layer * layer)
363 {
364         if (!layer)
365                 return;
366
367         layer->toggleLock();
368
369 #if 0
370         // Notify listeners:
371         for(int i=0; i<layerListListeners.count(); ++i)
372         {
373                 RS_LayerListListener * l = layerListListeners.at(i);
374                 l->layerToggled(layer);
375         }
376 #endif
377 }
378
379 /**
380  * Freezes or defreezes all layers.
381  *
382  * @param freeze true: freeze, false: defreeze
383  */
384 void RS_LayerList::freezeAll(bool freeze)
385 {
386         for(uint l=0; l<count(); l++)
387                 at(l)->freeze(freeze);
388
389 #if 0
390         for(int i=0; i<layerListListeners.count(); ++i)
391         {
392                 RS_LayerListListener * l = layerListListeners.at(i);
393                 l->layerToggled(NULL);
394         }
395 #endif
396 }
397
398 #if 0
399 /**
400  * adds a LayerListListener to the list of listeners. Listeners
401  * are notified when the layer list changes.
402  *
403  * Typical listeners are: layer list widgets, pen toolbar, graphic view
404  */
405 void RS_LayerList::addListener(RS_LayerListListener * listener)
406 {
407         layerListListeners.append(listener);
408 }
409
410 /**
411  * removes a LayerListListener from the list of listeners.
412  */
413 void RS_LayerList::removeListener(RS_LayerListListener * listener)
414 {
415 //      layerListListeners.remove(listener);
416
417         int i = layerListListeners.indexOf(listener);
418
419         if (i != -1)
420                 layerListListeners.takeAt(i);
421 }
422 #endif
423
424 /**
425  * Sets the layer lists modified status to 'm'.
426  */
427 void RS_LayerList::setModified(bool m)
428 {
429         modified = m;
430 }
431
432 /**
433  * @retval true The layer list has been modified.
434  * @retval false The layer list has not been modified.
435  */
436 /*virtual*/ bool RS_LayerList::isModified() const
437 {
438         return modified;
439 }
440
441 /**
442  * Dumps the layers to stdout.
443  */
444 std::ostream & operator<<(std::ostream & os, RS_LayerList & l)
445 {
446         os << "Layerlist: \n";
447
448         for(uint i=0; i<l.count(); i++)
449                 os << *(l.at(i)) << "\n";
450
451         return os;
452 }