Code

Refactor snapping mechanisms: in seltrans.cpp, a GSList was converted to a std::list...
[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"
19 #include "libnr/nr-maybe.h"
21 #include "snapped-point.h"
22 #include "snapped-line.h"
24 struct SnappedConstraints {
25     std::list<Inkscape::SnappedPoint> points; 
26     std::list<Inkscape::SnappedLineSegment> lines;
27     std::list<Inkscape::SnappedLine> grid_lines;
28     std::list<Inkscape::SnappedLine> guide_lines;
29 };
31 struct SPNamedView;
32 struct SPItem;
34 namespace Inkscape
35 {
37 /// Parent for classes that can snap points to something
38 class Snapper
39 {
40 public:
41     Snapper() {}
42     Snapper(SPNamedView const *nv, ::NR::Coord const d);
43     virtual ~Snapper() {}
45     /// Point types to snap.
46     typedef int PointType;
47     static const PointType SNAPPOINT_NODE;
48     static const PointType SNAPPOINT_BBOX;
49     static const PointType SNAPPOINT_GUIDE;
51     void setSnapFrom(PointType t, bool s);
52     bool getSnapFrom(PointType t) const;
53     
54     void setSnapperTolerance(NR::Coord t);    
55     NR::Coord getSnapperTolerance() const; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
56     bool getSnapperAlwaysSnap() const; //if true, then the snapper will always snap, regardless of its tolerance
57     
58     /**
59     *  \return true if this Snapper will snap at least one kind of point.
60     */
61     virtual bool ThisSnapperMightSnap() const {return (_snap_enabled && _snap_from != 0);} // will likely be overridden by derived classes
63     void setEnabled(bool s);
64     bool getEnabled() const {return _snap_enabled;}
66     void freeSnap(SnappedConstraints &sc,
67                           PointType const &t,
68                           NR::Point const &p,
69                           bool const &first_point,                                             
70                           std::vector<NR::Point> &points_to_snap,                         
71                           SPItem const *it) const;
72                           
73     void freeSnap(SnappedConstraints &sc,
74                           PointType const &t,
75                           NR::Point const &p,
76                           bool const &first_point,                                             
77                           std::vector<NR::Point> &points_to_snap,                         
78                           std::vector<SPItem const *> const &it,
79                           std::vector<NR::Point> *unselected_nodes) const;
80     
81     class ConstraintLine
82     {
83     public:
84         ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
85         ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
87         bool hasPoint() const {
88             return _has_point;
89         }
91         NR::Point getPoint() const {
92             return _point;
93         }
95         NR::Point getDirection() const {
96             return _direction;
97         }
98         
99     private:
101         bool _has_point;
102         NR::Point _point;
103         NR::Point _direction;
104     };
106     void constrainedSnap(SnappedConstraints &sc,
107                                  PointType const &t,
108                                  NR::Point const &p,
109                                  bool const &first_point,
110                                  std::vector<NR::Point> &points_to_snap,       
111                                  ConstraintLine const &c,
112                                  SPItem const *it) const;
114     void constrainedSnap(SnappedConstraints &sc,
115                                  PointType const &t,
116                                  NR::Point const &p,
117                                  bool const &first_point,
118                                  std::vector<NR::Point> &points_to_snap,                         
119                                  ConstraintLine const &c,
120                                  std::vector<SPItem const *> const &it) const;
121                                  
122 protected:
123     SPNamedView const *_named_view;
124     int _snap_from; ///< bitmap of point types that we will snap from
125     bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
126     
127 private:
128     NR::Coord _snapper_tolerance;   ///< snap tolerance in desktop coordinates 
129                                     // must be private to enforce the usage of getTolerance(), which retrieves 
130                                     // the tolerance in screen pixels (making it zoom independent)
133     /**
134      *  Try to snap a point to whatever this snapper is interested in.  Any
135      *  snap that occurs will be to the nearest "interesting" thing (e.g. a
136      *  grid or guide line)
137      *
138      *  \param p Point to snap (desktop coordinates).
139      *  \param it Items that should not be snapped to.
140      *  \return Snapped point.
141      */
142     virtual void _doFreeSnap(SnappedConstraints &sc,
143                                      PointType const &t,
144                                      NR::Point const &p,
145                                      bool const &first_point,                                             
146                                      std::vector<NR::Point> &points_to_snap,
147                                      std::vector<SPItem const *> const &it,
148                                      std::vector<NR::Point> *unselected_nodes) const = 0;
150     /**
151      *  Try to snap a point to whatever this snapper is interested in, where
152      *  the snap point is constrained to lie along a specified vector from the
153      *  original point.
154      *
155      *  \param p Point to snap (desktop coordinates).
156      *  \param c Vector to constrain the snap to.
157      *  \param it Items that should not be snapped to.
158      *  \return Snapped point.
159      */    
160     virtual void _doConstrainedSnap(SnappedConstraints &sc,
161                                             PointType const &t,
162                                             NR::Point const &p,
163                                             bool const &first_point,
164                                             std::vector<NR::Point> &points_to_snap,
165                                             ConstraintLine const &c,
166                                             std::vector<SPItem const *> const &it) const = 0;
167 };
171 #endif /* !SEEN_SNAPPER_H */
173 /*
174   Local Variables:
175   mode:c++
176   c-file-style:"stroustrup"
177   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
178   indent-tabs-mode:nil
179   fill-column:99
180   End:
181 */
182 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :