Code

add accessor for the waiting_cursor flag (sorry for the recompile)
[inkscape.git] / src / seltrans.cpp
index 6ae7948f1eee14c2d5d7d3d82c8566f5bcc15ff6..e55c25d797a2ce1fc643394a87b06f7a0e21c62a 100644 (file)
@@ -1,9 +1,7 @@
-#define __SELTRANS_C__
-
-/*
- * Helper object for transforming selected items
- *
- * Authors:
+/** @file
+ * @brief Helper object for transforming selected items
+ */
+/* Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
  *   Carl Hetherington <inkscape@carlh.net>
@@ -41,7 +39,7 @@
 #include "verbs.h"
 #include <glibmm/i18n.h>
 #include "display/sp-ctrlline.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "xml/repr.h"
 #include "mod360.h"
 #include <2geom/angle.h>
@@ -101,8 +99,9 @@ Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
     _stamp_cache(NULL),
     _message_context(desktop->messageStack())
 {
-    int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
-    _snap_bbox_type = (prefs_bbox ==0)?
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    int prefs_bbox = prefs->getBool("/tools/bounding_box");
+    _snap_bbox_type = !prefs_bbox ?
         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
 
     g_return_if_fail(desktop != NULL);
@@ -286,16 +285,20 @@ void Inkscape::SelTrans::grab(Geom::Point const &p, gdouble x, gdouble y, bool s
 
     // Next, get all points to consider for snapping
     SnapManager const &m = _desktop->namedview->snap_manager;
+       Inkscape::SnapPreferences local_snapprefs = m.snapprefs;
+       local_snapprefs.setSnapToItemNode(true); // We should get at least the cusp nodes here. This might
+       // have been turned off because (for example) the user only want paths as a snap target, not nodes
+       // but as a snap source we still need some nodes though!
     _snap_points.clear();
-    _snap_points = selection->getSnapPoints(m.getIncludeItemCenter());
-    std::vector<Geom::Point> snap_points_hull = selection->getSnapPointsConvexHull();
-    if (_snap_points.size() > 100) {
-        /* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
-        An average user would rarely ever try to snap such a large number of nodes anyway, because
-        (s)he could hardly discern which node would be snapping */
-        _snap_points = snap_points_hull;
-        // Unfortunately, by now we will have lost the font-baseline snappoints :-(
-    }
+       _snap_points = selection->getSnapPoints(&local_snapprefs);
+       std::vector<Geom::Point> snap_points_hull = selection->getSnapPointsConvexHull(&local_snapprefs);
+       if (_snap_points.size() > 100) {
+               /* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
+               An average user would rarely ever try to snap such a large number of nodes anyway, because
+               (s)he could hardly discern which node would be snapping */
+               _snap_points = snap_points_hull;
+               // Unfortunately, by now we will have lost the font-baseline snappoints :-(
+       }
 
     // Find bbox hulling all special points, which excludes stroke width. Here we need to include the
     // path nodes, for example because a rectangle which has been converted to a path doesn't have
@@ -313,11 +316,10 @@ void Inkscape::SelTrans::grab(Geom::Point const &p, gdouble x, gdouble y, bool s
 
     _bbox_points.clear();
     if (_bbox) {
-        // ... and add the bbox corners to _bbox_points
-        for ( unsigned i = 0 ; i < 4 ; i++ ) {
-            _bbox_points.push_back(_bbox->corner(i));
-        }
-        // There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
+       if (m.snapprefs.getSnapModeBBox()) {
+               getBBoxPoints(_bbox, &_bbox_points, true, m.snapprefs.getSnapBBoxEdgeMidpoints(), m.snapprefs.getSnapBBoxMidpoints());
+       }
+       // There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
         //  - one for snapping the boundingbox, which can be either visual or geometric
         //  - one for snapping the special points
         // The "opposite" in case of a geometric boundingbox always coincides with the "opposite" for the special points
@@ -328,7 +330,47 @@ void Inkscape::SelTrans::grab(Geom::Point const &p, gdouble x, gdouble y, bool s
         _opposite = _opposite_for_bboxpoints;
     }
 
-    // The lines below are usefull for debugging any snapping issues, as they'll spit out all points that are considered for snapping
+    // When snapping the node closest to the mouse pointer is absolutely preferred over the closest snap
+    // (i.e. when weight == 1), then we will not even try to snap to other points and discard those other
+    // points immediately.
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+       if (prefs->getBool("/options/snapclosestonly/value", false)) {
+       if (m.snapprefs.getSnapModeNode()) {
+                       _keepClosestPointOnly(_snap_points, p);
+       } else {
+               _snap_points.clear(); // don't keep any point
+       }
+
+       if (m.snapprefs.getSnapModeBBox()) {
+                       _keepClosestPointOnly(_bbox_points, p);
+               } else {
+                       _bbox_points.clear(); // don't keep any point
+               }
+
+       g_assert(_bbox_points.size() < 2 && _snap_points.size() < 2);
+       if (_snap_points.size() == 1 && _bbox_points.size() == 1) { //both vectors can only have either one or zero elements
+               // So we have exactly one bbox corner and one node left; now find out which is closest and delete the other one
+               if (Geom::L2(_snap_points.at(0) - p) < Geom::L2(_bbox_points.at(0) - p)) {
+                       _bbox_points.clear();
+               } else {
+                       _snap_points.clear();
+               }
+               }
+
+       // Now either _bbox_points or _snap_points has a single element, the other one has zero..... or both have zero elements
+       g_assert((_bbox_points.size() + _snap_points.size()) < 2);
+       if (m.snapprefs.getSnapEnabledGlobally()) {
+                       if (_bbox_points.size() == 1) {
+                               _desktop->snapindicator->set_new_snapsource(_bbox_points.at(0));
+                       } else if (_snap_points.size() == 1){
+                               _desktop->snapindicator->set_new_snapsource(_snap_points.at(0));
+                       }
+       }
+    }
+
+       sp_canvas_set_snap_delay_active(_desktop->canvas, true);
+
+    // The lines below are useful for debugging any snapping issues, as they'll spit out all points that are considered for snapping
 
     /*std::cout << "Number of snap points:  " << _snap_points.size() << std::endl;
     for (std::vector<Geom::Point>::const_iterator i = _snap_points.begin(); i != _snap_points.end(); i++)
@@ -394,6 +436,10 @@ void Inkscape::SelTrans::ungrab()
     _grabbed = false;
     _show_handles = true;
 
+    sp_canvas_set_snap_delay_active(_desktop->canvas, false);
+
+    _desktop->snapindicator->remove_snapsource();
+
     Inkscape::Selection *selection = sp_desktop_selection(_desktop);
     _updateVolatileState();
 
@@ -815,9 +861,10 @@ gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, Geom::Point *position,
 void Inkscape::SelTrans::_selChanged(Inkscape::Selection */*selection*/)
 {
     if (!_grabbed) {
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
         // reread in case it changed on the fly:
-        int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
-         _snap_bbox_type = (prefs_bbox ==0)?
+        int prefs_bbox = prefs->getBool("/tools/bounding_box");
+         _snap_bbox_type = !prefs_bbox ?
             SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
         //SPItem::APPROXIMATE_BBOX will be replaced by SPItem::VISUAL_BBOX, as soon as the latter is implemented properly
 
@@ -932,8 +979,8 @@ gboolean Inkscape::SelTrans::scaleRequest(Geom::Point &pt, guint state)
             }
 
             // Snap along a suitable constraint vector from the origin.
-            bb = m.constrainedSnapScale(Snapper::SNAPPOINT_BBOX, _bbox_points, default_scale, _origin_for_bboxpoints);
-            sn = m.constrainedSnapScale(Snapper::SNAPPOINT_NODE, _snap_points, geom_scale, _origin_for_specpoints);
+            bb = m.constrainedSnapScale(SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, default_scale, _origin_for_bboxpoints);
+            sn = m.constrainedSnapScale(SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, geom_scale, _origin_for_specpoints);
 
             /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
             ** just compare difference in s[X].
@@ -942,8 +989,8 @@ gboolean Inkscape::SelTrans::scaleRequest(Geom::Point &pt, guint state)
             sd = sn.getSnapped() ? fabs(sn.getTransformation()[Geom::X] - geom_scale[Geom::X]) : NR_HUGE;
         } else {
             /* Scale aspect ratio is unlocked */
-            bb = m.freeSnapScale(Snapper::SNAPPOINT_BBOX, _bbox_points, default_scale, _origin_for_bboxpoints);
-            sn = m.freeSnapScale(Snapper::SNAPPOINT_NODE, _snap_points, geom_scale, _origin_for_specpoints);
+            bb = m.freeSnapScale(SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, default_scale, _origin_for_bboxpoints);
+            sn = m.freeSnapScale(SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, geom_scale, _origin_for_specpoints);
 
             /* Pick the snap that puts us closest to the original scale */
             bd = bb.getSnapped() ? fabs(Geom::L2(bb.getTransformation()) - Geom::L2(Geom::Point(default_scale[Geom::X], default_scale[Geom::Y]))) : NR_HUGE;
@@ -953,15 +1000,15 @@ gboolean Inkscape::SelTrans::scaleRequest(Geom::Point &pt, guint state)
         if (!(bb.getSnapped() || sn.getSnapped())) {
             // We didn't snap at all! Don't update the handle position, just calculate the new transformation
             _calcAbsAffineDefault(default_scale);
-            _desktop->snapindicator->remove_snappoint();
+            _desktop->snapindicator->remove_snaptarget();
         } else if (bd < sd) {
             // We snapped the bbox (which is either visual or geometric)
-            _desktop->snapindicator->set_new_snappoint(bb);
+            _desktop->snapindicator->set_new_snaptarget(bb);
             default_scale = Geom::Scale(bb.getTransformation());
             // Calculate the new transformation and update the handle position
             pt = _calcAbsAffineDefault(default_scale);
         } else {
-            _desktop->snapindicator->set_new_snappoint(sn);
+            _desktop->snapindicator->set_new_snaptarget(sn);
             // We snapped the special points (e.g. nodes), which are not at the visual bbox
             // The handle location however (pt) might however be at the visual bbox, so we
             // will have to calculate pt taking the stroke width into account
@@ -1031,8 +1078,8 @@ gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, Geom
 
         bool symmetrical = state & GDK_CONTROL_MASK;
 
-        bb = m.constrainedSnapStretch(Snapper::SNAPPOINT_BBOX, _bbox_points, Geom::Coord(default_scale[axis]), _origin_for_bboxpoints, Geom::Dim2(axis), symmetrical);
-        sn = m.constrainedSnapStretch(Snapper::SNAPPOINT_NODE, _snap_points, Geom::Coord(geom_scale[axis]), _origin_for_specpoints, Geom::Dim2(axis), symmetrical);
+        bb = m.constrainedSnapStretch(SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, Geom::Coord(default_scale[axis]), _origin_for_bboxpoints, Geom::Dim2(axis), symmetrical);
+        sn = m.constrainedSnapStretch(SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, Geom::Coord(geom_scale[axis]), _origin_for_specpoints, Geom::Dim2(axis), symmetrical);
 
         if (bb.getSnapped()) {
             // We snapped the bbox (which is either visual or geometric)
@@ -1055,13 +1102,13 @@ gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, Geom
         if (!(bb.getSnapped() || sn.getSnapped())) {
             // We didn't snap at all! Don't update the handle position, just calculate the new transformation
             _calcAbsAffineDefault(default_scale);
-            _desktop->snapindicator->remove_snappoint();
+            _desktop->snapindicator->remove_snaptarget();
         } else if (bd < sd) {
-            _desktop->snapindicator->set_new_snappoint(bb);
+            _desktop->snapindicator->set_new_snaptarget(bb);
             // Calculate the new transformation and update the handle position
             pt = _calcAbsAffineDefault(default_scale);
         } else {
-            _desktop->snapindicator->set_new_snappoint(sn);
+            _desktop->snapindicator->set_new_snaptarget(sn);
             // We snapped the special points (e.g. nodes), which are not at the visual bbox
             // The handle location however (pt) might however be at the visual bbox, so we
             // will have to calculate pt taking the stroke width into account
@@ -1129,8 +1176,9 @@ gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, Geom::P
     double radians = atan(skew[dim_a] / scale[dim_a]);
 
     if (state & GDK_CONTROL_MASK) {
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
         // Snap to defined angle increments
-        int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
+        int snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
         if (snaps) {
             double sections = floor(radians * snaps / M_PI + .5);
             if (fabs(sections) >= snaps / 2) {
@@ -1148,15 +1196,15 @@ gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, Geom::P
         Inkscape::Snapper::ConstraintLine const constraint(component_vectors[dim_b]);
         // When skewing, we cannot snap the corners of the bounding box, see the comment in "constrainedSnapSkew" for details
         Geom::Point const s(skew[dim_a], scale[dim_a]);
-        Inkscape::SnappedPoint sn = m.constrainedSnapSkew(Inkscape::Snapper::SNAPPOINT_NODE, _snap_points, constraint, s, _origin, Geom::Dim2(dim_b));
+        Inkscape::SnappedPoint sn = m.constrainedSnapSkew(Inkscape::SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, constraint, s, _origin, Geom::Dim2(dim_b));
 
         if (sn.getSnapped()) {
             // We snapped something, so change the skew to reflect it
             Geom::Coord const sd = sn.getSnapped() ? sn.getTransformation()[0] : NR_HUGE;
-             _desktop->snapindicator->set_new_snappoint(sn);
+             _desktop->snapindicator->set_new_snaptarget(sn);
             skew[dim_a] = sd;
         } else {
-            _desktop->snapindicator->remove_snappoint();
+            _desktop->snapindicator->remove_snaptarget();
         }
     }
 
@@ -1198,7 +1246,8 @@ gboolean Inkscape::SelTrans::rotateRequest(Geom::Point &pt, guint state)
      *    the handle; otherwise it will be relative to the center as set for the selection
      */
 
-    int snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    int snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
 
     // rotate affine in rotate
     Geom::Point const d1 = _point - _origin;
@@ -1250,7 +1299,7 @@ gboolean Inkscape::SelTrans::centerRequest(Geom::Point &pt, guint state)
 {
     SnapManager &m = _desktop->namedview->snap_manager;
     m.setup(_desktop);
-    m.freeSnapReturnByRef(Snapper::SNAPPOINT_NODE, pt);
+    m.freeSnapReturnByRef(SnapPreferences::SNAPPOINT_NODE, pt);
 
     if (state & GDK_CONTROL_MASK) {
         if ( fabs(_point[Geom::X] - pt[Geom::X]) > fabs(_point[Geom::Y] - pt[Geom::Y]) ) {
@@ -1358,7 +1407,7 @@ void Inkscape::SelTrans::moveTo(Geom::Point const &xy, guint state)
         ** FIXME: this will snap to more than just the grid, nowadays.
         */
 
-        m.freeSnapReturnByRef(Snapper::SNAPPOINT_NODE, dxy);
+        m.freeSnapReturnByRef(SnapPreferences::SNAPPOINT_NODE, dxy);
 
     } else if (!shift) {
 
@@ -1380,13 +1429,15 @@ void Inkscape::SelTrans::moveTo(Geom::Point const &xy, guint state)
                 // the constraint-line once. The constraint lines are parallel, but might not be colinear.
                 // Therefore we will have to set the point through which the constraint-line runs
                 // individually for each point to be snapped; this will be handled however by _snapTransformed()
-                s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAPPOINT_BBOX,
+                s.push_back(m.constrainedSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_BBOX,
                                                          _bbox_points,
+                                                         _point,
                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
                                                          dxy));
 
-                s.push_back(m.constrainedSnapTranslation(Inkscape::Snapper::SNAPPOINT_NODE,
+                s.push_back(m.constrainedSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_NODE,
                                                          _snap_points,
+                                                         _point,
                                                          Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
                                                          dxy));
             }
@@ -1399,8 +1450,8 @@ void Inkscape::SelTrans::moveTo(Geom::Point const &xy, guint state)
                g_get_current_time(&starttime); */
 
             /* Snap to things with no constraint */
-                       s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAPPOINT_BBOX, _bbox_points, dxy));
-               s.push_back(m.freeSnapTranslation(Inkscape::Snapper::SNAPPOINT_NODE, _snap_points, dxy));
+                       s.push_back(m.freeSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, dxy));
+               s.push_back(m.freeSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, dxy));
 
                /*g_get_current_time(&endtime);
                double elapsed = ((((double)endtime.tv_sec - starttime.tv_sec) * G_USEC_PER_SEC + (endtime.tv_usec - starttime.tv_usec))) / 1000.0;
@@ -1409,20 +1460,19 @@ void Inkscape::SelTrans::moveTo(Geom::Point const &xy, guint state)
 
         /* Pick one */
         Inkscape::SnappedPoint best_snapped_point;
-        g_assert(best_snapped_point.getDistance() == NR_HUGE);
         for (std::list<Inkscape::SnappedPoint>::const_iterator i = s.begin(); i != s.end(); i++) {
             if (i->getSnapped()) {
-                if (i->getDistance() < best_snapped_point.getDistance()) {
+                if (best_snapped_point.isOtherSnapBetter(*i, true)) {
                     best_snapped_point = *i;
                     dxy = i->getTransformation();
                 }
             }
         }
         if (best_snapped_point.getSnapped()) {
-            _desktop->snapindicator->set_new_snappoint(best_snapped_point);
+            _desktop->snapindicator->set_new_snaptarget(best_snapped_point);
         } else {
-            // We didn't snap, so remove any previous snap indicator 
-            _desktop->snapindicator->remove_snappoint();            
+            // We didn't snap, so remove any previous snap indicator
+            _desktop->snapindicator->remove_snaptarget();
             if (control) {
                 // If we didn't snap, then we should still constrain horizontally or vertically
                 // (When we did snap, then this constraint has already been enforced by
@@ -1435,7 +1485,7 @@ void Inkscape::SelTrans::moveTo(Geom::Point const &xy, guint state)
             }
         }
     }
-    
+
     Geom::Matrix const move((Geom::Translate(dxy)));
     Geom::Point const norm(0, 0);
     transform(move, norm);
@@ -1471,7 +1521,8 @@ Geom::Point Inkscape::SelTrans::_getGeomHandlePos(Geom::Point const &visual_hand
     Geom::Point normalized_handle_pos = (visual_handle_pos - new_bbox.min()) * Geom::Scale(new_bbox.dimensions()).inverse();
 
     // Calculate the absolute affine while taking into account the scaling of the stroke width
-    int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    bool transform_stroke = prefs->getBool("/options/transform/stroke", true);
     Geom::Matrix abs_affine = get_scale_transform_with_stroke (*_bbox, _strokewidth, transform_stroke,
                     new_bbox.min()[Geom::X], new_bbox.min()[Geom::Y], new_bbox.max()[Geom::X], new_bbox.max()[Geom::Y]);
 
@@ -1510,11 +1561,12 @@ Geom::Point Inkscape::SelTrans::_calcAbsAffineDefault(Geom::Scale const default_
     Geom::Point new_bbox_min = _approximate_bbox->min() * abs_affine;
     Geom::Point new_bbox_max = _approximate_bbox->max() * abs_affine;
 
-    int transform_stroke = false;
+    bool transform_stroke = false;
     gdouble strokewidth = 0;
 
     if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
-        transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+        transform_stroke = prefs->getBool("/options/transform/stroke", true);
         strokewidth = _strokewidth;
     }
 
@@ -1531,13 +1583,32 @@ Geom::Point Inkscape::SelTrans::_calcAbsAffineGeom(Geom::Scale const geom_scale)
     _relative_affine = Geom::Matrix(geom_scale);
     _absolute_affine = Geom::Translate(-_origin_for_specpoints) * _relative_affine * Geom::Translate(_origin_for_specpoints);
 
-    bool const transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    bool const transform_stroke = prefs->getBool("/options/transform/stroke", true);
     Geom::Rect visual_bbox = get_visual_bbox(_geometric_bbox, _absolute_affine, _strokewidth, transform_stroke);
 
     // return the new handle position
     return visual_bbox.min() + visual_bbox.dimensions() * Geom::Scale(_handle_x, _handle_y);
 }
 
+void Inkscape::SelTrans::_keepClosestPointOnly(std::vector<Geom::Point> &points, const Geom::Point &reference)
+{
+       if (points.size() < 2) return;
+
+       Geom::Point closest_point = Geom::Point(NR_HUGE, NR_HUGE);
+       Geom::Coord closest_dist = NR_HUGE;
+
+       for(std::vector<Geom::Point>::const_iterator i = points.begin(); i != points.end(); i++) {
+               Geom::Coord dist = Geom::L2(*i - reference);
+               if (i == points.begin() || dist < closest_dist) {
+                       closest_point = *i;
+                       closest_dist = dist;
+               }
+    }
+
+       points.clear();
+       points.push_back(closest_point);
+}
 
 /*
   Local Variables:
@@ -1548,4 +1619,4 @@ Geom::Point Inkscape::SelTrans::_calcAbsAffineGeom(Geom::Scale const geom_scale)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :