Code

A simple layout document as to what, why and how is cppification.
[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 "helper/recthull.h"
28 #include "xml/repr.h"
30 #include "sp-shape.h"
31 #include "sp-path.h"
32 #include "sp-item-group.h"
33 #include "box3d.h"
34 #include "box3d.h"
35 #include "persp3d.h"
37 #include <sigc++/functors/mem_fun.h>
39 #define SP_SELECTION_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE + 1)
41 namespace Inkscape {
43 Selection::Selection(SPDesktop *desktop) :
44     _objs(NULL),
45     _reprs(NULL),
46     _items(NULL),
47     _desktop(desktop),
48     _selection_context(NULL),
49     _flags(0),
50     _idle(0)
51 {
52 }
54 Selection::~Selection() {
55     _clear();
56     _desktop = NULL;
57     if (_idle) {
58         g_source_remove(_idle);
59         _idle = 0;
60     }
61 }
63 /* Handler for selected objects "modified" signal */
65 void Selection::_schedule_modified(SPObject */*obj*/, guint flags) {
66     if (!this->_idle) {
67         /* Request handling to be run in _idle loop */
68         this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
69     }
71     /* Collect all flags */
72     this->_flags |= flags;
73 }
75 gboolean
76 Selection::_emit_modified(Selection *selection)
77 {
78     /* force new handler to be created if requested before we return */
79     selection->_idle = 0;
80     guint flags = selection->_flags;
81     selection->_flags = 0;
83     selection->_emitModified(flags);
85     /* drop this handler */
86     return FALSE;
87 }
89 void Selection::_emitModified(guint flags) {
90     inkscape_selection_modified(this, flags);
91     _modified_signal.emit(this, flags);
92 }
94 void Selection::_emitChanged(bool persist_selection_context/* = false */) {
95     if (persist_selection_context) {
96         if (NULL == _selection_context) {
97             _selection_context = desktop()->currentLayer();
98             sp_object_ref(_selection_context, NULL);
99             _context_release_connection = _selection_context->connectRelease(sigc::mem_fun(*this, &Selection::_releaseContext));
100         }
101     } else {
102         _releaseContext(_selection_context);
103     }
105     inkscape_selection_changed(this);
106     _changed_signal.emit(this);
109 void
110 Selection::_releaseContext(SPObject *obj)
112     if (NULL == _selection_context || _selection_context != obj)
113         return;
115     _context_release_connection.disconnect();
117     sp_object_unref(_selection_context, NULL);
118     _selection_context = NULL;
121 void Selection::_invalidateCachedLists() {
122     g_slist_free(_items);
123     _items = NULL;
125     g_slist_free(_reprs);
126     _reprs = NULL;
129 void Selection::_clear() {
130     _invalidateCachedLists();
131     while (_objs) {
132         SPObject *obj=reinterpret_cast<SPObject *>(_objs->data);
133         _remove(obj);
134     }
137 SPObject *Selection::activeContext() {
138     if (NULL != _selection_context)
139         return _selection_context;
140     return desktop()->currentLayer();
141     }
143 bool Selection::includes(SPObject *obj) const {
144     if (obj == NULL)
145         return FALSE;
147     g_return_val_if_fail(SP_IS_OBJECT(obj), FALSE);
149     return ( g_slist_find(_objs, obj) != NULL );
152 void Selection::add(SPObject *obj, bool persist_selection_context/* = false */) {
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_3D_boxes_recursively(SPObject *obj) {
166     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
168     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
169         SPBox3D *box = *i;
170         _3dboxes.push_back(box);
171     }
174 void Selection::_add(SPObject *obj) {
175     // unselect any of the item's ancestors and descendants which may be selected
176     // (to prevent double-selection)
177     _removeObjectDescendants(obj);
178     _removeObjectAncestors(obj);
180     _objs = g_slist_prepend(_objs, obj);
182     add_3D_boxes_recursively(obj);
184     _release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
185     _modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
188 void Selection::set(SPObject *object, bool persist_selection_context) {
189     _clear();
190     add(object, persist_selection_context);
193 void Selection::toggle(SPObject *obj) {
194     if (includes (obj)) {
195         remove (obj);
196     } else {
197         add(obj);
198     }
201 void Selection::remove(SPObject *obj) {
202     g_return_if_fail(obj != NULL);
203     g_return_if_fail(SP_IS_OBJECT(obj));
204     g_return_if_fail(includes(obj));
206     _invalidateCachedLists();
207     _remove(obj);
208     _emitChanged();
211 void Selection::remove_3D_boxes_recursively(SPObject *obj) {
212     std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
214     for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
215         SPBox3D *box = *i;
216         std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
217         if (b == _3dboxes.end()) {
218             g_print ("Warning! Trying to remove unselected box from selection.\n");
219             return;
220         }
221         _3dboxes.erase(b);
222     }
225 void Selection::_remove(SPObject *obj) {
226     _modified_connections[obj].disconnect();
227     _modified_connections.erase(obj);
229     _release_connections[obj].disconnect();
230     _release_connections.erase(obj);
232     remove_3D_boxes_recursively(obj);
234     _objs = g_slist_remove(_objs, obj);
237 void Selection::setList(GSList const *list) {
238     _clear();
240     if ( list != NULL ) {
241         for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
242             _add(reinterpret_cast<SPObject *>(iter->data));
243         }
244     }
246     _emitChanged();
249 void Selection::addList(GSList const *list) {
251     if (list == NULL)
252         return;
254     _invalidateCachedLists();
256     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
257         SPObject *obj = reinterpret_cast<SPObject *>(iter->data);
258         if (includes(obj)) {
259             continue;
260         }
261         _add (obj);
262     }
264     _emitChanged();
267 void Selection::setReprList(GSList const *list) {
268     _clear();
270     for ( GSList const *iter = list ; iter != NULL ; iter = iter->next ) {
271         SPObject *obj=_objectForXMLNode(reinterpret_cast<Inkscape::XML::Node *>(iter->data));
272         if (obj) {
273             _add(obj);
274         }
275     }
277     _emitChanged();
280 void Selection::clear() {
281     _clear();
282     _emitChanged();
285 GSList const *Selection::list() {
286     return _objs;
289 GSList const *Selection::itemList() {
290     if (_items) {
291         return _items;
292     }
294     for ( GSList const *iter=_objs ; iter != NULL ; iter = iter->next ) {
295         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
296         if (SP_IS_ITEM(obj)) {
297             _items = g_slist_prepend(_items, SP_ITEM(obj));
298         }
299     }
300     _items = g_slist_reverse(_items);
302     return _items;
305 GSList const *Selection::reprList() {
306     if (_reprs) { return _reprs; }
308     for ( GSList const *iter=itemList() ; iter != NULL ; iter = iter->next ) {
309         SPObject *obj=reinterpret_cast<SPObject *>(iter->data);
310         _reprs = g_slist_prepend(_reprs, SP_OBJECT_REPR(obj));
311     }
312     _reprs = g_slist_reverse(_reprs);
314     return _reprs;
317 std::list<Persp3D *> const Selection::perspList() {
318     std::list<Persp3D *> pl;
319     for (std::list<SPBox3D *>::iterator i = _3dboxes.begin(); i != _3dboxes.end(); ++i) {
320         Persp3D *persp = box3d_get_perspective(*i);
321         if (std::find(pl.begin(), pl.end(), persp) == pl.end())
322             pl.push_back(persp);
323     }
324     return pl;
327 std::list<SPBox3D *> const Selection::box3DList(Persp3D *persp) {
328     std::list<SPBox3D *> boxes;
329     if (persp) {
330         SPBox3D *box;
331         for (std::list<SPBox3D *>::iterator i = _3dboxes.begin(); i != _3dboxes.end(); ++i) {
332             box = *i;
333             if (persp == box3d_get_perspective(box))
334                 boxes.push_back(box);
335         }
336     } else {
337         boxes = _3dboxes;
338     }
339     return boxes;
342 SPObject *Selection::single() {
343     if ( _objs != NULL && _objs->next == NULL ) {
344         return reinterpret_cast<SPObject *>(_objs->data);
345     } else {
346         return NULL;
347     }
350 SPItem *Selection::singleItem() {
351     GSList const *items=itemList();
352     if ( items != NULL && items->next == NULL ) {
353         return reinterpret_cast<SPItem *>(items->data);
354     } else {
355         return NULL;
356     }
359 Inkscape::XML::Node *Selection::singleRepr() {
360     SPObject *obj=single();
361     return obj ? SP_OBJECT_REPR(obj) : NULL;
364 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
366     g_return_val_if_fail (bbox != NULL, NULL);
367     *bbox = NRRect(bounds(type));
368     return bbox;
371 Geom::OptRect Selection::bounds(SPItem::BBoxType type) const
373     GSList const *items = const_cast<Selection *>(this)->itemList();
375     Geom::OptRect bbox;
376     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
377         bbox = unify(bbox, SP_ITEM(i->data)->getBboxDesktop(type));
378     }
379     return bbox;
382 NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
383     g_return_val_if_fail (bbox != NULL, NULL);
385     GSList const *items=const_cast<Selection *>(this)->itemList();
386     if (!items) {
387         bbox->x0 = bbox->y0 = bbox->x1 = bbox->y1 = 0.0;
388         return bbox;
389     }
391     bbox->x0 = bbox->y0 = 1e18;
392     bbox->x1 = bbox->y1 = -1e18;
394     for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
395         SPItem *item=SP_ITEM(iter->data);
396         Geom::Matrix i2doc(item->i2doc_affine());
397         item->invoke_bbox( bbox, i2doc, FALSE, type);
398     }
400     return bbox;
403 Geom::OptRect Selection::boundsInDocument(SPItem::BBoxType type) const {
404     NRRect r;
405     return to_2geom(boundsInDocument(&r, type)->upgrade());
408 /** Extract the position of the center from the first selected object */
409 boost::optional<Geom::Point> Selection::center() const {
410     GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
411     Geom::Point center;
412     if (items) {
413         SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
414         if (first->isCenterSet()) { // only if set explicitly
415             return first->getCenter();
416         }
417     }
418     Geom::OptRect bbox = bounds();
419     if (bbox) {
420         return bounds()->midpoint();
421     } else {
422         return boost::optional<Geom::Point>();
423     }
426 /**
427  * Compute the list of points in the selection that are to be considered for snapping from.
428  */
429 std::vector<Inkscape::SnapCandidatePoint> Selection::getSnapPoints(SnapPreferences const *snapprefs) const {
430     GSList const *items = const_cast<Selection *>(this)->itemList();
432     SnapPreferences snapprefs_dummy = *snapprefs; // create a local copy of the snapping prefs
433     snapprefs_dummy.setIncludeItemCenter(false); // locally disable snapping to the item center
434     snapprefs_dummy.setSnapToItemNode(true); // consider any type of nodes as a snap source
435     snapprefs_dummy.setSnapSmoothNodes(true); // i.e. disregard the smooth / cusp node preference
436     std::vector<Inkscape::SnapCandidatePoint> p;
437     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
438         SPItem *this_item = SP_ITEM(iter->data);
439         this_item->getSnappoints(p, &snapprefs_dummy);
441         //Include the transformation origin for snapping
442         //For a selection or group only the overall origin is considered
443         if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
444             p.push_back(Inkscape::SnapCandidatePoint(this_item->getCenter(), SNAPSOURCE_ROTATION_CENTER));
445         }
446     }
448     return p;
450 // TODO: both getSnapPoints and getSnapPointsConvexHull are called, subsequently. Can we do this more efficient?
451 // Why do we need to include the transformation center in one case and not the other?
452 std::vector<Inkscape::SnapCandidatePoint> Selection::getSnapPointsConvexHull(SnapPreferences const *snapprefs) const {
453     GSList const *items = const_cast<Selection *>(this)->itemList();
455     SnapPreferences snapprefs_dummy = *snapprefs; // create a local copy of the snapping prefs
456     snapprefs_dummy.setSnapToItemNode(true); // consider any type of nodes as a snap source
457     snapprefs_dummy.setSnapSmoothNodes(true); // i.e. disregard the smooth / cusp node preference
459     std::vector<Inkscape::SnapCandidatePoint> p;
460     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
461         SP_ITEM(iter->data)->getSnappoints(p, &snapprefs_dummy);
462     }
464     std::vector<Inkscape::SnapCandidatePoint> pHull;
465     if (!p.empty()) {
466         std::vector<Inkscape::SnapCandidatePoint>::iterator i;
467         Geom::RectHull cvh((p.front()).getPoint());
468         for (i = p.begin(); i != p.end(); i++) {
469             // these are the points we get back
470             cvh.add((*i).getPoint());
471         }
473         Geom::OptRect rHull = cvh.bounds();
474         if (rHull) {
475             for ( unsigned i = 0 ; i < 4 ; ++i ) {
476                 pHull.push_back(Inkscape::SnapCandidatePoint(rHull->corner(i), SNAPSOURCE_CONVEX_HULL_CORNER));
477             }
478         }
479     }
481     return pHull;
484 void Selection::_removeObjectDescendants(SPObject *obj) {
485     GSList *iter, *next;
486     for ( iter = _objs ; iter ; iter = next ) {
487         next = iter->next;
488         SPObject *sel_obj=reinterpret_cast<SPObject *>(iter->data);
489         SPObject *parent=SP_OBJECT_PARENT(sel_obj);
490         while (parent) {
491             if ( parent == obj ) {
492                 _remove(sel_obj);
493                 break;
494             }
495             parent = SP_OBJECT_PARENT(parent);
496         }
497     }
500 void Selection::_removeObjectAncestors(SPObject *obj) {
501         SPObject *parent=SP_OBJECT_PARENT(obj);
502         while (parent) {
503             if (includes(parent)) {
504                 _remove(parent);
505             }
506             parent = SP_OBJECT_PARENT(parent);
507         }
510 SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
511     g_return_val_if_fail(repr != NULL, NULL);
512     gchar const *id = repr->attribute("id");
513     g_return_val_if_fail(id != NULL, NULL);
514     SPObject *object=sp_desktop_document(_desktop)->getObjectById(id);
515     g_return_val_if_fail(object != NULL, NULL);
516     return object;
519 guint Selection::numberOfLayers() {
520     GSList const *items = const_cast<Selection *>(this)->itemList();
521     GSList *layers = NULL;
522     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
523         SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
524         if (g_slist_find (layers, layer) == NULL) {
525             layers = g_slist_prepend (layers, layer);
526         }
527     }
528     guint ret = g_slist_length (layers);
529     g_slist_free (layers);
530     return ret;
533 guint Selection::numberOfParents() {
534     GSList const *items = const_cast<Selection *>(this)->itemList();
535     GSList *parents = NULL;
536     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
537         SPObject *parent = SP_OBJECT_PARENT(iter->data);
538         if (g_slist_find (parents, parent) == NULL) {
539             parents = g_slist_prepend (parents, parent);
540         }
541     }
542     guint ret = g_slist_length (parents);
543     g_slist_free (parents);
544     return ret;
549 /*
550   Local Variables:
551   mode:c++
552   c-file-style:"stroustrup"
553   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
554   indent-tabs-mode:nil
555   fill-column:99
556   End:
557 */
558 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :