2
* Inkscape::LayerManager - a view of a document's layers, relative
3
* to a particular desktop
5
* Copyright 2006 MenTaLguY <mental@rydia.net>
7
* Released under GNU GPL, read the file 'COPYING' for more information
11
#include <sigc++/functors/mem_fun.h>
12
#include <sigc++/adaptors/hide.h>
13
#include "gc-managed.h"
14
#include "gc-finalized.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"
25
#include "xml/node-observer.h"
26
#include "util/format.h"
27
// #include "debug/event-tracker.h"
28
// #include "debug/simple-event.h"
33
using Inkscape::XML::Node;
35
class LayerManager::LayerWatcher : public Inkscape::XML::NodeObserver {
37
LayerWatcher(LayerManager* mgr, SPObject* obj, sigc::connection c) :
41
_lockedAttr(g_quark_from_string("sodipodi:insensitive")),
42
_labelAttr(g_quark_from_string("inkscape:label"))
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 ) {
52
_mgr->_objectModified( _obj, 0 );
59
sigc::connection _connection;
67
Util::ptr_shared<char> stringify_node(Node const &node);
69
Util::ptr_shared<char> stringify_obj(SPObject const &obj) {
73
string = g_strdup_printf("SPObject(%p)=%s repr(%p)", &obj, obj.id, obj.repr);
75
string = g_strdup_printf("SPObject(%p) repr(%p)", &obj, obj.repr);
78
Util::ptr_shared<char> result=Util::share_string(string);
84
typedef Debug::SimpleEvent<Debug::Event::OTHER> DebugLayer;
86
class DebugLayerNote : public DebugLayer {
88
DebugLayerNote(Util::ptr_shared<char> descr)
89
: DebugLayer(Util::share_static_string("layer-note"))
91
_addProperty("descr", descr);
95
class DebugLayerRebuild : public DebugLayer {
98
: DebugLayer(Util::share_static_string("rebuild-layers"))
103
class DebugLayerObj : public DebugLayer {
105
DebugLayerObj(SPObject const& obj, Util::ptr_shared<char> name)
108
_addProperty("layer", stringify_obj(obj));
112
class DebugAddLayer : public DebugLayerObj {
114
DebugAddLayer(SPObject const &obj)
115
: DebugLayerObj(obj, Util::share_static_string("add-layer"))
121
} // end of namespace
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();
147
void LayerManager::setCurrentLayer( SPObject* obj )
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();
160
void LayerManager::renameLayer( SPObject* obj, gchar const *label, bool uniquify )
162
Glib::ustring incoming( label ? label : "" );
163
Glib::ustring result(incoming);
164
Glib::ustring base(incoming);
169
Glib::ustring::size_type pos = base.rfind('#');
170
if ( pos != Glib::ustring::npos ) {
171
gchar* numpart = g_strdup(base.substr(pos+1).c_str());
174
guint64 val = g_ascii_strtoull( numpart, &endPtr, 10);
175
if ( ((val > 0) || (endPtr != numpart)) && (val < 65536) ) {
178
startNum = static_cast<int>(val);
184
std::set<Glib::ustring> currentNames;
185
GSList const *layers=sp_document_get_resource_list(_document, "layer");
186
SPObject *root=_desktop->currentRoot();
188
for ( GSList const *iter=layers ; iter ; iter = iter->next ) {
189
SPObject *layer=static_cast<SPObject *>(iter->data);
190
if ( layer != obj ) {
191
currentNames.insert( layer->label() ? Glib::ustring(layer->label()) : Glib::ustring() );
196
// Not sure if we need to cap it, but we'll just be paranoid for the moment
197
// Intentionally unsigned
198
guint endNum = startNum + 3000;
199
for ( guint i = startNum; (i < endNum) && (currentNames.find(result) != currentNames.end()); i++ ) {
200
gchar* suffix = g_strdup_printf("#%d", i);
209
obj->setLabel( result.c_str() );
214
void LayerManager::_setDocument(SPDocument *document) {
216
_resource_connection.disconnect();
218
_document = document;
220
_resource_connection = sp_document_resources_changed_connect(document, "layer", sigc::mem_fun(*this, &LayerManager::_rebuild));
225
void LayerManager::_objectModified( SPObject* obj, guint /*flags*/ )
227
_details_changed_signal.emit( obj );
230
void LayerManager::_rebuild() {
231
// Debug::EventTracker<DebugLayerRebuild> tracker1();
233
while ( !_watchers.empty() ) {
234
LayerWatcher* one = _watchers.back();
235
_watchers.pop_back();
237
Node* node = SP_OBJECT_REPR(one->_obj);
239
node->removeObserver(*one);
241
one->_connection.disconnect();
247
if (!_document) // http://sourceforge.net/mailarchive/forum.php?thread_name=5747bce9a7ed077c1b4fc9f0f4f8a5e0%40localhost&forum_name=inkscape-devel
250
GSList const *layers = sp_document_get_resource_list(_document, "layer");
251
SPObject *root=_desktop->currentRoot();
255
std::set<SPGroup*> layersToAdd;
257
for ( GSList const *iter = layers; iter; iter = iter->next ) {
258
SPObject *layer = static_cast<SPObject *>(iter->data);
259
// Debug::EventTracker<DebugLayerNote> tracker(Util::format("Examining %s", layer->label()));
260
bool needsAdd = false;
261
std::set<SPGroup*> additional;
263
if ( root->isAncestorOf(layer) ) {
265
for ( SPObject* curr = layer; curr && (curr != root) && needsAdd; curr = SP_OBJECT_PARENT(curr) ) {
266
if ( SP_IS_GROUP(curr) ) {
267
SPGroup* group = SP_GROUP(curr);
268
if ( group->layerMode() == SPGroup::LAYER ) {
269
// If we have a layer-group as the one or a parent, ensure it is listed as a valid layer.
270
needsAdd &= ( g_slist_find(const_cast<GSList *>(layers), curr) != NULL );
271
if ( (!(group->repr)) || (!(group->repr->parent())) ) {
275
// If a non-layer group is a parent of layer groups, then show it also as a layer.
276
// TODO add the magic Inkscape group mode?
277
if ( group->repr && group->repr->parent() ) {
278
additional.insert(group);
287
if ( !includes(layer) ) {
288
layersToAdd.insert(SP_GROUP(layer));
290
for ( std::set<SPGroup*>::iterator it = additional.begin(); it != additional.end(); ++it ) {
291
if ( !includes(*it) ) {
292
layersToAdd.insert(*it);
298
for ( std::set<SPGroup*>::iterator it = layersToAdd.begin(); it != layersToAdd.end(); ++it ) {
299
SPGroup* layer = *it;
300
// Filter out objects in the middle of being deleted
302
// Such may have been the cause of bug 1339397.
303
// See http://sourceforge.net/tracker/index.php?func=detail&aid=1339397&group_id=93438&atid=604306
305
SPObject const *higher = layer;
306
while ( higher && (SP_OBJECT_PARENT(higher) != root) ) {
307
higher = SP_OBJECT_PARENT(higher);
309
Node* node = higher ? SP_OBJECT_REPR(higher) : 0;
310
if ( node && node->parent() ) {
311
// Debug::EventTracker<DebugAddLayer> tracker(*layer);
313
sigc::connection connection = layer->connectModified(sigc::mem_fun(*this, &LayerManager::_objectModified));
315
LayerWatcher *eye = new LayerWatcher(this, layer, connection);
316
_watchers.push_back( eye );
317
SP_OBJECT_REPR(layer)->addObserver(*eye);
325
// Connected to the desktop's CurrentLayerChanged signal
326
void LayerManager::_selectedLayerChanged(SPObject *layer)
328
// notify anyone who's listening to this instead of directly to the desktop
329
_layer_changed_signal.emit(layer);
337
c-file-style:"stroustrup"
338
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
343
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :