Code

complete adding const to have only NArtBpath const * get_bpath() const; for accessing...
[inkscape.git] / src / object-snapper.cpp
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 "libnr/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 bbox_to_snap Bounding box hulling 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::vector<SPItem const *> const *it,
69                                               bool const &first_point,
70                                               NR::Rect const &bbox_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     
76     if (!(c1 || c2)) {
77         return;        
78     }
79     
80     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;
82     if (first_point) {
83         _candidates->clear();
84     }
85     
86     NR::Maybe<NR::Rect> bbox_of_item = NR::Rect(); // a default NR::Rect is infinitely large
87     NR::Rect bbox_to_snap_incl = bbox_to_snap; // _incl means: will include the snapper tolerance
88     bbox_to_snap_incl.growBy(getSnapperTolerance()); // see?
89     
90     for (SPObject* o = sp_object_first_child(r); o != NULL; o = SP_OBJECT_NEXT(o)) {
91         if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !desktop->itemIsHidden(SP_ITEM(o))) {
93             /* See if this item is on the ignore list */
94             std::vector<SPItem const *>::const_iterator i;
95             if (it != NULL) {
96                 i = it->begin();
97                 while (i != it->end() && *i != o) {
98                     i++;
99                 }
100             }
102             if (it == NULL || i == it->end()) {
103                 /* See if the item is within range */
104                 if (SP_IS_GROUP(o)) {
105                     _findCandidates(o, it, false, bbox_to_snap, snap_dim);
106                 } else {
107                     bbox_of_item = sp_item_bbox_desktop(SP_ITEM(o));
108                     if (bbox_of_item) {
109                         if (bbox_to_snap_incl.intersects(*bbox_of_item)) {
110                             //This item is within snapping range, so record it as a candidate
111                             _candidates->push_back(SP_ITEM(o));
112                         }
113                     }
114                 }
115             }
116         }
117     }
121 void Inkscape::ObjectSnapper::_collectNodes(Inkscape::Snapper::PointType const &t,
122                                          bool const &first_point) const
124     // Now, let's first collect all points to snap to. If we have a whole bunch of points to snap,
125     // e.g. when translating an item using the selector tool, then we will only do this for the
126     // first point and store the collection for later use. This significantly improves the performance
127     if (first_point) {
128         _points_to_snap_to->clear();
129         
130          // Determine the type of bounding box we should snap to
131         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
132         
133         bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
134         bool p_is_a_bbox = t & Inkscape::Snapper::SNAPPOINT_BBOX;
135         bool p_is_a_guide = t & Inkscape::Snapper::SNAPPOINT_GUIDE;
136         
137         // A point considered for snapping should be either a node, a bbox corner or a guide. Pick only ONE!
138         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));        
139         
140         if (_snap_to_bboxnode) {
141             int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
142             bbox_type = (prefs_bbox ==0)? 
143                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
144         }
146         for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
147             //NR::Matrix i2doc(NR::identity());
148             SPItem *root_item = *i;
149             if (SP_IS_USE(*i)) {
150                 root_item = sp_use_root(SP_USE(*i));
151             }
152             g_return_if_fail(root_item);
154             //Collect all nodes so we can snap to them
155             if (_snap_to_itemnode) {
156                 if (!(_strict_snapping && !p_is_a_node) || p_is_a_guide) {
157                     sp_item_snappoints(root_item, _include_item_center, SnapPointsIter(*_points_to_snap_to));
158                 }
159             }
161             //Collect the bounding box's corners so we can snap to them
162             if (_snap_to_bboxnode) {
163                 if (!(_strict_snapping && !p_is_a_bbox) || p_is_a_guide) {
164                     NR::Maybe<NR::Rect> b = sp_item_bbox_desktop(root_item, bbox_type);
165                     if (b) {
166                         for ( unsigned k = 0 ; k < 4 ; k++ ) {
167                             _points_to_snap_to->push_back(b->corner(k));
168                         }
169                     }
170                 }
171             }
172         }
173     }
176 void Inkscape::ObjectSnapper::_snapNodes(SnappedConstraints &sc,
177                                          Inkscape::Snapper::PointType const &t,
178                                          NR::Point const &p,
179                                          bool const &first_point,
180                                          std::vector<NR::Point> *unselected_nodes) const
182     // Iterate through all nodes, find out which one is the closest to p, and snap to it!
183     
184     _collectNodes(t, first_point);
185     
186     if (unselected_nodes != NULL) {
187         _points_to_snap_to->insert(_points_to_snap_to->end(), unselected_nodes->begin(), unselected_nodes->end());
188     }   
189     
190     SnappedPoint s;
191     bool success = false;
192     
193     for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
194         NR::Coord dist = NR::L2(*k - p);        
195         if (dist < getSnapperTolerance() && dist < s.getDistance()) {
196             s = SnappedPoint(*k, SNAPTARGET_NODE, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
197             success = true;
198         }
199     }
201     if (success) {
202         sc.points.push_back(s); 
203     }
206 void Inkscape::ObjectSnapper::_snapTranslatingGuideToNodes(SnappedConstraints &sc,
207                                          Inkscape::Snapper::PointType const &t,
208                                          NR::Point const &p,
209                                          NR::Point const &guide_normal) const
211     // Iterate through all nodes, find out which one is the closest to this guide, and snap to it!
212     _collectNodes(t, true);
213     
214     SnappedPoint s;
215     bool success = false;
216     
217     NR::Coord tol = getSnapperTolerance();
218     
219     for (std::vector<NR::Point>::const_iterator k = _points_to_snap_to->begin(); k != _points_to_snap_to->end(); k++) {
220         // Project each node (*k) on the guide line (running through point p)
221         NR::Point p_proj = project_on_linesegment(*k, p, p + NR::rot90(guide_normal));
222         NR::Coord dist = NR::L2(*k - p_proj); // distance from node to the guide         
223         NR::Coord dist2 = NR::L2(p - p_proj); // distance from projection of node on the guide, to the mouse location
224         if ((dist < tol && dist2 < tol || getSnapperAlwaysSnap()) && dist < s.getDistance()) {
225             s = SnappedPoint(*k, SNAPTARGET_NODE, dist, tol, getSnapperAlwaysSnap());
226             success = true;
227         }
228     }
230     if (success) {
231         sc.points.push_back(s); 
232     }
235 /**
236  * Returns index of first NR_END bpath in array.
237  */
238 static unsigned sp_bpath_length(NArtBpath const bpath[])
240     g_return_val_if_fail(bpath != NULL, FALSE);
241     unsigned ret = 0;
242     while ( bpath[ret].code != NR_END ) {
243         ++ret;
244     }
245     ++ret;
246     return ret;
249 void Inkscape::ObjectSnapper::_collectPaths(Inkscape::Snapper::PointType const &t,
250                                          bool const &first_point,
251                                          NArtBpath const *border_bpath) const
253     // Now, let's first collect all paths to snap to. If we have a whole bunch of points to snap,
254     // e.g. when translating an item using the selector tool, then we will only do this for the
255     // first point and store the collection for later use. This significantly improves the performance
256     if (first_point) {
257         _clear_paths();
258         
259         // Determine the type of bounding box we should snap to
260         SPItem::BBoxType bbox_type = SPItem::GEOMETRIC_BBOX;
261         
262         bool p_is_a_node = t & Inkscape::Snapper::SNAPPOINT_NODE;
263         
264         if (_snap_to_bboxpath) {
265             int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
266             bbox_type = (prefs_bbox ==0)? 
267                 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
268         }
269         
270         // Consider the page border for snapping
271         if (border_bpath != NULL) {
272             // make our own copy of the const*
273             NArtBpath *new_bpath;
274             unsigned const len = sp_bpath_length(border_bpath);
275             new_bpath = g_new(NArtBpath, len);
276             memcpy(new_bpath, border_bpath, len * sizeof(NArtBpath));
278             _bpaths_to_snap_to->push_back(new_bpath);    
279         }
280         
281         for (std::vector<SPItem*>::const_iterator i = _candidates->begin(); i != _candidates->end(); i++) {
283             /* Transform the requested snap point to this item's coordinates */
284             NR::Matrix i2doc(NR::identity());
285             SPItem *root_item = NULL;
286             /* We might have a clone at hand, so make sure we get the root item */
287             if (SP_IS_USE(*i)) {
288                 i2doc = sp_use_get_root_transform(SP_USE(*i));
289                 root_item = sp_use_root(SP_USE(*i));
290                 g_return_if_fail(root_item);
291             } else {
292                 i2doc = sp_item_i2doc_affine(*i);
293                 root_item = *i;
294             }
296             //Build a list of all paths considered for snapping to
298             //Add the item's path to snap to
299             if (_snap_to_itempath) {
300                 if (!(_strict_snapping && !p_is_a_node)) {
301                     // Snapping to the path of characters is very cool, but for a large
302                     // chunk of text this will take ages! So limit snapping to text paths
303                     // containing max. 240 characters. Snapping the bbox will not be affected
304                     bool very_lenghty_prose = false;
305                     if (SP_IS_TEXT(root_item) || SP_IS_FLOWTEXT(root_item)) {
306                         very_lenghty_prose =  sp_text_get_length(SP_TEXT(root_item)) > 240;
307                     }
308                     // On my AMD 3000+, the snapping lag becomes annoying at approx. 240 chars
309                     // which corresponds to a lag of 500 msec. This is for snapping a rect
310                     // to a single line of text.
312                     // Snapping for example to a traced bitmap is also very stressing for
313                     // the CPU, so we'll only snap to paths having no more than 500 nodes
314                     // This also leads to a lag of approx. 500 msec (in my lousy test set-up).
315                     bool very_complex_path = false;
316                     if (SP_IS_PATH(root_item)) {
317                         very_complex_path = sp_nodes_in_path(SP_PATH(root_item)) > 500;
318                     }
320                     if (!very_lenghty_prose && !very_complex_path) {
321                         SPCurve *curve = curve_for_item(root_item); 
322                         if (curve) {
323                             NArtBpath *bpath = bpath_for_curve(root_item, curve, true, true);
324                             _bpaths_to_snap_to->push_back(bpath); // we will get a dupe of the path, which must be freed at some point
325                             curve->unref();
326                         }
327                     }
328                 }
329             }
331             //Add the item's bounding box to snap to
332             if (_snap_to_bboxpath) {
333                 if (!(_strict_snapping && p_is_a_node)) {
334                     NRRect rect;
335                     sp_item_invoke_bbox(root_item, &rect, i2doc, TRUE, bbox_type);
336                     NArtBpath *bpath = nr_path_from_rect(rect);
337                     _bpaths_to_snap_to->push_back(bpath);
338                 }
339             }
340         }
341     }
343     
344 void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc,
345                                      Inkscape::Snapper::PointType const &t,
346                                      NR::Point const &p,
347                                      bool const &first_point,
348                                      std::vector<NR::Point> *unselected_nodes,
349                                      SPPath const *selected_path,
350                                      NArtBpath const *border_bpath) const
352     _collectPaths(t, first_point, border_bpath);
353     // Now we can finally do the real snapping, using the paths collected above
354     SnappedPoint s;
355     bool success = false;
356     
357     /* FIXME: this seems like a hack.  Perhaps Snappers should be
358     ** in SPDesktop rather than SPNamedView?
359     */
360     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
361     NR::Point const p_doc = desktop->dt2doc(p);    
362     
363     bool const node_tool_active = _snap_to_itempath && selected_path != NULL;
364     
365     if (first_point) {
366         /* While editing a path in the node tool, findCandidates must ignore that path because 
367          * of the node snapping requirements (i.e. only unselected nodes must be snapable).
368          * This path must not be ignored however when snapping to the paths, so we add it here
369          * manually when applicable. 
370          * 
371          * Note that this path must be the last in line!
372          * */        
373         if (node_tool_active) {        
374             SPCurve *curve = curve_for_item(SP_ITEM(selected_path)); 
375             if (curve) {
376                 NArtBpath *bpath = bpath_for_curve(SP_ITEM(selected_path), curve, true, true);
377                 _bpaths_to_snap_to->push_back(bpath); // we will get a dupe of the path, which must be freed at some point
378                 curve->unref();
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     }
391     
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
396             
397             for (unsigned i = 1 ; i < (*k)->pts.size() ; i++) { 
398                 //pts describes a polyline approximation, which might consist of 1000s of points!
399                 NR::Point start_point;
400                 NR::Point end_point;   
401                 NR::Maybe<Path::cut_position> o = NR::Nothing();                 
402                 if (being_edited) { 
403                     /* If the path is being edited, then we will try to snap to each piece of the 
404                      * path individually. We should only snap though to stationary pieces of the paths
405                      * and not to the pieces that are being dragged around. This way we avoid 
406                      * self-snapping. For this we check whether the nodes at both ends of the current
407                      * piece are unselected; if they are then this piece must be stationary 
408                      */                    
409                     unsigned piece = (*k)->pts[i].piece; 
410                     // Example: a cubic spline is a single piece within a path; it will be drawn using
411                     // a polyline, which is described by the collection of lines between the points pts[i] 
412                     (*k)->PointAt(piece, 0, start_point);
413                     (*k)->PointAt(piece, 1, end_point);
414                     start_point = desktop->doc2dt(start_point);
415                     end_point = desktop->doc2dt(end_point);
416                     g_assert(unselected_nodes != NULL);
417                     bool c1 = isUnselectedNode(start_point, unselected_nodes);
418                     bool c2 = isUnselectedNode(end_point, unselected_nodes);     
419                     if (c1 && c2) {
420                         o = get_nearest_position_on_Path(*k, p_doc, i);
421                     }
422                 } else {
423                     /* If the path is NOT being edited, then we will try to snap to the path as a
424                      * whole, so we need to do this only once and we will break out at the end of
425                      * this for-loop iteration */                    
426                     /* Look for the nearest position on this SPItem to our snap point */
427                     o = get_nearest_position_on_Path(*k, p_doc);
428                     (*k)->PointAt(o->piece, 0, start_point);
429                     (*k)->PointAt(o->piece, 1, end_point);
430                     start_point = desktop->doc2dt(start_point);
431                     end_point = desktop->doc2dt(end_point);
432                 }
433                 
434                 if (o && o->t >= 0 && o->t <= 1) {    
435                     /* Convert the nearest point back to desktop coordinates */
436                     NR::Point const o_it = get_point_on_Path(*k, o->piece, o->t);
437                     
438                     /* IF YOU'RE BUG HUNTING: IT LOOKS LIKE get_point_on_Path SOMETIMES
439                      * RETURNS THE WRONG POINT (WITH AN OFFSET, BUT STILL ON THE PATH.
440                      * THIS BUG WILL NO LONGER BE ENCOUNTERED ONCE WE'VE SWITCHED TO 
441                      * 2GEOM FOR THE OBJECT SNAPPER
442                      */
443                      
444                     NR::Point const o_dt = desktop->doc2dt(o_it);                
445                     NR::Coord const dist = NR::L2(o_dt - p);
446     
447                     if (dist < getSnapperTolerance()) {
448                         // if we snap to a straight line segment (within a path), then return this line segment
449                         if ((*k)->IsLineSegment(o->piece)) {
450                             sc.lines.push_back(Inkscape::SnappedLineSegment(o_dt, dist, getSnapperTolerance(), getSnapperAlwaysSnap(), start_point, end_point));    
451                         } else {                
452                             // for segments other than straight lines of a path, we'll return just the closest snapped point
453                             if (dist < s.getDistance()) {
454                                 s = SnappedPoint(o_dt, SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
455                                 success = true;
456                             }
457                         }
458                     }
459                 }        
460                 
461                 // If the path is NOT being edited, then we will try to snap to the path as a whole
462                 // so we need to do this only once
463                 if (!being_edited) break;
464             }
465         }
466     }
468     if (success) {
469         sc.points.push_back(s); 
470     }
473 /* Returns true if point is coincident with one of the unselected nodes */ 
474 bool Inkscape::ObjectSnapper::isUnselectedNode(NR::Point const &point, std::vector<NR::Point> const *unselected_nodes) const
476     if (unselected_nodes == NULL) {
477         return false;
478     }
479     
480     if (unselected_nodes->size() == 0) {
481         return false;
482     }
483     
484     for (std::vector<NR::Point>::const_iterator i = unselected_nodes->begin(); i != unselected_nodes->end(); i++) {
485         if (NR::L2(point - *i) < 1e-4) {
486             return true;
487         }   
488     }  
489     
490     return false;    
493 void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc,
494                                      Inkscape::Snapper::PointType const &t,
495                                      NR::Point const &p,
496                                      bool const &first_point,
497                                      ConstraintLine const &c) const
499     
500     // Consider the page's border for snapping to
501     NArtBpath const *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;
502     
503     _collectPaths(t, first_point, border_bpath);
504     
505     // Now we can finally do the real snapping, using the paths collected above
506     
507     /* FIXME: this seems like a hack.  Perhaps Snappers should be
508     ** in SPDesktop rather than SPNamedView?
509     */
510     SPDesktop const *desktop = SP_ACTIVE_DESKTOP;    
511     NR::Point const p_doc = desktop->dt2doc(p);    
512     
513     NR::Point direction_vector = c.getDirection();
514     if (!is_zero(direction_vector)) {
515         direction_vector = NR::unit_vector(direction_vector);
516     }
517     
518     NR::Point const p1_on_cl = c.hasPoint() ? c.getPoint() : p;
519     NR::Point const p2_on_cl = p1_on_cl + direction_vector;
520     
521     // The intersection point of the constraint line with any path, 
522     // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl
523     // The distance between those points is twice the snapping tolerance
524     NR::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl);
525     NR::Point const p_min_on_cl = desktop->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector);    
526     NR::Point const p_max_on_cl = desktop->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector);
527     
528     Geom::Path cl;
529     std::vector<Geom::Path> clv;    
530     cl.start(p_min_on_cl.to_2geom());
531     cl.appendNew<Geom::LineSegment>(p_max_on_cl.to_2geom());
532     clv.push_back(cl);
533     
534     for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
535         if (*k) {                        
536             // convert a Path object (see src/livarot/Path.h) to a 2geom's path object (see 2geom/path.h)
537             // TODO: (Diederik) Only do this once for the first point, needs some storage of pointers in a member variable
538             std::vector<Geom::Path> path_2geom = BPath_to_2GeomPath(*k);  
539             
540             Geom::CrossingSet cs = Geom::crossings(clv, path_2geom);
541             if (cs.size() > 0) {
542                 // We need only the first element of cs, because cl is only a single straight linesegment
543                 // This first element contains a vector filled with crossings of cl with path_2geom
544                 for (std::vector<Geom::Crossing>::const_iterator m = cs[0].begin(); m != cs[0].end(); m++) {                    
545                     if ((*m).ta >= 0 && (*m).ta <= 1 ) {
546                         // Reconstruct the point of intersection
547                         NR::Point p_inters = p_min_on_cl + ((*m).ta) * (p_max_on_cl - p_min_on_cl);
548                         // When it's within snapping range, then return it
549                         // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < ta < 1)                        
550                         NR::Coord dist = NR::L2(desktop->dt2doc(p_proj_on_cl) - p_inters);
551                         SnappedPoint s(desktop->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap());
552                         sc.points.push_back(s);    
553                     }  
554                 } 
555             }
556         }
557     }
561 void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc,
562                                             Inkscape::Snapper::PointType const &t,
563                                             NR::Point const &p,
564                                             bool const &first_point,
565                                             NR::Maybe<NR::Rect> const &bbox_to_snap,
566                                             std::vector<SPItem const *> const *it,
567                                             std::vector<NR::Point> *unselected_nodes) const
569     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
570         return;
571     }
573     /* Get a list of all the SPItems that we will try to snap to */
574     if (first_point) {
575         NR::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : NR::Rect(p, p);
576         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY);
577     }
578     
579     if (_snap_to_itemnode || _snap_to_bboxnode) {
580         _snapNodes(sc, t, p, first_point, unselected_nodes);
581     }
582     
583     // Consider the page's border for snapping to
584     NArtBpath const *border_bpath = _snap_to_page_border ? _getBorderBPath() : NULL;   
585     
586     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
587         unsigned n = (unselected_nodes == NULL) ? 0 : unselected_nodes->size();
588         if (n > 0) {
589             /* While editing a path in the node tool, findCandidates must ignore that path because 
590              * of the node snapping requirements (i.e. only unselected nodes must be snapable).
591              * That path must not be ignored however when snapping to the paths, so we add it here
592              * manually when applicable
593              */            
594             SPPath *path = NULL;
595             if (it != NULL) {
596                 g_assert(SP_IS_PATH(*it->begin()));
597                 g_assert(it->size() == 1);
598                 path = SP_PATH(*it->begin());
599             }
600             _snapPaths(sc, t, p, first_point, unselected_nodes, path, border_bpath);                
601         } else {
602             _snapPaths(sc, t, p, first_point, NULL, NULL, border_bpath);   
603         }        
604     }
607 void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc,
608                                                   Inkscape::Snapper::PointType const &t,
609                                                   NR::Point const &p,
610                                                   bool const &first_point,
611                                                   NR::Maybe<NR::Rect> const &bbox_to_snap,
612                                                   ConstraintLine const &c,
613                                                   std::vector<SPItem const *> const *it) const
615     if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) {
616         return;
617     }
619     /* Get a list of all the SPItems that we will try to snap to */
620     if (first_point) {
621         NR::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : NR::Rect(p, p);
622         _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY);
623     }
624     
625     // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line).
626     // This is usefull for example when scaling an object while maintaining a fixed aspect ratio. It's
627     // nodes are only allowed to move in one direction (i.e. in one degree of freedom).
628     
629     // When snapping to objects, we either snap to their nodes or their paths. It is however very
630     // unlikely that any node will be exactly at the constrained line, so for a constrained snap
631     // to objects we will only consider the object's paths. Beside, the nodes will be at these paths,
632     // so we will more or less snap to them anyhow.   
634     if (_snap_to_itempath || _snap_to_bboxpath || _snap_to_page_border) {
635         _snapPathsConstrained(sc, t, p, first_point, c);
636     }
640 // This method is used to snap a guide to nodes, while dragging the guide around
641 void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc,
642                                                                                 NR::Point const &p,
643                                             NR::Point const &guide_normal) const
645     if ( NULL == _named_view ) {
646         return;
647     }
648     
649     /* Get a list of all the SPItems that we will try to snap to */
650     std::vector<SPItem*> cand;
651     std::vector<SPItem const *> const it; //just an empty list
652     
653     DimensionToSnap snap_dim;
654     if (guide_normal == component_vectors[NR::Y]) {
655         snap_dim = GUIDE_TRANSL_SNAP_Y;
656     } else if (guide_normal == component_vectors[NR::X]) {
657         snap_dim = GUIDE_TRANSL_SNAP_X;
658     } else {
659         snap_dim = ANGLED_GUIDE_TRANSL_SNAP;
660     }
661     
662     // We don't support ANGLED_GUIDE_ROT_SNAP yet. 
663     
664     // It would be cool to allow the user to rotate a guide by dragging it, instead of
665     // only translating it. (For example when CTRL is pressed). We will need an UI part 
666     // for that first; and some important usability choices need to be made: 
667     // E.g. which point should be used for pivoting? A previously snapped point,
668     // or a transformation center (which can be moved after clicking for the
669     // second time on an object; but should this point then be constrained to the
670     // line, or can it be located anywhere?)
671     
672     _findCandidates(sp_document_root(_named_view->document), &it, true, NR::Rect(p, p), snap_dim);
673         _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal);
674     
675     // _snapRotatingGuideToNodes has not been implemented yet. 
678 /**
679  *  \return true if this Snapper will snap at least one kind of point.
680  */
681 bool Inkscape::ObjectSnapper::ThisSnapperMightSnap() const
683     bool snap_to_something = _snap_to_itempath || _snap_to_itemnode || _snap_to_bboxpath || _snap_to_bboxnode || _snap_to_page_border;
684     return (_snap_enabled && _snap_from != 0 && snap_to_something);
687 bool Inkscape::ObjectSnapper::GuidesMightSnap() const
689     bool snap_to_something = _snap_to_itemnode || _snap_to_bboxnode;
690     return (_snap_enabled && (_snap_from & SNAPPOINT_GUIDE) && snap_to_something);
693 void Inkscape::ObjectSnapper::_clear_paths() const 
695     for (std::vector<NArtBpath*>::const_iterator k = _bpaths_to_snap_to->begin(); k != _bpaths_to_snap_to->end(); k++) {
696         g_free(*k);
697     }
698     _bpaths_to_snap_to->clear();
699     
700     for (std::vector<Path*>::const_iterator k = _paths_to_snap_to->begin(); k != _paths_to_snap_to->end(); k++) {
701         delete *k;    
702     }
703     _paths_to_snap_to->clear();
706 NArtBpath const* Inkscape::ObjectSnapper::_getBorderBPath() const
708     NArtBpath const *border_bpath = NULL;
709     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)));
710     SPCurve const *border_curve = SPCurve::new_from_rect(border_rect);
711     if (border_curve) {
712         border_bpath = SP_CURVE_BPATH(border_curve); 
713     }
714         
715     return border_bpath;
717 /*
718   Local Variables:
719   mode:c++
720   c-file-style:"stroustrup"
721   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
722   indent-tabs-mode:nil
723   fill-column:99
724   End:
725 */
726 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :