Code

A simple layout document as to what, why and how is cppification.
[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 "preferences.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 "util/format.h"
27 // #include "debug/event-tracker.h"
28 // #include "debug/simple-event.h"
30 namespace Inkscape {
33 using Inkscape::XML::Node;
35 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
36 public:
37     LayerWatcher(LayerManager* mgr, SPObject* obj, sigc::connection c) :
38         _mgr(mgr),
39         _obj(obj),
40         _connection(c),
41         _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
42         _labelAttr(g_quark_from_string("inkscape:label"))
43     {}
45     virtual void notifyChildAdded( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
46     virtual void notifyChildRemoved( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
47     virtual void notifyChildOrderChanged( Node &/*node*/, Node &/*child*/, Node */*old_prev*/, Node */*new_prev*/ ) {}
48     virtual void notifyContentChanged( Node &/*node*/, Util::ptr_shared<char> /*old_content*/, Util::ptr_shared<char> /*new_content*/ ) {}
49     virtual void notifyAttributeChanged( Node &/*node*/, GQuark name, Util::ptr_shared<char> /*old_value*/, Util::ptr_shared<char> /*new_value*/ ) {
50         if ( name == _lockedAttr || name == _labelAttr ) {
51             if ( _mgr && _obj ) {
52                 _mgr->_objectModified( _obj, 0 );
53             }
54         }
55     }
57     LayerManager* _mgr;
58     SPObject* _obj;
59     sigc::connection _connection;
60     GQuark _lockedAttr;
61     GQuark _labelAttr;
62 };
64 /*
65 namespace {
67 Util::ptr_shared<char> stringify_node(Node const &node);
69 Util::ptr_shared<char> stringify_obj(SPObject const &obj) {
70     gchar *string;
72     if (obj.id) {
73         string = g_strdup_printf("SPObject(%p)=%s  repr(%p)", &obj, obj.id, obj.repr);
74     } else {
75         string = g_strdup_printf("SPObject(%p) repr(%p)", &obj, obj.repr);
76     }
78     Util::ptr_shared<char> result=Util::share_string(string);
79     g_free(string);
80     return result;
82 }
84 typedef Debug::SimpleEvent<Debug::Event::OTHER> DebugLayer;
86 class DebugLayerNote : public DebugLayer {
87 public:
88     DebugLayerNote(Util::ptr_shared<char> descr)
89         : DebugLayer(Util::share_static_string("layer-note"))
90     {
91         _addProperty("descr", descr);
92     }
93 };
95 class DebugLayerRebuild : public DebugLayer {
96 public:
97     DebugLayerRebuild()
98         : DebugLayer(Util::share_static_string("rebuild-layers"))
99     {
100     }
101 };
103 class DebugLayerObj : public DebugLayer {
104 public:
105     DebugLayerObj(SPObject const& obj, Util::ptr_shared<char> name)
106         : DebugLayer(name)
107     {
108         _addProperty("layer", stringify_obj(obj));
109     }
110 };
112 class DebugAddLayer : public DebugLayerObj {
113 public:
114     DebugAddLayer(SPObject const &obj)
115         : DebugLayerObj(obj, Util::share_static_string("add-layer"))
116     {
117     }
118 };
121 } // end of namespace
122 */
124 LayerManager::LayerManager(SPDesktop *desktop)
125 : _desktop(desktop), _document(NULL)
127     _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
129     sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
131     // This next line has problems on gcc 4.0.2
132     sigc::slot<void, SPDocument*> base2 = first;
134     sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
135     _document_connection = desktop->connectDocumentReplaced( slot2 );
137     _setDocument(desktop->doc());
140 LayerManager::~LayerManager()
142     _layer_connection.disconnect();
143     _document_connection.disconnect();
144     _resource_connection.disconnect();
145     _document = 0;
148 void LayerManager::setCurrentLayer( SPObject* obj )
150     //g_return_if_fail( _desktop->currentRoot() );
151     if ( _desktop->currentRoot() ) {
152         _desktop->setCurrentLayer( obj );
154         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
155         if (prefs->getBool("/options/selection/layerdeselect", true)) {
156             sp_desktop_selection( _desktop )->clear();
157         }
158     }
161 void LayerManager::renameLayer( SPObject* obj, gchar const *label, bool uniquify )
163     Glib::ustring incoming( label ? label : "" );
164     Glib::ustring result(incoming);
165     Glib::ustring base(incoming);
166     guint startNum = 1;
168     if (uniquify) {
170         Glib::ustring::size_type pos = base.rfind('#');
171         if ( pos != Glib::ustring::npos ) {
172             gchar* numpart = g_strdup(base.substr(pos+1).c_str());
173             if ( numpart ) {
174                 gchar* endPtr = 0;
175                 guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
176                 if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
177                     base.erase( pos );
178                     result = base;
179                     startNum = static_cast<int>(val);
180                 }
181                 g_free(numpart);
182             }
183         }
185         std::set<Glib::ustring> currentNames;
186         GSList const *layers=_document->get_resource_list("layer");
187         SPObject *root=_desktop->currentRoot();
188         if ( root ) {
189             for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
190                 SPObject *layer=static_cast<SPObject *>(iter->data);
191                 if ( layer != obj ) {
192                     currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
193                 }
194             }
195         }
197         // Not sure if we need to cap it, but we'll just be paranoid for the moment
198         // Intentionally unsigned
199         guint endNum = startNum + 3000;
200         for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
201             gchar* suffix = g_strdup_printf("#%d", i);
202             result = base;
203             result += suffix;
205             g_free(suffix);
206         }
208     }
210     obj->setLabel( result.c_str() );
215 void LayerManager::_setDocument(SPDocument *document) {
216     if (_document) {
217         _resource_connection.disconnect();
218     }
219     _document = document;
220     if (document) {
221         _resource_connection = document->resources_changed_connect("layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
222     }
223     _rebuild();
226 void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
228     _details_changed_signal.emit( obj );
231 void LayerManager::_rebuild() {
232 //     Debug::EventTracker<DebugLayerRebuild> tracker1();
234     while ( !_watchers.empty() ) {
235         LayerWatcher* one = _watchers.back();
236         _watchers.pop_back();
237         if ( one->_obj ) {
238             Node* node = SP_OBJECT_REPR(one->_obj);
239             if ( node ) {
240                 node->removeObserver(*one);
241             }
242             one->_connection.disconnect();
243         }
244     }
246     _clear();
248     if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
249         return;
251     GSList const *layers = _document->get_resource_list("layer");
252     SPObject *root=_desktop->currentRoot();
253     if ( root ) {
254         _addOne(root);
256         std::set<SPGroup*> layersToAdd;
258         for ( GSList const *iter = layers; iter; iter = iter->next ) {
259             SPObject *layer = static_cast<SPObject *>(iter->data);
260 //             Debug::EventTracker<DebugLayerNote> tracker(Util::format("Examining %s", layer->label()));
261             bool needsAdd = false;
262             std::set<SPGroup*> additional;
264             if ( root->isAncestorOf(layer) ) {
265                 needsAdd = true;
266                 for ( SPObject* curr = layer; curr && (curr != root) && needsAdd; curr = SP_OBJECT_PARENT(curr) ) {
267                     if ( SP_IS_GROUP(curr) ) {
268                         SPGroup* group = SP_GROUP(curr);
269                         if ( group->layerMode() == SPGroup::LAYER ) {
270                             // If we have a layer-group as the one or a parent, ensure it is listed as a valid layer.
271                             needsAdd &= ( g_slist_find(const_cast<GSList *>(layers), curr) != NULL );
272                                                         // XML Tree being used here directly while it shouldn't be...
273                             if ( (!(group->getRepr())) || (!(group->getRepr()->parent())) ) {
274                                 needsAdd = false;
275                             }
276                         } else {
277                             // If a non-layer group is a parent of layer groups, then show it also as a layer.
278                             // TODO add the magic Inkscape group mode?
279                                                         // XML Tree being used directly while it shouldn't be...
280                             if ( group->getRepr() && group->getRepr()->parent() ) {
281                                 additional.insert(group);
282                             } else {
283                                 needsAdd = false;
284                             }
285                         }
286                     }
287                 }
288             }
289             if ( needsAdd ) {
290                 if ( !includes(layer) ) {
291                     layersToAdd.insert(SP_GROUP(layer));
292                 }
293                 for ( std::set<SPGroup*>::iterator it = additional.begin(); it != additional.end(); ++it ) {
294                     if ( !includes(*it) ) {
295                         layersToAdd.insert(*it);
296                     }
297                 }
298             }
299         }
301         for ( std::set<SPGroup*>::iterator it = layersToAdd.begin(); it != layersToAdd.end(); ++it ) {
302             SPGroup* layer = *it;
303             // Filter out objects in the middle of being deleted
305             // Such may have been the cause of bug 1339397.
306             // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
308             SPObject const *higher = layer;
309             while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
310                 higher = SP_OBJECT_PARENT(higher);
311             }
312             Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
313             if ( node && node->parent() ) {
314 //                 Debug::EventTracker<DebugAddLayer> tracker(*layer);
316                 sigc::connection connection = layer->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
318                 LayerWatcher *eye = new LayerWatcher(this, layer, connection);
319                 _watchers.push_back( eye );
320                 SP_OBJECT_REPR(layer)->addObserver(*eye);
322                 _addOne(layer);
323             }
324         }
325     }
328 // Connected to the desktop's CurrentLayerChanged signal
329 void LayerManager::_selectedLayerChanged(SPObject *layer)
331     // notify anyone who's listening to this instead of directly to the desktop
332     _layer_changed_signal.emit(layer);
337 /*
338   Local Variables:
339   mode:c++
340   c-file-style:"stroustrup"
341   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
342   indent-tabs-mode:nil
343   fill-column:99
344   End:
345 */
346 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :