Code

Remove debugging messages
[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"
33 #include "box3d.h"
34 #include "persp3d.h"
36 #include <sigc++/functors/mem_fun.h>
38 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
40 namespace Inkscape {
42 Selection::Selection(SPDesktop *desktop) :
43     _objs(NULL),
44     _reprs(NULL),
45     _items(NULL),
46     _desktop(desktop),
47     _selection_context(NULL),
48     _flags(0),
49     _idle(0)
50 {
51 }
53 Selection::~Selection() {
54     _clear();
55     _desktop = NULL;
56     if (_idle) {
57         g_source_remove(_idle);
58         _idle = 0;
59     }
60 }
62 /* Handler for selected objects "modified" signal */
64 void Selection::_schedule_modified(SPObject */*obj*/, guint flags) {
65     if (!this->_idle) {
66         /* Request handling to be run in _idle loop */
67         this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
68     }
70     /* Collect all flags */
71     this->_flags |= flags;
72 }
74 gboolean
75 Selection::_emit_modified(Selection *selection)
76 {
77     /* force new handler to be created if requested before we return */
78     selection->_idle = 0;
79     guint flags = selection->_flags;
80     selection->_flags = 0;
82     selection->_emitModified(flags);
84     /* drop this handler */
85     return FALSE;
86 }
88 void Selection::_emitModified(guint flags) {
89     inkscape_selection_modified(this, flags);
90     _modified_signal.emit(this, flags);
91 }
93 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
94     if (persist_selection_context) {
95         if (NULL == _selection_context) {
96             _selection_context = desktop()->currentLayer();
97             sp_object_ref(_selection_context, NULL);
98             _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
99         }
100     } else {
101         _releaseContext(_selection_context);
102     }
104     inkscape_selection_changed(this);
105     _changed_signal.emit(this);
108 void
109 Selection::_releaseContext(SPObject *obj)
111     if (NULL == _selection_context || _selection_context != obj)
112         return;
114     _context_release_connection.disconnect();
116     sp_object_unref(_selection_context, NULL);
117     _selection_context = NULL;
120 void Selection::_invalidateCachedLists() {
121     g_slist_free(_items);
122     _items = NULL;
124     g_slist_free(_reprs);
125     _reprs = NULL;
128 void Selection::_clear() {
129     _invalidateCachedLists();
130     while (_objs) {
131         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
132         _remove(obj);
133     }
136 SPObject *Selection::activeContext() {
137     if (NULL != _selection_context)
138         return _selection_context;
139     return desktop()->currentLayer();
140     }
142 bool Selection::includes(SPObject *obj) const {
143     if (obj == NULL)
144         return FALSE;
146     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
148     return ( g_slist_find(_objs, obj) != NULL );
151 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
152     g_return_if_fail(obj != NULL);
153     g_return_if_fail(SP_IS_OBJECT(obj));
155     if (includes(obj)) {
156         return;
157     }
159     _invalidateCachedLists();
160     _add(obj);
161     _emitChanged(persist_selection_context);
164 void Selection::add_box_perspective(SPBox3D *box) {
165     Persp3D *persp = box3d_get_perspective(box);
166     std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
167     if (p != _persps.end()) {
168         (*p).second++;
169     } else {
170         _persps[persp] = 1;
171     }
174 void Selection::add_3D_boxes_recursively(SPObject *obj) {
175     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
177     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
178         SPBox3D *box = *i;
179         box3d_add_to_selection(box);
180         _3dboxes.push_back(box);
181         add_box_perspective(box);
182     }
185 void Selection::_add(SPObject *obj) {
186     // unselect any of the item's ancestors and descendants which may be selected
187     // (to prevent double-selection)
188     _removeObjectDescendants(obj);
189     _removeObjectAncestors(obj);
191     _objs = g_slist_prepend(_objs, obj);
193     add_3D_boxes_recursively(obj);
195     _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
196     _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
199 void Selection::set(SPObject *object, bool persist_selection_context) {
200     _clear();
201     add(object, persist_selection_context);
204 void Selection::toggle(SPObject *obj) {
205     if (includes (obj)) {
206         remove (obj);
207     } else {
208         add(obj);
209     }
212 void Selection::remove(SPObject *obj) {
213     g_return_if_fail(obj != NULL);
214     g_return_if_fail(SP_IS_OBJECT(obj));
215     g_return_if_fail(includes(obj));
217     _invalidateCachedLists();
218     _remove(obj);
219     _emitChanged();
222 void Selection::remove_box_perspective(SPBox3D *box) {
223     Persp3D *persp = box3d_get_perspective(box);
224     std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
225     if (p == _persps.end()) {
226         g_print ("Warning! Trying to remove unselected perspective from selection!\n");
227         return;
228     }
229     if ((*p).second > 1) {
230         _persps[persp]--;
231     } else {
232         _persps.erase(p);
233     }
236 void Selection::remove_3D_boxes_recursively(SPObject *obj) {
237     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
239     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
240         SPBox3D *box = *i;
241         box3d_remove_from_selection(box);
242         std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
243         if (b == _3dboxes.end()) {
244             g_print ("Warning! Trying to remove unselected box from selection.\n");
245             return;
246         }
247         _3dboxes.erase(b);
248         remove_box_perspective(box);
249     }
252 void Selection::_remove(SPObject *obj) {
253     _modified_connections[obj].disconnect();
254     _modified_connections.erase(obj);
256     _release_connections[obj].disconnect();
257     _release_connections.erase(obj);
259     remove_3D_boxes_recursively(obj);
261     _objs = g_slist_remove(_objs, obj);
264 void Selection::setList(GSList const *list) {
265     _clear();
267     if ( list != NULL ) {
268         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
269             _add(reinterpret_cast<SPObject *>(iter->data));
270         }
271     }
273     _emitChanged();
276 void Selection::addList(GSList const *list) {
278     if (list == NULL)
279         return;
281     _invalidateCachedLists();
283     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
284         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
285         if (includes(obj)) {
286             continue;
287         }
288         _add (obj);
289     }
291     _emitChanged();
294 void Selection::setReprList(GSList const *list) {
295     _clear();
297     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
298         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
299         if (obj) {
300             _add(obj);
301         }
302     }
304     _emitChanged();
307 void Selection::clear() {
308     _clear();
309     _emitChanged();
312 GSList const *Selection::list() {
313     return _objs;
316 GSList const *Selection::itemList() {
317     if (_items) {
318         return _items;
319     }
321     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
322         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
323         if (SP_IS_ITEM(obj)) {
324             _items = g_slist_prepend(_items, SP_ITEM(obj));
325         }
326     }
327     _items = g_slist_reverse(_items);
329     return _items;
332 GSList const *Selection::reprList() {
333     if (_reprs) { return _reprs; }
335     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
336         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
337         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
338     }
339     _reprs = g_slist_reverse(_reprs);
341     return _reprs;
344 std::list<Persp3D *> const Selection::perspList() {
345     std::list<Persp3D *> pl;
346     for (std::map<Persp3D *, unsigned int>::iterator p = _persps.begin(); p != _persps.end(); ++p) {
347         pl.push_back((*p).first);
348     }
349     return pl;
352 std::list<SPBox3D *> const Selection::box3DList() {
353     return _3dboxes;
356 SPObject *Selection::single() {
357     if ( _objs != NULL && _objs->next == NULL ) {
358         return reinterpret_cast<SPObject *>(_objs->data);
359     } else {
360         return NULL;
361     }
364 SPItem *Selection::singleItem() {
365     GSList const *items=itemList();
366     if ( items != NULL && items->next == NULL ) {
367         return reinterpret_cast<SPItem *>(items->data);
368     } else {
369         return NULL;
370     }
373 Inkscape::XML::Node *Selection::singleRepr() {
374     SPObject *obj=single();
375     return obj ? SP_OBJECT_REPR(obj) : NULL;
378 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
380     g_return_val_if_fail (bbox != NULL, NULL);
381     *bbox = NRRect(bounds(type));
382     return bbox;
385 boost::optional<NR::Rect> Selection::bounds(SPItem::BBoxType type) const
387     GSList const *items = const_cast<Selection *>(this)->itemList();
389     boost::optional<NR::Rect> bbox;
390     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
391         bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
392     }
393     return bbox;
396 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
397     g_return_val_if_fail (bbox != NULL, NULL);
399     GSList const *items=const_cast<Selection *>(this)->itemList();
400     if (!items) {
401         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
402         return bbox;
403     }
405     bbox->x0 = bbox->y0 = 1e18;
406     bbox->x1 = bbox->y1 = -1e18;
408     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
409         SPItem *item=SP_ITEM(iter->data);
410         Geom::Matrix i2doc(sp_item_i2doc_affine(item));
411         sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
412     }
414     return bbox;
417 boost::optional<NR::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
418     NRRect r;
419     return boundsInDocument(&r, type)->upgrade();
422 /** Extract the position of the center from the first selected object */
423 boost::optional<NR::Point> Selection::center() const {
424     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
425     NR::Point center;
426     if (items) {
427         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
428         if (first->isCenterSet()) { // only if set explicitly
429             return first->getCenter();
430         }
431     }
432     boost::optional<NR::Rect> bbox = bounds();
433     if (bbox) {
434         return bounds()->midpoint();
435     } else {
436         return boost::optional<NR::Point>();
437     }
440 /**
441  * Compute the list of points in the selection that are to be considered for snapping.
442  */
443 std::vector<NR::Point> Selection::getSnapPoints(bool includeItemCenter) const {
444     GSList const *items = const_cast<Selection *>(this)->itemList();
445     std::vector<NR::Point> p;
446     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
447         SPItem *this_item = SP_ITEM(iter->data);
448         sp_item_snappoints(this_item, false, SnapPointsIter(p));
449         //Include the transformation origin for snapping
450         //For a group only the group's origin is considered
451         if (includeItemCenter) {
452                 p.push_back(this_item->getCenter());
453         }  
454     }
456     return p;
459 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
460     GSList const *items = const_cast<Selection *>(this)->itemList();
462     std::vector<NR::Point> p;
463     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
464                 sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
465     }
467     std::vector<NR::Point> pHull;
468     if (!p.empty()) {
469         std::vector<NR::Point>::iterator i;
470         NR::ConvexHull cvh(p.front());
471         for (i = p.begin(); i != p.end(); i++) {
472             // these are the points we get back
473             cvh.add(*i);
474         }
476         boost::optional<NR::Rect> rHull = cvh.bounds();
477         if (rHull) {
478             for ( unsigned i = 0 ; i < 4 ; ++i ) {
479                 pHull.push_back(rHull->corner(i));
480             }
481         }
482     }
484     return pHull;
487 void Selection::_removeObjectDescendants(SPObject *obj) {
488     GSList *iter, *next;
489     for ( iter = _objs ; iter ; iter = next ) {
490         next = iter->next;
491         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
492         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
493         while (parent) {
494             if ( parent == obj ) {
495                 _remove(sel_obj);
496                 break;
497             }
498             parent = SP_OBJECT_PARENT(parent);
499         }
500     }
503 void Selection::_removeObjectAncestors(SPObject *obj) {
504         SPObject *parent=SP_OBJECT_PARENT(obj);
505         while (parent) {
506             if (includes(parent)) {
507                 _remove(parent);
508             }
509             parent = SP_OBJECT_PARENT(parent);
510         }
513 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
514     g_return_val_if_fail(repr != NULL, NULL);
515     gchar const *id = repr->attribute("id");
516     g_return_val_if_fail(id != NULL, NULL);
517     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
518     g_return_val_if_fail(object != NULL, NULL);
519     return object;
522 guint Selection::numberOfLayers() {
523       GSList const *items = const_cast<Selection *>(this)->itemList();
524         GSList *layers = NULL;
525         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
526                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
527                 if (g_slist_find (layers, layer) == NULL) {
528                         layers = g_slist_prepend (layers, layer);
529                 }
530         }
531         guint ret = g_slist_length (layers);
532         g_slist_free (layers);
533         return ret;
536 guint Selection::numberOfParents() {
537       GSList const *items = const_cast<Selection *>(this)->itemList();
538         GSList *parents = NULL;
539         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
540                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
541                 if (g_slist_find (parents, parent) == NULL) {
542                         parents = g_slist_prepend (parents, parent);
543                 }
544         }
545         guint ret = g_slist_length (parents);
546         g_slist_free (parents);
547         return ret;
552 /*
553   Local Variables:
554   mode:c++
555   c-file-style:"stroustrup"
556   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
557   indent-tabs-mode:nil
558   fill-column:99
559   End:
560 */
561 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :