Code

Make snapping to the item's transformation center optional, but not yet available...
[inkscape.git] / src / selection.cpp
1 /** \file
2  * Per-desktop selection container
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   MenTaLguY <mental@rydia.net>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Andrius R. <knutux@gmail.com>
9  *
10  * Copyright (C)      2006 Andrius R.
11  * Copyright (C) 2004-2005 MenTaLguY
12  * Copyright (C) 1999-2002 Lauris Kaplinski
13  * Copyright (C) 2001-2002 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include "macros.h"
22 #include "inkscape-private.h"
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "document.h"
26 #include "selection.h"
27 #include "xml/repr.h"
29 #include "sp-shape.h"
30 #include "sp-path.h"
31 #include "sp-item-group.h"
33 #include <sigc++/functors/mem_fun.h>
35 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
37 namespace Inkscape {
39 Selection::Selection(SPDesktop *desktop) :
40     _objs(NULL),
41     _reprs(NULL),
42     _items(NULL),
43     _desktop(desktop),
44     _selection_context(NULL),
45     _flags(0),
46     _idle(0)
47 {
48 }
50 Selection::~Selection() {
51     _clear();
52     _desktop = NULL;
53     if (_idle) {
54         g_source_remove(_idle);
55         _idle = 0;
56     }
57 }
59 /* Handler for selected objects "modified" signal */
61 void Selection::_schedule_modified(SPObject *obj, guint flags) {
62     if (!this->_idle) {
63         /* Request handling to be run in _idle loop */
64         this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
65     }
67     /* Collect all flags */
68     this->_flags |= flags;
69 }
71 gboolean
72 Selection::_emit_modified(Selection *selection)
73 {
74     /* force new handler to be created if requested before we return */
75     selection->_idle = 0;
76     guint flags = selection->_flags;
77     selection->_flags = 0;
79     selection->_emitModified(flags);
81     /* drop this handler */
82     return FALSE;
83 }
85 void Selection::_emitModified(guint flags) {
86     inkscape_selection_modified(this, flags);
87     _modified_signal.emit(this, flags);
88 }
90 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
91     if (persist_selection_context) {
92         if (NULL == _selection_context) {
93             _selection_context = desktop()->currentLayer();
94             sp_object_ref(_selection_context, NULL);
95             _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
96         }
97     } else {
98         _releaseContext(_selection_context);
99     }
101     inkscape_selection_changed(this);
102     _changed_signal.emit(this);
105 void
106 Selection::_releaseContext(SPObject *obj)
108     if (NULL == _selection_context || _selection_context != obj)
109         return;
111     _context_release_connection.disconnect();
113     sp_object_unref(_selection_context, NULL);
114     _selection_context = NULL;
117 void Selection::_invalidateCachedLists() {
118     g_slist_free(_items);
119     _items = NULL;
121     g_slist_free(_reprs);
122     _reprs = NULL;
125 void Selection::_clear() {
126     _invalidateCachedLists();
127     while (_objs) {
128         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
129         _remove(obj);
130     }
133 SPObject *Selection::activeContext() {
134     if (NULL != _selection_context)
135         return _selection_context;
136     return desktop()->currentLayer();
137     }
139 bool Selection::includes(SPObject *obj) const {
140     if (obj == NULL)
141         return FALSE;
143     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
145     return ( g_slist_find(_objs, obj) != NULL );
148 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
149     g_return_if_fail(obj != NULL);
150     g_return_if_fail(SP_IS_OBJECT(obj));
152     if (includes(obj)) {
153         return;
154     }
156     _invalidateCachedLists();
157     _add(obj);
158     _emitChanged(persist_selection_context);
161 void Selection::_add(SPObject *obj) {
162     // unselect any of the item's ancestors and descendants which may be selected
163     // (to prevent double-selection)
164     _removeObjectDescendants(obj);
165     _removeObjectAncestors(obj);
167     _objs = g_slist_prepend(_objs, obj);
169     _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
170     _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
173 void Selection::set(SPObject *object, bool persist_selection_context) {
174     _clear();
175     add(object, persist_selection_context);
178 void Selection::toggle(SPObject *obj) {
179     if (includes (obj)) {
180         remove (obj);
181     } else {
182         add(obj);
183     }
186 void Selection::remove(SPObject *obj) {
187     g_return_if_fail(obj != NULL);
188     g_return_if_fail(SP_IS_OBJECT(obj));
189     g_return_if_fail(includes(obj));
191     _invalidateCachedLists();
192     _remove(obj);
193     _emitChanged();
196 void Selection::_remove(SPObject *obj) {
197     _modified_connections[obj].disconnect();
198     _modified_connections.erase(obj);
200     _release_connections[obj].disconnect();
201     _release_connections.erase(obj);
203     _objs = g_slist_remove(_objs, obj);
206 void Selection::setList(GSList const *list) {
207     _clear();
209     if ( list != NULL ) {
210         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
211             _add(reinterpret_cast<SPObject *>(iter->data));
212         }
213     }
215     _emitChanged();
218 void Selection::addList(GSList const *list) {
220     if (list == NULL)
221         return;
223     _invalidateCachedLists();
225     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
226         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
227         if (includes(obj)) {
228             continue;
229         }
230         _add (obj);
231     }
233     _emitChanged();
236 void Selection::setReprList(GSList const *list) {
237     _clear();
239     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
240         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
241         if (obj) {
242             _add(obj);
243         }
244     }
246     _emitChanged();
249 void Selection::clear() {
250     _clear();
251     _emitChanged();
254 GSList const *Selection::list() {
255     return _objs;
258 GSList const *Selection::itemList() {
259     if (_items) {
260         return _items;
261     }
263     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
264         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
265         if (SP_IS_ITEM(obj)) {
266             _items = g_slist_prepend(_items, SP_ITEM(obj));
267         }
268     }
269     _items = g_slist_reverse(_items);
271     return _items;
274 GSList const *Selection::reprList() {
275     if (_reprs) { return _reprs; }
277     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
278         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
279         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
280     }
281     _reprs = g_slist_reverse(_reprs);
283     return _reprs;
286 SPObject *Selection::single() {
287     if ( _objs != NULL && _objs->next == NULL ) {
288         return reinterpret_cast<SPObject *>(_objs->data);
289     } else {
290         return NULL;
291     }
294 SPItem *Selection::singleItem() {
295     GSList const *items=itemList();
296     if ( items != NULL && items->next == NULL ) {
297         return reinterpret_cast<SPItem *>(items->data);
298     } else {
299         return NULL;
300     }
303 Inkscape::XML::Node *Selection::singleRepr() {
304     SPObject *obj=single();
305     return obj ? SP_OBJECT_REPR(obj) : NULL;
308 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
310     g_return_val_if_fail (bbox != NULL, NULL);
311     *bbox = NRRect(bounds(type));
312     return bbox;
315 NR::Maybe<NR::Rect> Selection::bounds(SPItem::BBoxType type) const
317     GSList const *items = const_cast<Selection *>(this)->itemList();
319     NR::Maybe<NR::Rect> bbox = NR::Nothing();
320     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
321         bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
322     }
323     return bbox;
326 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
327     g_return_val_if_fail (bbox != NULL, NULL);
329     GSList const *items=const_cast<Selection *>(this)->itemList();
330     if (!items) {
331         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
332         return bbox;
333     }
335     bbox->x0 = bbox->y0 = 1e18;
336     bbox->x1 = bbox->y1 = -1e18;
338     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
339         SPItem *item=SP_ITEM(iter->data);
340         NR::Matrix i2doc(sp_item_i2doc_affine(item));
341         sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
342     }
344     return bbox;
347 NR::Maybe<NR::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
348     NRRect r;
349     return boundsInDocument(&r, type)->upgrade();
352 /** Extract the position of the center from the first selected object */
353 NR::Maybe<NR::Point> Selection::center() const {
354     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
355     NR::Point center;
356     if (items) {
357         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
358         if (first->isCenterSet()) { // only if set explicitly
359             return first->getCenter();
360         }
361     }
362     NR::Maybe<NR::Rect> bbox = bounds();
363     if (bbox) {
364         return bounds()->midpoint();
365     } else {
366         return NR::Nothing();
367     }
370 /**
371  * Compute the list of points in the selection that are to be considered for snapping.
372  * This includes all special points of each item in the selection, except path nodes
373  */
374 std::vector<NR::Point> Selection::getSnapPoints(bool includeItemCenter) const {
375     GSList const *items = const_cast<Selection *>(this)->itemList();
376     std::vector<NR::Point> p;
377     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
378         // getSnapPoints() is only being used in the selector tool, which should
379         // not snap path nodes. Only the node tool should snap those.
380         SPItem *this_item = SP_ITEM(iter->data);
381         if (!SP_IS_PATH(this_item)) {
382             // Only snap if we don't have a path at hand
383             // (Same check occurs in sp-item-group)
384             sp_item_snappoints(this_item, SnapPointsIter(p));
385         }
386         //Include the transformation origin for snapping
387         //For a group only the group's origin is considered
388         if (includeItemCenter) {
389                 p.push_back(this_item->getCenter());
390         }  
391     }
393     return p;
396 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
397     GSList const *items = const_cast<Selection *>(this)->itemList();
399     std::vector<NR::Point> p;
400     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
401         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
402     }
404     std::vector<NR::Point> pHull;
405     if (!p.empty()) {
406         std::vector<NR::Point>::iterator i;
407         NR::ConvexHull cvh(p.front());
408         for (i = p.begin(); i != p.end(); i++) {
409             // these are the points we get back
410             cvh.add(*i);
411         }
413         NR::Maybe<NR::Rect> rHull = cvh.bounds();
414         if (rHull) {
415             for ( unsigned i = 0 ; i < 4 ; ++i ) {
416                 pHull.push_back(rHull->corner(i));
417             }
418         }
419     }
421     return pHull;
424 void Selection::_removeObjectDescendants(SPObject *obj) {
425     GSList *iter, *next;
426     for ( iter = _objs ; iter ; iter = next ) {
427         next = iter->next;
428         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
429         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
430         while (parent) {
431             if ( parent == obj ) {
432                 _remove(sel_obj);
433                 break;
434             }
435             parent = SP_OBJECT_PARENT(parent);
436         }
437     }
440 void Selection::_removeObjectAncestors(SPObject *obj) {
441         SPObject *parent=SP_OBJECT_PARENT(obj);
442         while (parent) {
443             if (includes(parent)) {
444                 _remove(parent);
445             }
446             parent = SP_OBJECT_PARENT(parent);
447         }
450 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
451     g_return_val_if_fail(repr != NULL, NULL);
452     gchar const *id = repr->attribute("id");
453     g_return_val_if_fail(id != NULL, NULL);
454     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
455     g_return_val_if_fail(object != NULL, NULL);
456     return object;
459 guint Selection::numberOfLayers() {
460       GSList const *items = const_cast<Selection *>(this)->itemList();
461         GSList *layers = NULL;
462         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
463                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
464                 if (g_slist_find (layers, layer) == NULL) {
465                         layers = g_slist_prepend (layers, layer);
466                 }
467         }
468         guint ret = g_slist_length (layers);
469         g_slist_free (layers);
470         return ret;
473 guint Selection::numberOfParents() {
474       GSList const *items = const_cast<Selection *>(this)->itemList();
475         GSList *parents = NULL;
476         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
477                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
478                 if (g_slist_find (parents, parent) == NULL) {
479                         parents = g_slist_prepend (parents, parent);
480                 }
481         }
482         guint ret = g_slist_length (parents);
483         g_slist_free (parents);
484         return ret;
489 /*
490   Local Variables:
491   mode:c++
492   c-file-style:"stroustrup"
493   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
494   indent-tabs-mode:nil
495   fill-column:99
496   End:
497 */
498 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :