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,
101 PointType const &t,
102 NR::Point const &p,
103 bool const &first_point,
104 std::vector<NR::Point> &points_to_snap,
105 SPItem const *it) const
106 {
107 std::list<SPItem const *> lit;
108 if (it) {
109 lit.push_back(it);
110 }
112 freeSnap(sc, t, p, first_point, points_to_snap, lit, NULL);
113 }
115 /**
116 * Try to snap a point to whatever this snapper is interested in. Any
117 * snap that occurs will be to the nearest "interesting" thing (e.g. a
118 * grid or guide line)
119 *
120 * \param t Point type.
121 * \param p Point to snap (desktop coordinates).
122 * \param it Items that should not be snapped to.
123 * \return Snapped point.
124 */
126 void Inkscape::Snapper::freeSnap(SnappedConstraints &sc,
128 PointType const &t,
129 NR::Point const &p,
130 bool const &first_point,
131 std::vector<NR::Point> &points_to_snap,
132 std::list<SPItem const *> const &it,
133 std::vector<NR::Point> *unselected_nodes) const
134 {
135 if (_snap_enabled == false || getSnapFrom(t) == false) {
136 return;
137 }
139 _doFreeSnap(sc, t, p, first_point, points_to_snap, it, unselected_nodes);
140 }
145 /**
146 * Try to snap a point to whatever this snapper is interested in, where
147 * the snap point is constrained to lie along a specified vector from the
148 * original point.
149 *
150 * \param p Point to snap (desktop coordinates).
151 * \param c Vector to constrain the snap to.
152 * \param it Items that should not be snapped to.
153 * \return Snapped point.
154 */
156 void Inkscape::Snapper::constrainedSnap(SnappedConstraints &sc,
158 PointType const &t,
159 NR::Point const &p,
160 bool const &first_point,
161 std::vector<NR::Point> &points_to_snap,
162 ConstraintLine const &c,
163 SPItem const *it) const
164 {
165 std::list<SPItem const *> lit;
166 lit.push_back(it);
167 constrainedSnap(sc, t, p, first_point, points_to_snap, c, lit);
168 }
171 /**
172 * Try to snap a point to whatever this snapper is interested in, where
173 * the snap point is constrained to lie along a specified vector from the
174 * original point.
175 *
176 * \param p Point to snap (desktop coordinates).
177 * \param c Vector to constrain the snap to.
178 * \param it Items that should not be snapped to.
179 * \return Snapped point.
180 */
182 void Inkscape::Snapper::constrainedSnap(SnappedConstraints &sc,
183 PointType const &t,
184 NR::Point const &p,
185 bool const &first_point,
186 std::vector<NR::Point> &points_to_snap,
187 ConstraintLine const &c,
188 std::list<SPItem const *> const &it) const
189 {
190 if (_snap_enabled == false || getSnapFrom(t) == false) {
191 return;
192 }
194 _doConstrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
195 }
197 /*
198 Local Variables:
199 mode:c++
200 c-file-style:"stroustrup"
201 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
202 indent-tabs-mode:nil
203 fill-column:99
204 End:
205 */
206 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :