Code

Connector tool: make connectors avoid the convex hull of shapes.
[inkscape.git] / src / object-snapper.cpp
index e2ca05408700299b1520009acb32d864bc7aa009..22d438c1ebeca020be06482e057d9f85b387e248 100644 (file)
  */
 
 #include "svg/svg.h"
-#include "libnr/nr-rect-ops.h"
-#include "libnr/nr-point-fns.h"
 #include <2geom/path-intersection.h>
 #include <2geom/point.h>
 #include <2geom/rect.h>
+#include <2geom/line.h>
 #include "document.h"
 #include "sp-namedview.h"
 #include "sp-image.h"
@@ -111,9 +110,9 @@ void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
 
     for (SPObject* o = sp_object_first_child(parent); o != NULL; o = SP_OBJECT_NEXT(o)) {
         g_assert(_snapmanager->getDesktop() != NULL);
-        if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(_snapmanager->getDesktop()->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) {
-            // Don't snap to locked items, and
-            // don't snap to hidden objects, unless they're a clipped path or a mask
+        if (SP_IS_ITEM(o) && !(_snapmanager->getDesktop()->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) {
+            // Snapping to items in a locked layer is allowed
+            // Don't snap to hidden objects, unless they're a clipped path or a mask
             /* See if this item is on the ignore list */
             std::vector<SPItem const *>::const_iterator i;
             if (it != NULL) {
@@ -161,6 +160,9 @@ void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent,
                         if (bbox_to_snap_incl.intersects(*bbox_of_item)) {
                             // This item is within snapping range, so record it as a candidate
                             _candidates->push_back(SnapCandidate(item, clip_or_mask, additional_affine));
+                            // For debugging: print the id of the candidate to the console
+                            //SPObject *obj = (SPObject*)item;
+                            //std::cout << "Snap candidate added: " << obj->id << std::endl;
                         }
                     }
                 }
@@ -294,25 +296,27 @@ void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &s
     // Iterate through all nodes, find out which one is the closest to this guide, and snap to it!
     _collectNodes(t, true);
 
+    // Although we won't snap to paths here (which would give us under constrained snaps) we can still snap to intersections of paths.
+    if (_snapmanager->snapprefs.getSnapToItemPath() || _snapmanager->snapprefs.getSnapToBBoxPath() || _snapmanager->snapprefs.getSnapToPageBorder()) {
+       _collectPaths(t, true);
+       _snapPaths(sc, t, p, SNAPSOURCE_GUIDE, true, NULL, NULL);
+       // The paths themselves should be discarded in findBestSnap(), as we should only snap to their intersections
+       }
+
     SnappedPoint s;
-    bool success = false;
 
     Geom::Coord tol = getSnapperTolerance();
 
     for (std::vector<std::pair<Geom::Point, int> >::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
         // Project each node (*k) on the guide line (running through point p)
-        Geom::Point p_proj = project_on_linesegment((*k).first, p, p + Geom::rot90(guide_normal));
+        Geom::Point p_proj = Geom::projection((*k).first, Geom::Line(p, p + Geom::rot90(guide_normal)));
         Geom::Coord dist = Geom::L2((*k).first - p_proj); // distance from node to the guide
         Geom::Coord dist2 = Geom::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
-        if ((dist < tol && dist2 < tol) || (getSnapperAlwaysSnap() && dist < s.getSnapDistance())) {
+        if ((dist < tol && dist2 < tol) || getSnapperAlwaysSnap()) {
             s = SnappedPoint((*k).first, SNAPSOURCE_GUIDE, static_cast<Inkscape::SnapTargetType>((*k).second), dist, tol, getSnapperAlwaysSnap(), true);
-            success = true;
+            sc.points.push_back(s);
         }
     }
-
-    if (success) {
-        sc.points.push_back(s);
-    }
 }
 
 
@@ -333,6 +337,7 @@ void Inkscape::ObjectSnapper::_collectPaths(Inkscape::SnapPreferences::PointType
         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
 
         bool p_is_a_node = t & Inkscape::SnapPreferences::SNAPPOINT_NODE;
+        bool p_is_a_guide = t & Inkscape::SnapPreferences::SNAPPOINT_GUIDE;
 
         if (_snapmanager->snapprefs.getSnapToBBoxPath()) {
             Inkscape::Preferences *prefs = Inkscape::Preferences::get();
@@ -368,7 +373,7 @@ void Inkscape::ObjectSnapper::_collectPaths(Inkscape::SnapPreferences::PointType
 
             //Add the item's path to snap to
             if (_snapmanager->snapprefs.getSnapToItemPath()) {
-                if (!(_snapmanager->snapprefs.getStrictSnapping() && !p_is_a_node)) {
+                if (p_is_a_guide || !(_snapmanager->snapprefs.getStrictSnapping() && !p_is_a_node)) {
                     // Snapping to the path of characters is very cool, but for a large
                     // chunk of text this will take ages! So limit snapping to text paths
                     // containing max. 240 characters. Snapping the bbox will not be affected
@@ -402,7 +407,7 @@ void Inkscape::ObjectSnapper::_collectPaths(Inkscape::SnapPreferences::PointType
 
             //Add the item's bounding box to snap to
             if (_snapmanager->snapprefs.getSnapToBBoxPath()) {
-                if (!(_snapmanager->snapprefs.getStrictSnapping() && p_is_a_node)) {
+                if (p_is_a_guide || !(_snapmanager->snapprefs.getStrictSnapping() && p_is_a_node)) {
                     // Discard the bbox of a clipped path / mask, because we don't want to snap to both the bbox
                     // of the item AND the bbox of the clipping path at the same time
                     if (!(*i).clip_or_mask) {
@@ -546,13 +551,10 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
         direction_vector = Geom::unit_vector(direction_vector);
     }
 
-    Geom::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
-    Geom::Point const p2_on_cl = p1_on_cl + direction_vector;
-
     // The intersection point of the constraint line with any path,
     // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
     // The distance between those points is twice the snapping tolerance
-    Geom::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
+    Geom::Point const p_proj_on_cl = p; // projection has already been taken care of in constrainedSnap in the snapmanager;
     Geom::Point const p_min_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);
     Geom::Point const p_max_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
 
@@ -622,9 +624,10 @@ void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
              */
             SPPath *path = NULL;
             if (it != NULL) {
-                g_assert(SP_IS_PATH(*it->begin()));
-                g_assert(it->size() == 1);
-                path = SP_PATH(*it->begin());
+                if (it->size() == 1 && SP_IS_PATH(*it->begin())) {
+                                       path = SP_PATH(*it->begin());
+                } // else: *it->begin() might be a SPGroup, e.g. when editing a LPE of text that has been converted to a group of paths
+                // as reported in bug #356743. In that case we can just ignore it, i.e. not snap to this item
             }
             _snapPaths(sc, t, p, source_type, first_point, unselected_nodes, path);
         } else {
@@ -668,7 +671,7 @@ void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
 
 
 // This method is used to snap a guide to nodes, while dragging the guide around
-void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
+void Inkscape::ObjectSnapper::guideFreeSnap(SnappedConstraints &sc,
                                         Geom::Point const &p,
                                         Geom::Point const &guide_normal) const
 {
@@ -685,19 +688,33 @@ void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
         snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
     }
 
-    // We don't support ANGLED_GUIDE_ROT_SNAP yet.
+    _findCandidates(sp_document_root(_snapmanager->getDocument()), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity());
+    _snapTranslatingGuideToNodes(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, guide_normal);
+
+}
+
+// This method is used to snap the origin of a guide to nodes/paths, while dragging the origin along the guide
+void Inkscape::ObjectSnapper::guideConstrainedSnap(SnappedConstraints &sc,
+                                        Geom::Point const &p,
+                                        Geom::Point const &guide_normal,
+                                        ConstraintLine const &/*c*/) const
+{
+    /* Get a list of all the SPItems that we will try to snap to */
+    std::vector<SPItem*> cand;
+    std::vector<SPItem const *> const it; //just an empty list
 
-    // It would be cool to allow the user to rotate a guide by dragging it, instead of
-    // only translating it. (For example when CTRL is pressed). We will need an UI part
-    // for that first; and some important usability choices need to be made:
-    // E.g. which point should be used for pivoting? A previously snapped point,
-    // or a transformation center (which can be moved after clicking for the
-    // second time on an object; but should this point then be constrained to the
-    // line, or can it be located anywhere?)
+    DimensionToSnap snap_dim;
+    if (guide_normal == to_2geom(component_vectors[Geom::Y])) {
+        snap_dim = GUIDE_TRANSL_SNAP_Y;
+    } else if (guide_normal == to_2geom(component_vectors[Geom::X])) {
+        snap_dim = GUIDE_TRANSL_SNAP_X;
+    } else {
+        snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
+    }
 
     _findCandidates(sp_document_root(_snapmanager->getDocument()), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity());
     _snapTranslatingGuideToNodes(sc, Inkscape::SnapPreferences::SNAPPOINT_GUIDE, p, guide_normal);
-    // _snapRotatingGuideToNodes has not been implemented yet.
+
 }
 
 /**
@@ -717,9 +734,16 @@ bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
     return (_snap_enabled && _snapmanager->snapprefs.getSnapModeBBoxOrNodes() && snap_to_something);
 }
 
-bool Inkscape::ObjectSnapper::GuidesMightSnap() const
+bool Inkscape::ObjectSnapper::GuidesMightSnap() const // almost the same as ThisSnapperMightSnap above, but only looking at points (and not paths)
 {
-    bool snap_to_something = _snapmanager->snapprefs.getSnapToItemNode() || _snapmanager->snapprefs.getSnapToBBoxNode();
+    bool snap_to_something = _snapmanager->snapprefs.getSnapToItemNode()
+                                               || _snapmanager->snapprefs.getSnapToPageBorder()
+                                               || (_snapmanager->snapprefs.getSnapModeBBox() && _snapmanager->snapprefs.getSnapToBBoxNode())
+                                               || (_snapmanager->snapprefs.getSnapModeBBox() && (_snapmanager->snapprefs.getSnapBBoxEdgeMidpoints() || _snapmanager->snapprefs.getSnapBBoxMidpoints()))
+                                               || (_snapmanager->snapprefs.getSnapModeNode() && (_snapmanager->snapprefs.getSnapLineMidpoints() || _snapmanager->snapprefs.getSnapObjectMidpoints()))
+                                               || (_snapmanager->snapprefs.getSnapModeNode() && _snapmanager->snapprefs.getIncludeItemCenter())
+                                               || (_snapmanager->snapprefs.getSnapModeNode() && (_snapmanager->snapprefs.getSnapToItemPath() && _snapmanager->snapprefs.getSnapIntersectionCS()));
+
     return (_snap_enabled && _snapmanager->snapprefs.getSnapModeGuide() && snap_to_something);
 }