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"
23 #include "snap-candidate.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 class SnapManager;
34 struct SPItem;
36 namespace Inkscape
37 {
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 Inkscape::SnapCandidatePoint const &/*p*/,
62 Geom::OptRect const &/*bbox_to_snap*/,
63 std::vector<SPItem const *> const */*it*/,
64 std::vector<SnapCandidatePoint> */*unselected_nodes*/) const {};
66 class ConstraintLine
67 {
68 public:
69 ConstraintLine(Geom::Point const &d) : _has_point(false), _direction(d) {}
70 ConstraintLine(Geom::Point const &p, Geom::Point const &d) : _has_point(true), _point(p), _direction(d) {}
71 ConstraintLine(Geom::Line const &l) : _has_point(true), _point(l.origin()), _direction(l.versor()) {}
73 bool hasPoint() const {
74 return _has_point;
75 }
77 Geom::Point getPoint() const {
78 return _point;
79 }
81 Geom::Point getDirection() const {
82 return _direction;
83 }
85 void setPoint(Geom::Point const &p) {
86 _point = p;
87 _has_point = true;
88 }
90 Geom::Point projection(Geom::Point const &p) const { // returns the projection of p on this constraintline
91 Geom::Point const p1_on_cl = _has_point ? _point : p;
92 Geom::Point const p2_on_cl = p1_on_cl + _direction;
93 return Geom::projection(p, Geom::Line(p1_on_cl, p2_on_cl));
94 }
96 private:
98 bool _has_point;
99 Geom::Point _point;
100 Geom::Point _direction;
101 };
103 virtual void constrainedSnap(SnappedConstraints &/*sc*/,
104 Inkscape::SnapCandidatePoint const &/*p*/,
105 Geom::OptRect const &/*bbox_to_snap*/,
106 ConstraintLine const &/*c*/,
107 std::vector<SPItem const *> const */*it*/) const {};
109 protected:
110 SnapManager *_snapmanager;
112 // This is only used for grids, for which snapping can be enabled individually
113 bool _snap_enabled; ///< true if this snapper is enabled, otherwise false
114 bool _snap_visible_only;
115 };
117 }
119 #endif /* !SEEN_SNAPPER_H */
121 /*
122 Local Variables:
123 mode:c++
124 c-file-style:"stroustrup"
125 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
126 indent-tabs-mode:nil
127 fill-column:99
128 End:
129 */
130 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :