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