Code

http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8...
[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 "xml/node.h"
24 #include "xml/node-observer.h"
26 namespace Inkscape {
29 using Inkscape::XML::Node;
31 class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
32 public:
33     LayerWatcher(LayerManager* mgr, SPObject* obj, sigc::connection c) :
34         _mgr(mgr),
35         _obj(obj),
36         _connection(c),
37         _lockedAttr(g_quark_from_string("sodipodi:insensitive")),
38         _labelAttr(g_quark_from_string("inkscape:label"))
39     {}
41     virtual void notifyChildAdded( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
42     virtual void notifyChildRemoved( Node &/*node*/, Node &/*child*/, Node */*prev*/ ) {}
43     virtual void notifyChildOrderChanged( Node &/*node*/, Node &/*child*/, Node */*old_prev*/, Node */*new_prev*/ ) {}
44     virtual void notifyContentChanged( Node &/*node*/, Util::ptr_shared<char> /*old_content*/, Util::ptr_shared<char> /*new_content*/ ) {}
45     virtual void notifyAttributeChanged( Node &/*node*/, GQuark name, Util::ptr_shared<char> /*old_value*/, Util::ptr_shared<char> /*new_value*/ ) {
46         if ( name == _lockedAttr || name == _labelAttr ) {
47             if ( _mgr && _obj ) {
48                 _mgr->_objectModified( _obj, 0 );
49             }
50         }
51     }
53     LayerManager* _mgr;
54     SPObject* _obj;
55     sigc::connection _connection;
56     GQuark _lockedAttr;
57     GQuark _labelAttr;
58 };
61 LayerManager::LayerManager(SPDesktop *desktop)
62 : _desktop(desktop), _document(NULL)
63 {
64     _layer_connection = desktop->connectCurrentLayerChanged( sigc::mem_fun(*this, &LayerManager::_selectedLayerChanged) );
66     sigc::bound_mem_functor1<void, Inkscape::LayerManager, SPDocument*> first = sigc::mem_fun(*this, &LayerManager::_setDocument);
68     // This next line has problems on gcc 4.0.2
69     sigc::slot<void, SPDocument*> base2 = first;
71     sigc::slot<void,SPDesktop*,SPDocument*> slot2 = sigc::hide<0>( base2 );
72     _document_connection = desktop->connectDocumentReplaced( slot2 );
74     _setDocument(desktop->doc());
75 }
77 LayerManager::~LayerManager()
78 {
79     _layer_connection.disconnect();
80     _document_connection.disconnect();
81     _resource_connection.disconnect();
82 }
84 void LayerManager::setCurrentLayer( SPObject* obj )
85 {
86     //g_return_if_fail( _desktop->currentRoot() );
87     if ( _desktop->currentRoot() ) {
88         _desktop->setCurrentLayer( obj );
90         if ( prefs_get_int_attribute_limited("options.selection", "layerdeselect", 1, 0, 1) ) {
91             sp_desktop_selection( _desktop )->clear();
92         }
93     }
94 }
96 void LayerManager::renameLayer( SPObject* obj, gchar const *label )
97 {
98     Glib::ustring incoming( label ? label : "" );
99     Glib::ustring result(incoming);
100     Glib::ustring base(incoming);
101     guint startNum = 1;
103     Glib::ustring::size_type pos = base.rfind('#');
104     if ( pos != Glib::ustring::npos ) {
105         gchar* numpart = g_strdup(base.substr(pos+1).c_str());
106         if ( numpart ) {
107             gchar* endPtr = 0;
108             guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
109             if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
110                 base.erase( pos );
111                 result = base;
112                 startNum = static_cast<int>(val);
113             }
114             g_free(numpart);
115         }
116     }
118     std::set<Glib::ustring> currentNames;
119     GSList const *layers=sp_document_get_resource_list(_document, "layer");
120     SPObject *root=_desktop->currentRoot();
121     if ( root ) {
122         for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
123             SPObject *layer=static_cast<SPObject *>(iter->data);
124             if ( layer != obj ) {
125                 currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
126             }
127         }
128     }
130     // Not sure if we need to cap it, but we'll just be paranoid for the moment
131     // Intentionally unsigned
132     guint endNum = startNum + 3000;
133     for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
134         gchar* suffix = g_strdup_printf("#%d", i);
135         result = base;
136         result += suffix;
138         g_free(suffix);
139     }
141     obj->setLabel( result.c_str() );
146 void LayerManager::_setDocument(SPDocument *document) {
147     if (_document) {
148         _resource_connection.disconnect();
149     }
150     _document = document;
151     if (document) {
152         _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
153     }
154     _rebuild();
157 void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
159     _details_changed_signal.emit( obj );
162 void LayerManager::_rebuild() {
163     while ( !_watchers.empty() ) {
164         LayerWatcher* one = _watchers.back();
165         _watchers.pop_back();
166         if ( one->_obj ) {
167             Node* node = SP_OBJECT_REPR(one->_obj);
168             if ( node ) {
169                 node->removeObserver(*one);
170             }
171             one->_connection.disconnect();
172         }
173     }
175     _clear();
177     if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
178         return;
180     GSList const *layers=sp_document_get_resource_list(_document, "layer");
181     SPObject *root=_desktop->currentRoot();
182     if ( root ) {
183         _addOne(root);
185         for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
186             SPObject *layer=static_cast<SPObject *>(iter->data);
188             for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
189                 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
190                     // Filter out objects in the middle of being deleted
192                     // Such may have been the cause of bug 1339397.
193                     // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
195                     SPObject const *higher = curr;
196                     while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
197                         higher = SP_OBJECT_PARENT(higher);
198                     }
199                     Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
200                     if ( node && node->parent() ) {
201                         sigc::connection connection = curr->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
203                         LayerWatcher *eye = new LayerWatcher(this, curr, connection);
204                         _watchers.push_back( eye );
205                         SP_OBJECT_REPR(curr)->addObserver(*eye);
207                         _addOne(curr);
208                     }
209                 }
210             }
211         }
212     }
215 // Connected to the desktop's CurrentLayerChanged signal
216 void LayerManager::_selectedLayerChanged(SPObject *layer)
218     // notify anyone who's listening to this instead of directly to the desktop
219     _layer_changed_signal.emit(layer);
224 /*
225   Local Variables:
226   mode:c++
227   c-file-style:"stroustrup"
228   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
229   indent-tabs-mode:nil
230   fill-column:99
231   End:
232 */
233 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :