1 /**
2 * \file object-snapper.cpp
3 * \brief Snapping things to objects.
4 *
5 * Authors:
6 * Carl Hetherington <inkscape@carlh.net>
7 * Diederik van Lierop <mail@diedenrezi.nl>
8 *
9 * Copyright (C) 2005 - 2008 Authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "libnr/n-art-bpath.h"
15 #include "libnr/nr-path.h"
16 #include "libnr/nr-rect-ops.h"
17 #include "libnr/nr-point-fns.h"
18 #include "live_effects/n-art-bpath-2geom.h"
19 #include "2geom/path-intersection.h"
20 #include "document.h"
21 #include "sp-namedview.h"
22 #include "sp-image.h"
23 #include "sp-item-group.h"
24 #include "sp-item.h"
25 #include "sp-use.h"
26 #include "display/curve.h"
27 #include "desktop.h"
28 #include "inkscape.h"
29 #include "prefs-utils.h"
30 #include "sp-text.h"
31 #include "sp-flowtext.h"
32 #include "text-editing.h"
34 Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, NR::Coord const d)
35 : Snapper(nv, d), _snap_to_itemnode(true), _snap_to_itempath(true),
36 _snap_to_bboxnode(true), _snap_to_bboxpath(true), _snap_to_page_border(false),
37 _strict_snapping(true), _include_item_center(false)
38 {
39 _candidates = new std::vector<SPItem*>;
40 _points_to_snap_to = new std::vector<NR::Point>;
41 _bpaths_to_snap_to = new std::vector<NArtBpath*>;
42 _paths_to_snap_to = new std::vector<Path*>;
43 }
45 Inkscape::ObjectSnapper::~ObjectSnapper()
46 {
47 _candidates->clear(); //Don't delete the candidates themselves, as these are not ours!
48 delete _candidates;
50 _points_to_snap_to->clear();
51 delete _points_to_snap_to;
53 _clear_paths();
54 delete _paths_to_snap_to;
55 delete _bpaths_to_snap_to;
56 }
58 /**
59 * Find all items within snapping range.
60 * \param r Pointer to the current document
61 * \param it List of items to ignore
62 * \param first_point If true then this point is the first one from a whole bunch of points
63 * \param points_to_snap The whole bunch of points, all from the same selection and having the same transformation
64 * \param DimensionToSnap Snap in X, Y, or both directions.
65 */
67 void Inkscape::ObjectSnapper::_findCandidates(SPObject* r,
68 std::list<SPItem const *> const &it,
69 bool const &first_point,
70 std::vector<NR::Point> &points_to_snap,
71 DimensionToSnap const snap_dim) const
72 {
73 bool const c1 = (snap_dim == TRANSL_SNAP_XY) && ThisSnapperMightSnap();
74 bool const c2 = (snap_dim != TRANSL_SNAP_XY) && GuidesMightSnap();
75 bool const c3 = points_to_snap.size() == 0;
77 if (!(c1 || c2) || c3) {
78 return;
79 }
81 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
83 if (first_point) {
84 _candidates->clear();
85 }
87 NR::Maybe<NR::Rect> bbox = NR::Rect(); // a default NR::Rect is infinitely large
88 NR::Coord t = getSnapperTolerance();
90 // When dragging a guide...
91 NR::Point p_guide = points_to_snap[0];
92 if (!getSnapperAlwaysSnap()) {
93 bbox = NR::Rect(p_guide, p_guide); // bbox is now just a single point: p_guide
94 bbox->growBy(t); // bbox width and height now measure 2x snapper tolerance
95 // for angled guidelines the bbox is now larger than really needed
96 // (up to sqrt(2) for 45 deg. guidelines) but we'll leave it like that
97 } // else: use an infinitely large bbox to find candidates
99 for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
100 if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
102 /* See if this item is on the ignore list */
103 std::list<SPItem const *>::const_iterator i = it.begin();
104 while (i != it.end() && *i != o) {
105 i++;
106 }
108 if (i == it.end()) {
109 /* See if the item is within range */
110 if (SP_IS_GROUP(o)) {
111 _findCandidates(o, it, false, points_to_snap, snap_dim);
112 } else {
113 // Now let's see if any of the snapping points is within snapping range of this object
114 if (snap_dim == TRANSL_SNAP_XY) {
115 bbox = sp_item_bbox_desktop(SP_ITEM(o));
116 } // else: we're snapping a guide to an object and we will use the bbox as defined above
118 if (bbox) {
119 for (std::vector<NR::Point>::const_iterator i = points_to_snap.begin(); i != points_to_snap.end(); i++) {
120 NR::Point b_min = bbox->min();
121 NR::Point b_max = bbox->max();
122 bool withinX = ((*i)[NR::X] >= b_min[NR::X] - t) && ((*i)[NR::X] <= b_max[NR::X] + t);
123 bool withinY = ((*i)[NR::Y] >= b_min[NR::Y] - t) && ((*i)[NR::Y] <= b_max[NR::Y] + t);
124 if (withinX && withinY) {
125 //We've found a point that is within snapping range
126 //of this object, so record it as a candidate
127 _candidates->push_back(SP_ITEM(o));
128 break;
129 }
130 }
131 }
132 }
133 }
134 }
135 }
136 }
139 void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &t,
140 bool const &first_point) const
141 {
142 // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
143 // e.g. when translating an item using the selector tool, then we will only do this for the
144 // first point and store the collection for later use. This significantly improves the performance
145 if (first_point) {
146 _points_to_snap_to->clear();
148 // Determine the type of bounding box we should snap to
149 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
151 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
152 bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
153 bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
155 // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
156 g_assert(!(p_is_a_node && p_is_a_bbox || p_is_a_bbox && p_is_a_guide || p_is_a_node && p_is_a_guide));
158 if (_snap_to_bboxnode) {
159 gchar const *prefs_bbox = prefs_get_string_attribute("tools", "bounding_box");
160 bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
161 }
163 for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
164 //NR::Matrix i2doc(NR::identity());
165 SPItem *root_item = *i;
166 if (SP_IS_USE(*i)) {
167 root_item = sp_use_root(SP_USE(*i));
168 }
169 g_return_if_fail(root_item);
171 //Collect all nodes so we can snap to them
172 if (_snap_to_itemnode) {
173 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
174 sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(*_points_to_snap_to));
175 }
176 }
178 //Collect the bounding box's corners so we can snap to them
179 if (_snap_to_bboxnode) {
180 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
181 NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
182 if (b) {
183 for ( unsigned k = 0 ; k < 4 ; k++ ) {
184 _points_to_snap_to->push_back(b->corner(k));
185 }
186 }
187 }
188 }
189 }
190 }
191 }
193 void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
194 Inkscape::Snapper::PointType const &t,
195 NR::Point const &p,
196 bool const &first_point,
197 std::vector<NR::Point> *unselected_nodes) const
198 {
199 // Iterate through all nodes, find out which one is the closest to p, and snap to it!
201 _collectNodes(t, first_point);
203 if (unselected_nodes != NULL) {
204 _points_to_snap_to->insert(_points_to_snap_to->end(), unselected_nodes->begin(), unselected_nodes->end());
205 }
207 SnappedPoint s;
208 bool success = false;
210 for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
211 NR::Coord dist = NR::L2(*k - p);
212 if (dist < getSnapperTolerance() && dist < s.getDistance()) {
213 s = SnappedPoint(*k, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
214 success = true;
215 }
216 }
218 if (success) {
219 sc.points.push_back(s);
220 }
221 }
223 void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &sc,
224 Inkscape::Snapper::PointType const &t,
225 NR::Point const &p,
226 NR::Point const &guide_normal) const
227 {
228 // Iterate through all nodes, find out which one is the closest to this guide, and snap to it!
229 _collectNodes(t, true);
231 SnappedPoint s;
232 bool success = false;
234 NR::Coord tol = getSnapperTolerance();
236 for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
237 // Project each node (*k) on the guide line (running through point p)
238 NR::Point p_proj = project_on_linesegment(*k, p, p + NR::rot90(guide_normal));
239 NR::Coord dist = NR::L2(*k - p_proj); // distance from node to the guide
240 NR::Coord dist2 = NR::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
241 if ((dist < tol && dist2 < tol || getSnapperAlwaysSnap()) && dist < s.getDistance()) {
242 s = SnappedPoint(*k, dist, tol, getSnapperAlwaysSnap());
243 success = true;
244 }
245 }
247 if (success) {
248 sc.points.push_back(s);
249 }
250 }
252 void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &t,
253 bool const &first_point,
254 NArtBpath *border_bpath) const
255 {
256 // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
257 // e.g. when translating an item using the selector tool, then we will only do this for the
258 // first point and store the collection for later use. This significantly improves the performance
259 if (first_point) {
260 _clear_paths();
262 // Determine the type of bounding box we should snap to
263 SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
265 bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
267 if (_snap_to_bboxpath) {
268 gchar const *prefs_bbox = prefs_get_string_attribute("tools", "bounding_box");
269 bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::APPROXIMATE_BBOX;
270 }
272 // Consider the page border for snapping
273 if (border_bpath != NULL) {
274 _bpaths_to_snap_to->push_back(border_bpath);
275 }
277 for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
279 /* Transform the requested snap point to this item's coordinates */
280 NR::Matrix i2doc(NR::identity());
281 SPItem *root_item = NULL;
282 /* We might have a clone at hand, so make sure we get the root item */
283 if (SP_IS_USE(*i)) {
284 i2doc = sp_use_get_root_transform(SP_USE(*i));
285 root_item = sp_use_root(SP_USE(*i));
286 g_return_if_fail(root_item);
287 } else {
288 i2doc = sp_item_i2doc_affine(*i);
289 root_item = *i;
290 }
292 //Build a list of all paths considered for snapping to
294 //Add the item's path to snap to
295 if (_snap_to_itempath) {
296 if (!(_strict_snapping && !p_is_a_node)) {
297 // Snapping to the path of characters is very cool, but for a large
298 // chunk of text this will take ages! So limit snapping to text paths
299 // containing max. 240 characters. Snapping the bbox will not be affected
300 bool very_lenghty_prose = false;
301 if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
302 very_lenghty_prose = sp_text_get_length(SP_TEXT(root_item)) > 240;
303 }
304 // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
305 // which corresponds to a lag of 500 msec. This is for snapping a rect
306 // to a single line of text.
308 // Snapping for example to a traced bitmap is also very stressing for
309 // the CPU, so we'll only snap to paths having no more than 500 nodes
310 // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
311 bool very_complex_path = false;
312 if (SP_IS_PATH(root_item)) {
313 very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
314 }
316 if (!very_lenghty_prose && !very_complex_path) {
317 SPCurve *curve = curve_for_item(root_item);
318 if (curve) {
319 NArtBpath *bpath = bpath_for_curve(root_item, curve, true, true);
320 _bpaths_to_snap_to->push_back(bpath);
321 // Because we set doTransformation to true in bpath_for_curve, we
322 // will get a dupe of the path, which must be freed at some point
323 sp_curve_unref(curve);
324 }
325 }
326 }
327 }
329 //Add the item's bounding box to snap to
330 if (_snap_to_bboxpath) {
331 if (!(_strict_snapping && p_is_a_node)) {
332 NRRect rect;
333 sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
334 NArtBpath *bpath = nr_path_from_rect(rect);
335 _bpaths_to_snap_to->push_back(bpath);
336 }
337 }
338 }
339 }
340 }
342 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
343 Inkscape::Snapper::PointType const &t,
344 NR::Point const &p,
345 bool const &first_point,
346 std::vector<NR::Point> *unselected_nodes,
347 SPPath const *selected_path,
348 NArtBpath *border_bpath) const
349 {
350 _collectPaths(t, first_point, border_bpath);
351 // Now we can finally do the real snapping, using the paths collected above
352 SnappedPoint s;
353 bool success = false;
355 /* FIXME: this seems like a hack. Perhaps Snappers should be
356 ** in SPDesktop rather than SPNamedView?
357 */
358 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
359 NR::Point const p_doc = desktop->dt2doc(p);
361 bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
363 if (first_point) {
364 /* While editing a path in the node tool, findCandidates must ignore that path because
365 * of the node snapping requirements (i.e. only unselected nodes must be snapable).
366 * This path must not be ignored however when snapping to the paths, so we add it here
367 * manually when applicable.
368 *
369 * Note that this path must be the last in line!
370 * */
371 if (node_tool_active) {
372 SPCurve *curve = curve_for_item(SP_ITEM(selected_path));
373 if (curve) {
374 NArtBpath *bpath = bpath_for_curve(SP_ITEM(selected_path), curve, true, true);
375 _bpaths_to_snap_to->push_back(bpath);
376 // Because we set doTransformation to true in bpath_for_curve, we
377 // will get a dupe of the path, which must be freed at some point
378 sp_curve_unref(curve);
379 }
380 }
381 // Convert all bpaths to Paths, because here we really must have Paths
382 // (whereas in _snapPathsConstrained we will use the original bpaths)
383 for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
384 Path *path = bpath_to_Path(*k);
385 if (path) {
386 path->ConvertWithBackData(0.01); //This is extremely time consuming!
387 _paths_to_snap_to->push_back(path);
388 }
389 }
390 }
392 for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
393 if (*k) {
394 bool const being_edited = (node_tool_active && (*k) == _paths_to_snap_to->back());
395 //if true then this path k is currently being edited in the node tool
397 for (unsigned i = 1 ; i < (*k)->pts.size() ; i++) {
398 NR::Point start_point;
399 NR::Point end_point;
400 NR::Maybe<Path::cut_position> o = NR::Nothing();
401 if (being_edited) {
402 /* If the path is being edited, then we will try to snap to each piece of the
403 * path individually. We should only snap though to stationary pieces of the paths
404 * and not to the pieces that are being dragged around. This way we avoid
405 * self-snapping. For this we check whether the nodes at both ends of the current
406 * piece are unselected; if they are then this piece must be stationary
407 */
408 unsigned piece = (*k)->pts[i].piece;
409 // Example: a cubic spline is a single piece within a path; it will be drawn using
410 // a polyline, which is described by the collection of lines between the points pts[i]
411 (*k)->PointAt(piece, 0, start_point);
412 (*k)->PointAt(piece, 1, end_point);
413 start_point = desktop->doc2dt(start_point);
414 end_point = desktop->doc2dt(end_point);
415 g_assert(unselected_nodes != NULL);
416 bool c1 = isUnselectedNode(start_point, unselected_nodes);
417 bool c2 = isUnselectedNode(end_point, unselected_nodes);
418 if (c1 && c2) {
419 o = get_nearest_position_on_Path(*k, p_doc, i);
420 }
421 } else {
422 /* If the path is NOT being edited, then we will try to snap to the path as a
423 * whole, so we need to do this only once and we will break out at the end of
424 * this for-loop iteration */
425 /* Look for the nearest position on this SPItem to our snap point */
426 o = get_nearest_position_on_Path(*k, p_doc);
427 (*k)->PointAt(o->piece, 0, start_point);
428 (*k)->PointAt(o->piece, 1, end_point);
429 start_point = desktop->doc2dt(start_point);
430 end_point = desktop->doc2dt(end_point);
431 }
433 if (o && o->t >= 0 && o->t <= 1) {
434 /* Convert the nearest point back to desktop coordinates */
435 NR::Point const o_it = get_point_on_Path(*k, o->piece, o->t);
436 NR::Point const o_dt = desktop->doc2dt(o_it);
437 NR::Coord const dist = NR::L2(o_dt - p);
439 if (dist < getSnapperTolerance()) {
440 // if we snap to a straight line segment (within a path), then return this line segment
441 if ((*k)->IsLineSegment(o->piece)) {
442 sc.lines.push_back(Inkscape::SnappedLineSegment(o_dt, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), start_point, end_point));
443 } else {
444 // for segments other than straight lines of a path, we'll return just the closest snapped point
445 if (dist < s.getDistance()) {
446 s = SnappedPoint(o_dt, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
447 success = true;
448 }
449 }
450 }
451 }
453 // If the path is NOT being edited, then we will try to snap to the path as a whole
454 // so we need to do this only once
455 if (!being_edited) break;
456 }
457 }
458 }
460 if (success) {
461 sc.points.push_back(s);
462 }
463 }
465 /* Returns true if point is coincident with one of the unselected nodes */
466 bool Inkscape::ObjectSnapper::isUnselectedNode(NR::Point const &point, std::vector<NR::Point> const *unselected_nodes) const
467 {
468 if (unselected_nodes == NULL) {
469 return false;
470 }
472 if (unselected_nodes->size() == 0) {
473 return false;
474 }
476 for (std::vector<NR::Point>::const_iterator i = unselected_nodes->begin(); i != unselected_nodes->end(); i++) {
477 if (NR::L2(point - *i) < 1e-4) {
478 return true;
479 }
480 }
482 return false;
483 }
485 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
486 Inkscape::Snapper::PointType const &t,
487 NR::Point const &p,
488 bool const &first_point,
489 ConstraintLine const &c) const
490 {
492 // Consider the page's border for snapping to
493 NArtBpath *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;
495 _collectPaths(t, first_point, border_bpath);
497 // Now we can finally do the real snapping, using the paths collected above
499 /* FIXME: this seems like a hack. Perhaps Snappers should be
500 ** in SPDesktop rather than SPNamedView?
501 */
502 SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
503 NR::Point const p_doc = desktop->dt2doc(p);
505 NR::Point direction_vector = c.getDirection();
506 if (!is_zero(direction_vector)) {
507 direction_vector = NR::unit_vector(direction_vector);
508 }
510 NR::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
511 NR::Point const p2_on_cl = p1_on_cl + direction_vector;
513 // The intersection point of the constraint line with any path,
514 // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
515 // The distance between those points is twice the snapping tolerance
516 NR::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
517 NR::Point const p_min_on_cl = desktop->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);
518 NR::Point const p_max_on_cl = desktop->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
520 Geom::Path cl;
521 cl.start(p_min_on_cl.to_2geom());
522 cl.appendNew<Geom::LineSegment>(p_max_on_cl.to_2geom());
524 for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
525 if (*k) {
526 // convert a Path object (see src/livarot/Path.h) to a 2geom's path object (see 2geom/path.h)
527 // TODO: (Diederik) Only do this once for the first point, needs some storage of pointers in a member variable
528 std::vector<Geom::Path> path_2geom = BPath_to_2GeomPath(*k);
530 for (std::vector<Geom::Path>::const_iterator l = path_2geom.begin(); l != path_2geom.end(); l++) {
531 Geom::SimpleCrosser sxr;
532 Geom::Crossings crossings = sxr.crossings(*l, cl);
533 for (std::vector<Geom::Crossing>::const_iterator m = crossings.begin(); m != crossings.end(); m++) {
534 // Reconstruct the point of intersection
535 NR::Point p_inters = p_min_on_cl + ((*m).tb) * (p_max_on_cl - p_min_on_cl);
536 // When it's within snapping range, then return it
537 // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < tb < 1)
538 if ((*m).tb >= 0 && (*m).tb <= 1 ) {
539 NR::Coord dist = NR::L2(desktop->dt2doc(p_proj_on_cl) - p_inters);
540 SnappedPoint s(desktop->doc2dt(p_inters), dist, getSnapperTolerance(), getSnapperAlwaysSnap());
541 sc.points.push_back(s);
542 }
543 }
544 }
545 }
546 }
547 }
550 void Inkscape::ObjectSnapper::_doFreeSnap(SnappedConstraints &sc,
551 Inkscape::Snapper::PointType const &t,
552 NR::Point const &p,
553 bool const &first_point,
554 std::vector<NR::Point> &points_to_snap,
555 std::list<SPItem const *> const &it,
556 std::vector<NR::Point> *unselected_nodes) const
557 {
558 if ( NULL == _named_view ) {
559 return;
560 }
562 /* Get a list of all the SPItems that we will try to snap to */
563 if (first_point) {
564 _findCandidates(sp_document_root(_named_view->document), it, first_point, points_to_snap, TRANSL_SNAP_XY);
565 }
567 if (_snap_to_itemnode || _snap_to_bboxnode) {
568 _snapNodes(sc, t, p, first_point, unselected_nodes);
569 }
571 // Consider the page's border for snapping to
572 NArtBpath *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;
574 if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
575 unsigned n = (unselected_nodes == NULL) ? 0 : unselected_nodes->size();
576 if (n > 0) {
577 /* While editing a path in the node tool, findCandidates must ignore that path because
578 * of the node snapping requirements (i.e. only unselected nodes must be snapable).
579 * That path must not be ignored however when snapping to the paths, so we add it here
580 * manually when applicable
581 */
582 g_assert(it.size() == 1);
583 g_assert(SP_IS_PATH(*it.begin()));
584 _snapPaths(sc, t, p, first_point, unselected_nodes, SP_PATH(*it.begin()), border_bpath);
585 } else {
586 _snapPaths(sc, t, p, first_point, NULL, NULL, border_bpath);
587 }
588 }
589 }
591 void Inkscape::ObjectSnapper::_doConstrainedSnap( SnappedConstraints &sc,
592 Inkscape::Snapper::PointType const &t,
593 NR::Point const &p,
594 bool const &first_point,
595 std::vector<NR::Point> &points_to_snap,
596 ConstraintLine const &c,
597 std::list<SPItem const *> const &it) const
598 {
599 if ( NULL == _named_view ) {
600 return;
601 }
603 /* Get a list of all the SPItems that we will try to snap to */
604 if (first_point) {
605 _findCandidates(sp_document_root(_named_view->document), it, first_point, points_to_snap, TRANSL_SNAP_XY);
606 }
608 // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
609 // This is usefull for example when scaling an object while maintaining a fixed aspect ratio. It's
610 // nodes are only allowed to move in one direction (i.e. in one degree of freedom).
612 // When snapping to objects, we either snap to their nodes or their paths. It is however very
613 // unlikely that any node will be exactly at the constrained line, so for a constrained snap
614 // to objects we will only consider the object's paths. Beside, the nodes will be at these paths,
615 // so we will more or less snap to them anyhow.
617 if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
618 _snapPathsConstrained(sc, t, p, first_point, c);
619 }
620 }
623 // This method is used to snap a guide to nodes, while dragging the guide around
624 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
625 NR::Point const &p,
626 NR::Point const &guide_normal) const
627 {
628 if ( NULL == _named_view ) {
629 return;
630 }
632 /* Get a list of all the SPItems that we will try to snap to */
633 std::vector<SPItem*> cand;
634 std::list<SPItem const *> const it; //just an empty list
636 std::vector<NR::Point> points_to_snap;
637 points_to_snap.push_back(p);
639 DimensionToSnap snap_dim;
640 if (guide_normal == component_vectors[NR::Y]) {
641 snap_dim = GUIDE_TRANSL_SNAP_Y;
642 } else if (guide_normal == component_vectors[NR::X]) {
643 snap_dim = GUIDE_TRANSL_SNAP_X;
644 } else {
645 snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
646 }
647 // We don't support ANGLED_GUIDE_ROT_SNAP yet.
649 // It would be cool to allow the user to rotate a guide by dragging it, instead of
650 // only translating it. (For example when CTRL is pressed). We will need an UI part
651 // for that first; and some important usability choices need to be made:
652 // E.g. which point should be used for pivoting? A previously snapped point,
653 // or a transformation center (which can be moved after clicking for the
654 // second time on an object; but should this point then be constrained to the
655 // line, or can it be located anywhere?)
657 _findCandidates(sp_document_root(_named_view->document), it, true, points_to_snap, snap_dim);
658 _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal);
660 // _snapRotatingGuideToNodes has not been implemented yet.
661 }
663 /**
664 * \return true if this Snapper will snap at least one kind of point.
665 */
666 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
667 {
668 bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
669 return (_snap_enabled && _snap_from != 0 && snap_to_something);
670 }
672 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
673 {
674 bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
675 return (_snap_enabled && (_snap_from & SNAPPOINT_GUIDE) && snap_to_something);
676 }
678 void Inkscape::ObjectSnapper::_clear_paths() const
679 {
680 for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
681 g_free(*k);
682 }
683 _bpaths_to_snap_to->clear();
685 for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
686 delete *k;
687 }
688 _paths_to_snap_to->clear();
689 }
691 NArtBpath* Inkscape::ObjectSnapper::_getBorderBPath() const
692 {
693 NArtBpath *border_bpath = NULL;
694 NR::Rect const border_rect = NR::Rect(NR::Point(0,0), NR::Point(sp_document_width(_named_view->document),sp_document_height(_named_view->document)));
695 SPCurve const *border_curve = sp_curve_new_from_rect(border_rect);
696 if (border_curve) {
697 border_bpath = SP_CURVE_BPATH(border_curve);
698 }
700 return border_bpath;
701 }
702 /*
703 Local Variables:
704 mode:c++
705 c-file-style:"stroustrup"
706 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
707 indent-tabs-mode:nil
708 fill-column:99
709 End:
710 */
711 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :