Code

Oops, don't use tabs! (replace tabs by 4 spaces)
[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  *
11  *    Released under GNU GPL, read the file 'COPYING' for more information.
12  */
14 #include <map>
15 #include <list>
16 #include "libnr/nr-coord.h"
17 #include "libnr/nr-point.h"
19 #include "snapped-point.h"
20 #include "snapped-line.h"
22 struct SnappedConstraints {
23     std::list<Inkscape::SnappedPoint> points; 
24     std::list<Inkscape::SnappedLine> lines;
25     std::list<Inkscape::SnappedInfiniteLine> grid_lines;
26     std::list<Inkscape::SnappedInfiniteLine> guide_lines;
27 };
29 struct SPNamedView;
30 struct SPItem;
32 namespace Inkscape
33 {
35 /// Parent for classes that can snap points to something
36 class Snapper
37 {
38 public:
39     Snapper() {}
40     Snapper(SPNamedView const *nv, ::NR::Coord const d);
41     virtual ~Snapper() {}
43     /// Point types to snap.
44     typedef int PointType;
45     static const PointType SNAPPOINT_NODE;
46     static const PointType SNAPPOINT_BBOX;
47     static const PointType SNAPPOINT_GUIDE;
49     void setSnapFrom(PointType t, bool s);
50     void setDistance(::NR::Coord d);
52     bool getSnapFrom(PointType t) const;
53     ::NR::Coord getDistance() const;
55     /**
56     *  \return true if this Snapper will snap at least one kind of point.
57     */
58     virtual bool ThisSnapperMightSnap() const {return (_enabled && _snap_from != 0);} // will likely be overridden by derived classes
60     void setEnabled(bool s);
62     void freeSnap(SnappedConstraints &sc,
63                           PointType const &t,
64                           NR::Point const &p,
65                           bool const &first_point,                                             
66                           std::vector<NR::Point> &points_to_snap,                         
67                           SPItem const *it) const;
69     void freeSnap(SnappedConstraints &sc,
70                           PointType const &t,
71                           NR::Point const &p,
72                           bool const &first_point,                                             
73                           std::vector<NR::Point> &points_to_snap,                         
74                           std::list<SPItem const *> const &it) const;
76     class ConstraintLine
77     {
78     public:
79         ConstraintLine(NR::Point const &d) : _has_point(false), _direction(d) {}
80         ConstraintLine(NR::Point const &p, NR::Point const &d) : _has_point(true), _point(p), _direction(d) {}
82         bool hasPoint() const {
83             return _has_point;
84         }
86         NR::Point getPoint() const {
87             return _point;
88         }
90         NR::Point getDirection() const {
91             return _direction;
92         }
93         
94     private:
96         bool _has_point;
97         NR::Point _point;
98         NR::Point _direction;
99     };
101     void constrainedSnap(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                                  ConstraintLine const &c,
107                                  SPItem const *it) const;
109     void constrainedSnap(SnappedConstraints &sc,
110                                  PointType const &t,
111                                  NR::Point const &p,
112                                  bool const &first_point,
113                                  std::vector<NR::Point> &points_to_snap,                         
114                                  ConstraintLine const &c,
115                                  std::list<SPItem const *> const &it) const;
116                                  
117 protected:
118     SPNamedView const *_named_view;
119     int _snap_from; ///< bitmap of point types that we will snap from
120     bool _enabled; ///< true if this snapper is enabled, otherwise false
121     
122 private:
124     /**
125      *  Try to snap a point to whatever this snapper is interested in.  Any
126      *  snap that occurs will be to the nearest "interesting" thing (e.g. a
127      *  grid or guide line)
128      *
129      *  \param p Point to snap (desktop coordinates).
130      *  \param it Items that should not be snapped to.
131      *  \return Snapped point.
132      */
133     virtual void _doFreeSnap(SnappedConstraints &sc,
134                                      PointType const &t,
135                                      NR::Point const &p,
136                                      bool const &first_point,                                             
137                                      std::vector<NR::Point> &points_to_snap,
138                                      std::list<SPItem const *> const &it) const = 0;
140     /**
141      *  Try to snap a point to whatever this snapper is interested in, where
142      *  the snap point is constrained to lie along a specified vector from the
143      *  original point.
144      *
145      *  \param p Point to snap (desktop coordinates).
146      *  \param c Vector to constrain the snap to.
147      *  \param it Items that should not be snapped to.
148      *  \return Snapped point.
149      */    
150     virtual void _doConstrainedSnap(SnappedConstraints &sc,
151                                             PointType const &t,
152                                             NR::Point const &p,
153                                             bool const &first_point,
154                                             std::vector<NR::Point> &points_to_snap,
155                                             ConstraintLine const &c,
156                                             std::list<SPItem const *> const &it) const = 0;
157     
158     NR::Coord _distance; ///< snap distance (desktop coordinates)
159 };
163 #endif /* !SEEN_SNAPPER_H */
165 /*
166   Local Variables:
167   mode:c++
168   c-file-style:"stroustrup"
169   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
170   indent-tabs-mode:nil
171   fill-column:99
172   End:
173 */
174 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :