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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 06/01/2010 Added this text. :-)
15 #include "rs_layerlist.h"
18 #include "rs_layerlistlistener.h"
19 #include "rs_entity.h"
22 * Default constructor.
24 RS_LayerList::RS_LayerList()
26 // Good news, we don't have to screw with this shit
27 // layers.setAutoDelete(false);
28 // layerListListeners.setAutoDelete(false);
33 /*virtual*/ RS_LayerList::~RS_LayerList()
38 * Removes all layers in the layerlist.
40 void RS_LayerList::clear()
47 * @return Number of layers in the list.
49 uint RS_LayerList::count() const
51 return layers.count();
55 * @return Layer at given position or NULL if i is out of range.
57 RS_Layer * RS_LayerList::at(uint i)
63 * Activates the given layer.
65 * @param notify Notify listeners.
67 void RS_LayerList::activate(const QString & name, bool notify)
69 RS_DEBUG->print("RS_LayerList::activate: %s, notify: %d begin", name.toLatin1().data(), notify);
71 activate(find(name), notify);
73 if (activeLayer==NULL) {
74 RS_DEBUG->print("activeLayer is NULL");
76 RS_DEBUG->print("activeLayer is %s", activeLayer->getName().latin1());
80 RS_DEBUG->print("RS_LayerList::activate: %s end", name.toLatin1().data());
84 * Activates the given layer.
86 * @param notify Notify listeners.
88 void RS_LayerList::activate(RS_Layer * layer, bool notify)
90 RS_DEBUG->print("RS_LayerList::activate notify: %d begin", notify);
93 RS_DEBUG->print("RS_LayerList::activate: %s",
94 layer->getName().latin1());
96 RS_DEBUG->print("RS_LayerList::activate: NULL");
103 for(int i=0; i<layerListListeners.count(); ++i)
105 RS_LayerListListener * l = layerListListeners.at(i);
106 l->layerActivated(activeLayer);
107 RS_DEBUG->print("RS_LayerList::activate listener notified");
111 RS_DEBUG->print("RS_LayerList::activate end");
114 //! @return The active layer of NULL if no layer is activated.
115 RS_Layer * RS_LayerList::getActive()
121 * Adds a layer to the layer list.
122 * If there is already a layer with the same name, no layer is
123 * added. In that case the layer passed to the methode will be deleted!
124 * If no layer was active so far, the new layer becomes the active one.
126 * Listeners are notified.
128 void RS_LayerList::add(RS_Layer * layer)
130 RS_DEBUG->print("RS_LayerList::addLayer()");
135 // check if layer already exists:
136 RS_Layer * l = find(layer->getName());
140 layers.append(layer);
143 for(int i=0; i<layerListListeners.count(); ++i)
145 RS_LayerListListener * l = layerListListeners.at(i);
146 l->layerAdded(layer);
151 // if there was no active layer so far, activate this one.
152 if (activeLayer ==NULL)
157 // if there was no active layer so far, activate this one.
158 if (activeLayer == NULL)
161 l->setPen(layer->getPen());
169 * Removes a layer from the list.
170 * Listeners are notified after the layer was removed from
171 * the list but before it gets deleted.
173 void RS_LayerList::remove(RS_Layer * layer)
175 RS_DEBUG->print("RS_LayerList::removeLayer()");
180 // here the layer is removed from the list but not deleted
181 // layers.remove(layer);
182 int idx = layers.indexOf(layer);
187 for(int i=0; i<layerListListeners.count(); ++i)
189 RS_LayerListListener * l = layerListListeners.at(i);
190 l->layerRemoved(layer);
195 // activate an other layer if necessary:
196 if (activeLayer == layer)
197 activate(layers.first());
199 // now it's save to delete the layer
204 * Changes a layer's attributes. The attributes of layer 'layer'
205 * are copied from layer 'source'.
206 * Listeners are notified.
208 void RS_LayerList::edit(RS_Layer * layer, const RS_Layer & source)
215 for(int i=0; i<layerListListeners.count(); ++i)
217 RS_LayerListListener * l = layerListListeners.at(i);
218 l->layerEdited(layer);
225 * @return Pointer to the layer with the given name or
226 * \p NULL if no such layer was found.
228 RS_Layer * RS_LayerList::find(const QString & name)
230 //RS_DEBUG->print("RS_LayerList::find begin");
232 RS_Layer * ret = NULL;
234 // for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
235 for(int i=0; i<layers.size(); i++)
237 RS_Layer * l = layers[i];
239 if (l->getName() == name)
243 //RS_DEBUG->print("RS_LayerList::find end");
249 * @return Index of the given layer in the layer list or -1 if the layer
252 int RS_LayerList::getIndex(const QString & name)
255 //RS_DEBUG->print("RS_LayerList::find begin");
260 for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
262 if (l->getName() == name)
271 //RS_DEBUG->print("RS_LayerList::find end");
275 for(int i=0; i<layers.size(); i++)
277 RS_Layer * l = layers[i];
279 if (l->getName() == name)
288 * @return Index of the given layer in the layer list or -1 if the layer
291 int RS_LayerList::getIndex(RS_Layer * layer)
294 //RS_DEBUG->print("RS_LayerList::find begin");
299 for(RS_Layer* l=layers.first(); l!=NULL; l=layers.next())
310 //RS_DEBUG->print("RS_LayerList::find end");
314 return layers.indexOf(layer);
319 * Switches on / off the given layer.
320 * Listeners are notified.
322 void RS_LayerList::toggle(const QString & name)
328 * Switches on / off the given layer.
329 * Listeners are notified.
331 void RS_LayerList::toggle(RS_Layer * layer)
339 for(int i=0; i < layerListListeners.count(); ++i)
341 RS_LayerListListener * l = layerListListeners.at(i);
342 l->layerToggled(layer);
347 * Locks or unlocks the given layer.
348 * Listeners are notified.
350 void RS_LayerList::toggleLock(RS_Layer * layer)
358 for(int i=0; i<layerListListeners.count(); ++i)
360 RS_LayerListListener * l = layerListListeners.at(i);
361 l->layerToggled(layer);
366 * Freezes or defreezes all layers.
368 * @param freeze true: freeze, false: defreeze
370 void RS_LayerList::freezeAll(bool freeze)
372 for(uint l=0; l<count(); l++)
373 at(l)->freeze(freeze);
375 for(int i=0; i<layerListListeners.count(); ++i)
377 RS_LayerListListener * l = layerListListeners.at(i);
378 l->layerToggled(NULL);
383 * adds a LayerListListener to the list of listeners. Listeners
384 * are notified when the layer list changes.
386 * Typical listeners are: layer list widgets, pen toolbar, graphic view
388 void RS_LayerList::addListener(RS_LayerListListener * listener)
390 layerListListeners.append(listener);
394 * removes a LayerListListener from the list of listeners.
396 void RS_LayerList::removeListener(RS_LayerListListener * listener)
398 // layerListListeners.remove(listener);
400 int i = layerListListeners.indexOf(listener);
403 layerListListeners.takeAt(i);
407 * Sets the layer lists modified status to 'm'.
409 void RS_LayerList::setModified(bool m)
415 * @retval true The layer list has been modified.
416 * @retval false The layer list has not been modified.
418 /*virtual*/ bool RS_LayerList::isModified() const
424 * Dumps the layers to stdout.
426 std::ostream & operator<<(std::ostream & os, RS_LayerList & l)
428 os << "Layerlist: \n";
430 for(uint i=0; i<l.count(); i++)
431 os << *(l.at(i)) << "\n";