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 <boost/optional.hpp>
19 #include "snapped-point.h"
20 #include "snapped-line.h"
21 #include "snapped-curve.h"
22 #include "snap-preferences.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 std::list<Inkscape::SnappedCurve> curves;
30 };
32 class SnapManager;
33 struct SPItem;
35 namespace Inkscape
36 {
38 /// Parent for classes that can snap points to something
39 class Snapper
40 {
41 public:
42 Snapper() {}
43 Snapper(SnapManager *sm, ::Geom::Coord const t);
44 virtual ~Snapper() {}
46 virtual Geom::Coord getSnapperTolerance() const = 0; //returns the tolerance of the snapper in screen pixels (i.e. independent of zoom)
47 virtual bool getSnapperAlwaysSnap() const = 0; //if true, then the snapper will always snap, regardless of its tolerance
49 /**
50 * \return true if this Snapper will snap at least one kind of point.
51 */
52 virtual bool ThisSnapperMightSnap() const {return _snap_enabled;} // will likely be overridden by derived classes
54 // These four methods are only used for grids, for which snapping can be enabled individually
55 void setEnabled(bool s);
56 void setSnapVisibleOnly(bool s);
57 bool getEnabled() const {return _snap_enabled;}
58 bool getSnapVisibleOnly() const {return _snap_visible_only;}
60 virtual void freeSnap(SnappedConstraints &/*sc*/,
61 SnapPreferences::PointType const &/*t*/,
62 Geom::Point const &/*p*/,
63 SnapSourceType const &/*source_type*/,
64 bool const &/*first_point*/,
65 Geom::OptRect const &/*bbox_to_snap*/,
66 std::vector<SPItem const *> const */*it*/,
67 std::vector<std::pair<Geom::Point, int> > */*unselected_nodes*/) const {};
69 class ConstraintLine
70 {
71 public:
72 ConstraintLine(Geom::Point const &d) : _has_point(false), _direction(d) {}
73 ConstraintLine(Geom::Point const &p, Geom::Point const &d) : _has_point(true), _point(p), _direction(d) {}
75 bool hasPoint() const {
76 return _has_point;
77 }
79 Geom::Point getPoint() const {
80 return _point;
81 }
83 Geom::Point getDirection() const {
84 return _direction;
85 }
87 void setPoint(Geom::Point const &p) {
88 _point = p;
89 _has_point = true;
90 }
92 Geom::Point projection(Geom::Point const &p) const { // returns the projection of p on this constraintline
93 Geom::Point const p1_on_cl = _has_point ? _point : p;
94 Geom::Point const p2_on_cl = p1_on_cl + _direction;
95 return Geom::projection(p, Geom::Line(p1_on_cl, p2_on_cl));
96 }
98 private:
100 bool _has_point;
101 Geom::Point _point;
102 Geom::Point _direction;
103 };
105 virtual void constrainedSnap(SnappedConstraints &/*sc*/,
106 SnapPreferences::PointType const &/*t*/,
107 Geom::Point const &/*p*/,
108 SnapSourceType const &/*source_type*/,
109 bool const &/*first_point*/,
110 Geom::OptRect const &/*bbox_to_snap*/,
111 ConstraintLine const &/*c*/,
112 std::vector<SPItem const *> const */*it*/) const {};
114 protected:
115 SnapManager *_snapmanager;
117 // This is only used for grids, for which snapping can be enabled individually
118 bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
119 bool _snap_visible_only;
120 };
122 }
124 #endif /* !SEEN_SNAPPER_H */
126 /*
127 Local Variables:
128 mode:c++
129 c-file-style:"stroustrup"
130 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
131 indent-tabs-mode:nil
132 fill-column:99
133 End:
134 */
135 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :