Code

Selector tool shouldn't snap to path nodes, see Bulia's comment in bug #1589436
[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"
32 #include <sigc++/functors/mem_fun.h>
34 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
36 namespace Inkscape {
38 Selection::Selection(SPDesktop *desktop) :
39     _objs(NULL),
40     _reprs(NULL),
41     _items(NULL),
42     _desktop(desktop),
43     _selection_context(NULL),
44     _flags(0),
45     _idle(0)
46 {
47 }
49 Selection::~Selection() {
50     _clear();
51     _desktop = NULL;
52     if (_idle) {
53         g_source_remove(_idle);
54         _idle = 0;
55     }
56 }
58 /* Handler for selected objects "modified" signal */
60 void Selection::_schedule_modified(SPObject *obj, guint flags) {
61     if (!this->_idle) {
62         /* Request handling to be run in _idle loop */
63         this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
64     }
66     /* Collect all flags */
67     this->_flags |= flags;
68 }
70 gboolean
71 Selection::_emit_modified(Selection *selection)
72 {
73     /* force new handler to be created if requested before we return */
74     selection->_idle = 0;
75     guint flags = selection->_flags;
76     selection->_flags = 0;
78     selection->_emitModified(flags);
80     /* drop this handler */
81     return FALSE;
82 }
84 void Selection::_emitModified(guint flags) {
85     inkscape_selection_modified(this, flags);
86     _modified_signal.emit(this, flags);
87 }
89 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
90     if (persist_selection_context) {
91         if (NULL == _selection_context) {
92             _selection_context = desktop()->currentLayer();
93             sp_object_ref(_selection_context, NULL);
94             _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
95         }
96     } else {
97         _releaseContext(_selection_context);
98     }
100     inkscape_selection_changed(this);
101     _changed_signal.emit(this);
104 void
105 Selection::_releaseContext(SPObject *obj)
107     if (NULL == _selection_context || _selection_context != obj)
108         return;
110     _context_release_connection.disconnect();
112     sp_object_unref(_selection_context, NULL);
113     _selection_context = NULL;
116 void Selection::_invalidateCachedLists() {
117     g_slist_free(_items);
118     _items = NULL;
120     g_slist_free(_reprs);
121     _reprs = NULL;
124 void Selection::_clear() {
125     _invalidateCachedLists();
126     while (_objs) {
127         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
128         _remove(obj);
129     }
132 SPObject *Selection::activeContext() {
133     if (NULL != _selection_context)
134         return _selection_context;
135     return desktop()->currentLayer();
136     }
138 bool Selection::includes(SPObject *obj) const {
139     if (obj == NULL)
140         return FALSE;
142     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
144     return ( g_slist_find(_objs, obj) != NULL );
147 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
148     g_return_if_fail(obj != NULL);
149     g_return_if_fail(SP_IS_OBJECT(obj));
151     if (includes(obj)) {
152         return;
153     }
155     _invalidateCachedLists();
156     _add(obj);
157     _emitChanged(persist_selection_context);
160 void Selection::_add(SPObject *obj) {
161     // unselect any of the item's ancestors and descendants which may be selected
162     // (to prevent double-selection)
163     _removeObjectDescendants(obj);
164     _removeObjectAncestors(obj);
166     _objs = g_slist_prepend(_objs, obj);
168     _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
169     _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
172 void Selection::set(SPObject *object, bool persist_selection_context) {
173     _clear();
174     add(object, persist_selection_context);
177 void Selection::toggle(SPObject *obj) {
178     if (includes (obj)) {
179         remove (obj);
180     } else {
181         add(obj);
182     }
185 void Selection::remove(SPObject *obj) {
186     g_return_if_fail(obj != NULL);
187     g_return_if_fail(SP_IS_OBJECT(obj));
188     g_return_if_fail(includes(obj));
190     _invalidateCachedLists();
191     _remove(obj);
192     _emitChanged();
195 void Selection::_remove(SPObject *obj) {
196     _modified_connections[obj].disconnect();
197     _modified_connections.erase(obj);
199     _release_connections[obj].disconnect();
200     _release_connections.erase(obj);
202     _objs = g_slist_remove(_objs, obj);
205 void Selection::setList(GSList const *list) {
206     _clear();
208     if ( list != NULL ) {
209         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
210             _add(reinterpret_cast<SPObject *>(iter->data));
211         }
212     }
214     _emitChanged();
217 void Selection::addList(GSList const *list) {
219     if (list == NULL)
220         return;
222     _invalidateCachedLists();
224     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
225         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
226         if (includes(obj)) {
227             continue;
228         }
229         _add (obj);
230     }
232     _emitChanged();
235 void Selection::setReprList(GSList const *list) {
236     _clear();
238     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
239         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
240         if (obj) {
241             _add(obj);
242         }
243     }
245     _emitChanged();
248 void Selection::clear() {
249     _clear();
250     _emitChanged();
253 GSList const *Selection::list() {
254     return _objs;
257 GSList const *Selection::itemList() {
258     if (_items) {
259         return _items;
260     }
262     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
263         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
264         if (SP_IS_ITEM(obj)) {
265             _items = g_slist_prepend(_items, SP_ITEM(obj));
266         }
267     }
268     _items = g_slist_reverse(_items);
270     return _items;
273 GSList const *Selection::reprList() {
274     if (_reprs) { return _reprs; }
276     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
277         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
278         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
279     }
280     _reprs = g_slist_reverse(_reprs);
282     return _reprs;
285 SPObject *Selection::single() {
286     if ( _objs != NULL && _objs->next == NULL ) {
287         return reinterpret_cast<SPObject *>(_objs->data);
288     } else {
289         return NULL;
290     }
293 SPItem *Selection::singleItem() {
294     GSList const *items=itemList();
295     if ( items != NULL && items->next == NULL ) {
296         return reinterpret_cast<SPItem *>(items->data);
297     } else {
298         return NULL;
299     }
302 Inkscape::XML::Node *Selection::singleRepr() {
303     SPObject *obj=single();
304     return obj ? SP_OBJECT_REPR(obj) : NULL;
307 NRRect *Selection::bounds(NRRect *bbox) const
309     g_return_val_if_fail (bbox != NULL, NULL);
310     *bbox = NRRect(bounds());
311     return bbox;
314 NR::Maybe<NR::Rect> Selection::bounds() const
316     GSList const *items = const_cast<Selection *>(this)->itemList();
318     NR::Maybe<NR::Rect> bbox = NR::Nothing();
319     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
320         bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data)));
321     }
322     return bbox;
325 NRRect *Selection::boundsInDocument(NRRect *bbox) const {
326     g_return_val_if_fail (bbox != NULL, NULL);
328     GSList const *items=const_cast<Selection *>(this)->itemList();
329     if (!items) {
330         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
331         return bbox;
332     }
334     bbox->x0 = bbox->y0 = 1e18;
335     bbox->x1 = bbox->y1 = -1e18;
337     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
338         SPItem *item=SP_ITEM(iter->data);
339         NR::Matrix const i2doc(sp_item_i2doc_affine(item));
340         sp_item_invoke_bbox(item, bbox, i2doc, FALSE);
341     }
343     return bbox;
346 NR::Maybe<NR::Rect> Selection::boundsInDocument() const {
347     NRRect r;
348     return boundsInDocument(&r)->upgrade();
351 /** Extract the position of the center from the first selected object */
352 NR::Maybe<NR::Point> Selection::center() const {
353     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
354     NR::Point center;
355     if (items) {
356         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
357         if (first->isCenterSet()) { // only if set explicitly
358             return first->getCenter();
359         }
360     }
361     NR::Maybe<NR::Rect> bbox = bounds();
362     if (bbox) {
363         return bounds()->midpoint();
364     } else {
365         return NR::Nothing();
366     }
369 /**
370  * Compute the list of points in the selection that are to be considered for snapping.
371  * This includes all special points of each item in the selection, except path nodes
372  */
373 std::vector<NR::Point> Selection::getSnapPoints() const {
374     GSList const *items = const_cast<Selection *>(this)->itemList();
375     std::vector<NR::Point> p;
376     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
377         // getSnapPoints() is only being used in the selector tool, which should
378         // not snap path nodes. Only the node tool should snap those.
379         SPItem *this_item = SP_ITEM(iter->data);
380         if (!SP_IS_PATH(this_item)) {
381             // Only snap if we don't have a path at hand
382             // (Same check occurs in sp-item-group)
383             sp_item_snappoints(this_item, SnapPointsIter(p));
384         }
385     }
387     return p;
390 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
391     GSList const *items = const_cast<Selection *>(this)->itemList();
393     std::vector<NR::Point> p;
394     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
395         sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
396     }
398     std::vector<NR::Point> pHull;
399     if (!p.empty()) {
400         std::vector<NR::Point>::iterator i;
401         NR::ConvexHull cvh(p.front());
402         for (i = p.begin(); i != p.end(); i++) {
403             // these are the points we get back
404             cvh.add(*i);
405         }
407         NR::Maybe<NR::Rect> rHull = cvh.bounds();
408         if (rHull) {
409             for ( unsigned i = 0 ; i < 4 ; ++i ) {
410                 pHull.push_back(rHull->corner(i));
411             }
412         }
413     }
415     return pHull;
418 void Selection::_removeObjectDescendants(SPObject *obj) {
419     GSList *iter, *next;
420     for ( iter = _objs ; iter ; iter = next ) {
421         next = iter->next;
422         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
423         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
424         while (parent) {
425             if ( parent == obj ) {
426                 _remove(sel_obj);
427                 break;
428             }
429             parent = SP_OBJECT_PARENT(parent);
430         }
431     }
434 void Selection::_removeObjectAncestors(SPObject *obj) {
435         SPObject *parent=SP_OBJECT_PARENT(obj);
436         while (parent) {
437             if (includes(parent)) {
438                 _remove(parent);
439             }
440             parent = SP_OBJECT_PARENT(parent);
441         }
444 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
445     g_return_val_if_fail(repr != NULL, NULL);
446     gchar const *id = repr->attribute("id");
447     g_return_val_if_fail(id != NULL, NULL);
448     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
449     g_return_val_if_fail(object != NULL, NULL);
450     return object;
453 guint Selection::numberOfLayers() {
454       GSList const *items = const_cast<Selection *>(this)->itemList();
455         GSList *layers = NULL;
456         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
457                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
458                 if (g_slist_find (layers, layer) == NULL) {
459                         layers = g_slist_prepend (layers, layer);
460                 }
461         }
462         guint ret = g_slist_length (layers);
463         g_slist_free (layers);
464         return ret;
467 guint Selection::numberOfParents() {
468       GSList const *items = const_cast<Selection *>(this)->itemList();
469         GSList *parents = NULL;
470         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
471                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
472                 if (g_slist_find (parents, parent) == NULL) {
473                         parents = g_slist_prepend (parents, parent);
474                 }
475         }
476         guint ret = g_slist_length (parents);
477         g_slist_free (parents);
478         return ret;
483 /*
484   Local Variables:
485   mode:c++
486   c-file-style:"stroustrup"
487   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
488   indent-tabs-mode:nil
489   fill-column:99
490   End:
491 */
492 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :