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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 06/01/2010 Added this text. :-)
17 #include "rs_layerlist.h"
19 //#include "rs_layerlistlistener.h"
21 #include "rs_entity.h"
25 * Default constructor.
27 RS_LayerList::RS_LayerList()
29 // Good news, we don't have to screw with this shit
30 // layers.setAutoDelete(false);
31 // layerListListeners.setAutoDelete(false);
36 /*virtual*/ RS_LayerList::~RS_LayerList()
41 * Removes all layers in the layerlist.
43 void RS_LayerList::clear()
50 * @return Number of layers in the list.
52 uint RS_LayerList::count() const
54 return layers.count();
58 * @return Layer at given position or NULL if i is out of range.
60 RS_Layer * RS_LayerList::at(uint i)
66 * Activates the given layer.
68 * @param notify Notify listeners.
70 void RS_LayerList::activate(const QString & name, bool notify)
72 RS_DEBUG->print("RS_LayerList::activate: %s, notify: %d begin", name.toLatin1().data(), notify);
74 activate(find(name), notify);
76 if (activeLayer==NULL) {
77 RS_DEBUG->print("activeLayer is NULL");
79 RS_DEBUG->print("activeLayer is %s", activeLayer->getName().latin1());
83 RS_DEBUG->print("RS_LayerList::activate: %s end", name.toLatin1().data());
87 * Activates the given layer.
89 * @param notify Notify listeners.
91 void RS_LayerList::activate(RS_Layer * layer, bool notify)
93 RS_DEBUG->print("RS_LayerList::activate notify: %d begin", notify);
96 RS_DEBUG->print("RS_LayerList::activate: %s",
97 layer->getName().latin1());
99 RS_DEBUG->print("RS_LayerList::activate: NULL");
107 for(int i=0; i<layerListListeners.count(); ++i)
109 RS_LayerListListener * l = layerListListeners.at(i);
110 l->layerActivated(activeLayer);
111 RS_DEBUG->print("RS_LayerList::activate listener notified");
116 RS_DEBUG->print("RS_LayerList::activate end");
119 //! @return The active layer of NULL if no layer is activated.
120 RS_Layer * RS_LayerList::getActive()
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.
131 * Listeners are notified.
133 void RS_LayerList::add(RS_Layer * layer)
135 RS_DEBUG->print("RS_LayerList::addLayer()");
140 // check if layer already exists:
141 RS_Layer * l = find(layer->getName());
145 layers.append(layer);
149 for(int i=0; i<layerListListeners.count(); ++i)
151 RS_LayerListListener * l = layerListListeners.at(i);
152 l->layerAdded(layer);
158 // if there was no active layer so far, activate this one.
159 if (activeLayer == NULL)
164 // if there was no active layer so far, activate this one.
165 if (activeLayer == NULL)
168 l->setPen(layer->getPen());
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.
180 void RS_LayerList::remove(RS_Layer * layer)
182 RS_DEBUG->print("RS_LayerList::removeLayer()");
187 // here the layer is removed from the list but not deleted
188 // layers.remove(layer);
189 int idx = layers.indexOf(layer);
195 for(int i=0; i<layerListListeners.count(); ++i)
197 RS_LayerListListener * l = layerListListeners.at(i);
198 l->layerRemoved(layer);
204 // activate an other layer if necessary:
205 if (activeLayer == layer)
206 activate(layers.first());
208 // now it's save to delete the layer
213 * Changes a layer's attributes. The attributes of layer 'layer'
214 * are copied from layer 'source'.
215 * Listeners are notified.
217 void RS_LayerList::edit(RS_Layer * layer, const RS_Layer & source)
225 for(int i=0; i<layerListListeners.count(); ++i)
227 RS_LayerListListener * l = layerListListeners.at(i);
228 l->layerEdited(layer);
236 * @return Pointer to the layer with the given name or
237 * \p NULL if no such layer was found.
239 RS_Layer * RS_LayerList::find(const QString & name)
241 //RS_DEBUG->print("RS_LayerList::find begin");
243 RS_Layer * ret = NULL;
245 for(int i=0; i<layers.size(); i++)
247 RS_Layer * l = layers[i];
249 if (l->getName() == name)
253 //RS_DEBUG->print("RS_LayerList::find end");
259 * @return Index of the given layer in the layer list or -1 if the layer
262 int RS_LayerList::getIndex(const QString & name)
265 //RS_DEBUG->print("RS_LayerList::find begin");
270 for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
272 if (l->getName() == name)
281 //RS_DEBUG->print("RS_LayerList::find end");
285 for(int i=0; i<layers.size(); i++)
287 RS_Layer * l = layers[i];
289 if (l->getName() == name)
298 * @return Index of the given layer in the layer list or -1 if the layer
301 int RS_LayerList::getIndex(RS_Layer * layer)
304 //RS_DEBUG->print("RS_LayerList::find begin");
309 for(RS_Layer* l=layers.first(); l!=NULL; l=layers.next())
320 //RS_DEBUG->print("RS_LayerList::find end");
324 return layers.indexOf(layer);
329 * Switches on / off the given layer.
330 * Listeners are notified.
332 void RS_LayerList::toggle(const QString & name)
338 * Switches on / off the given layer.
339 * Listeners are notified.
341 void RS_LayerList::toggle(RS_Layer * layer)
350 for(int i=0; i < layerListListeners.count(); ++i)
352 RS_LayerListListener * l = layerListListeners.at(i);
353 l->layerToggled(layer);
359 * Locks or unlocks the given layer.
360 * Listeners are notified.
362 void RS_LayerList::toggleLock(RS_Layer * layer)
371 for(int i=0; i<layerListListeners.count(); ++i)
373 RS_LayerListListener * l = layerListListeners.at(i);
374 l->layerToggled(layer);
380 * Freezes or defreezes all layers.
382 * @param freeze true: freeze, false: defreeze
384 void RS_LayerList::freezeAll(bool freeze)
386 for(uint l=0; l<count(); l++)
387 at(l)->freeze(freeze);
390 for(int i=0; i<layerListListeners.count(); ++i)
392 RS_LayerListListener * l = layerListListeners.at(i);
393 l->layerToggled(NULL);
400 * adds a LayerListListener to the list of listeners. Listeners
401 * are notified when the layer list changes.
403 * Typical listeners are: layer list widgets, pen toolbar, graphic view
405 void RS_LayerList::addListener(RS_LayerListListener * listener)
407 layerListListeners.append(listener);
411 * removes a LayerListListener from the list of listeners.
413 void RS_LayerList::removeListener(RS_LayerListListener * listener)
415 // layerListListeners.remove(listener);
417 int i = layerListListeners.indexOf(listener);
420 layerListListeners.takeAt(i);
425 * Sets the layer lists modified status to 'm'.
427 void RS_LayerList::setModified(bool m)
433 * @retval true The layer list has been modified.
434 * @retval false The layer list has not been modified.
436 /*virtual*/ bool RS_LayerList::isModified() const
442 * Dumps the layers to stdout.
444 std::ostream & operator<<(std::ostream & os, RS_LayerList & l)
446 os << "Layerlist: \n";
448 for(uint i=0; i<l.count(); i++)
449 os << *(l.at(i)) << "\n";