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 "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)
126 {
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());
138 }
140 LayerManager::~LayerManager()
141 {
142 _layer_connection.disconnect();
143 _document_connection.disconnect();
144 _resource_connection.disconnect();
145 }
147 void LayerManager::setCurrentLayer( SPObject* obj )
148 {
149 //g_return_if_fail( _desktop->currentRoot() );
150 if ( _desktop->currentRoot() ) {
151 _desktop->setCurrentLayer( obj );
153 if ( prefs_get_int_attribute_limited("options.selection", "layerdeselect", 1, 0, 1) ) {
154 sp_desktop_selection( _desktop )->clear();
155 }
156 }
157 }
159 void LayerManager::renameLayer( SPObject* obj, gchar const *label )
160 {
161 Glib::ustring incoming( label ? label : "" );
162 Glib::ustring result(incoming);
163 Glib::ustring base(incoming);
164 guint startNum = 1;
166 Glib::ustring::size_type pos = base.rfind('#');
167 if ( pos != Glib::ustring::npos ) {
168 gchar* numpart = g_strdup(base.substr(pos+1).c_str());
169 if ( numpart ) {
170 gchar* endPtr = 0;
171 guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
172 if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
173 base.erase( pos );
174 result = base;
175 startNum = static_cast<int>(val);
176 }
177 g_free(numpart);
178 }
179 }
181 std::set<Glib::ustring> currentNames;
182 GSList const *layers=sp_document_get_resource_list(_document, "layer");
183 SPObject *root=_desktop->currentRoot();
184 if ( root ) {
185 for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
186 SPObject *layer=static_cast<SPObject *>(iter->data);
187 if ( layer != obj ) {
188 currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
189 }
190 }
191 }
193 // Not sure if we need to cap it, but we'll just be paranoid for the moment
194 // Intentionally unsigned
195 guint endNum = startNum + 3000;
196 for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
197 gchar* suffix = g_strdup_printf("#%d", i);
198 result = base;
199 result += suffix;
201 g_free(suffix);
202 }
204 obj->setLabel( result.c_str() );
205 }
209 void LayerManager::_setDocument(SPDocument *document) {
210 if (_document) {
211 _resource_connection.disconnect();
212 }
213 _document = document;
214 if (document) {
215 _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
216 }
217 _rebuild();
218 }
220 void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
221 {
222 _details_changed_signal.emit( obj );
223 }
225 void LayerManager::_rebuild() {
226 // Debug::EventTracker<DebugLayerRebuild> tracker1();
228 while ( !_watchers.empty() ) {
229 LayerWatcher* one = _watchers.back();
230 _watchers.pop_back();
231 if ( one->_obj ) {
232 Node* node = SP_OBJECT_REPR(one->_obj);
233 if ( node ) {
234 node->removeObserver(*one);
235 }
236 one->_connection.disconnect();
237 }
238 }
240 _clear();
242 if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
243 return;
245 GSList const *layers = sp_document_get_resource_list(_document, "layer");
246 SPObject *root=_desktop->currentRoot();
247 if ( root ) {
248 _addOne(root);
250 std::set<SPGroup*> layersToAdd;
252 for ( GSList const *iter = layers; iter; iter = iter->next ) {
253 SPObject *layer = static_cast<SPObject *>(iter->data);
254 // Debug::EventTracker<DebugLayerNote> tracker(Util::format("Examining %s", layer->label()));
255 bool needsAdd = false;
256 std::set<SPGroup*> additional;
258 if ( root->isAncestorOf(layer) ) {
259 needsAdd = true;
260 for ( SPObject* curr = layer; curr && (curr != root) && needsAdd; curr = SP_OBJECT_PARENT(curr) ) {
261 if ( SP_IS_GROUP(curr) ) {
262 SPGroup* group = SP_GROUP(curr);
263 if ( group->layerMode() == SPGroup::LAYER ) {
264 // If we have a layer-group as the one or a parent, ensure it is listed as a valid layer.
265 needsAdd &= ( g_slist_find(const_cast<GSList *>(layers), curr) != NULL );
266 if ( (!(group->repr)) || (!(group->repr->parent())) ) {
267 needsAdd = false;
268 }
269 } else {
270 // If a non-layer group is a parent of layer groups, then show it also as a layer.
271 // TODO add the magic Inkscape group mode?
272 if ( group->repr && group->repr->parent() ) {
273 additional.insert(group);
274 } else {
275 needsAdd = false;
276 }
277 }
278 }
279 }
280 }
281 if ( needsAdd ) {
282 if ( !includes(layer) ) {
283 layersToAdd.insert(SP_GROUP(layer));
284 }
285 for ( std::set<SPGroup*>::iterator it = additional.begin(); it != additional.end(); ++it ) {
286 if ( !includes(*it) ) {
287 layersToAdd.insert(*it);
288 }
289 }
290 }
291 }
293 for ( std::set<SPGroup*>::iterator it = layersToAdd.begin(); it != layersToAdd.end(); ++it ) {
294 SPGroup* layer = *it;
295 // Filter out objects in the middle of being deleted
297 // Such may have been the cause of bug 1339397.
298 // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
300 SPObject const *higher = layer;
301 while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
302 higher = SP_OBJECT_PARENT(higher);
303 }
304 Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
305 if ( node && node->parent() ) {
306 // Debug::EventTracker<DebugAddLayer> tracker(*layer);
308 sigc::connection connection = layer->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
310 LayerWatcher *eye = new LayerWatcher(this, layer, connection);
311 _watchers.push_back( eye );
312 SP_OBJECT_REPR(layer)->addObserver(*eye);
314 _addOne(layer);
315 }
316 }
317 }
318 }
320 // Connected to the desktop's CurrentLayerChanged signal
321 void LayerManager::_selectedLayerChanged(SPObject *layer)
322 {
323 // notify anyone who's listening to this instead of directly to the desktop
324 _layer_changed_signal.emit(layer);
325 }
327 }
329 /*
330 Local Variables:
331 mode:c++
332 c-file-style:"stroustrup"
333 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
334 indent-tabs-mode:nil
335 fill-column:99
336 End:
337 */
338 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :