Code

OCAL. Fix for Bug #638844 (Errors printed to console if openclipart search fails).
[inkscape.git] / src / snapped-point.cpp
index 352f2dd1e363867cc81d5bd2d6cf0231669f1190..52f0a7839b0468c10035ed913711822124733bdd 100644 (file)
@@ -61,7 +61,7 @@ Inkscape::SnappedPoint::SnappedPoint()
 {
     _point = Geom::Point(0,0);
     _source = SNAPSOURCE_UNDEFINED,
-    _source_num = 0,
+    _source_num = -1,
     _target = SNAPTARGET_UNDEFINED,
     _at_intersection = false;
     _constrained_snap = false;
@@ -81,7 +81,7 @@ Inkscape::SnappedPoint::SnappedPoint(Geom::Point const &p)
 {
     _point = p;
     _source = SNAPSOURCE_UNDEFINED,
-    _source_num = 0,
+    _source_num = -1,
     _target = SNAPTARGET_UNDEFINED,
     _at_intersection = false;
     _fully_constrained = false;
@@ -100,7 +100,7 @@ Inkscape::SnappedPoint::~SnappedPoint()
 {
 }
 
-void Inkscape::SnappedPoint::getPoint(Geom::Point &p) const
+void Inkscape::SnappedPoint::getPointIfSnapped(Geom::Point &p) const
 {
     // When we have snapped
     if (getSnapped()) {
@@ -127,7 +127,7 @@ bool getClosestSP(std::list<Inkscape::SnappedPoint> const &list, Inkscape::Snapp
 bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &other_one, bool weighted) const
 {
 
-    if (!other_one.getSnapped()) {
+    if (getSnapped() && !other_one.getSnapped()) {
         return false;
     }
 
@@ -138,7 +138,6 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
     // there's more than one). It is not useful when trying to find the best snapped target point.
     // (both the snap distance and the pointer distance are measured in document pixels, not in screen pixels)
     if (weighted) {
-
         Geom::Coord const dist_pointer_other = other_one.getPointerDistance();
         Geom::Coord const dist_pointer_this = getPointerDistance();
         // Weight factor: controls which node should be preferred for snapping, which is either
@@ -171,10 +170,10 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
     // When snapping to a constraint line only, which is not really a snap but merely a projection
     // to the constraint line, then give this snap a very low priority. Basically, any other snap will do
     if (other_one.getTarget() == SNAPTARGET_CONSTRAINT) {
-        dist_other += NR_HUGE/2;
+        dist_other += 1e6;
     }
     if (getTarget() == SNAPTARGET_CONSTRAINT) {
-        dist_this += NR_HUGE/2;
+        dist_this += 1e6;
     }
 
     // If it's closer
@@ -194,13 +193,21 @@ bool Inkscape::SnappedPoint::isOtherSnapBetter(Inkscape::SnappedPoint const &oth
     // But don't fall back...
     bool c4n = d && other_one.getAtIntersection() && !getAtIntersection();
 
-    // or, if it's just as close then consider the second distance
+    // or, if it's just as close then consider the second distance ...
     bool c5a = (dist_other == dist_this);
     bool c5b = (other_one.getSecondSnapDistance() < getSecondSnapDistance()) && (getSecondSnapDistance() < NR_HUGE);
+    // ... or prefer free snaps over constrained snaps
+    bool c5c = !other_one.getConstrainedSnap() && getConstrainedSnap();
+
+    bool other_is_better = (c1 || c2 || c3 || c4 || (c5a && (c5b || c5c))) && !c2n && (!c3n || c2) && !c4n;
+
+    /*
+    std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
+    std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << " | c5c = " << c5c << std::endl;
+    std::cout << "Other one provides a better snap: " << other_is_better << std::endl;
+    */
 
-    // std::cout << other_one.getPoint() << " (Other one, dist = " << dist_other << ") vs. " << getPoint() << " (this one, dist = " << dist_this << ") ---> ";
-    // std::cout << "c1 = " << c1 << " | c2 = " << c2 << " | c2n = " << c2n << " | c3 = " << c3 << " | c3n = " << c3n << " | c4 = " << c4 << " | c4n = " << c4n << " | c5a = " << c5a << " | c5b = " << c5b << std::endl;
-    return (c1 || c2 || c3 || c4 || (c5a && c5b)) && !c2n && (!c3n || c2) && !c4n;
+    return other_is_better;
 }
 
 /*