1 /**
2 * \file src/snapper.cpp
3 * \brief Snapper class.
4 *
5 * Authors:
6 * Carl Hetherington <inkscape@carlh.net>
7 * Diederik van Lierop <mail@diedenrezi.nl>
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information.
10 */
12 #include "libnr/nr-values.h"
13 #include "sp-namedview.h"
14 #include "inkscape.h"
15 #include "desktop.h"
17 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAPPOINT_BBOX = 0x1;
18 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAPPOINT_NODE = 0x2;
19 Inkscape::Snapper::PointType const Inkscape::Snapper::SNAPPOINT_GUIDE = 0x4;
21 /**
22 * Construct new Snapper for named view.
23 * \param nv Named view.
24 * \param d Snap tolerance.
25 */
26 Inkscape::Snapper::Snapper(SPNamedView const *nv, NR::Coord const t) : _named_view(nv), _snap_enabled(true), _snapper_tolerance(t)
27 {
28 g_assert(_named_view != NULL);
29 g_assert(SP_IS_NAMEDVIEW(_named_view));
31 setSnapFrom(SNAPPOINT_BBOX | SNAPPOINT_NODE, true); //Snap any point. In v0.45 and earlier, this was controlled in the preferences tab
32 }
34 /**
35 * Set snap tolerance.
36 * \param d New snap tolerance (desktop coordinates)
37 */
38 void Inkscape::Snapper::setSnapperTolerance(NR::Coord const d)
39 {
40 _snapper_tolerance = d;
41 }
43 /**
44 * \return Snap tolerance (desktop coordinates); depends on current zoom so that it's always the same in screen pixels
45 */
46 NR::Coord Inkscape::Snapper::getSnapperTolerance() const
47 {
48 return _snapper_tolerance / SP_ACTIVE_DESKTOP->current_zoom();
49 }
51 bool Inkscape::Snapper::getSnapperAlwaysSnap() const
52 {
53 return _snapper_tolerance == 10000; //TODO: Replace this threshold of 10000 by a constant; see also tolerance-slider.cpp
54 }
56 /**
57 * Turn on/off snapping of specific point types.
58 * \param t Point type.
59 * \param s true to snap to this point type, otherwise false;
60 */
61 void Inkscape::Snapper::setSnapFrom(PointType t, bool s)
62 {
63 if (s) {
64 _snap_from |= t;
65 } else {
66 _snap_from &= ~t;
67 }
68 }
70 /**
71 * \param t Point type.
72 * \return true if snapper will snap this type of point, otherwise false.
73 */
74 bool Inkscape::Snapper::getSnapFrom(PointType t) const
75 {
76 return (_snap_from & t);
77 }
79 /**
80 * \param s true to enable this snapper, otherwise false.
81 */
83 void Inkscape::Snapper::setEnabled(bool s)
84 {
85 _snap_enabled = s;
86 }
89 /**
90 * Try to snap a point to whatever this snapper is interested in. Any
91 * snap that occurs will be to the nearest "interesting" thing (e.g. a
92 * grid or guide line)
93 *
94 * \param t Point type.
95 * \param p Point to snap (desktop coordinates).
96 * \param it Item that should not be snapped to.
97 * \return Snapped point.
98 */
100 void Inkscape::Snapper::freeSnap(SnappedConstraints &sc,
102 PointType const &t,
103 NR::Point const &p,
104 bool const &first_point,
105 std::vector<NR::Point> &points_to_snap,
106 SPItem const *it) const
107 {
108 std::list<SPItem const *> lit;
109 lit.push_back(it);
110 freeSnap(sc, t, p, first_point, points_to_snap, lit);
111 }
114 /**
115 * Try to snap a point to whatever this snapper is interested in. Any
116 * snap that occurs will be to the nearest "interesting" thing (e.g. a
117 * grid or guide line)
118 *
119 * \param t Point type.
120 * \param p Point to snap (desktop coordinates).
121 * \param it Items that should not be snapped to.
122 * \return Snapped point.
123 */
125 void Inkscape::Snapper::freeSnap(SnappedConstraints &sc,
127 PointType const &t,
128 NR::Point const &p,
129 bool const &first_point,
130 std::vector<NR::Point> &points_to_snap,
131 std::list<SPItem const *> const &it) const
132 {
133 if (_snap_enabled == false || getSnapFrom(t) == false) {
134 return;
135 }
137 _doFreeSnap(sc, t, p, first_point, points_to_snap, it);
138 }
143 /**
144 * Try to snap a point to whatever this snapper is interested in, where
145 * the snap point is constrained to lie along a specified vector from the
146 * original point.
147 *
148 * \param p Point to snap (desktop coordinates).
149 * \param c Vector to constrain the snap to.
150 * \param it Items that should not be snapped to.
151 * \return Snapped point.
152 */
154 void Inkscape::Snapper::constrainedSnap(SnappedConstraints &sc,
156 PointType const &t,
157 NR::Point const &p,
158 bool const &first_point,
159 std::vector<NR::Point> &points_to_snap,
160 ConstraintLine const &c,
161 SPItem const *it) const
162 {
163 std::list<SPItem const *> lit;
164 lit.push_back(it);
165 constrainedSnap(sc, t, p, first_point, points_to_snap, c, lit);
166 }
169 /**
170 * Try to snap a point to whatever this snapper is interested in, where
171 * the snap point is constrained to lie along a specified vector from the
172 * original point.
173 *
174 * \param p Point to snap (desktop coordinates).
175 * \param c Vector to constrain the snap to.
176 * \param it Items that should not be snapped to.
177 * \return Snapped point.
178 */
180 void Inkscape::Snapper::constrainedSnap(SnappedConstraints &sc,
181 PointType const &t,
182 NR::Point const &p,
183 bool const &first_point,
184 std::vector<NR::Point> &points_to_snap,
185 ConstraintLine const &c,
186 std::list<SPItem const *> const &it) const
187 {
188 if (_snap_enabled == false || getSnapFrom(t) == false) {
189 return;
190 }
192 _doConstrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
193 }
195 /*
196 Local Variables:
197 mode:c++
198 c-file-style:"stroustrup"
199 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
200 indent-tabs-mode:nil
201 fill-column:99
202 End:
203 */
204 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :