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"
20 #include "rs_layerlistlistener.h"
21 #include "rs_entity.h"
24 * Default constructor.
26 RS_LayerList::RS_LayerList()
28 // Good news, we don't have to screw with this shit
29 // layers.setAutoDelete(false);
30 // layerListListeners.setAutoDelete(false);
35 /*virtual*/ RS_LayerList::~RS_LayerList()
40 * Removes all layers in the layerlist.
42 void RS_LayerList::clear()
49 * @return Number of layers in the list.
51 uint RS_LayerList::count() const
53 return layers.count();
57 * @return Layer at given position or NULL if i is out of range.
59 RS_Layer * RS_LayerList::at(uint i)
65 * Activates the given layer.
67 * @param notify Notify listeners.
69 void RS_LayerList::activate(const QString & name, bool notify)
71 RS_DEBUG->print("RS_LayerList::activate: %s, notify: %d begin", name.toLatin1().data(), notify);
73 activate(find(name), notify);
75 if (activeLayer==NULL) {
76 RS_DEBUG->print("activeLayer is NULL");
78 RS_DEBUG->print("activeLayer is %s", activeLayer->getName().latin1());
82 RS_DEBUG->print("RS_LayerList::activate: %s end", name.toLatin1().data());
86 * Activates the given layer.
88 * @param notify Notify listeners.
90 void RS_LayerList::activate(RS_Layer * layer, bool notify)
92 RS_DEBUG->print("RS_LayerList::activate notify: %d begin", notify);
95 RS_DEBUG->print("RS_LayerList::activate: %s",
96 layer->getName().latin1());
98 RS_DEBUG->print("RS_LayerList::activate: NULL");
105 for(int i=0; i<layerListListeners.count(); ++i)
107 RS_LayerListListener * l = layerListListeners.at(i);
108 l->layerActivated(activeLayer);
109 RS_DEBUG->print("RS_LayerList::activate listener notified");
113 RS_DEBUG->print("RS_LayerList::activate end");
116 //! @return The active layer of NULL if no layer is activated.
117 RS_Layer * RS_LayerList::getActive()
123 * Adds a layer to the layer list.
124 * If there is already a layer with the same name, no layer is
125 * added. In that case the layer passed to the methode will be deleted!
126 * If no layer was active so far, the new layer becomes the active one.
128 * Listeners are notified.
130 void RS_LayerList::add(RS_Layer * layer)
132 RS_DEBUG->print("RS_LayerList::addLayer()");
137 // check if layer already exists:
138 RS_Layer * l = find(layer->getName());
142 layers.append(layer);
145 for(int i=0; i<layerListListeners.count(); ++i)
147 RS_LayerListListener * l = layerListListeners.at(i);
148 l->layerAdded(layer);
153 // if there was no active layer so far, activate this one.
154 if (activeLayer ==NULL)
159 // if there was no active layer so far, activate this one.
160 if (activeLayer == NULL)
163 l->setPen(layer->getPen());
171 * Removes a layer from the list.
172 * Listeners are notified after the layer was removed from
173 * the list but before it gets deleted.
175 void RS_LayerList::remove(RS_Layer * layer)
177 RS_DEBUG->print("RS_LayerList::removeLayer()");
182 // here the layer is removed from the list but not deleted
183 // layers.remove(layer);
184 int idx = layers.indexOf(layer);
189 for(int i=0; i<layerListListeners.count(); ++i)
191 RS_LayerListListener * l = layerListListeners.at(i);
192 l->layerRemoved(layer);
197 // activate an other layer if necessary:
198 if (activeLayer == layer)
199 activate(layers.first());
201 // now it's save to delete the layer
206 * Changes a layer's attributes. The attributes of layer 'layer'
207 * are copied from layer 'source'.
208 * Listeners are notified.
210 void RS_LayerList::edit(RS_Layer * layer, const RS_Layer & source)
217 for(int i=0; i<layerListListeners.count(); ++i)
219 RS_LayerListListener * l = layerListListeners.at(i);
220 l->layerEdited(layer);
227 * @return Pointer to the layer with the given name or
228 * \p NULL if no such layer was found.
230 RS_Layer * RS_LayerList::find(const QString & name)
232 //RS_DEBUG->print("RS_LayerList::find begin");
234 RS_Layer * ret = NULL;
236 // for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
237 for(int i=0; i<layers.size(); i++)
239 RS_Layer * l = layers[i];
241 if (l->getName() == name)
245 //RS_DEBUG->print("RS_LayerList::find end");
251 * @return Index of the given layer in the layer list or -1 if the layer
254 int RS_LayerList::getIndex(const QString & name)
257 //RS_DEBUG->print("RS_LayerList::find begin");
262 for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
264 if (l->getName() == name)
273 //RS_DEBUG->print("RS_LayerList::find end");
277 for(int i=0; i<layers.size(); i++)
279 RS_Layer * l = layers[i];
281 if (l->getName() == name)
290 * @return Index of the given layer in the layer list or -1 if the layer
293 int RS_LayerList::getIndex(RS_Layer * layer)
296 //RS_DEBUG->print("RS_LayerList::find begin");
301 for(RS_Layer* l=layers.first(); l!=NULL; l=layers.next())
312 //RS_DEBUG->print("RS_LayerList::find end");
316 return layers.indexOf(layer);
321 * Switches on / off the given layer.
322 * Listeners are notified.
324 void RS_LayerList::toggle(const QString & name)
330 * Switches on / off the given layer.
331 * Listeners are notified.
333 void RS_LayerList::toggle(RS_Layer * layer)
341 for(int i=0; i < layerListListeners.count(); ++i)
343 RS_LayerListListener * l = layerListListeners.at(i);
344 l->layerToggled(layer);
349 * Locks or unlocks the given layer.
350 * Listeners are notified.
352 void RS_LayerList::toggleLock(RS_Layer * layer)
360 for(int i=0; i<layerListListeners.count(); ++i)
362 RS_LayerListListener * l = layerListListeners.at(i);
363 l->layerToggled(layer);
368 * Freezes or defreezes all layers.
370 * @param freeze true: freeze, false: defreeze
372 void RS_LayerList::freezeAll(bool freeze)
374 for(uint l=0; l<count(); l++)
375 at(l)->freeze(freeze);
377 for(int i=0; i<layerListListeners.count(); ++i)
379 RS_LayerListListener * l = layerListListeners.at(i);
380 l->layerToggled(NULL);
385 * adds a LayerListListener to the list of listeners. Listeners
386 * are notified when the layer list changes.
388 * Typical listeners are: layer list widgets, pen toolbar, graphic view
390 void RS_LayerList::addListener(RS_LayerListListener * listener)
392 layerListListeners.append(listener);
396 * removes a LayerListListener from the list of listeners.
398 void RS_LayerList::removeListener(RS_LayerListListener * listener)
400 // layerListListeners.remove(listener);
402 int i = layerListListeners.indexOf(listener);
405 layerListListeners.takeAt(i);
409 * Sets the layer lists modified status to 'm'.
411 void RS_LayerList::setModified(bool m)
417 * @retval true The layer list has been modified.
418 * @retval false The layer list has not been modified.
420 /*virtual*/ bool RS_LayerList::isModified() const
426 * Dumps the layers to stdout.
428 std::ostream & operator<<(std::ostream & os, RS_LayerList & l)
430 os << "Layerlist: \n";
432 for(uint i=0; i<l.count(); i++)
433 os << *(l.at(i)) << "\n";