Code

warning cleanup
[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 }
78 void LayerManager::setCurrentLayer( SPObject* obj )
79 {
80     //g_return_if_fail( _desktop->currentRoot() );
81     if ( _desktop->currentRoot() ) {
82         _desktop->setCurrentLayer( obj );
84         if ( prefs_get_int_attribute_limited("options.selection", "layerdeselect", 1, 0, 1) ) {
85             sp_desktop_selection( _desktop )->clear();
86         }
87     }
88 }
90 void LayerManager::renameLayer( SPObject* obj, gchar const *label )
91 {
92     Glib::ustring incoming( label ? label : "" );
93     Glib::ustring result(incoming);
94     Glib::ustring base(incoming);
95     guint startNum = 1;
97     Glib::ustring::size_type pos = base.rfind('#');
98     if ( pos != Glib::ustring::npos ) {
99         gchar* numpart = g_strdup(base.substr(pos+1).c_str());
100         if ( numpart ) {
101             gchar* endPtr = 0;
102             guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
103             if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
104                 base.erase( pos );
105                 result = base;
106                 startNum = static_cast<int>(val);
107             }
108             g_free(numpart);
109         }
110     }
112     std::set<Glib::ustring> currentNames;
113     GSList const *layers=sp_document_get_resource_list(_document, "layer");
114     SPObject *root=_desktop->currentRoot();
115     if ( root ) {
116         for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
117             SPObject *layer=static_cast<SPObject *>(iter->data);
118             if ( layer != obj ) {
119                 currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
120             }
121         }
122     }
124     // Not sure if we need to cap it, but we'll just be paranoid for the moment
125     // Intentionally unsigned
126     guint endNum = startNum + 3000;
127     for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
128         gchar* suffix = g_strdup_printf("#%d", i);
129         result = base;
130         result += suffix;
132         g_free(suffix);
133     }
135     obj->setLabel( result.c_str() );
140 void LayerManager::_setDocument(SPDocument *document) {
141     if (_document) {
142         _resource_connection.disconnect();
143     }
144     _document = document;
145     if (document) {
146         _resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
147     }
148     _rebuild();
151 void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
153     _details_changed_signal.emit( obj );
156 void LayerManager::_rebuild() {
157     while ( !_watchers.empty() ) {
158         LayerWatcher* one = _watchers.back();
159         _watchers.pop_back();
160         if ( one->_obj ) {
161             Node* node = SP_OBJECT_REPR(one->_obj);
162             if ( node ) {
163                 node->removeObserver(*one);
164             }
165             one->_connection.disconnect();
166         }
167     }
169     _clear();
171     GSList const *layers=sp_document_get_resource_list(_document, "layer");
172     SPObject *root=_desktop->currentRoot();
173     if ( root ) {
174         _addOne(root);
176         for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
177             SPObject *layer=static_cast<SPObject *>(iter->data);
179             for ( SPObject* curr = layer; curr && (curr != root) ; curr = SP_OBJECT_PARENT(curr) ) {
180                 if ( (curr != root) && root->isAncestorOf(curr) && !includes(curr) ) {
181                     // Filter out objects in the middle of being deleted
183                     // Such may have been the cause of bug 1339397.
184                     // See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
186                     SPObject const *higher = curr;
187                     while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
188                         higher = SP_OBJECT_PARENT(higher);
189                     }
190                     Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
191                     if ( node && node->parent() ) {
192                         sigc::connection connection = curr->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
194                         LayerWatcher *eye = new LayerWatcher(this, curr, connection);
195                         _watchers.push_back( eye );
196                         SP_OBJECT_REPR(curr)->addObserver(*eye);
198                         _addOne(curr);
199                     }
200                 }
201             }
202         }
203     }
206 // Connected to the desktop's CurrentLayerChanged signal
207 void LayerManager::_selectedLayerChanged(SPObject *layer)
209     // notify anyone who's listening to this instead of directly to the desktop
210     _layer_changed_signal.emit(layer);
215 /*
216   Local Variables:
217   mode:c++
218   c-file-style:"stroustrup"
219   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
220   indent-tabs-mode:nil
221   fill-column:99
222   End:
223 */
224 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :