Code

Fix bbox snapping as reported in LP bug #562205
[inkscape.git] / src / snap.cpp
1 #define __SP_DESKTOP_SNAP_C__
3 /**
4  * \file snap.cpp
5  * \brief SnapManager class.
6  *
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   Frank Felfe <innerspace@iname.com>
10  *   Nathan Hurst <njh@njhurst.com>
11  *   Carl Hetherington <inkscape@carlh.net>
12  *   Diederik van Lierop <mail@diedenrezi.nl>
13  *
14  * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
15  * Copyrigth (C) 2004      Nathan Hurst
16  * Copyright (C) 1999-2010 Authors
17  *
18  * Released under GNU GPL, read the file 'COPYING' for more information
19  */
21 #include <utility>
23 #include "sp-namedview.h"
24 #include "snap.h"
25 #include "snapped-line.h"
26 #include "snapped-curve.h"
28 #include "display/canvas-grid.h"
29 #include "display/snap-indicator.h"
31 #include "inkscape.h"
32 #include "desktop.h"
33 #include "selection.h"
34 #include "sp-guide.h"
35 #include "preferences.h"
36 #include "event-context.h"
37 using std::vector;
39 /**
40  *  Construct a SnapManager for a SPNamedView.
41  *
42  *  \param v `Owning' SPNamedView.
43  */
45 SnapManager::SnapManager(SPNamedView const *v) :
46     guide(this, 0),
47     object(this, 0),
48     snapprefs(),
49     _named_view(v)
50 {
51 }
53 /**
54  *  \brief Return a list of snappers
55  *
56  *  Inkscape snaps to objects, grids, and guides. For each of these snap targets a
57  *  separate class is used, which has been derived from the base Snapper class. The
58  *  getSnappers() method returns a list of pointers to instances of this class. This
59  *  list contains exactly one instance of the guide snapper and of the object snapper
60  *  class, but any number of grid snappers (because each grid has its own snapper
61  *  instance)
62  *
63  *  \return List of snappers that we use.
64  */
65 SnapManager::SnapperList
66 SnapManager::getSnappers() const
67 {
68     SnapManager::SnapperList s;
69     s.push_back(&guide);
70     s.push_back(&object);
72     SnapManager::SnapperList gs = getGridSnappers();
73     s.splice(s.begin(), gs);
75     return s;
76 }
78 /**
79  *  \brief Return a list of gridsnappers
80  *
81  *  Each grid has its own instance of the snapper class. This way snapping can
82  *  be enabled per grid individually. A list will be returned containing the
83  *  pointers to these instances, but only for grids that are being displayed
84  *  and for which snapping is enabled.
85  *
86  *  \return List of gridsnappers that we use.
87  */
88 SnapManager::SnapperList
89 SnapManager::getGridSnappers() const
90 {
91     SnapperList s;
93     if (_desktop && _desktop->gridsEnabled() && snapprefs.getSnapToGrids()) {
94         for ( GSList const *l = _named_view->grids; l != NULL; l = l->next) {
95             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
96             s.push_back(grid->snapper);
97         }
98     }
100     return s;
103 /**
104  * \brief Return true if any snapping might occur, whether its to grids, guides or objects
105  *
106  * Each snapper instance handles its own snapping target, e.g. grids, guides or
107  * objects. This method iterates through all these snapper instances and returns
108  * true if any of the snappers might possible snap, considering only the relevant
109  * snapping preferences.
110  *
111  * \return true if one of the snappers will try to snap to something.
112  */
114 bool SnapManager::someSnapperMightSnap() const
116     if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
117         return false;
118     }
120     SnapperList const s = getSnappers();
121     SnapperList::const_iterator i = s.begin();
122     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
123         i++;
124     }
126     return (i != s.end());
129 /**
130  * \return true if one of the grids might be snapped to.
131  */
133 bool SnapManager::gridSnapperMightSnap() const
135     if ( !snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally() ) {
136         return false;
137     }
139     SnapperList const s = getGridSnappers();
140     SnapperList::const_iterator i = s.begin();
141     while (i != s.end() && (*i)->ThisSnapperMightSnap() == false) {
142         i++;
143     }
145     return (i != s.end());
148 /**
149  *  \brief Try to snap a point to grids, guides or objects.
150  *
151  *  Try to snap a point to grids, guides or objects, in two degrees-of-freedom,
152  *  i.e. snap in any direction on the two dimensional canvas to the nearest
153  *  snap target. freeSnapReturnByRef() is equal in snapping behavior to
154  *  freeSnap(), but the former returns the snapped point trough the referenced
155  *  parameter p. This parameter p initially contains the position of the snap
156  *  source and will we overwritten by the target position if snapping has occurred.
157  *  This makes snapping transparent to the calling code. If this is not desired
158  *  because either the calling code must know whether snapping has occurred, or
159  *  because the original position should not be touched, then freeSnap() should be
160  *  called instead.
161  *
162  *  PS:
163  *  1) SnapManager::setup() must have been called before calling this method,
164  *  but only once for a set of points
165  *  2) Only to be used when a single source point is to be snapped; it assumes
166  *  that source_num = 0, which is inefficient when snapping sets our source points
167  *
168  *  \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred
169  *  \param source_type Detailed description of the source type, will be used by the snap indicator
170  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
171  */
173 void SnapManager::freeSnapReturnByRef(Geom::Point &p,
174                                       Inkscape::SnapSourceType const source_type,
175                                       Geom::OptRect const &bbox_to_snap) const
177     //TODO: SnapCandidatePoint and point_type are somewhat redundant; can't we get rid of the point_type parameter?
178     Inkscape::SnappedPoint const s = freeSnap(Inkscape::SnapCandidatePoint(p, source_type), bbox_to_snap);
179     s.getPoint(p);
183 /**
184  *  \brief Try to snap a point to grids, guides or objects.
185  *
186  *  Try to snap a point to grids, guides or objects, in two degrees-of-freedom,
187  *  i.e. snap in any direction on the two dimensional canvas to the nearest
188  *  snap target. freeSnap() is equal in snapping behavior to
189  *  freeSnapReturnByRef(). Please read the comments of the latter for more details
190  *
191  *  PS: SnapManager::setup() must have been called before calling this method,
192  *  but only once for a set of points
193  *
194  *  \param p Source point to be snapped
195  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
196  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
197  */
200 Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::SnapCandidatePoint const &p,
201                                              Geom::OptRect const &bbox_to_snap) const
203     if (!someSnapperMightSnap()) {
204         return Inkscape::SnappedPoint(p, Inkscape::SNAPTARGET_UNDEFINED, NR_HUGE, 0, false, false);
205     }
207     SnappedConstraints sc;
208     SnapperList const snappers = getSnappers();
210     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
211         (*i)->freeSnap(sc, p, bbox_to_snap, &_items_to_ignore, _unselected_nodes);
212     }
214     return findBestSnap(p, sc, false);
217 void SnapManager::preSnap(Inkscape::SnapCandidatePoint const &p)
219     // setup() must have been called before calling this method!
221     if (_snapindicator) {
222         _snapindicator = false; // prevent other methods from drawing a snap indicator; we want to control this here
223         Inkscape::SnappedPoint s = freeSnap(p);
224         if (s.getSnapped()) {
225             _desktop->snapindicator->set_new_snaptarget(s, true);
226         } else {
227             _desktop->snapindicator->remove_snaptarget(true);
228         }
229         _snapindicator = true; // restore the original value
230     }
233 /**
234  * \brief Snap to the closest multiple of a grid pitch
235  *
236  * When pasting, we would like to snap to the grid. Problem is that we don't know which
237  * nodes were aligned to the grid at the time of copying, so we don't know which nodes
238  * to snap. If we'd snap an unaligned node to the grid, previously aligned nodes would
239  * become unaligned. That's undesirable. Instead we will make sure that the offset
240  * between the source and its pasted copy is a multiple of the grid pitch. If the source
241  * was aligned, then the copy will therefore also be aligned.
242  *
243  * PS: Whether we really find a multiple also depends on the snapping range! Most users
244  * will have "always snap" enabled though, in which case a multiple will always be found.
245  * PS2: When multiple grids are present then the result will become ambiguous. There is no
246  * way to control to which grid this method will snap.
247  *
248  * \param t Vector that represents the offset of the pasted copy with respect to the original
249  * \return Offset vector after snapping to the closest multiple of a grid pitch
250  */
252 Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t, Geom::Point const &origin)
254     if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally())
255         return t;
257     if (_desktop && _desktop->gridsEnabled()) {
258         bool success = false;
259         Geom::Point nearest_multiple;
260         Geom::Coord nearest_distance = NR_HUGE;
261         Inkscape::SnappedPoint bestSnappedPoint(t);
263         // It will snap to the grid for which we find the closest snap. This might be a different
264         // grid than to which the objects were initially aligned. I don't see an easy way to fix
265         // this, so when using multiple grids one can get unexpected results
267         // Cannot use getGridSnappers() because we need both the grids AND their snappers
268         // Therefore we iterate through all grids manually
269         for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
270             Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
271             const Inkscape::Snapper* snapper = grid->snapper;
272             if (snapper && snapper->ThisSnapperMightSnap()) {
273                 // To find the nearest multiple of the grid pitch for a given translation t, we
274                 // will use the grid snapper. Simply snapping the value t to the grid will do, but
275                 // only if the origin of the grid is at (0,0). If it's not then compensate for this
276                 // in the translation t
277                 Geom::Point const t_offset = t + grid->origin;
278                 SnappedConstraints sc;
279                 // Only the first three parameters are being used for grid snappers
280                 snapper->freeSnap(sc, Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_GRID_PITCH),Geom::OptRect(), NULL, NULL);
281                 // Find the best snap for this grid, including intersections of the grid-lines
282                 bool old_val = _snapindicator;
283                 _snapindicator = false;
284                 Inkscape::SnappedPoint s = findBestSnap(Inkscape::SnapCandidatePoint(t_offset, Inkscape::SNAPSOURCE_GRID_PITCH), sc, false, false, true);
285                 _snapindicator = old_val;
286                 if (s.getSnapped() && (s.getSnapDistance() < nearest_distance)) {
287                     // use getSnapDistance() instead of getWeightedDistance() here because the pointer's position
288                     // doesn't tell us anything about which node to snap
289                     success = true;
290                     nearest_multiple = s.getPoint() - to_2geom(grid->origin);
291                     nearest_distance = s.getSnapDistance();
292                     bestSnappedPoint = s;
293                 }
294             }
295         }
297         if (success) {
298             bestSnappedPoint.setPoint(origin + nearest_multiple);
299             _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
300             return nearest_multiple;
301         }
302     }
304     return t;
307 /**
308  *  \brief Try to snap a point along a constraint line to grids, guides or objects.
309  *
310  *  Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
311  *  i.e. snap in a specific direction on the two dimensional canvas to the nearest
312  *  snap target.
313  *
314  *  constrainedSnapReturnByRef() is equal in snapping behavior to
315  *  constrainedSnap(), but the former returns the snapped point trough the referenced
316  *  parameter p. This parameter p initially contains the position of the snap
317  *  source and will we overwritten by the target position if snapping has occurred.
318  *  This makes snapping transparent to the calling code. If this is not desired
319  *  because either the calling code must know whether snapping has occurred, or
320  *  because the original position should not be touched, then constrainedSnap() should
321  *  be called instead.
322  *
323  *  PS:
324  *  1) SnapManager::setup() must have been called before calling this method,
325  *  but only once for a set of points
326  *  2) Only to be used when a single source point is to be snapped; it assumes
327  *  that source_num = 0, which is inefficient when snapping sets our source points
329  *
330  *  \param p Current position of the snap source; will be overwritten by the position of the snap target if snapping has occurred
331  *  \param source_type Detailed description of the source type, will be used by the snap indicator
332  *  \param constraint The direction or line along which snapping must occur
333  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
334  */
336 void SnapManager::constrainedSnapReturnByRef(Geom::Point &p,
337                                              Inkscape::SnapSourceType const source_type,
338                                              Inkscape::Snapper::ConstraintLine const &constraint,
339                                              Geom::OptRect const &bbox_to_snap) const
341     Inkscape::SnappedPoint const s = constrainedSnap(Inkscape::SnapCandidatePoint(p, source_type, 0), constraint, bbox_to_snap);
342     s.getPoint(p);
345 /**
346  *  \brief Try to snap a point along a constraint line to grids, guides or objects.
347  *
348  *  Try to snap a point to grids, guides or objects, in only one degree-of-freedom,
349  *  i.e. snap in a specific direction on the two dimensional canvas to the nearest
350  *  snap target. constrainedSnap is equal in snapping behavior to
351  *  constrainedSnapReturnByRef(). Please read the comments of the latter for more details.
352  *
353  *  PS: SnapManager::setup() must have been called before calling this method,
354  *  but only once for a set of points
355  *
356  *  \param p Source point to be snapped
357  *  \param constraint The direction or line along which snapping must occur
358  *  \param bbox_to_snap Bounding box hulling the set of points, all from the same selection and having the same transformation
359  */
361 Inkscape::SnappedPoint SnapManager::constrainedSnap(Inkscape::SnapCandidatePoint const &p,
362                                                     Inkscape::Snapper::ConstraintLine const &constraint,
363                                                     Geom::OptRect const &bbox_to_snap) const
365     // First project the mouse pointer onto the constraint
366     Geom::Point pp = constraint.projection(p.getPoint());
368     Inkscape::SnappedPoint no_snap = Inkscape::SnappedPoint(pp, p.getSourceType(), p.getSourceNum(), Inkscape::SNAPTARGET_CONSTRAINT, Geom::L2(pp - p.getPoint()), 0, false, false);
370     if (!someSnapperMightSnap()) {
371         // Always return point on constraint
372         return no_snap;
373     }
375     SnappedConstraints sc;
376     SnapperList const snappers = getSnappers();
377     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
378         (*i)->constrainedSnap(sc, p, bbox_to_snap, constraint, &_items_to_ignore);
379     }
381     Inkscape::SnappedPoint result = findBestSnap(p, sc, true);
383     if (result.getSnapped()) {
384         // only change the snap indicator if we really snapped to something
385         if (_snapindicator) {
386             _desktop->snapindicator->set_new_snaptarget(result);
387         }
388         return result;
389     }
390     return no_snap;
393 /**
394  *  \brief Try to snap a point of a guide to another guide or to a node
395  *
396  *  Try to snap a point of a guide to another guide or to a node in two degrees-
397  *  of-freedom, i.e. snap in any direction on the two dimensional canvas to the
398  *  nearest snap target. This method is used when dragging or rotating a guide
399  *
400  *  PS: SnapManager::setup() must have been called before calling this method,
401  *
402  *  \param p Current position of the point on the guide that is to be snapped; will be overwritten by the position of the snap target if snapping has occurred
403  *  \param guide_normal Vector normal to the guide line
404  */
405 void SnapManager::guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, SPGuideDragType drag_type) const
407     if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
408         return;
409     }
411     if (!(object.GuidesMightSnap() || snapprefs.getSnapToGuides())) {
412         return;
413     }
415     Inkscape::SnapCandidatePoint candidate(p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN);
416     if (drag_type == SP_DRAG_ROTATE) {
417         candidate = Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_GUIDE);
418     }
420     // Snap to nodes
421     SnappedConstraints sc;
422     if (object.GuidesMightSnap()) {
423         object.guideFreeSnap(sc, p, guide_normal);
424     }
426     // Snap to guides & grid lines
427     SnapperList snappers = getGridSnappers();
428     snappers.push_back(&guide);
429     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
430         (*i)->freeSnap(sc, candidate, Geom::OptRect(), NULL, NULL);
431     }
433     // Snap to intersections of curves, but not to the curves themselves! (see _snapTranslatingGuideToNodes in object-snapper.cpp)
434     Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false, true);
436     s.getPoint(p);
439 /**
440  *  \brief Try to snap a point on a guide to the intersection with another guide or a path
441  *
442  *  Try to snap a point on a guide to the intersection of that guide with another
443  *  guide or with a path. The snapped point will lie somewhere on the guide-line,
444  *  making this is a constrained snap, i.e. in only one degree-of-freedom.
445  *  This method is used when dragging the origin of the guide along the guide itself.
446  *
447  *  PS: SnapManager::setup() must have been called before calling this method,
448  *
449  *  \param p Current position of the point on the guide that is to be snapped; will be overwritten by the position of the snap target if snapping has occurred
450  *  \param guide_normal Vector normal to the guide line
451  */
453 void SnapManager::guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const
455     if (!snapprefs.getSnapEnabledGlobally() || snapprefs.getSnapPostponedGlobally()) {
456         return;
457     }
459     if (!(object.ThisSnapperMightSnap() || snapprefs.getSnapToGuides())) {
460         return;
461     }
463     Inkscape::SnapCandidatePoint candidate(p, Inkscape::SNAPSOURCE_GUIDE_ORIGIN, Inkscape::SNAPTARGET_UNDEFINED);
465     // Snap to nodes or paths
466     SnappedConstraints sc;
467     Inkscape::Snapper::ConstraintLine cl(guideline.point_on_line, Geom::rot90(guideline.normal_to_line));
468     if (object.ThisSnapperMightSnap()) {
469         object.constrainedSnap(sc, candidate, Geom::OptRect(), cl, NULL);
470     }
472     // Snap to guides & grid lines
473     SnapperList snappers = getGridSnappers();
474     snappers.push_back(&guide);
475     for (SnapperList::const_iterator i = snappers.begin(); i != snappers.end(); i++) {
476         (*i)->constrainedSnap(sc, candidate, Geom::OptRect(), cl, NULL);
477     }
479     Inkscape::SnappedPoint const s = findBestSnap(candidate, sc, false);
480     s.getPoint(p);
483 /**
484  *  \brief Method for snapping sets of points while they are being transformed
485  *
486  *  Method for snapping sets of points while they are being transformed, when using
487  *  for example the selector tool. This method is for internal use only, and should
488  *  not have to be called directly. Use freeSnapTransalation(), constrainedSnapScale(),
489  *  etc. instead.
490  *
491  *  This is what is being done in this method: transform each point, find out whether
492  *  a free snap or constrained snap is more appropriate, do the snapping, calculate
493  *  some metrics to quantify the snap "distance", and see if it's better than the
494  *  previous snap. Finally, the best ("nearest") snap from all these points is returned.
495  *
496  *  \param points Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
497  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
498  *  \param constrained true if the snap is constrained, e.g. for stretching or for purely horizontal translation.
499  *  \param constraint The direction or line along which snapping must occur, if 'constrained' is true; otherwise undefined.
500  *  \param transformation_type Type of transformation to apply to points before trying to snap them.
501  *  \param transformation Description of the transformation; details depend on the type.
502  *  \param origin Origin of the transformation, if applicable.
503  *  \param dim Dimension to which the transformation applies, if applicable.
504  *  \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
505  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
506  */
508 Inkscape::SnappedPoint SnapManager::_snapTransformed(
509     std::vector<Inkscape::SnapCandidatePoint> const &points,
510     Geom::Point const &pointer,
511     bool constrained,
512     Inkscape::Snapper::ConstraintLine const &constraint,
513     Transformation transformation_type,
514     Geom::Point const &transformation,
515     Geom::Point const &origin,
516     Geom::Dim2 dim,
517     bool uniform) const
519     /* We have a list of points, which we are proposing to transform in some way.  We need to see
520     ** if any of these points, when transformed, snap to anything.  If they do, we return the
521     ** appropriate transformation with `true'; otherwise we return the original scale with `false'.
522     */
524     /* Quick check to see if we have any snappers that are enabled
525     ** Also used to globally disable all snapping
526     */
527     if (someSnapperMightSnap() == false || points.size() == 0) {
528         return Inkscape::SnappedPoint(pointer);
529     }
531     std::vector<Inkscape::SnapCandidatePoint> transformed_points;
532     Geom::Rect bbox;
534     long source_num = 0;
535     for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) {
537         /* Work out the transformed version of this point */
538         Geom::Point transformed = _transformPoint(*i, transformation_type, transformation, origin, dim, uniform);
540         // add the current transformed point to the box hulling all transformed points
541         if (i == points.begin()) {
542             bbox = Geom::Rect(transformed, transformed);
543         } else {
544             bbox.expandTo(transformed);
545         }
547         transformed_points.push_back(Inkscape::SnapCandidatePoint(transformed, (*i).getSourceType(), source_num));
548         source_num++;
549     }
551     /* The current best transformation */
552     Geom::Point best_transformation = transformation;
554     /* The current best metric for the best transformation; lower is better, NR_HUGE
555     ** means that we haven't snapped anything.
556     */
557     Geom::Point best_scale_metric(NR_HUGE, NR_HUGE);
558     Inkscape::SnappedPoint best_snapped_point;
559     g_assert(best_snapped_point.getAlwaysSnap() == false); // Check initialization of snapped point
560     g_assert(best_snapped_point.getAtIntersection() == false);
562     std::vector<Inkscape::SnapCandidatePoint>::iterator j = transformed_points.begin();
565     // std::cout << std::endl;
566     bool first_free_snap = true;
567     for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator i = points.begin(); i != points.end(); i++) {
569         /* Snap it */
570         Inkscape::SnappedPoint snapped_point;
571         Inkscape::Snapper::ConstraintLine dedicated_constraint = constraint;
572         Geom::Point const b = ((*i).getPoint() - origin); // vector to original point
574         if (constrained) {
575             if ((transformation_type == SCALE || transformation_type == STRETCH) && uniform) {
576                 // When uniformly scaling, each point will have its own unique constraint line,
577                 // running from the scaling origin to the original untransformed point. We will
578                 // calculate that line here
579                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, b);
580             } else if (transformation_type == STRETCH) { // when non-uniform stretching {
581                 dedicated_constraint = Inkscape::Snapper::ConstraintLine((*i).getPoint(), component_vectors[dim]);
582             } else if (transformation_type == TRANSLATION) {
583                 // When doing a constrained translation, all points will move in the same direction, i.e.
584                 // either horizontally or vertically. The lines along which they move are therefore all
585                 // parallel, but might not be colinear. Therefore we will have to set the point through
586                 // which the constraint-line runs here, for each point individually.
587                 dedicated_constraint.setPoint((*i).getPoint());
588             } // else: leave the original constraint, e.g. for skewing
589             if (transformation_type == SCALE && !uniform) {
590                 g_warning("Non-uniform constrained scaling is not supported!");
591             }
592             snapped_point = constrainedSnap(*j, dedicated_constraint, bbox);
593         } else {
594             bool const c1 = fabs(b[Geom::X]) < 1e-6;
595             bool const c2 = fabs(b[Geom::Y]) < 1e-6;
596             if (transformation_type == SCALE && (c1 || c2) && !(c1 && c2)) {
597                 // When scaling, a point aligned either horizontally or vertically with the origin can only
598                 // move in that specific direction; therefore it should only snap in that direction, otherwise
599                 // we will get snapped points with an invalid transformation
600                 dedicated_constraint = Inkscape::Snapper::ConstraintLine(origin, component_vectors[c1]);
601                 snapped_point = constrainedSnap(*j, dedicated_constraint, bbox);
602             } else {
603                 // If we have a collection of SnapCandidatePoints, with mixed constrained snapping and free snapping
604                 // requirements, then freeSnap might never see the SnapCandidatePoint with source_num == 0. The freeSnap()
605                 // method in the object snapper depends on this, because only for source-num == 0 the target nodes will
606                 // be collected. Therefore we enforce that the first SnapCandidatePoint that is to be freeSnapped always
607                 // has source_num == 0;
608                 // TODO: This is a bit ugly so fix this; do we need sourcenum for anything else? if we don't then get rid
609                 // of it and explicitely communicate to the object snapper that this is a first point
610                 if (first_free_snap) {
611                     (*j).setSourceNum(0);
612                     first_free_snap = false;
613                 }
614                 snapped_point = freeSnap(*j, bbox);
615             }
616         }
617         // std::cout << "dist = " << snapped_point.getSnapDistance() << std::endl;
618         snapped_point.setPointerDistance(Geom::L2(pointer - (*i).getPoint()));
620         Geom::Point result;
622         if (snapped_point.getSnapped()) {
623             /* We snapped.  Find the transformation that describes where the snapped point has
624             ** ended up, and also the metric for this transformation.
625             */
626             Geom::Point const a = (snapped_point.getPoint() - origin); // vector to snapped point
627             //Geom::Point const b = (*i - origin); // vector to original point
629             switch (transformation_type) {
630                 case TRANSLATION:
631                     result = snapped_point.getPoint() - (*i).getPoint();
632                     /* Consider the case in which a box is almost aligned with a grid in both
633                      * horizontal and vertical directions. The distance to the intersection of
634                      * the grid lines will always be larger then the distance to a single grid
635                      * line. If we prefer snapping to an intersection instead of to a single
636                      * grid line, then we cannot use "metric = Geom::L2(result)". Therefore the
637                      * snapped distance will be used as a metric. Please note that the snapped
638                      * distance is defined as the distance to the nearest line of the intersection,
639                      * and not to the intersection itself!
640                      */
641                     // Only for translations, the relevant metric will be the real snapped distance,
642                     // so we don't have to do anything special here
643                     break;
644                 case SCALE:
645                 {
646                     result = Geom::Point(NR_HUGE, NR_HUGE);
647                     // If this point *i is horizontally or vertically aligned with
648                     // the origin of the scaling, then it will scale purely in X or Y
649                     // We can therefore only calculate the scaling in this direction
650                     // and the scaling factor for the other direction should remain
651                     // untouched (unless scaling is uniform ofcourse)
652                     for (int index = 0; index < 2; index++) {
653                         if (fabs(b[index]) > 1e-6) { // if SCALING CAN occur in this direction
654                             if (fabs(fabs(a[index]/b[index]) - fabs(transformation[index])) > 1e-12) { // if SNAPPING DID occur in this direction
655                                 result[index] = a[index] / b[index]; // then calculate it!
656                             }
657                             // we might leave result[1-index] = NR_HUGE
658                             // if scaling didn't occur in the other direction
659                         }
660                     }
661                     if (uniform) {
662                         if (fabs(result[0]) < fabs(result[1])) {
663                             result[1] = result[0];
664                         } else {
665                             result[0] = result[1];
666                         }
667                     }
668                     // Compare the resulting scaling with the desired scaling
669                     Geom::Point scale_metric = Geom::abs(result - transformation); // One or both of its components might be NR_HUGE
670                     snapped_point.setSnapDistance(std::min(scale_metric[0], scale_metric[1]));
671                     snapped_point.setSecondSnapDistance(std::max(scale_metric[0], scale_metric[1]));
672                     break;
673                 }
674                 case STRETCH:
675                     result = Geom::Point(NR_HUGE, NR_HUGE);
676                     if (fabs(b[dim]) > 1e-6) { // if STRETCHING will occur for this point
677                         result[dim] = a[dim] / b[dim];
678                         result[1-dim] = uniform ? result[dim] : 1;
679                     } else { // STRETCHING might occur for this point, but only when the stretching is uniform
680                         if (uniform && fabs(b[1-dim]) > 1e-6) {
681                            result[1-dim] = a[1-dim] / b[1-dim];
682                            result[dim] = result[1-dim];
683                         }
684                     }
685                     // Store the metric for this transformation as a virtual distance
686                     snapped_point.setSnapDistance(std::abs(result[dim] - transformation[dim]));
687                     snapped_point.setSecondSnapDistance(NR_HUGE);
688                     break;
689                 case SKEW:
690                     result[0] = (snapped_point.getPoint()[dim] - ((*i).getPoint())[dim]) / (((*i).getPoint())[1 - dim] - origin[1 - dim]); // skew factor
691                     result[1] = transformation[1]; // scale factor
692                     // Store the metric for this transformation as a virtual distance
693                     snapped_point.setSnapDistance(std::abs(result[0] - transformation[0]));
694                     snapped_point.setSecondSnapDistance(NR_HUGE);
695                     break;
696                 default:
697                     g_assert_not_reached();
698             }
700             // When scaling, we're considering the best transformation in each direction separately. We will have a metric in each
701             // direction, whereas for all other transformation we only a single one-dimensional metric. That's why we need to handle
702             // the scaling metric differently
703             if (best_snapped_point.isOtherSnapBetter(snapped_point, true)) {
704                 best_transformation = result;
705                 best_snapped_point = snapped_point;
706             }
707         }
709         j++;
710     }
712     Geom::Coord best_metric;
713     if (transformation_type == SCALE) {
714         // When scaling, don't ever exit with one of scaling components set to NR_HUGE
715         for (int index = 0; index < 2; index++) {
716             if (best_transformation[index] == NR_HUGE) {
717                 if (uniform && best_transformation[1-index] < NR_HUGE) {
718                     best_transformation[index] = best_transformation[1-index];
719                 } else {
720                     best_transformation[index] = transformation[index];
721                 }
722             }
723         }
724     }
726     best_metric = best_snapped_point.getSnapDistance();
727     best_snapped_point.setTransformation(best_transformation);
728     // Using " < 1e6" instead of " < NR_HUGE" for catching some rounding errors
729     // These rounding errors might be caused by NRRects, see bug #1584301
730     best_snapped_point.setSnapDistance(best_metric < 1e6 ? best_metric : NR_HUGE);
731     return best_snapped_point;
735 /**
736  *  \brief Apply a translation to a set of points and try to snap freely in 2 degrees-of-freedom
737  *
738  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
739  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
740  *  \param tr Proposed translation; the final translation can only be calculated after snapping has occurred
741  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
742  */
744 Inkscape::SnappedPoint SnapManager::freeSnapTranslation(std::vector<Inkscape::SnapCandidatePoint> const &p,
745                                                         Geom::Point const &pointer,
746                                                         Geom::Point const &tr) const
748     if (p.size() == 1) {
749         Geom::Point pt = _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
750         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
751     }
753     return _snapTransformed(p, pointer, false, Geom::Point(0,0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
756 /**
757  *  \brief Apply a translation to a set of points and try to snap along a constraint
758  *
759  *  \param point_type Category of points to which the source point belongs: node or bounding box.
760  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
761  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
762  *  \param constraint The direction or line along which snapping must occur.
763  *  \param tr Proposed translation; the final translation can only be calculated after snapping has occurred.
764  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
765  */
767 Inkscape::SnappedPoint SnapManager::constrainedSnapTranslation(std::vector<Inkscape::SnapCandidatePoint> const &p,
768                                                                Geom::Point const &pointer,
769                                                                Inkscape::Snapper::ConstraintLine const &constraint,
770                                                                Geom::Point const &tr) const
772     if (p.size() == 1) {
773         Geom::Point pt = _transformPoint(p.at(0), TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
774         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
775     }
777     return _snapTransformed(p, pointer, true, constraint, TRANSLATION, tr, Geom::Point(0,0), Geom::X, false);
781 /**
782  *  \brief Apply a scaling to a set of points and try to snap freely in 2 degrees-of-freedom
783  *
784  *  \param point_type Category of points to which the source point belongs: node or bounding box.
785  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
786  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
787  *  \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
788  *  \param o Origin of the scaling
789  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
790  */
792 Inkscape::SnappedPoint SnapManager::freeSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p,
793                                                   Geom::Point const &pointer,
794                                                   Geom::Scale const &s,
795                                                   Geom::Point const &o) const
797     if (p.size() == 1) {
798         Geom::Point pt = _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
799         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
800     }
802     return _snapTransformed(p, pointer, false, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, false);
806 /**
807  *  \brief Apply a scaling to a set of points and snap such that the aspect ratio of the selection is preserved
808  *
809  *  \param point_type Category of points to which the source point belongs: node or bounding box.
810  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
811  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
812  *  \param s Proposed scaling; the final scaling can only be calculated after snapping has occurred
813  *  \param o Origin of the scaling
814  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
815  */
817 Inkscape::SnappedPoint SnapManager::constrainedSnapScale(std::vector<Inkscape::SnapCandidatePoint> const &p,
818                                                          Geom::Point const &pointer,
819                                                          Geom::Scale const &s,
820                                                          Geom::Point const &o) const
822     // When constrained scaling, only uniform scaling is supported.
823     if (p.size() == 1) {
824         Geom::Point pt = _transformPoint(p.at(0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
825         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
826     }
828     return _snapTransformed(p, pointer, true, Geom::Point(0,0), SCALE, Geom::Point(s[Geom::X], s[Geom::Y]), o, Geom::X, true);
831 /**
832  *  \brief Apply a stretch to a set of points and snap such that the direction of the stretch is preserved
833  *
834  *  \param point_type Category of points to which the source point belongs: node or bounding box.
835  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
836  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
837  *  \param s Proposed stretch; the final stretch can only be calculated after snapping has occurred
838  *  \param o Origin of the stretching
839  *  \param d Dimension in which to apply proposed stretch.
840  *  \param u true if the stretch should be uniform (i.e. to be applied equally in both dimensions)
841  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
842  */
844 Inkscape::SnappedPoint SnapManager::constrainedSnapStretch(std::vector<Inkscape::SnapCandidatePoint> const &p,
845                                                             Geom::Point const &pointer,
846                                                             Geom::Coord const &s,
847                                                             Geom::Point const &o,
848                                                             Geom::Dim2 d,
849                                                             bool u) const
851     if (p.size() == 1) {
852         Geom::Point pt = _transformPoint(p.at(0), STRETCH, Geom::Point(s, s), o, d, u);
853         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
854     }
856     return _snapTransformed(p, pointer, true, Geom::Point(0,0), STRETCH, Geom::Point(s, s), o, d, u);
859 /**
860  *  \brief Apply a skew to a set of points and snap such that the direction of the skew is preserved
861  *
862  *  \param point_type Category of points to which the source point belongs: node or bounding box.
863  *  \param p Collection of points to snap (snap sources), at their untransformed position, all points undergoing the same transformation. Paired with an identifier of the type of the snap source.
864  *  \param pointer Location of the mouse pointer at the time dragging started (i.e. when the selection was still untransformed).
865  *  \param constraint The direction or line along which snapping must occur.
866  *  \param s Proposed skew; the final skew can only be calculated after snapping has occurred
867  *  \param o Origin of the proposed skew
868  *  \param d Dimension in which to apply proposed skew.
869  *  \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics.
870  */
872 Inkscape::SnappedPoint SnapManager::constrainedSnapSkew(std::vector<Inkscape::SnapCandidatePoint> const &p,
873                                                  Geom::Point const &pointer,
874                                                  Inkscape::Snapper::ConstraintLine const &constraint,
875                                                  Geom::Point const &s,
876                                                  Geom::Point const &o,
877                                                  Geom::Dim2 d) const
879     // "s" contains skew factor in s[0], and scale factor in s[1]
881     // Snapping the nodes of the bounding box of a selection that is being transformed, will only work if
882     // the transformation of the bounding box is equal to the transformation of the individual nodes. This is
883     // NOT the case for example when rotating or skewing. The bounding box itself cannot possibly rotate or skew,
884     // so it's corners have a different transformation. The snappers cannot handle this, therefore snapping
885     // of bounding boxes is not allowed here.
886     if (p.size() > 0) {
887         g_assert(!(p.at(0).getSourceType() & Inkscape::SNAPSOURCE_BBOX_CATEGORY));
888     }
890     if (p.size() == 1) {
891         Geom::Point pt = _transformPoint(p.at(0), SKEW, s, o, d, false);
892         _displaySnapsource(Inkscape::SnapCandidatePoint(pt, p.at(0).getSourceType()));
893     }
895     return _snapTransformed(p, pointer, true, constraint, SKEW, s, o, d, false);
898 /**
899  * \brief Given a set of possible snap targets, find the best target (which is not necessarily
900  * also the nearest target), and show the snap indicator if requested
901  *
902  * \param p Source point to be snapped
903  * \param sc A structure holding all snap targets that have been found so far
904  * \param constrained True if the snap is constrained, e.g. for stretching or for purely horizontal translation.
905  * \param noCurves If true, then do consider snapping to intersections of curves, but not to the curves themselves
906  * \param allowOffScreen If true, then snapping to points which are off the screen is allowed (needed for example when pasting to the grid)
907  * \return An instance of the SnappedPoint class, which holds data on the snap source, snap target, and various metrics
908  */
910 Inkscape::SnappedPoint SnapManager::findBestSnap(Inkscape::SnapCandidatePoint const &p,
911                                                  SnappedConstraints const &sc,
912                                                  bool constrained,
913                                                  bool noCurves,
914                                                  bool allowOffScreen) const
918     /*
919     std::cout << "Type and number of snapped constraints: " << std::endl;
920     std::cout << "  Points      : " << sc.points.size() << std::endl;
921     std::cout << "  Lines       : " << sc.lines.size() << std::endl;
922     std::cout << "  Grid lines  : " << sc.grid_lines.size()<< std::endl;
923     std::cout << "  Guide lines : " << sc.guide_lines.size()<< std::endl;
924     std::cout << "  Curves      : " << sc.curves.size()<< std::endl;
925     */
927     // Store all snappoints
928     std::list<Inkscape::SnappedPoint> sp_list;
930     // search for the closest snapped point
931     Inkscape::SnappedPoint closestPoint;
932     if (getClosestSP(sc.points, closestPoint)) {
933         sp_list.push_back(closestPoint);
934     }
936     // search for the closest snapped curve
937     if (!noCurves) {
938         Inkscape::SnappedCurve closestCurve;
939         if (getClosestCurve(sc.curves, closestCurve)) {
940             sp_list.push_back(Inkscape::SnappedPoint(closestCurve));
941         }
942     }
944     if (snapprefs.getSnapIntersectionCS()) {
945         // search for the closest snapped intersection of curves
946         Inkscape::SnappedPoint closestCurvesIntersection;
947         if (getClosestIntersectionCS(sc.curves, p.getPoint(), closestCurvesIntersection, _desktop->dt2doc())) {
948             closestCurvesIntersection.setSource(p.getSourceType());
949             sp_list.push_back(closestCurvesIntersection);
950         }
951     }
953     // search for the closest snapped grid line
954     Inkscape::SnappedLine closestGridLine;
955     if (getClosestSL(sc.grid_lines, closestGridLine)) {
956         sp_list.push_back(Inkscape::SnappedPoint(closestGridLine));
957     }
959     // search for the closest snapped guide line
960     Inkscape::SnappedLine closestGuideLine;
961     if (getClosestSL(sc.guide_lines, closestGuideLine)) {
962         sp_list.push_back(Inkscape::SnappedPoint(closestGuideLine));
963     }
965     // When freely snapping to a grid/guide/path, only one degree of freedom is eliminated
966     // Therefore we will try get fully constrained by finding an intersection with another grid/guide/path
968     // When doing a constrained snap however, we're already at an intersection of the constrained line and
969     // the grid/guide/path we're snapping to. This snappoint is therefore fully constrained, so there's
970     // no need to look for additional intersections
971     if (!constrained) {
972         // search for the closest snapped intersection of grid lines
973         Inkscape::SnappedPoint closestGridPoint;
974         if (getClosestIntersectionSL(sc.grid_lines, closestGridPoint)) {
975             closestGridPoint.setSource(p.getSourceType());
976             closestGridPoint.setTarget(Inkscape::SNAPTARGET_GRID_INTERSECTION);
977             sp_list.push_back(closestGridPoint);
978         }
980         // search for the closest snapped intersection of guide lines
981         Inkscape::SnappedPoint closestGuidePoint;
982         if (getClosestIntersectionSL(sc.guide_lines, closestGuidePoint)) {
983             closestGuidePoint.setSource(p.getSourceType());
984             closestGuidePoint.setTarget(Inkscape::SNAPTARGET_GUIDE_INTERSECTION);
985             sp_list.push_back(closestGuidePoint);
986         }
988         // search for the closest snapped intersection of grid with guide lines
989         if (snapprefs.getSnapIntersectionGG()) {
990             Inkscape::SnappedPoint closestGridGuidePoint;
991             if (getClosestIntersectionSL(sc.grid_lines, sc.guide_lines, closestGridGuidePoint)) {
992                 closestGridGuidePoint.setSource(p.getSourceType());
993                 closestGridGuidePoint.setTarget(Inkscape::SNAPTARGET_GRID_GUIDE_INTERSECTION);
994                 sp_list.push_back(closestGridGuidePoint);
995             }
996         }
997     }
999     // now let's see which snapped point gets a thumbs up
1000     Inkscape::SnappedPoint bestSnappedPoint(p.getPoint());
1001     // std::cout << "Finding the best snap..." << std::endl;
1002     for (std::list<Inkscape::SnappedPoint>::const_iterator i = sp_list.begin(); i != sp_list.end(); i++) {
1003         // std::cout << "sp = " << (*i).getPoint() << " | source = " << (*i).getSource() << " | target = " << (*i).getTarget();
1004         bool onScreen = _desktop->get_display_area().contains((*i).getPoint());
1005         if (onScreen || allowOffScreen) { // Only snap to points which are not off the screen
1006             if ((*i).getSnapDistance() <= (*i).getTolerance()) { // Only snap to points within snapping range
1007                 // if it's the first point, or if it is closer than the best snapped point so far
1008                 if (i == sp_list.begin() || bestSnappedPoint.isOtherSnapBetter(*i, false)) {
1009                     // then prefer this point over the previous one
1010                     bestSnappedPoint = *i;
1011                 }
1012             }
1013         }
1014         // std::cout << std::endl;
1015     }
1017     // Update the snap indicator, if requested
1018     if (_snapindicator) {
1019         if (bestSnappedPoint.getSnapped()) {
1020             _desktop->snapindicator->set_new_snaptarget(bestSnappedPoint);
1021         } else {
1022             _desktop->snapindicator->remove_snaptarget();
1023         }
1024     }
1026     // std::cout << "findBestSnap = " << bestSnappedPoint.getPoint() << " | dist = " << bestSnappedPoint.getSnapDistance() << std::endl;
1027     return bestSnappedPoint;
1030 /// Convenience shortcut when there is only one item to ignore
1031 void SnapManager::setup(SPDesktop const *desktop,
1032                         bool snapindicator,
1033                         SPItem const *item_to_ignore,
1034                         std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1035                         SPGuide *guide_to_ignore)
1037     g_assert(desktop != NULL);
1038     _items_to_ignore.clear();
1039     _items_to_ignore.push_back(item_to_ignore);
1040     _desktop = desktop;
1041     _snapindicator = snapindicator;
1042     _unselected_nodes = unselected_nodes;
1043     _guide_to_ignore = guide_to_ignore;
1046 /**
1047  * \brief Prepare the snap manager for the actual snapping, which includes building a list of snap targets
1048  * to ignore and toggling the snap indicator
1049  *
1050  * There are two overloaded setup() methods, of which the other one only allows for a single item to be ignored
1051  * whereas this one will take a list of items to ignore
1052  *
1053  * \param desktop Reference to the desktop to which this snap manager is attached
1054  * \param snapindicator If true then a snap indicator will be displayed automatically (when enabled in the preferences)
1055  * \param items_to_ignore These items will not be snapped to, e.g. the items that are currently being dragged. This avoids "self-snapping"
1056  * \param unselected_nodes Stationary nodes of the path that is currently being edited in the node tool and
1057  * that can be snapped too. Nodes not in this list will not be snapped to, to avoid "self-snapping". Of each
1058  * unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
1059  * \param guide_to_ignore Guide that is currently being dragged and should not be snapped to
1060  */
1062 void SnapManager::setup(SPDesktop const *desktop,
1063                         bool snapindicator,
1064                         std::vector<SPItem const *> &items_to_ignore,
1065                         std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1066                         SPGuide *guide_to_ignore)
1068     g_assert(desktop != NULL);
1069     _items_to_ignore = items_to_ignore;
1070     _desktop = desktop;
1071     _snapindicator = snapindicator;
1072     _unselected_nodes = unselected_nodes;
1073     _guide_to_ignore = guide_to_ignore;
1076 /// Setup, taking the list of items to ignore from the desktop's selection.
1077 void SnapManager::setupIgnoreSelection(SPDesktop const *desktop,
1078                                       bool snapindicator,
1079                                       std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes,
1080                                       SPGuide *guide_to_ignore)
1082     _desktop = desktop;
1083     _snapindicator = snapindicator;
1084     _unselected_nodes = unselected_nodes;
1085     _guide_to_ignore = guide_to_ignore;
1086     _items_to_ignore.clear();
1088     Inkscape::Selection *sel = _desktop->selection;
1089     GSList const *items = sel->itemList();
1090     for (GSList *i = const_cast<GSList*>(items); i; i = i->next) {
1091         _items_to_ignore.push_back(static_cast<SPItem const *>(i->data));
1092     }
1095 SPDocument *SnapManager::getDocument() const
1097     return _named_view->document;
1100 /**
1101  * \brief Takes an untransformed point, applies the given transformation, and returns the transformed point. Eliminates lots of duplicated code
1102  *
1103  * \param p The untransformed position of the point, paired with an identifier of the type of the snap source.
1104  * \param transformation_type Type of transformation to apply.
1105  * \param transformation Mathematical description of the transformation; details depend on the type.
1106  * \param origin Origin of the transformation, if applicable.
1107  * \param dim Dimension to which the transformation applies, if applicable.
1108  * \param uniform true if the transformation should be uniform; only applicable for stretching and scaling.
1109  * \return The position of the point after transformation
1110  */
1112 Geom::Point SnapManager::_transformPoint(Inkscape::SnapCandidatePoint const &p,
1113                                         Transformation const transformation_type,
1114                                         Geom::Point const &transformation,
1115                                         Geom::Point const &origin,
1116                                         Geom::Dim2 const dim,
1117                                         bool const uniform) const
1119     /* Work out the transformed version of this point */
1120     Geom::Point transformed;
1121     switch (transformation_type) {
1122         case TRANSLATION:
1123             transformed = p.getPoint() + transformation;
1124             break;
1125         case SCALE:
1126             transformed = (p.getPoint() - origin) * Geom::Scale(transformation[Geom::X], transformation[Geom::Y]) + origin;
1127             break;
1128         case STRETCH:
1129         {
1130             Geom::Scale s(1, 1);
1131             if (uniform)
1132                 s[Geom::X] = s[Geom::Y] = transformation[dim];
1133             else {
1134                 s[dim] = transformation[dim];
1135                 s[1 - dim] = 1;
1136             }
1137             transformed = ((p.getPoint() - origin) * s) + origin;
1138             break;
1139         }
1140         case SKEW:
1141             // Apply the skew factor
1142             transformed[dim] = (p.getPoint())[dim] + transformation[0] * ((p.getPoint())[1 - dim] - origin[1 - dim]);
1143             // While skewing, mirroring and scaling (by integer multiples) in the opposite direction is also allowed.
1144             // Apply that scale factor here
1145             transformed[1-dim] = (p.getPoint() - origin)[1 - dim] * transformation[1] + origin[1 - dim];
1146             break;
1147         default:
1148             g_assert_not_reached();
1149     }
1151     return transformed;
1154 /**
1155  * \brief Mark the location of the snap source (not the snap target!) on the canvas by drawing a symbol
1156  *
1157  * \param point_type Category of points to which the source point belongs: node, guide or bounding box
1158  * \param p The transformed position of the source point, paired with an identifier of the type of the snap source.
1159  */
1161 void SnapManager::_displaySnapsource(Inkscape::SnapCandidatePoint const &p) const {
1163     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1164     if (prefs->getBool("/options/snapclosestonly/value")) {
1165         bool p_is_a_node = p.getSourceType() & Inkscape::SNAPSOURCE_NODE_CATEGORY;
1166         bool p_is_a_bbox = p.getSourceType() & Inkscape::SNAPSOURCE_BBOX_CATEGORY;
1168         if (snapprefs.getSnapEnabledGlobally() && ((p_is_a_node && snapprefs.getSnapModeNode()) || (p_is_a_bbox && snapprefs.getSnapModeBBox()))) {
1169             _desktop->snapindicator->set_new_snapsource(p);
1170         } else {
1171             _desktop->snapindicator->remove_snapsource();
1172         }
1173     }
1176 /*
1177   Local Variables:
1178   mode:c++
1179   c-file-style:"stroustrup"
1180   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1181   indent-tabs-mode:nil
1182   fill-column:99
1183   End:
1184 */
1185 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :