Code

Refactoring of 3D box tool, mainly to avoid unnecessary creation of perspectives.
[inkscape.git] / src / selection.cpp
index bd695ff8b3c85f3da3697e9a4726a3ef2825e85d..ed8a9d57b86034dfcaf4c36915bace17a5ff3671 100644 (file)
@@ -224,7 +224,7 @@ void Selection::remove_box_perspective(SPBox3D *box) {
     Persp3D *persp = box3d_get_perspective(box);
     std::map<Persp3D *, unsigned int>::iterator p = _persps.find(persp);
     if (p == _persps.end()) {
-        g_print ("Warning! Trying to remove unselected perspective from selection!\n");
+        //g_print ("Warning! Trying to remove unselected perspective from selection!\n");
         return;
     }
     if ((*p).second > 1) {
@@ -379,15 +379,15 @@ Inkscape::XML::Node *Selection::singleRepr() {
 NRRect *Selection::bounds(NRRect *bbox, SPItem::BBoxType type) const
 {
     g_return_val_if_fail (bbox != NULL, NULL);
-    bounds(bbox, type);
+    *bbox = NRRect(bounds(type));
     return bbox;
 }
 
-boost::optional<Geom::Rect> Selection::bounds(SPItem::BBoxType type) const
+Geom::OptRect Selection::bounds(SPItem::BBoxType type) const
 {
     GSList const *items = const_cast<Selection *>(this)->itemList();
 
-    boost::optional<Geom::Rect> bbox;
+    Geom::OptRect bbox;
     for ( GSList const *i = items ; i != NULL ; i = i->next ) {
         bbox = unify(bbox, sp_item_bbox_desktop(SP_ITEM(i->data), type));
     }
@@ -415,7 +415,7 @@ NRRect *Selection::boundsInDocument(NRRect *bbox, SPItem::BBoxType type) const {
     return bbox;
 }
 
-boost::optional<Geom::Rect> Selection::boundsInDocument(SPItem::BBoxType type) const {
+Geom::OptRect Selection::boundsInDocument(SPItem::BBoxType type) const {
     NRRect r;
     return to_2geom(boundsInDocument(&r, type)->upgrade());
 }
@@ -430,7 +430,7 @@ boost::optional<Geom::Point> Selection::center() const {
             return first->getCenter();
         }
     }
-    boost::optional<Geom::Rect> bbox = bounds();
+    Geom::OptRect bbox = bounds();
     if (bbox) {
         return bounds()->midpoint();
     } else {
@@ -441,43 +441,48 @@ boost::optional<Geom::Point> Selection::center() const {
 /**
  * Compute the list of points in the selection that are to be considered for snapping.
  */
-std::vector<Geom::Point> Selection::getSnapPoints(bool includeItemCenter) const {
+std::vector<std::pair<Geom::Point, int> > Selection::getSnapPoints(SnapPreferences const *snapprefs) const {
     GSList const *items = const_cast<Selection *>(this)->itemList();
-    std::vector<Geom::Point> 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<std::pair<Geom::Point, int> > p;
     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
         SPItem *this_item = SP_ITEM(iter->data);
-        sp_item_snappoints(this_item, false, SnapPointsIter(p));
+        sp_item_snappoints(this_item, false, p, &snapprefs_dummy);
+
         //Include the transformation origin for snapping
-        //For a group only the group's origin is considered
-        if (includeItemCenter) {
-               p.push_back(this_item->getCenter());
-        }  
+        //For a selection or group only the overall origin is considered
+        if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
+               p.push_back(std::make_pair(this_item->getCenter(), SNAPSOURCE_ROTATION_CENTER));
+        }
     }
 
     return p;
 }
 
-std::vector<Geom::Point> Selection::getSnapPointsConvexHull() const {
+std::vector<std::pair<Geom::Point, int> > Selection::getSnapPointsConvexHull(SnapPreferences const *snapprefs) const {
     GSList const *items = const_cast<Selection *>(this)->itemList();
 
-    std::vector<Geom::Point> p;
+    std::vector<std::pair<Geom::Point, int> > p;
     for (GSList const *iter = items; iter != NULL; iter = iter->next) {
-               sp_item_snappoints(SP_ITEM(iter->data), false, SnapPointsIter(p));
+               sp_item_snappoints(SP_ITEM(iter->data), false, p, snapprefs);
     }
 
-    std::vector<Geom::Point> pHull;
+    std::vector<std::pair<Geom::Point, int> > pHull;
     if (!p.empty()) {
-        std::vector<Geom::Point>::iterator i;
-        Geom::RectHull cvh(p.front());
+       std::vector<std::pair<Geom::Point, int> >::iterator i;
+        Geom::RectHull cvh((p.front()).first);
         for (i = p.begin(); i != p.end(); i++) {
             // these are the points we get back
-            cvh.add(*i);
+            cvh.add((*i).first);
         }
 
-        boost::optional<Geom::Rect> rHull = cvh.bounds();
+        Geom::OptRect rHull = cvh.bounds();
         if (rHull) {
             for ( unsigned i = 0 ; i < 4 ; ++i ) {
-                pHull.push_back(rHull->corner(i));
+                pHull.push_back(std::make_pair(rHull->corner(i), SNAPSOURCE_CONVEX_HULL_CORNER));
             }
         }
     }
@@ -521,31 +526,31 @@ SPObject *Selection::_objectForXMLNode(Inkscape::XML::Node *repr) const {
 }
 
 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;
 }
 
 }