Code

Snap to intersections of any kind of path (were we previously only could snap to...
[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 <boost/optional.hpp>
21 #include "snapped-point.h"
22 #include "snapped-line.h"
23 #include "snapped-curve.h"
25 struct SnappedConstraints {
26     std::list<Inkscape::SnappedPoint> points;
27     std::list<Inkscape::SnappedLineSegment> lines;
28     std::list<Inkscape::SnappedLine> grid_lines;
29     std::list<Inkscape::SnappedLine> guide_lines;
30     std::list<Inkscape::SnappedCurve> curves;
31 };
33 struct SPNamedView;
34 struct SPItem;
36 namespace Inkscape
37 {
39 /// Parent for classes that can snap points to something
40 class Snapper
41 {
42 public:
43     Snapper() {}
44     Snapper(SPNamedView const *nv, ::NR::Coord const d);
45     virtual ~Snapper() {}
47     /// Point types to snap.
48     typedef int PointType;
49     static const PointType SNAPPOINT_NODE;
50     static const PointType SNAPPOINT_BBOX;
51     static const PointType SNAPPOINT_GUIDE;
53     void setSnapFrom(PointType t, bool s);
54     bool getSnapFrom(PointType t) const;
56     void setSnapperTolerance(NR::Coord t);
57     NR::Coord getSnapperTolerance() const; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
58     bool getSnapperAlwaysSnap() const; //if true, then the snapper will always snap, regardless of its tolerance
60     /**
61     *  \return true if this Snapper will snap at least one kind of point.
62     */
63     virtual bool ThisSnapperMightSnap() const {return (_snap_enabled && _snap_from != 0);} // will likely be overridden by derived classes
65     void setEnabled(bool s);
66     bool getEnabled() const {return _snap_enabled;}
68     virtual void freeSnap(SnappedConstraints &/*sc*/,
69                           PointType const &/*t*/,
70                           NR::Point const &/*p*/,
71                           bool const &/*first_point*/,
72                           boost::optional<NR::Rect> const &/*bbox_to_snap*/,
73                           std::vector<SPItem const *> const */*it*/,
74                           std::vector<NR::Point> */*unselected_nodes*/) 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         void setPoint(NR::Point const &p) {
95             _point = p;
96             _has_point = true;        
97         }
99     private:
101         bool _has_point;
102         NR::Point _point;
103         NR::Point _direction;
104     };
106     virtual void constrainedSnap(SnappedConstraints &/*sc*/,
107                                  PointType const &/*t*/,
108                                  NR::Point const &/*p*/,
109                                  bool const &/*first_point*/,
110                                  boost::optional<NR::Rect> const &/*bbox_to_snap*/,
111                                  ConstraintLine const &/*c*/,
112                                  std::vector<SPItem const *> const */*it*/) const {};
114 protected:
115     SPNamedView const *_named_view;
116     int _snap_from; ///< bitmap of point types that we will snap from
117     bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
119 private:
120     NR::Coord _snapper_tolerance;   ///< snap tolerance in desktop coordinates
121                                     // must be private to enforce the usage of getTolerance(), which retrieves
122                                     // the tolerance in screen pixels (making it zoom independent)
124 };
128 #endif /* !SEEN_SNAPPER_H */
130 /*
131   Local Variables:
132   mode:c++
133   c-file-style:"stroustrup"
134   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
135   indent-tabs-mode:nil
136   fill-column:99
137   End:
138 */
139 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :