Code

Respect "relink duplicates clones" setting with linked offsets.
[inkscape.git] / src / object-snapper.cpp
index f9e21174acb3694eb4fb80f9844d4e363936a0fa..51b4944980cff4a0ddc24b5f25d7e1e6d6b6825d 100644 (file)
@@ -229,10 +229,17 @@ void Inkscape::ObjectSnapper::_collectNodes(Inkscape::SnapSourceType const &t,
                     _snapmanager->snapprefs.setSnapIntersectionCS(false);
                 }
 
+                // We should not snap a transformation center to any of the centers of the items in the
+                // current selection (see the comment in SelTrans::centerRequest())
                 bool old_pref2 = _snapmanager->snapprefs.getIncludeItemCenter();
-                if ((*i).item == _snapmanager->getRotationCenterSource()) {
-                    // don't snap to this item's rotation center
-                    _snapmanager->snapprefs.setIncludeItemCenter(false);
+                if (old_pref2) {
+                    for ( GSList const *itemlist = _snapmanager->getRotationCenterSource(); itemlist != NULL; itemlist = g_slist_next(itemlist) ) {
+                        if ((*i).item == reinterpret_cast<SPItem*>(itemlist->data)) {
+                            // don't snap to this item's rotation center
+                            _snapmanager->snapprefs.setIncludeItemCenter(false);
+                            break;
+                        }
+                    }
                 }
 
                 sp_item_snappoints(root_item, *_points_to_snap_to, &_snapmanager->snapprefs);
@@ -263,7 +270,7 @@ void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
 {
     // Iterate through all nodes, find out which one is the closest to p, and snap to it!
 
-    _collectNodes(p.getSourceType(), p.getSourceNum() == 0);
+    _collectNodes(p.getSourceType(), p.getSourceNum() <= 0);
 
     if (unselected_nodes != NULL && unselected_nodes->size() > 0) {
         g_assert(_points_to_snap_to != NULL);
@@ -447,7 +454,7 @@ void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
                                      std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
                                      SPPath const *selected_path) const
 {
-    _collectPaths(p.getPoint(), p.getSourceType(), p.getSourceNum() == 0);
+    _collectPaths(p.getPoint(), p.getSourceType(), p.getSourceNum() <= 0);
     // Now we can finally do the real snapping, using the paths collected above
 
     g_assert(_snapmanager->getDesktop() != NULL);
@@ -455,7 +462,7 @@ void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
 
     bool const node_tool_active = _snapmanager->snapprefs.getSnapToItemPath() && selected_path != NULL;
 
-    if (p.getSourceNum() == 0) {
+    if (p.getSourceNum() <= 0) {
         /* findCandidates() is used for snapping to both paths and nodes. It ignores the path that is
          * currently being edited, because that path requires special care: when snapping to nodes
          * only the unselected nodes of that path should be considered, and these will be passed on separately.
@@ -552,7 +559,7 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
                                      Geom::Point const &p_proj_on_constraint) const
 {
 
-    _collectPaths(p_proj_on_constraint, p.getSourceType(), p.getSourceNum() == 0);
+    _collectPaths(p_proj_on_constraint, p.getSourceType(), p.getSourceNum() <= 0);
 
     // Now we can finally do the real snapping, using the paths collected above
 
@@ -583,29 +590,57 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
         constraint_line.appendNew<Geom::LineSegment>(p_max_on_cl);
         constraint_path.push_back(constraint_line);
     }
+    // Length of constraint_path will always be one
 
+    // Find all intersections of the constrained path with the snap target candidates
+    std::vector<Geom::Point> intersections;
     for (std::vector<Inkscape::SnapCandidatePath >::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
         if (k->path_vector) {
+            // Do the intersection math
             Geom::CrossingSet cs = Geom::crossings(constraint_path, *(k->path_vector));
+            // Store the results as intersection points
             unsigned int index = 0;
             for (Geom::CrossingSet::const_iterator i = cs.begin(); i != cs.end(); i++) {
                 if (index >= constraint_path.size()) {
                     break;
                 }
+                // Reconstruct and store the points of intersection
                 for (Geom::Crossings::const_iterator m = (*i).begin(); m != (*i).end(); m++) {
-                    //std::cout << "ta = " << (*m).ta << " | tb = " << (*m).tb << std::endl;
-                    // Reconstruct the point of intersection
-                    Geom::Point p_inters = constraint_path[index].pointAt((*m).ta);
-                    // .. and convert it to desktop coordinates
-                    p_inters = _snapmanager->getDesktop()->doc2dt(p_inters);
-                    Geom::Coord dist = Geom::L2(p_proj_on_constraint - p_inters);
-                    SnappedPoint s = SnappedPoint(p_inters, p.getSourceType(), p.getSourceNum(), k->target_type, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true, k->target_bbox);;
-                    if (dist <= tolerance) { // If the intersection is within snapping range, then we might snap to it
-                        sc.points.push_back(s);
-                    }
+                    intersections.push_back(constraint_path[index].pointAt((*m).ta));
                 }
                 index++;
             }
+
+            //Geom::crossings will not consider the closing segment apparently, so we'll handle that separately here
+            //TODO: This should have been fixed in rev. #9859, which makes this workaround obsolete
+            for(Geom::PathVector::iterator it_pv = k->path_vector->begin(); it_pv != k->path_vector->end(); ++it_pv) {
+                if (it_pv->closed()) {
+                    // Get the closing linesegment and convert it to a path
+                    Geom::Path cls;
+                    cls.close(false);
+                    cls.append(it_pv->back_closed());
+                    // Intersect that closing path with the constrained path
+                    Geom::Crossings cs = Geom::crossings(constraint_path.front(), cls);
+                    // Reconstruct and store the points of intersection
+                    index = 0; // assuming the constraint path vector has only one path
+                    for (Geom::Crossings::const_iterator m = cs.begin(); m != cs.end(); m++) {
+                        intersections.push_back(constraint_path[index].pointAt((*m).ta));
+                    }
+                }
+            }
+
+            // Convert the collected points of intersection to snapped points
+            for (std::vector<Geom::Point>::iterator p_inters = intersections.begin(); p_inters != intersections.end(); p_inters++) {
+                // Convert to desktop coordinates
+                (*p_inters) = _snapmanager->getDesktop()->doc2dt(*p_inters);
+                // Construct a snapped point
+                Geom::Coord dist = Geom::L2(p.getPoint() - *p_inters);
+                SnappedPoint s = SnappedPoint(*p_inters, p.getSourceType(), p.getSourceNum(), k->target_type, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), true, k->target_bbox);;
+                // Store the snapped point
+                if (dist <= tolerance) { // If the intersection is within snapping range, then we might snap to it
+                    sc.points.push_back(s);
+                }
+            }
         }
     }
 }
@@ -622,9 +657,9 @@ void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
     }
 
     /* Get a list of all the SPItems that we will try to snap to */
-    if (p.getSourceNum() == 0) {
+    if (p.getSourceNum() <= 0) {
         Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p.getPoint(), p.getPoint());
-        _findCandidates(sp_document_root(_snapmanager->getDocument()), it, p.getSourceNum() == 0, local_bbox_to_snap, false, Geom::identity());
+        _findCandidates(sp_document_root(_snapmanager->getDocument()), it, p.getSourceNum() <= 0, local_bbox_to_snap, false, Geom::identity());
     }
 
     // TODO: Argh, UGLY! Get rid of this here, move this logic to the snap manager
@@ -685,9 +720,9 @@ void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
     Geom::Point pp = c.projection(p.getPoint());
 
     /* Get a list of all the SPItems that we will try to snap to */
-    if (p.getSourceNum() == 0) {
+    if (p.getSourceNum() <= 0) {
         Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(pp, pp);
-        _findCandidates(sp_document_root(_snapmanager->getDocument()), it, p.getSourceNum() == 0, local_bbox_to_snap, false, Geom::identity());
+        _findCandidates(sp_document_root(_snapmanager->getDocument()), it, p.getSourceNum() <= 0, local_bbox_to_snap, false, Geom::identity());
     }
 
     // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
@@ -818,15 +853,15 @@ void Inkscape::getBBoxPoints(Geom::OptRect const bbox,
         // collect the corners of the bounding box
         for ( unsigned k = 0 ; k < 4 ; k++ ) {
             if (includeCorners) {
-                points->push_back(Inkscape::SnapCandidatePoint(bbox->corner(k), Inkscape::SNAPSOURCE_BBOX_CORNER, 0, Inkscape::SNAPTARGET_BBOX_CORNER, *bbox));
+                points->push_back(Inkscape::SnapCandidatePoint(bbox->corner(k), Inkscape::SNAPSOURCE_BBOX_CORNER, -1, Inkscape::SNAPTARGET_BBOX_CORNER, *bbox));
             }
             // optionally, collect the midpoints of the bounding box's edges too
             if (includeLineMidpoints) {
-                points->push_back(Inkscape::SnapCandidatePoint((bbox->corner(k) + bbox->corner((k+1) % 4))/2, Inkscape::SNAPSOURCE_BBOX_EDGE_MIDPOINT, 0, Inkscape::SNAPTARGET_BBOX_EDGE_MIDPOINT, *bbox));
+                points->push_back(Inkscape::SnapCandidatePoint((bbox->corner(k) + bbox->corner((k+1) % 4))/2, Inkscape::SNAPSOURCE_BBOX_EDGE_MIDPOINT, -1, Inkscape::SNAPTARGET_BBOX_EDGE_MIDPOINT, *bbox));
             }
         }
         if (includeObjectMidpoints) {
-            points->push_back(Inkscape::SnapCandidatePoint(bbox->midpoint(), Inkscape::SNAPSOURCE_BBOX_MIDPOINT, 0, Inkscape::SNAPTARGET_BBOX_MIDPOINT, *bbox));
+            points->push_back(Inkscape::SnapCandidatePoint(bbox->midpoint(), Inkscape::SNAPSOURCE_BBOX_MIDPOINT, -1, Inkscape::SNAPTARGET_BBOX_MIDPOINT, *bbox));
         }
     }
 }