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 "layerlist.h"
19 //#include "layerlistlistener.h"
25 * Default constructor.
27 LayerList::LayerList()
29 // Good news, we don't have to screw with this shit
30 // layers.setAutoDelete(false);
31 // layerListListeners.setAutoDelete(false);
36 /*virtual*/ LayerList::~LayerList()
41 * Removes all layers in the layerlist.
43 void LayerList::clear()
50 * @return Number of layers in the list.
52 uint LayerList::count() const
54 return layers.count();
58 * @return Layer at given position or NULL if i is out of range.
60 Layer * LayerList::at(uint i)
66 * Activates the given layer.
68 * @param notify Notify listeners.
70 void LayerList::activate(const QString & name, bool notify)
72 DEBUG->print("LayerList::activate: %s, notify: %d begin", name.toLatin1().data(), notify);
74 activate(find(name), notify);
76 if (activeLayer==NULL) {
77 DEBUG->print("activeLayer is NULL");
79 DEBUG->print("activeLayer is %s", activeLayer->getName().latin1());
83 DEBUG->print("LayerList::activate: %s end", name.toLatin1().data());
87 * Activates the given layer.
89 * @param notify Notify listeners.
91 void LayerList::activate(Layer * layer, bool notify)
93 DEBUG->print("LayerList::activate notify: %d begin", notify);
96 DEBUG->print("LayerList::activate: %s",
97 layer->getName().latin1());
99 DEBUG->print("LayerList::activate: NULL");
107 for(int i=0; i<layerListListeners.count(); ++i)
109 LayerListListener * l = layerListListeners.at(i);
110 l->layerActivated(activeLayer);
111 DEBUG->print("LayerList::activate listener notified");
116 DEBUG->print("LayerList::activate end");
119 //! @return The active layer of NULL if no layer is activated.
120 Layer * 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 LayerList::add(Layer * layer)
135 DEBUG->print("LayerList::addLayer()");
140 // check if layer already exists:
141 Layer * l = find(layer->getName());
145 layers.append(layer);
149 for(int i=0; i<layerListListeners.count(); ++i)
151 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 LayerList::remove(Layer * layer)
182 DEBUG->print("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 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 LayerList::edit(Layer * layer, const Layer & source)
225 for(int i=0; i<layerListListeners.count(); ++i)
227 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 Layer * LayerList::find(const QString & name)
241 //DEBUG->print("LayerList::find begin");
245 for(int i=0; i<layers.size(); i++)
247 Layer * l = layers[i];
249 if (l->getName() == name)
253 //DEBUG->print("LayerList::find end");
259 * @return Index of the given layer in the layer list or -1 if the layer
262 int LayerList::getIndex(const QString & name)
265 //DEBUG->print("LayerList::find begin");
270 for(Layer * l=layers.first(); l!=NULL; l=layers.next())
272 if (l->getName() == name)
281 //DEBUG->print("LayerList::find end");
285 for(int i=0; i<layers.size(); i++)
287 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 LayerList::getIndex(Layer * layer)
304 //DEBUG->print("LayerList::find begin");
309 for(Layer* l=layers.first(); l!=NULL; l=layers.next())
320 //DEBUG->print("LayerList::find end");
324 return layers.indexOf(layer);
329 * Switches on / off the given layer.
330 * Listeners are notified.
332 void LayerList::toggle(const QString & name)
338 * Switches on / off the given layer.
339 * Listeners are notified.
341 void LayerList::toggle(Layer * layer)
350 for(int i=0; i < layerListListeners.count(); ++i)
352 LayerListListener * l = layerListListeners.at(i);
353 l->layerToggled(layer);
359 * Locks or unlocks the given layer.
360 * Listeners are notified.
362 void LayerList::toggleLock(Layer * layer)
371 for(int i=0; i<layerListListeners.count(); ++i)
373 LayerListListener * l = layerListListeners.at(i);
374 l->layerToggled(layer);
380 * Freezes or defreezes all layers.
382 * @param freeze true: freeze, false: defreeze
384 void 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 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 LayerList::addListener(LayerListListener * listener)
407 layerListListeners.append(listener);
411 * removes a LayerListListener from the list of listeners.
413 void LayerList::removeListener(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 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 LayerList::isModified() const
442 * Dumps the layers to stdout.
444 std::ostream & operator<<(std::ostream & os, LayerList & l)
446 os << "Layerlist: \n";
448 for(uint i=0; i<l.count(); i++)
449 os << *(l.at(i)) << "\n";