Code

Oops, don't use tabs! (replace tabs by 4 spaces)
[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 void Inkscape::Snapper::freeSnap(SnappedConstraints &sc,
95                                                    
96                                                    PointType const &t,
97                                                    NR::Point const &p,
98                                                    bool const &first_point,
99                                                     std::vector<NR::Point> &points_to_snap,                         
100                                                    SPItem const *it) const
102     std::list<SPItem const *> lit;
103     lit.push_back(it);
104     freeSnap(sc, t, p, first_point, points_to_snap, lit);
108 /**
109  *  Try to snap a point to whatever this snapper is interested in.  Any
110  *  snap that occurs will be to the nearest "interesting" thing (e.g. a
111  *  grid or guide line)
112  *
113  *  \param t Point type.
114  *  \param p Point to snap (desktop coordinates).
115  *  \param it Items that should not be snapped to.
116  *  \return Snapped point.
117  */
119 void Inkscape::Snapper::freeSnap(SnappedConstraints &sc, 
120                                                    
121                                                    PointType const &t,
122                                                    NR::Point const &p,
123                                                    bool const &first_point,
124                                                     std::vector<NR::Point> &points_to_snap,                     
125                                                    std::list<SPItem const *> const &it) const
127     if (_enabled == false || getSnapFrom(t) == false) {
128         return;
129     }
131     _doFreeSnap(sc, t, p, first_point, points_to_snap, it);
137 /**
138  *  Try to snap a point to whatever this snapper is interested in, where
139  *  the snap point is constrained to lie along a specified vector from the
140  *  original point.
141  *
142  *  \param p Point to snap (desktop coordinates).
143  *  \param c Vector to constrain the snap to.
144  *  \param it Items that should not be snapped to.
145  *  \return Snapped point.
146  */
148 void Inkscape::Snapper::constrainedSnap(SnappedConstraints &sc, 
149                                                           
150                                                           PointType const &t,
151                                                           NR::Point const &p,
152                                                           bool const &first_point,
153                                                            std::vector<NR::Point> &points_to_snap,
154                                                           ConstraintLine const &c,
155                                                           SPItem const *it) const
157     std::list<SPItem const *> lit;
158     lit.push_back(it);
159     constrainedSnap(sc, t, p, first_point, points_to_snap, c, lit);
163 /**
164  *  Try to snap a point to whatever this snapper is interested in, where
165  *  the snap point is constrained to lie along a specified vector from the
166  *  original point.
167  *
168  *  \param p Point to snap (desktop coordinates).
169  *  \param c Vector to constrain the snap to.
170  *  \param it Items that should not be snapped to.
171  *  \return Snapped point.
172  */
174 void Inkscape::Snapper::constrainedSnap(SnappedConstraints &sc, 
175                                                           
176                                                           PointType const &t,
177                                                           NR::Point const &p,
178                                                           bool const &first_point,
179                                                            std::vector<NR::Point> &points_to_snap,                         
180                                                           ConstraintLine const &c,
181                                                           std::list<SPItem const *> const &it) const
183     if (_enabled == false || getSnapFrom(t) == false) {
184         return;
185     }
187     _doConstrainedSnap(sc, t, p, first_point, points_to_snap, c, it);
190 /*
191   Local Variables:
192   mode:c++
193   c-file-style:"stroustrup"
194   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
195   indent-tabs-mode:nil
196   fill-column:99
197   End:
198 */
199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :