Code

Many 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_print ("Selection::add()\n");
153     g_return_if_fail(obj != NULL);
154     g_return_if_fail(SP_IS_OBJECT(obj));
156     if (includes(obj)) {
157         return;
158     }
160     _invalidateCachedLists();
161     _add(obj);
162     _emitChanged(persist_selection_context);
165 void Selection::add_box_perspective(SPBox3D *box) {
166     Persp3D *persp = box3d_get_perspective(box);
167     std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
168     if (p != _persps.end()) {
169         (*p).second++;
170     } else {
171         _persps[persp] = 1;
172     }
175 void Selection::add_3D_boxes_recursively(SPObject *obj) {
176     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
178     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
179         SPBox3D *box = *i;
180         box3d_add_to_selection(box);
181         _3dboxes.push_back(box);
182         add_box_perspective(box);
183     }
186 void Selection::_add(SPObject *obj) {
187     // unselect any of the item's ancestors and descendants which may be selected
188     // (to prevent double-selection)
189     _removeObjectDescendants(obj);
190     _removeObjectAncestors(obj);
192     _objs = g_slist_prepend(_objs, obj);
194     add_3D_boxes_recursively(obj);
196     _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
197     _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
200 void Selection::set(SPObject *object, bool persist_selection_context) {
201     _clear();
202     add(object, persist_selection_context);
205 void Selection::toggle(SPObject *obj) {
206     if (includes (obj)) {
207         remove (obj);
208     } else {
209         add(obj);
210     }
213 void Selection::remove(SPObject *obj) {
214     g_print ("Selection::remove()\n");
215     g_return_if_fail(obj != NULL);
216     g_return_if_fail(SP_IS_OBJECT(obj));
217     g_return_if_fail(includes(obj));
219     _invalidateCachedLists();
220     _remove(obj);
221     _emitChanged();
224 void Selection::remove_box_perspective(SPBox3D *box) {
225     Persp3D *persp = box3d_get_perspective(box);
226     std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
227     if (p == _persps.end()) {
228         g_print ("Warning! Trying to remove unselected perspective from selection!\n");
229         return;
230     }
231     if ((*p).second > 1) {
232         _persps[persp]--;
233     } else {
234         _persps.erase(p);
235     }
238 void Selection::remove_3D_boxes_recursively(SPObject *obj) {
239     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
241     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
242         SPBox3D *box = *i;
243         box3d_remove_from_selection(box);
244         std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
245         if (b == _3dboxes.end()) {
246             g_print ("Warning! Trying to remove unselected box from selection.\n");
247             return;
248         }
249         _3dboxes.erase(b);
250         remove_box_perspective(box);
251     }
254 void Selection::_remove(SPObject *obj) {
255     _modified_connections[obj].disconnect();
256     _modified_connections.erase(obj);
258     _release_connections[obj].disconnect();
259     _release_connections.erase(obj);
261     remove_3D_boxes_recursively(obj);
263     _objs = g_slist_remove(_objs, obj);
266 void Selection::setList(GSList const *list) {
267     _clear();
269     if ( list != NULL ) {
270         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
271             _add(reinterpret_cast<SPObject *>(iter->data));
272         }
273     }
275     _emitChanged();
278 void Selection::addList(GSList const *list) {
280     if (list == NULL)
281         return;
283     _invalidateCachedLists();
285     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
286         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
287         if (includes(obj)) {
288             continue;
289         }
290         _add (obj);
291     }
293     _emitChanged();
296 void Selection::setReprList(GSList const *list) {
297     _clear();
299     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
300         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
301         if (obj) {
302             _add(obj);
303         }
304     }
306     _emitChanged();
309 void Selection::clear() {
310     _clear();
311     _emitChanged();
314 GSList const *Selection::list() {
315     return _objs;
318 GSList const *Selection::itemList() {
319     if (_items) {
320         return _items;
321     }
323     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
324         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
325         if (SP_IS_ITEM(obj)) {
326             _items = g_slist_prepend(_items, SP_ITEM(obj));
327         }
328     }
329     _items = g_slist_reverse(_items);
331     return _items;
334 GSList const *Selection::reprList() {
335     if (_reprs) { return _reprs; }
337     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
338         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
339         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
340     }
341     _reprs = g_slist_reverse(_reprs);
343     return _reprs;
346 std::list<Persp3D *> const Selection::perspList() {
347     std::list<Persp3D *> pl;
348     for (std::map<Persp3D *, unsigned int>::iterator p = _persps.begin(); p != _persps.end(); ++p) {
349         pl.push_back((*p).first);
350     }
351     return pl;
354 std::list<SPBox3D *> const Selection::box3DList() {
355     return _3dboxes;
358 SPObject *Selection::single() {
359     if ( _objs != NULL && _objs->next == NULL ) {
360         return reinterpret_cast<SPObject *>(_objs->data);
361     } else {
362         return NULL;
363     }
366 SPItem *Selection::singleItem() {
367     GSList const *items=itemList();
368     if ( items != NULL && items->next == NULL ) {
369         return reinterpret_cast<SPItem *>(items->data);
370     } else {
371         return NULL;
372     }
375 Inkscape::XML::Node *Selection::singleRepr() {
376     SPObject *obj=single();
377     return obj ? SP_OBJECT_REPR(obj) : NULL;
380 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
382     g_return_val_if_fail (bbox != NULL, NULL);
383     *bbox = NRRect(bounds(type));
384     return bbox;
387 boost::optional<NR::Rect> Selection::bounds(SPItem::BBoxType type) const
389     GSList const *items = const_cast<Selection *>(this)->itemList();
391     boost::optional<NR::Rect> bbox;
392     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
393         bbox = NR::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
394     }
395     return bbox;
398 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
399     g_return_val_if_fail (bbox != NULL, NULL);
401     GSList const *items=const_cast<Selection *>(this)->itemList();
402     if (!items) {
403         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
404         return bbox;
405     }
407     bbox->x0 = bbox->y0 = 1e18;
408     bbox->x1 = bbox->y1 = -1e18;
410     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
411         SPItem *item=SP_ITEM(iter->data);
412         Geom::Matrix i2doc(sp_item_i2doc_affine(item));
413         sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
414     }
416     return bbox;
419 boost::optional<NR::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
420     NRRect r;
421     return boundsInDocument(&r, type)->upgrade();
424 /** Extract the position of the center from the first selected object */
425 boost::optional<NR::Point> Selection::center() const {
426     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
427     NR::Point center;
428     if (items) {
429         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
430         if (first->isCenterSet()) { // only if set explicitly
431             return first->getCenter();
432         }
433     }
434     boost::optional<NR::Rect> bbox = bounds();
435     if (bbox) {
436         return bounds()->midpoint();
437     } else {
438         return boost::optional<NR::Point>();
439     }
442 /**
443  * Compute the list of points in the selection that are to be considered for snapping.
444  */
445 std::vector<NR::Point> Selection::getSnapPoints(bool includeItemCenter) const {
446     GSList const *items = const_cast<Selection *>(this)->itemList();
447     std::vector<NR::Point> p;
448     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
449         SPItem *this_item = SP_ITEM(iter->data);
450         sp_item_snappoints(this_item, false, SnapPointsIter(p));
451         //Include the transformation origin for snapping
452         //For a group only the group's origin is considered
453         if (includeItemCenter) {
454                 p.push_back(this_item->getCenter());
455         }  
456     }
458     return p;
461 std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
462     GSList const *items = const_cast<Selection *>(this)->itemList();
464     std::vector<NR::Point> p;
465     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
466                 sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
467     }
469     std::vector<NR::Point> pHull;
470     if (!p.empty()) {
471         std::vector<NR::Point>::iterator i;
472         NR::ConvexHull cvh(p.front());
473         for (i = p.begin(); i != p.end(); i++) {
474             // these are the points we get back
475             cvh.add(*i);
476         }
478         boost::optional<NR::Rect> rHull = cvh.bounds();
479         if (rHull) {
480             for ( unsigned i = 0 ; i < 4 ; ++i ) {
481                 pHull.push_back(rHull->corner(i));
482             }
483         }
484     }
486     return pHull;
489 void Selection::_removeObjectDescendants(SPObject *obj) {
490     GSList *iter, *next;
491     for ( iter = _objs ; iter ; iter = next ) {
492         next = iter->next;
493         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
494         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
495         while (parent) {
496             if ( parent == obj ) {
497                 _remove(sel_obj);
498                 break;
499             }
500             parent = SP_OBJECT_PARENT(parent);
501         }
502     }
505 void Selection::_removeObjectAncestors(SPObject *obj) {
506         SPObject *parent=SP_OBJECT_PARENT(obj);
507         while (parent) {
508             if (includes(parent)) {
509                 _remove(parent);
510             }
511             parent = SP_OBJECT_PARENT(parent);
512         }
515 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
516     g_return_val_if_fail(repr != NULL, NULL);
517     gchar const *id = repr->attribute("id");
518     g_return_val_if_fail(id != NULL, NULL);
519     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
520     g_return_val_if_fail(object != NULL, NULL);
521     return object;
524 guint Selection::numberOfLayers() {
525       GSList const *items = const_cast<Selection *>(this)->itemList();
526         GSList *layers = NULL;
527         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
528                 SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
529                 if (g_slist_find (layers, layer) == NULL) {
530                         layers = g_slist_prepend (layers, layer);
531                 }
532         }
533         guint ret = g_slist_length (layers);
534         g_slist_free (layers);
535         return ret;
538 guint Selection::numberOfParents() {
539       GSList const *items = const_cast<Selection *>(this)->itemList();
540         GSList *parents = NULL;
541         for (GSList const *iter = items; iter != NULL; iter = iter->next) {
542                 SPObject *parent = SP_OBJECT_PARENT(iter->data);
543                 if (g_slist_find (parents, parent) == NULL) {
544                         parents = g_slist_prepend (parents, parent);
545                 }
546         }
547         guint ret = g_slist_length (parents);
548         g_slist_free (parents);
549         return ret;
554 /*
555   Local Variables:
556   mode:c++
557   c-file-style:"stroustrup"
558   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
559   indent-tabs-mode:nil
560   fill-column:99
561   End:
562 */
563 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :