Code

Node tool: snap to paths and their nodes, incl. to the path currently being edited
[inkscape.git] / src / snapper.h
1 #ifndef SEEN_SNAPPER_H
2 #define SEEN_SNAPPER_H
4 /**
5  *    \file src/snapper.h
6  *    \brief Snapper class.
7  *
8  *    Authors:
9  *      Carl Hetherington <inkscape@carlh.net>
10  *      Diederik van Lierop <mail@diedenrezi.nl>
11  *
12  *    Released under GNU GPL, read the file 'COPYING' for more information.
13  */
15 #include <map>
16 #include <list>
17 #include "libnr/nr-coord.h"
18 #include "libnr/nr-point.h"
20 #include "snapped-point.h"
21 #include "snapped-line.h"
23 struct SnappedConstraints {
24     std::list<Inkscape::SnappedPoint> points; 
25     std::list<Inkscape::SnappedLineSegment> lines;
26     std::list<Inkscape::SnappedLine> grid_lines;
27     std::list<Inkscape::SnappedLine> guide_lines;
28 };
30 struct SPNamedView;
31 struct SPItem;
33 namespace Inkscape
34 {
36 /// Parent for classes that can snap points to something
37 class Snapper
38 {
39 public:
40     Snapper() {}
41     Snapper(SPNamedView const *nv, ::NR::Coord const d);
42     virtual ~Snapper() {}
44     /// Point types to snap.
45     typedef int PointType;
46     static const PointType SNAPPOINT_NODE;
47     static const PointType SNAPPOINT_BBOX;
48     static const PointType SNAPPOINT_GUIDE;
50     void setSnapFrom(PointType t, bool s);
51     bool getSnapFrom(PointType t) const;
52     
53     void setSnapperTolerance(NR::Coord t);    
54     NR::Coord getSnapperTolerance() const; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
55     bool getSnapperAlwaysSnap() const; //if true, then the snapper will always snap, regardless of its tolerance
56     
57     /**
58     *  \return true if this Snapper will snap at least one kind of point.
59     */
60     virtual bool ThisSnapperMightSnap() const {return (_snap_enabled && _snap_from != 0);} // will likely be overridden by derived classes
62     void setEnabled(bool s);
63     bool getEnabled() const {return _snap_enabled;}
65     void freeSnap(SnappedConstraints &sc,
66                           PointType const &t,
67                           NR::Point const &p,
68                           bool const &first_point,                                             
69                           std::vector<NR::Point> &points_to_snap,                         
70                           SPItem const *it) const;
71                           
72     void freeSnap(SnappedConstraints &sc,
73                           PointType const &t,
74                           NR::Point const &p,
75                           bool const &first_point,                                             
76                           std::vector<NR::Point> &points_to_snap,                         
77                           std::list<SPItem const *> const &it,
78                           std::vector<NR::Point> *unselected_nodes) const;
80     class ConstraintLine
81     {
82     public:
83         ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
84         ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
86         bool hasPoint() const {
87             return _has_point;
88         }
90         NR::Point getPoint() const {
91             return _point;
92         }
94         NR::Point getDirection() const {
95             return _direction;
96         }
97         
98     private:
100         bool _has_point;
101         NR::Point _point;
102         NR::Point _direction;
103     };
105     void constrainedSnap(SnappedConstraints &sc,
106                                  PointType const &t,
107                                  NR::Point const &p,
108                                  bool const &first_point,
109                                  std::vector<NR::Point> &points_to_snap,       
110                                  ConstraintLine const &c,
111                                  SPItem const *it) const;
113     void constrainedSnap(SnappedConstraints &sc,
114                                  PointType const &t,
115                                  NR::Point const &p,
116                                  bool const &first_point,
117                                  std::vector<NR::Point> &points_to_snap,                         
118                                  ConstraintLine const &c,
119                                  std::list<SPItem const *> const &it) const;
120                                  
121 protected:
122     SPNamedView const *_named_view;
123     int _snap_from; ///< bitmap of point types that we will snap from
124     bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
125     
126 private:
127     NR::Coord _snapper_tolerance;   ///< snap tolerance in desktop coordinates 
128                                     // must be private to enforce the usage of getTolerance(), which retrieves 
129                                     // the tolerance in screen pixels (making it zoom independent)
132     /**
133      *  Try to snap a point to whatever this snapper is interested in.  Any
134      *  snap that occurs will be to the nearest "interesting" thing (e.g. a
135      *  grid or guide line)
136      *
137      *  \param p Point to snap (desktop coordinates).
138      *  \param it Items that should not be snapped to.
139      *  \return Snapped point.
140      */
141     virtual void _doFreeSnap(SnappedConstraints &sc,
142                                      PointType const &t,
143                                      NR::Point const &p,
144                                      bool const &first_point,                                             
145                                      std::vector<NR::Point> &points_to_snap,
146                                      std::list<SPItem const *> const &it,
147                                      std::vector<NR::Point> *unselected_nodes) const = 0;
149     /**
150      *  Try to snap a point to whatever this snapper is interested in, where
151      *  the snap point is constrained to lie along a specified vector from the
152      *  original point.
153      *
154      *  \param p Point to snap (desktop coordinates).
155      *  \param c Vector to constrain the snap to.
156      *  \param it Items that should not be snapped to.
157      *  \return Snapped point.
158      */    
159     virtual void _doConstrainedSnap(SnappedConstraints &sc,
160                                             PointType const &t,
161                                             NR::Point const &p,
162                                             bool const &first_point,
163                                             std::vector<NR::Point> &points_to_snap,
164                                             ConstraintLine const &c,
165                                             std::list<SPItem const *> const &it) const = 0;
166 };
170 #endif /* !SEEN_SNAPPER_H */
172 /*
173   Local Variables:
174   mode:c++
175   c-file-style:"stroustrup"
176   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
177   indent-tabs-mode:nil
178   fill-column:99
179   End:
180 */
181 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :