Code

17d7b7137467cf93615d3f2f790de9e0d060a77d
[inkscape.git] / src / snapper.cpp
1 /**
2  *  \file src/snapper.cpp
3  *  \brief Snapper class.
4  *
5  *  Authors:
6  *    Carl Hetherington <inkscape@carlh.net>
7  *
8  *  Released under GNU GPL, read the file 'COPYING' for more information.
9  */
11 #include "libnr/nr-values.h"
12 #include "sp-namedview.h"
13 #include "inkscape.h"
14 #include "desktop.h"
16 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAPPOINT_BBOX = 0x1;
17 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAPPOINT_NODE = 0x2;
18 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAPPOINT_GUIDE = 0x4;
20 /**
21  *  Construct new Snapper for named view.
22  *  \param nv Named view.
23  *  \param d Snap distance.
24  */
25 Inkscape::Snapper::Snapper(SPNamedView const *nv, NR::Coord const d) : _named_view(nv), _enabled(true), _distance(d)
26 {
27     g_assert(_named_view != NULL);
28     g_assert(SP_IS_NAMEDVIEW(_named_view));
30     setSnapFrom(SNAPPOINT_BBOX | SNAPPOINT_NODE, true); //Snap any point. In v0.45 and earlier, this was controlled in the preferences tab
31 }
33 /**
34  *  Set snap distance.
35  *  \param d New snap distance (desktop coordinates)
36  */
37 void Inkscape::Snapper::setDistance(NR::Coord const d)
38 {
39     _distance = d;
40 }
42 /**
43  *  \return Snap distance (desktop coordinates); depends on current zoom so that it's always the same in screen pixels
44  */
45 NR::Coord Inkscape::Snapper::getDistance() const
46 {
47     return _distance / SP_ACTIVE_DESKTOP->current_zoom();
48 }
50 /**
51  *  Turn on/off snapping of specific point types.
52  *  \param t Point type.
53  *  \param s true to snap to this point type, otherwise false;
54  */
55 void Inkscape::Snapper::setSnapFrom(PointType t, bool s)
56 {
57     if (s) {
58         _snap_from |= t;
59     } else {
60         _snap_from &= ~t;
61     }
62 }
64 /**
65  *  \param t Point type.
66  *  \return true if snapper will snap this type of point, otherwise false.
67  */
68 bool Inkscape::Snapper::getSnapFrom(PointType t) const
69 {
70     return (_snap_from & t);
71 }
73 /**
74  *  \param s true to enable this snapper, otherwise false.
75  */
77 void Inkscape::Snapper::setEnabled(bool s)
78 {
79     _enabled = s;
80 }
83 /**
84  *  Try to snap a point to whatever this snapper is interested in.  Any
85  *  snap that occurs will be to the nearest "interesting" thing (e.g. a
86  *  grid or guide line)
87  *
88  *  \param t Point type.
89  *  \param p Point to snap (desktop coordinates).
90  *  \param it Item that should not be snapped to.
91  *  \return Snapped point.
92  */
94 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType const &t,
95                                                    NR::Point const &p,
96                                                    bool const &first_point,
97                                                    std::vector<NR::Point> &points_to_snap,                                               
98                                                    SPItem const *it) const
99 {
100     std::list<SPItem const *> lit;
101     lit.push_back(it);
102     return freeSnap(t, p, first_point, points_to_snap, lit);
106 /**
107  *  Try to snap a point to whatever this snapper is interested in.  Any
108  *  snap that occurs will be to the nearest "interesting" thing (e.g. a
109  *  grid or guide line)
110  *
111  *  \param t Point type.
112  *  \param p Point to snap (desktop coordinates).
113  *  \param it Items that should not be snapped to.
114  *  \return Snapped point.
115  */
117 Inkscape::SnappedPoint Inkscape::Snapper::freeSnap(PointType const &t,
118                                                    NR::Point const &p,
119                                                    bool const &first_point,
120                                                    std::vector<NR::Point> &points_to_snap,                                       
121                                                    std::list<SPItem const *> const &it) const
123     if (_enabled == false || getSnapFrom(t) == false) {
124         return SnappedPoint(p, NR_HUGE);
125     }
127     return _doFreeSnap(t, p, first_point, points_to_snap, it);
133 /**
134  *  Try to snap a point to whatever this snapper is interested in, where
135  *  the snap point is constrained to lie along a specified vector from the
136  *  original point.
137  *
138  *  \param p Point to snap (desktop coordinates).
139  *  \param c Vector to constrain the snap to.
140  *  \param it Items that should not be snapped to.
141  *  \return Snapped point.
142  */
144 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType const &t,
145                                                           NR::Point const &p,
146                                                           bool const &first_point,
147                                                                   std::vector<NR::Point> &points_to_snap,
148                                                           ConstraintLine const &c,
149                                                           SPItem const *it) const
151     std::list<SPItem const *> lit;
152     lit.push_back(it);
153     return constrainedSnap(t, p, first_point, points_to_snap, c, lit);
157 /**
158  *  Try to snap a point to whatever this snapper is interested in, where
159  *  the snap point is constrained to lie along a specified vector from the
160  *  original point.
161  *
162  *  \param p Point to snap (desktop coordinates).
163  *  \param c Vector to constrain the snap to.
164  *  \param it Items that should not be snapped to.
165  *  \return Snapped point.
166  */
168 Inkscape::SnappedPoint Inkscape::Snapper::constrainedSnap(PointType const &t,
169                                                           NR::Point const &p,
170                                                           bool const &first_point,
171                                                                   std::vector<NR::Point> &points_to_snap,                                                
172                                                           ConstraintLine const &c,
173                                                           std::list<SPItem const *> const &it) const
175     if (_enabled == false || getSnapFrom(t) == false) {
176         return SnappedPoint(p, NR_HUGE);
177     }
179     return _doConstrainedSnap(t, p, first_point, points_to_snap, c, it);
182 /*
183   Local Variables:
184   mode:c++
185   c-file-style:"stroustrup"
186   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
187   indent-tabs-mode:nil
188   fill-column:99
189   End:
190 */
191 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :