Code

Bug fix: prevent perspectives from being transformed more than once (by keeping track...
[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"
32 #include "box3d.h"
34 #include <sigc++/functors/mem_fun.h>
36 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
38 namespace Inkscape {
40 Selection::Selection(SPDesktop *desktop) :
41     _objs(NULL),
42     _reprs(NULL),
43     _items(NULL),
44     _desktop(desktop),
45     _selection_context(NULL),
46     _flags(0),
47     _idle(0)
48 {
49 }
51 Selection::~Selection() {
52     _clear();
53     _desktop = NULL;
54     if (_idle) {
55         g_source_remove(_idle);
56         _idle = 0;
57     }
58 }
60 /* Handler for selected objects "modified" signal */
62 void Selection::_schedule_modified(SPObject */*obj*/, guint flags) {
63     if (!this->_idle) {
64         /* Request handling to be run in _idle loop */
65         this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
66     }
68     /* Collect all flags */
69     this->_flags |= flags;
70 }
72 gboolean
73 Selection::_emit_modified(Selection *selection)
74 {
75     /* force new handler to be created if requested before we return */
76     selection->_idle = 0;
77     guint flags = selection->_flags;
78     selection->_flags = 0;
80     selection->_emitModified(flags);
82     /* drop this handler */
83     return FALSE;
84 }
86 void Selection::_emitModified(guint flags) {
87     inkscape_selection_modified(this, flags);
88     _modified_signal.emit(this, flags);
89 }
91 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
92     if (persist_selection_context) {
93         if (NULL == _selection_context) {
94             _selection_context = desktop()->currentLayer();
95             sp_object_ref(_selection_context, NULL);
96             _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
97         }
98     } else {
99         _releaseContext(_selection_context);
100     }
102     inkscape_selection_changed(this);
103     _changed_signal.emit(this);
106 void
107 Selection::_releaseContext(SPObject *obj)
109     if (NULL == _selection_context || _selection_context != obj)
110         return;
112     _context_release_connection.disconnect();
114     sp_object_unref(_selection_context, NULL);
115     _selection_context = NULL;
118 void Selection::_invalidateCachedLists() {
119     g_slist_free(_items);
120     _items = NULL;
122     g_slist_free(_reprs);
123     _reprs = NULL;
126 void Selection::_clear() {
127     _invalidateCachedLists();
128     while (_objs) {
129         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
130         _remove(obj);
131     }
134 SPObject *Selection::activeContext() {
135     if (NULL != _selection_context)
136         return _selection_context;
137     return desktop()->currentLayer();
138     }
140 bool Selection::includes(SPObject *obj) const {
141     if (obj == NULL)
142         return FALSE;
144     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
146     return ( g_slist_find(_objs, obj) != NULL );
149 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
150     g_return_if_fail(obj != NULL);
151     g_return_if_fail(SP_IS_OBJECT(obj));
153     if (includes(obj)) {
154         return;
155     }
157     _invalidateCachedLists();
158     _add(obj);
159     _emitChanged(persist_selection_context);
162 void Selection::_add(SPObject *obj) {
163     // unselect any of the item's ancestors and descendants which may be selected
164     // (to prevent double-selection)
165     _removeObjectDescendants(obj);
166     _removeObjectAncestors(obj);
168     _objs = g_slist_prepend(_objs, obj);
170     if (SP_IS_BOX3D(obj)) {
171         // keep track of selected boxes for transformations
172         box3d_add_to_selection(SP_BOX3D(obj));
173     }
175     _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
176     _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
179 void Selection::set(SPObject *object, bool persist_selection_context) {
180     _clear();
181     add(object, persist_selection_context);
184 void Selection::toggle(SPObject *obj) {
185     if (includes (obj)) {
186         remove (obj);
187     } else {
188         add(obj);
189     }
192 void Selection::remove(SPObject *obj) {
193     g_return_if_fail(obj != NULL);
194     g_return_if_fail(SP_IS_OBJECT(obj));
195     g_return_if_fail(includes(obj));
197     _invalidateCachedLists();
198     _remove(obj);
199     _emitChanged();
202 void Selection::_remove(SPObject *obj) {
203     _modified_connections[obj].disconnect();
204     _modified_connections.erase(obj);
206     _release_connections[obj].disconnect();
207     _release_connections.erase(obj);
209     if (SP_IS_BOX3D(obj)) {
210         // keep track of selected boxes for transformations
211         box3d_remove_from_selection(SP_BOX3D(obj));
212     }
214     _objs = g_slist_remove(_objs, obj);
217 void Selection::setList(GSList const *list) {
218     _clear();
220     if ( list != NULL ) {
221         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
222             _add(reinterpret_cast<SPObject *>(iter->data));
223         }
224     }
226     _emitChanged();
229 void Selection::addList(GSList const *list) {
231     if (list == NULL)
232         return;
234     _invalidateCachedLists();
236     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
237         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
238         if (includes(obj)) {
239             continue;
240         }
241         _add (obj);
242     }
244     _emitChanged();
247 void Selection::setReprList(GSList const *list) {
248     _clear();
250     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
251         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
252         if (obj) {
253             _add(obj);
254         }
255     }
257     _emitChanged();
260 void Selection::clear() {
261     _clear();
262     _emitChanged();
265 GSList const *Selection::list() {
266     return _objs;
269 GSList const *Selection::itemList() {
270     if (_items) {
271         return _items;
272     }
274     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
275         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
276         if (SP_IS_ITEM(obj)) {
277             _items = g_slist_prepend(_items, SP_ITEM(obj));
278         }
279     }
280     _items = g_slist_reverse(_items);
282     return _items;
285 GSList const *Selection::reprList() {
286     if (_reprs) { return _reprs; }
288     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
289         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
290         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
291     }
292     _reprs = g_slist_reverse(_reprs);
294     return _reprs;
297 SPObject *Selection::single() {
298     if ( _objs != NULL && _objs->next == NULL ) {
299         return reinterpret_cast<SPObject *>(_objs->data);
300     } else {
301         return NULL;
302     }
305 SPItem *Selection::singleItem() {
306     GSList const *items=itemList();
307     if ( items != NULL && items->next == NULL ) {
308         return reinterpret_cast<SPItem *>(items->data);
309     } else {
310         return NULL;
311     }
314 Inkscape::XML::Node *Selection::singleRepr() {
315     SPObject *obj=single();
316     return obj ? SP_OBJECT_REPR(obj) : NULL;
319 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
321     g_return_val_if_fail (bbox != NULL, NULL);
322     *bbox = NRRect(bounds(type));
323     return bbox;
326 NR::Maybe<NR::Rect> Selection::bounds(SPItem::BBoxType type) const
328     GSList const *items = const_cast<Selection *>(this)->itemList();
330     NR::Maybe<NR::Rect> bbox = NR::Nothing();
331     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
332         bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
333     }
334     return bbox;
337 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
338     g_return_val_if_fail (bbox != NULL, NULL);
340     GSList const *items=const_cast<Selection *>(this)->itemList();
341     if (!items) {
342         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
343         return bbox;
344     }
346     bbox->x0 = bbox->y0 = 1e18;
347     bbox->x1 = bbox->y1 = -1e18;
349     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
350         SPItem *item=SP_ITEM(iter->data);
351         NR::Matrix i2doc(sp_item_i2doc_affine(item));
352         sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
353     }
355     return bbox;
358 NR::Maybe<NR::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
359     NRRect r;
360     return boundsInDocument(&r, type)->upgrade();
363 /** Extract the position of the center from the first selected object */
364 NR::Maybe<NR::Point> Selection::center() const {
365     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
366     NR::Point center;
367     if (items) {
368         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
369         if (first->isCenterSet()) { // only if set explicitly
370             return first->getCenter();
371         }
372     }
373     NR::Maybe<NR::Rect> bbox = bounds();
374     if (bbox) {
375         return bounds()->midpoint();
376     } else {
377         return NR::Nothing();
378     }
381 /**
382  * Compute the list of points in the selection that are to be considered for snapping.
383  */
384 std::vector<NR::Point> Selection::getSnapPoints(bool includeItemCenter) const {
385     GSList const *items = const_cast<Selection *>(this)->itemList();
386     std::vector<NR::Point> p;
387     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
388         SPItem *this_item = SP_ITEM(iter->data);
389         sp_item_snappoints(this_item, false, SnapPointsIter(p));
390         //Include the transformation origin for snapping
391         //For a group only the group's origin is considered
392         if (includeItemCenter) {
393                 p.push_back(this_item->getCenter());
394         }  
395     }
397     return p;
400 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
401     GSList const *items = const_cast<Selection *>(this)->itemList();
403     std::vector<NR::Point> p;
404     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
405                 sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
406     }
408     std::vector<NR::Point> pHull;
409     if (!p.empty()) {
410         std::vector<NR::Point>::iterator i;
411         NR::ConvexHull cvh(p.front());
412         for (i = p.begin(); i != p.end(); i++) {
413             // these are the points we get back
414             cvh.add(*i);
415         }
417         NR::Maybe<NR::Rect> rHull = cvh.bounds();
418         if (rHull) {
419             for ( unsigned i = 0 ; i < 4 ; ++i ) {
420                 pHull.push_back(rHull->corner(i));
421             }
422         }
423     }
425     return pHull;
428 void Selection::_removeObjectDescendants(SPObject *obj) {
429     GSList *iter, *next;
430     for ( iter = _objs ; iter ; iter = next ) {
431         next = iter->next;
432         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
433         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
434         while (parent) {
435             if ( parent == obj ) {
436                 _remove(sel_obj);
437                 break;
438             }
439             parent = SP_OBJECT_PARENT(parent);
440         }
441     }
444 void Selection::_removeObjectAncestors(SPObject *obj) {
445         SPObject *parent=SP_OBJECT_PARENT(obj);
446         while (parent) {
447             if (includes(parent)) {
448                 _remove(parent);
449             }
450             parent = SP_OBJECT_PARENT(parent);
451         }
454 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
455     g_return_val_if_fail(repr != NULL, NULL);
456     gchar const *id = repr->attribute("id");
457     g_return_val_if_fail(id != NULL, NULL);
458     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
459     g_return_val_if_fail(object != NULL, NULL);
460     return object;
463 guint Selection::numberOfLayers() {
464       GSList const *items = const_cast<Selection *>(this)->itemList();
465         GSList *layers = NULL;
466         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
467                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
468                 if (g_slist_find (layers, layer) == NULL) {
469                         layers = g_slist_prepend (layers, layer);
470                 }
471         }
472         guint ret = g_slist_length (layers);
473         g_slist_free (layers);
474         return ret;
477 guint Selection::numberOfParents() {
478       GSList const *items = const_cast<Selection *>(this)->itemList();
479         GSList *parents = NULL;
480         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
481                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
482                 if (g_slist_find (parents, parent) == NULL) {
483                         parents = g_slist_prepend (parents, parent);
484                 }
485         }
486         guint ret = g_slist_length (parents);
487         g_slist_free (parents);
488         return ret;
493 /*
494   Local Variables:
495   mode:c++
496   c-file-style:"stroustrup"
497   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
498   indent-tabs-mode:nil
499   fill-column:99
500   End:
501 */
502 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :