Code

2edc26de4104c41648ca251c43a188403c27d8ef
[inkscape.git] / src / layer-manager.cpp
1 /*
2  * Inkscape::LayerManager - a view of a document's layers, relative
3  *                          to a particular desktop
4  *
5  * Copyright 2006  MenTaLguY  <mental@rydia.net>
6  *
7  * Released under GNU GPL, read the file 'COPYING' for more information
8  */
10 #include <set>
11 #include <sigc++/functors/mem_fun.h>
12 #include <sigc++/adaptors/hide.h>
13 #include "gc-managed.h"
14 #include "gc-finalized.h"
15 #include "document.h"
16 #include "desktop.h"
17 #include "desktop-handles.h"
18 #include "layer-manager.h"
19 #include "prefs-utils.h"
20 #include "ui/view/view.h"
21 #include "selection.h"
22 #include "sp-object.h"
23 #include "sp-item-group.h"
24 #include "xml/node.h"
25 #include "xml/node-observer.h"
26 // #include "debug/event-tracker.h"
27 // #include "debug/simple-event.h"
29 namespace Inkscape {
32 using Inkscape::XML::Node;
34 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
35 public:
36     LayerWatcher(LayerManager* mgr, SPObject* obj, sigc::connection c) :
37         _mgr(mgr),
38         _obj(obj),
39         _connection(c),
40         _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
41         _labelAttr(g_quark_from_string("inkscape:label"))
42     {}
44     virtual void notifyChildAdded( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
45     virtual void notifyChildRemoved( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
46     virtual void notifyChildOrderChanged( Node &/*node*/, Node &/*child*/, Node */*old_prev*/, Node */*new_prev*/ ) {}
47     virtual void notifyContentChanged( Node &/*node*/, Util::ptr_shared<char> /*old_content*/, Util::ptr_shared<char> /*new_content*/ ) {}
48     virtual void notifyAttributeChanged( Node &/*node*/, GQuark name, Util::ptr_shared<char> /*old_value*/, Util::ptr_shared<char> /*new_value*/ ) {
49         if ( name == _lockedAttr || name == _labelAttr ) {
50             if ( _mgr && _obj ) {
51                 _mgr->_objectModified( _obj, 0 );
52             }
53         }
54     }
56     LayerManager* _mgr;
57     SPObject* _obj;
58     sigc::connection _connection;
59     GQuark _lockedAttr;
60     GQuark _labelAttr;
61 };
63 /*
64 namespace {
66 Util::ptr_shared<char> stringify_node(Node const &node);
68 Util::ptr_shared<char> stringify_obj(SPObject const &obj) {
69     gchar *string;
71     if (obj.id) {
72         string = g_strdup_printf("SPObject(%p)=%s  repr(%p)", &obj, obj.id, obj.repr);
73     } else {
74         string = g_strdup_printf("SPObject(%p) repr(%p)", &obj, obj.repr);
75     }
77     Util::ptr_shared<char> result=Util::share_string(string);
78     g_free(string);
79     return result;
81 }
83 typedef Debug::SimpleEvent<Debug::Event::OTHER> DebugLayer;
86 class DebugLayerRebuild : public DebugLayer {
87 public:
88     DebugLayerRebuild()
89         : DebugLayer(Util::share_static_string("rebuild-layers"))
90     {
91         _addProperty("simple", Util::share_static_string("foo"));
92     }
93 };
95 class DebugLayerObj : public DebugLayer {
96 public:
97     DebugLayerObj(SPObject const& obj, Util::ptr_shared<char> name)
98         : DebugLayer(name)
99     {
100         _addProperty("layer", stringify_obj(obj));
101     }
102 };
104 class DebugAddLayer : public DebugLayerObj {
105 public:
106     DebugAddLayer(SPObject const &obj)
107         : DebugLayerObj(obj, Util::share_static_string("add-layer"))
108     {
109     }
110 };
114 */
116 LayerManager::LayerManager(SPDesktop *desktop)
117 : _desktop(desktop), _document(NULL)
119     _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
121     sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
123     // This next line has problems on gcc 4.0.2
124     sigc::slot<void, SPDocument*> base2 = first;
126     sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
127     _document_connection = desktop->connectDocumentReplaced( slot2 );
129     _setDocument(desktop->doc());
132 LayerManager::~LayerManager()
134     _layer_connection.disconnect();
135     _document_connection.disconnect();
136     _resource_connection.disconnect();
139 void LayerManager::setCurrentLayer( SPObject* obj )
141     //g_return_if_fail( _desktop->currentRoot() );
142     if ( _desktop->currentRoot() ) {
143         _desktop->setCurrentLayer( obj );
145         if ( prefs_get_int_attribute_limited("options.selection", "layerdeselect", 1, 0, 1) ) {
146             sp_desktop_selection( _desktop )->clear();
147         }
148     }
151 void LayerManager::renameLayer( SPObject* obj, gchar const *label )
153     Glib::ustring incoming( label ? label : "" );
154     Glib::ustring result(incoming);
155     Glib::ustring base(incoming);
156     guint startNum = 1;
158     Glib::ustring::size_type pos = base.rfind('#');
159     if ( pos != Glib::ustring::npos ) {
160         gchar* numpart = g_strdup(base.substr(pos+1).c_str());
161         if ( numpart ) {
162             gchar* endPtr = 0;
163             guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
164             if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
165                 base.erase( pos );
166                 result = base;
167                 startNum = static_cast<int>(val);
168             }
169             g_free(numpart);
170         }
171     }
173     std::set<Glib::ustring> currentNames;
174     GSList const *layers=sp_document_get_resource_list(_document, "layer");
175     SPObject *root=_desktop->currentRoot();
176     if ( root ) {
177         for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
178             SPObject *layer=static_cast<SPObject *>(iter->data);
179             if ( layer != obj ) {
180                 currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
181             }
182         }
183     }
185     // Not sure if we need to cap it, but we'll just be paranoid for the moment
186     // Intentionally unsigned
187     guint endNum = startNum + 3000;
188     for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
189         gchar* suffix = g_strdup_printf("#%d", i);
190         result = base;
191         result += suffix;
193         g_free(suffix);
194     }
196     obj->setLabel( result.c_str() );
201 void LayerManager::_setDocument(SPDocument *document) {
202     if (_document) {
203         _resource_connection.disconnect();
204     }
205     _document = document;
206     if (document) {
207         _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
208     }
209     _rebuild();
212 void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
214     _details_changed_signal.emit( obj );
217 void LayerManager::_rebuild() {
218 //     Debug::EventTracker<DebugLayerRebuild> tracker1();
220     while ( !_watchers.empty() ) {
221         LayerWatcher* one = _watchers.back();
222         _watchers.pop_back();
223         if ( one->_obj ) {
224             Node* node = SP_OBJECT_REPR(one->_obj);
225             if ( node ) {
226                 node->removeObserver(*one);
227             }
228             one->_connection.disconnect();
229         }
230     }
232     _clear();
234     if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
235         return;
237     GSList const *layers = sp_document_get_resource_list(_document, "layer");
238     SPObject *root=_desktop->currentRoot();
239     if ( root ) {
240         _addOne(root);
242         std::set<SPGroup*> layersToAdd;
244         for ( GSList const *iter = layers; iter; iter = iter->next ) {
245             SPObject *layer = static_cast<SPObject *>(iter->data);
246             bool needsAdd = false;
247             std::set<SPGroup*> additional;
249             if ( root->isAncestorOf(layer) ) {
250                 needsAdd = true;
251                 for ( SPObject* curr = layer; curr && (curr != root) && needsAdd; curr = SP_OBJECT_PARENT(curr) ) {
252                     if ( SP_IS_GROUP(curr) ) {
253                         SPGroup* group = SP_GROUP(curr);
254                         if ( group->layerMode() == SPGroup::LAYER ) {
255                             // If we have a layer-group as the one or a parent, ensure it is listed as a valid layer.
256                             needsAdd &= ( g_slist_find(const_cast<GSList *>(layers), curr) != NULL );
257                         } else {
258                             // If a non-layer group is a parent of layer groups, then show it also as a layer.
259                             // TODO add the magic Inkscape group mode?
260                             additional.insert(group);
261                         }
262                     }
263                 }
264             }
265             if ( needsAdd ) {
266                 if ( !includes(layer) ) {
267                     layersToAdd.insert(SP_GROUP(layer));
268                 }
269                 for ( std::set<SPGroup*>::iterator it = additional.begin(); it != additional.end(); ++it ) {
270                     layersToAdd.insert(*it);
271                 }
272             }
273         }
275         for ( std::set<SPGroup*>::iterator it = layersToAdd.begin(); it != layersToAdd.end(); ++it ) {
276             SPGroup* layer = *it;
277             // Filter out objects in the middle of being deleted
279             // Such may have been the cause of bug 1339397.
280             // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
282             SPObject const *higher = layer;
283             while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
284                 higher = SP_OBJECT_PARENT(higher);
285             }
286             Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
287             if ( node && node->parent() ) {
288 //                 Debug::EventTracker<DebugAddLayer> tracker(*layer);
290                 sigc::connection connection = layer->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
292                 LayerWatcher *eye = new LayerWatcher(this, layer, connection);
293                 _watchers.push_back( eye );
294                 SP_OBJECT_REPR(layer)->addObserver(*eye);
295                 
296                 _addOne(layer);
297             }
298         }
299     }
302 // Connected to the desktop's CurrentLayerChanged signal
303 void LayerManager::_selectedLayerChanged(SPObject *layer)
305     // notify anyone who's listening to this instead of directly to the desktop
306     _layer_changed_signal.emit(layer);
311 /*
312   Local Variables:
313   mode:c++
314   c-file-style:"stroustrup"
315   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
316   indent-tabs-mode:nil
317   fill-column:99
318   End:
319 */
320 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :