1 /****************************************************************************
2 ** $Id: rs_layerlist.cpp 1938 2004-12-09 23:09:53Z andrew $
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
6 ** This file is part of the qcadlib Library project.
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 ** See http://www.ribbonsoft.com for further details.
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
25 **********************************************************************/
27 #include "rs_layerlist.h"
30 #include "rs_layerlistlistener.h"
31 #include "rs_entity.h"
34 * Default constructor.
36 RS_LayerList::RS_LayerList()
38 // Good news, we don't have to screw with this shit
39 // layers.setAutoDelete(false);
40 // layerListListeners.setAutoDelete(false);
45 /*virtual*/ RS_LayerList::~RS_LayerList()
50 * Removes all layers in the layerlist.
52 void RS_LayerList::clear()
59 * @return Number of layers in the list.
61 uint RS_LayerList::count() const
63 return layers.count();
67 * @return Layer at given position or NULL if i is out of range.
69 RS_Layer * RS_LayerList::at(uint i)
75 * Activates the given layer.
77 * @param notify Notify listeners.
79 void RS_LayerList::activate(const QString & name, bool notify)
81 RS_DEBUG->print("RS_LayerList::activate: %s, notify: %d begin", name.toLatin1().data(), notify);
83 activate(find(name), notify);
85 if (activeLayer==NULL) {
86 RS_DEBUG->print("activeLayer is NULL");
88 RS_DEBUG->print("activeLayer is %s", activeLayer->getName().latin1());
92 RS_DEBUG->print("RS_LayerList::activate: %s end", name.toLatin1().data());
96 * Activates the given layer.
98 * @param notify Notify listeners.
100 void RS_LayerList::activate(RS_Layer * layer, bool notify)
102 RS_DEBUG->print("RS_LayerList::activate notify: %d begin", notify);
105 RS_DEBUG->print("RS_LayerList::activate: %s",
106 layer->getName().latin1());
108 RS_DEBUG->print("RS_LayerList::activate: NULL");
115 for(int i=0; i<layerListListeners.count(); ++i)
117 RS_LayerListListener * l = layerListListeners.at(i);
118 l->layerActivated(activeLayer);
119 RS_DEBUG->print("RS_LayerList::activate listener notified");
123 RS_DEBUG->print("RS_LayerList::activate end");
126 //! @return The active layer of NULL if no layer is activated.
127 RS_Layer * RS_LayerList::getActive()
133 * Adds a layer to the layer list.
134 * If there is already a layer with the same name, no layer is
135 * added. In that case the layer passed to the methode will be deleted!
136 * If no layer was active so far, the new layer becomes the active one.
138 * Listeners are notified.
140 void RS_LayerList::add(RS_Layer * layer)
142 RS_DEBUG->print("RS_LayerList::addLayer()");
147 // check if layer already exists:
148 RS_Layer * l = find(layer->getName());
152 layers.append(layer);
155 for(int i=0; i<layerListListeners.count(); ++i)
157 RS_LayerListListener * l = layerListListeners.at(i);
158 l->layerAdded(layer);
163 // if there was no active layer so far, activate this one.
164 if (activeLayer ==NULL)
169 // if there was no active layer so far, activate this one.
170 if (activeLayer == NULL)
173 l->setPen(layer->getPen());
181 * Removes a layer from the list.
182 * Listeners are notified after the layer was removed from
183 * the list but before it gets deleted.
185 void RS_LayerList::remove(RS_Layer * layer)
187 RS_DEBUG->print("RS_LayerList::removeLayer()");
192 // here the layer is removed from the list but not deleted
193 // layers.remove(layer);
194 int idx = layers.indexOf(layer);
199 for(int i=0; i<layerListListeners.count(); ++i)
201 RS_LayerListListener * l = layerListListeners.at(i);
202 l->layerRemoved(layer);
207 // activate an other layer if necessary:
208 if (activeLayer == layer)
209 activate(layers.first());
211 // now it's save to delete the layer
216 * Changes a layer's attributes. The attributes of layer 'layer'
217 * are copied from layer 'source'.
218 * Listeners are notified.
220 void RS_LayerList::edit(RS_Layer * layer, const RS_Layer & source)
227 for(int i=0; i<layerListListeners.count(); ++i)
229 RS_LayerListListener * l = layerListListeners.at(i);
230 l->layerEdited(layer);
237 * @return Pointer to the layer with the given name or
238 * \p NULL if no such layer was found.
240 RS_Layer * RS_LayerList::find(const QString & name)
242 //RS_DEBUG->print("RS_LayerList::find begin");
244 RS_Layer * ret = NULL;
246 // for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
247 for(int i=0; i<layers.size(); i++)
249 RS_Layer * l = layers[i];
251 if (l->getName() == name)
255 //RS_DEBUG->print("RS_LayerList::find end");
261 * @return Index of the given layer in the layer list or -1 if the layer
264 int RS_LayerList::getIndex(const QString & name)
267 //RS_DEBUG->print("RS_LayerList::find begin");
272 for(RS_Layer * l=layers.first(); l!=NULL; l=layers.next())
274 if (l->getName() == name)
283 //RS_DEBUG->print("RS_LayerList::find end");
287 for(int i=0; i<layers.size(); i++)
289 RS_Layer * l = layers[i];
291 if (l->getName() == name)
300 * @return Index of the given layer in the layer list or -1 if the layer
303 int RS_LayerList::getIndex(RS_Layer * layer)
306 //RS_DEBUG->print("RS_LayerList::find begin");
311 for(RS_Layer* l=layers.first(); l!=NULL; l=layers.next())
322 //RS_DEBUG->print("RS_LayerList::find end");
326 return layers.indexOf(layer);
331 * Switches on / off the given layer.
332 * Listeners are notified.
334 void RS_LayerList::toggle(const QString & name)
340 * Switches on / off the given layer.
341 * Listeners are notified.
343 void RS_LayerList::toggle(RS_Layer * layer)
351 for(int i=0; i < layerListListeners.count(); ++i)
353 RS_LayerListListener * l = layerListListeners.at(i);
354 l->layerToggled(layer);
359 * Locks or unlocks the given layer.
360 * Listeners are notified.
362 void RS_LayerList::toggleLock(RS_Layer * layer)
370 for(int i=0; i<layerListListeners.count(); ++i)
372 RS_LayerListListener * l = layerListListeners.at(i);
373 l->layerToggled(layer);
378 * Freezes or defreezes all layers.
380 * @param freeze true: freeze, false: defreeze
382 void RS_LayerList::freezeAll(bool freeze)
384 for(uint l=0; l<count(); l++)
385 at(l)->freeze(freeze);
387 for(int i=0; i<layerListListeners.count(); ++i)
389 RS_LayerListListener * l = layerListListeners.at(i);
390 l->layerToggled(NULL);
395 * adds a LayerListListener to the list of listeners. Listeners
396 * are notified when the layer list changes.
398 * Typical listeners are: layer list widgets, pen toolbar, graphic view
400 void RS_LayerList::addListener(RS_LayerListListener * listener)
402 layerListListeners.append(listener);
406 * removes a LayerListListener from the list of listeners.
408 void RS_LayerList::removeListener(RS_LayerListListener * listener)
410 // layerListListeners.remove(listener);
412 int i = layerListListeners.indexOf(listener);
415 layerListListeners.takeAt(i);
419 * Sets the layer lists modified status to 'm'.
421 void RS_LayerList::setModified(bool m)
427 * @retval true The layer list has been modified.
428 * @retval false The layer list has not been modified.
430 /*virtual*/ bool RS_LayerList::isModified() const
436 * Dumps the layers to stdout.
438 std::ostream & operator<<(std::ostream & os, RS_LayerList & l)
440 os << "Layerlist: \n";
442 for(uint i=0; i<l.count(); i++)
443 os << *(l.at(i)) << "\n";