diff --git a/src/selection.cpp b/src/selection.cpp
index 82c82b5232c8053dd4eba0f5a0615580b8a0b636..1e14591fae7474ffb838c274aabdf53f29328b7e 100644 (file)
--- a/src/selection.cpp
+++ b/src/selection.cpp
#include "desktop-handles.h"
#include "document.h"
#include "selection.h"
+#include "helper/recthull.h"
#include "xml/repr.h"
#include "sp-shape.h"
+#include "sp-path.h"
+#include "sp-item-group.h"
+#include "box3d.h"
+#include "box3d.h"
+#include "persp3d.h"
#include <sigc++/functors/mem_fun.h>
/* Handler for selected objects "modified" signal */
-void Selection::_schedule_modified(SPObject *obj, guint flags) {
+void Selection::_schedule_modified(SPObject */*obj*/, guint flags) {
if (!this->_idle) {
/* Request handling to be run in _idle loop */
this->_idle = g_idle_add_full(SP_SELECTION_UPDATE_PRIORITY, GSourceFunc(&Selection::_emit_modified), this, NULL);
@@ -156,6 +162,15 @@ void Selection::add(SPObject *obj, bool persist_selection_context/* = false */)
_emitChanged(persist_selection_context);
}
+void Selection::add_3D_boxes_recursively(SPObject *obj) {
+ std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
+
+ for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
+ SPBox3D *box = *i;
+ _3dboxes.push_back(box);
+ }
+}
+
void Selection::_add(SPObject *obj) {
// unselect any of the item's ancestors and descendants which may be selected
// (to prevent double-selection)
_objs = g_slist_prepend(_objs, obj);
+ add_3D_boxes_recursively(obj);
+
_release_connections[obj] = obj->connectRelease(sigc::mem_fun(*this, (void (Selection::*)(SPObject *))&Selection::remove));
_modified_connections[obj] = obj->connectModified(sigc::mem_fun(*this, &Selection::_schedule_modified));
}
_emitChanged();
}
+void Selection::remove_3D_boxes_recursively(SPObject *obj) {
+ std::list<SPBox3D *> boxes = box3d_extract_boxes(obj);
+
+ for (std::list<SPBox3D *>::iterator i = boxes.begin(); i != boxes.end(); ++i) {
+ SPBox3D *box = *i;
+ std::list<SPBox3D *>::iterator b = std::find(_3dboxes.begin(), _3dboxes.end(), box);
+ if (b == _3dboxes.end()) {
+ g_print ("Warning! Trying to remove unselected box from selection.\n");
+ return;
+ }
+ _3dboxes.erase(b);
+ }
+}
+
void Selection::_remove(SPObject *obj) {
_modified_connections[obj].disconnect();
_modified_connections.erase(obj);
_release_connections[obj].disconnect();
_release_connections.erase(obj);
+ remove_3D_boxes_recursively(obj);
+
_objs = g_slist_remove(_objs, obj);
}
return _reprs;
}
+std::list<Persp3D *> const Selection::perspList() {
+ std::list<Persp3D *> pl;
+ for (std::list<SPBox3D *>::iterator i = _3dboxes.begin(); i != _3dboxes.end(); ++i) {
+ Persp3D *persp = box3d_get_perspective(*i);
+ if (std::find(pl.begin(), pl.end(), persp) == pl.end())
+ pl.push_back(persp);
+ }
+ return pl;
+}
+
+std::list<SPBox3D *> const Selection::box3DList(Persp3D *persp) {
+ std::list<SPBox3D *> boxes;
+ if (persp) {
+ SPBox3D *box;
+ for (std::list<SPBox3D *>::iterator i = _3dboxes.begin(); i != _3dboxes.end(); ++i) {
+ box = *i;
+ if (persp == box3d_get_perspective(box))
+ boxes.push_back(box);
+ }
+ } else {
+ boxes = _3dboxes;
+ }
+ return boxes;
+}
+
SPObject *Selection::single() {
if ( _objs != NULL && _objs->next == NULL ) {
return reinterpret_cast<SPObject *>(_objs->data);
return obj ? SP_OBJECT_REPR(obj) : NULL;
}
-NRRect *Selection::bounds(NRRect *bbox) const
+NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
{
g_return_val_if_fail (bbox != NULL, NULL);
- NR::Rect const b = bounds();
- bbox->x0 = b.min()[NR::X];
- bbox->y0 = b.min()[NR::Y];
- bbox->x1 = b.max()[NR::X];
- bbox->y1 = b.max()[NR::Y];
+ *bbox = NRRect(bounds(type));
return bbox;
}
-NR::Rect Selection::bounds() const
+Geom::OptRect Selection::bounds(SPItem::BBoxType type) const
{
GSList const *items = const_cast<Selection *>(this)->itemList();
- NR::Maybe<NR::Rect> bbox = NR::Nothing();
+ Geom::OptRect bbox;
for ( GSList const *i = items ; i != NULL ; i = i->next ) {
- bbox = NR::Rect::union_bounds(bbox, sp_item_bbox_desktop(SP_ITEM(i->data)));
- }
-
- // TODO: return NR::Maybe<NR::Rect>
- if (bbox) {
- return *bbox;
- } else {
- return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
+ bbox = unify(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
}
+ return bbox;
}
-NRRect *Selection::boundsInDocument(NRRect *bbox) const {
+NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
g_return_val_if_fail (bbox != NULL, NULL);
GSList const *items=const_cast<Selection *>(this)->itemList();
for ( GSList const *iter=items ; iter != NULL ; iter = iter->next ) {
SPItem *item=SP_ITEM(iter->data);
- NR::Matrix const i2doc(sp_item_i2doc_affine(item));
- sp_item_invoke_bbox(item, bbox, i2doc, FALSE);
+ Geom::Matrix i2doc(sp_item_i2doc_affine(item));
+ sp_item_invoke_bbox(item, bbox, i2doc, FALSE, type);
}
return bbox;
}
-NR::Rect Selection::boundsInDocument() const {
+Geom::OptRect Selection::boundsInDocument(SPItem::BBoxType type) const {
NRRect r;
- return NR::Rect(*boundsInDocument(&r));
+ return to_2geom(boundsInDocument(&r, type)->upgrade());
}
/** Extract the position of the center from the first selected object */
-NR::Point Selection::center() const {
+boost::optional<Geom::Point> Selection::center() const {
GSList *items = (GSList *) const_cast<Selection *>(this)->itemList();
- NR::Point center;
+ Geom::Point center;
if (items) {
SPItem *first = reinterpret_cast<SPItem*>(g_slist_last(items)->data); // from the first item in selection
if (first->isCenterSet()) { // only if set explicitly
- center = first->getCenter();
- } else {
- center = bounds().midpoint();
+ return first->getCenter();
}
+ }
+ Geom::OptRect bbox = bounds();
+ if (bbox) {
+ return bounds()->midpoint();
} else {
- center = bounds().midpoint();
+ return boost::optional<Geom::Point>();
}
- return center;
}
/**
* Compute the list of points in the selection that are to be considered for snapping.
*/
-std::vector<NR::Point> Selection::getSnapPoints() const {
+std::vector<Inkscape::SnapCandidatePoint> Selection::getSnapPoints(SnapPreferences const *snapprefs) const {
GSList const *items = const_cast<Selection *>(this)->itemList();
- std::vector<NR::Point> p;
- for (GSList const *iter = items; iter != NULL; iter = iter->next) {
- sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
- }
- return p;
-}
+ SnapPreferences snapprefs_dummy = *snapprefs; // create a local copy of the snapping prefs
+ snapprefs_dummy.setIncludeItemCenter(false); // locally disable snapping to the item center
-std::vector<NR::Point> Selection::getSnapPointsConvexHull() const {
- GSList const *items = const_cast<Selection *>(this)->itemList();
- std::vector<NR::Point> p;
+ std::vector<Inkscape::SnapCandidatePoint> p;
for (GSList const *iter = items; iter != NULL; iter = iter->next) {
- sp_item_snappoints(SP_ITEM(iter->data), SnapPointsIter(p));
- }
+ SPItem *this_item = SP_ITEM(iter->data);
+ sp_item_snappoints(this_item, p, &snapprefs_dummy);
- std::vector<NR::Point>::iterator i;
- NR::ConvexHull cvh(*(p.begin()));
- for (i = p.begin(); i != p.end(); i++) {
- // these are the points we get back
- cvh.add(*i);
+ //Include the transformation origin for snapping
+ //For a selection or group only the overall origin is considered
+ if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
+ p.push_back(Inkscape::SnapCandidatePoint(this_item->getCenter(), SNAPSOURCE_ROTATION_CENTER));
+ }
}
- NR::Rect rHull = cvh.bounds();
- std::vector<NR::Point> pHull(4);
- pHull[0] = rHull.corner(0);
- pHull[1] = rHull.corner(1);
- pHull[2] = rHull.corner(2);
- pHull[3] = rHull.corner(3);
-
- return pHull;
+ return p;
}
-std::vector<NR::Point> Selection::getBBoxPoints() const {
+std::vector<Inkscape::SnapCandidatePoint> Selection::getSnapPointsConvexHull(SnapPreferences const *snapprefs) const {
GSList const *items = const_cast<Selection *>(this)->itemList();
- std::vector<NR::Point> p;
+
+ std::vector<Inkscape::SnapCandidatePoint> p;
for (GSList const *iter = items; iter != NULL; iter = iter->next) {
- NR::Rect b = sp_item_bbox_desktop(SP_ITEM(iter->data));
- p.push_back(b.min());
- p.push_back(b.max());
+ sp_item_snappoints(SP_ITEM(iter->data), p, snapprefs);
}
- return p;
-}
+ std::vector<Inkscape::SnapCandidatePoint> pHull;
+ if (!p.empty()) {
+ std::vector<Inkscape::SnapCandidatePoint>::iterator i;
+ Geom::RectHull cvh((p.front()).getPoint());
+ for (i = p.begin(); i != p.end(); i++) {
+ // these are the points we get back
+ cvh.add((*i).getPoint());
+ }
-std::vector<NR::Point> Selection::getBBoxPointsOuter() const {
- std::vector<NR::Point> p;
- NR::Rect bbox = bounds();
- p.push_back(bbox.min());
- p.push_back(bbox.max());
- return p;
+ Geom::OptRect rHull = cvh.bounds();
+ if (rHull) {
+ for ( unsigned i = 0 ; i < 4 ; ++i ) {
+ pHull.push_back(Inkscape::SnapCandidatePoint(rHull->corner(i), SNAPSOURCE_CONVEX_HULL_CORNER));
+ }
+ }
+ }
+
+ return pHull;
}
void Selection::_removeObjectDescendants(SPObject *obj) {
}
guint Selection::numberOfLayers() {
- GSList const *items = const_cast<Selection *>(this)->itemList();
- GSList *layers = NULL;
- for (GSList const *iter = items; iter != NULL; iter = iter->next) {
- SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
- if (g_slist_find (layers, layer) == NULL) {
- layers = g_slist_prepend (layers, layer);
- }
- }
- guint ret = g_slist_length (layers);
- g_slist_free (layers);
- return ret;
+ GSList const *items = const_cast<Selection *>(this)->itemList();
+ GSList *layers = NULL;
+ for (GSList const *iter = items; iter != NULL; iter = iter->next) {
+ SPObject *layer = desktop()->layerForObject(SP_OBJECT(iter->data));
+ if (g_slist_find (layers, layer) == NULL) {
+ layers = g_slist_prepend (layers, layer);
+ }
+ }
+ guint ret = g_slist_length (layers);
+ g_slist_free (layers);
+ return ret;
}
guint Selection::numberOfParents() {
- GSList const *items = const_cast<Selection *>(this)->itemList();
- GSList *parents = NULL;
- for (GSList const *iter = items; iter != NULL; iter = iter->next) {
- SPObject *parent = SP_OBJECT_PARENT(iter->data);
- if (g_slist_find (parents, parent) == NULL) {
- parents = g_slist_prepend (parents, parent);
- }
- }
- guint ret = g_slist_length (parents);
- g_slist_free (parents);
- return ret;
+ GSList const *items = const_cast<Selection *>(this)->itemList();
+ GSList *parents = NULL;
+ for (GSList const *iter = items; iter != NULL; iter = iter->next) {
+ SPObject *parent = SP_OBJECT_PARENT(iter->data);
+ if (g_slist_find (parents, parent) == NULL) {
+ parents = g_slist_prepend (parents, parent);
+ }
+ }
+ guint ret = g_slist_length (parents);
+ g_slist_free (parents);
+ return ret;
}
}