1 #ifndef SEEN_SNAP_H
2 #define SEEN_SNAP_H
4 /**
5 * \file snap.h
6 * \brief SnapManager class.
7 *
8 * Authors:
9 * Lauris Kaplinski <lauris@kaplinski.com>
10 * Frank Felfe <innerspace@iname.com>
11 * Carl Hetherington <inkscape@carlh.net>
12 * Diederik van Lierop <mail@diedenrezi.nl>
13 *
14 * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
15 * Copyright (C) 2000-2002 Lauris Kaplinski
16 * Copyright (C) 2000-2008 Authors
17 *
18 * Released under GNU GPL, read the file 'COPYING' for more information
19 */
21 #include <vector>
23 #include <libnr/nr-coord.h>
24 #include <libnr/nr-dim2.h>
25 #include <libnr/nr-forward.h>
26 #include <libnr/nr-scale.h>
28 #include "guide-snapper.h"
29 #include "object-snapper.h"
30 #include "snap-preferences.h"
32 class SPNamedView;
34 /// Class to coordinate snapping operations
36 /**
37 * Each SPNamedView has one of these. It offers methods to snap points to whatever
38 * snappers are defined (e.g. grid, guides etc.). It also allows callers to snap
39 * points which have undergone some transformation (e.g. translation, scaling etc.)
40 */
42 class SnapManager
43 {
44 public:
45 enum Transformation {
46 TRANSLATION,
47 SCALE,
48 STRETCH,
49 SKEW
50 };
52 SnapManager(SPNamedView const *v);
54 typedef std::list<const Inkscape::Snapper*> SnapperList;
56 bool someSnapperMightSnap() const;
57 bool gridSnapperMightSnap() const;
59 void setup(SPDesktop const *desktop, bool snapindicator = true, SPItem const *item_to_ignore = NULL, std::vector<Geom::Point> *unselected_nodes = NULL);
60 void setup(SPDesktop const *desktop, bool snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<Geom::Point> *unselected_nodes = NULL);
62 // freeSnapReturnByRef() is preferred over freeSnap(), because it only returns a
63 // point if snapping has occured (by overwriting p); otherwise p is untouched
64 void freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
65 Geom::Point &p,
66 bool first_point = true,
67 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
69 Inkscape::SnappedPoint freeSnap(Inkscape::SnapPreferences::PointType point_type,
70 Geom::Point const &p,
71 bool first_point = true,
72 Geom::OptRect const &bbox_to_snap = Geom::OptRect() ) const;
74 Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
76 // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
77 // point, by overwriting p, if snapping has occured; otherwise p is untouched
78 void constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
79 Geom::Point &p,
80 Inkscape::Snapper::ConstraintLine const &constraint,
81 bool first_point = true,
82 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
84 Inkscape::SnappedPoint constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
85 Geom::Point const &p,
86 Inkscape::Snapper::ConstraintLine const &constraint,
87 bool first_point = true,
88 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
90 void guideSnap(Geom::Point &p, Geom::Point const &guide_normal) const;
92 Inkscape::SnappedPoint freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
93 std::vector<Geom::Point> const &p,
94 Geom::Point const &pointer,
95 Geom::Point const &tr) const;
97 Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
98 std::vector<Geom::Point> const &p,
99 Geom::Point const &pointer,
100 Inkscape::Snapper::ConstraintLine const &constraint,
101 Geom::Point const &tr) const;
103 Inkscape::SnappedPoint freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
104 std::vector<Geom::Point> const &p,
105 Geom::Point const &pointer,
106 Geom::Scale const &s,
107 Geom::Point const &o) const;
109 Inkscape::SnappedPoint constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
110 std::vector<Geom::Point> const &p,
111 Geom::Point const &pointer,
112 Geom::Scale const &s,
113 Geom::Point const &o) const;
115 Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
116 std::vector<Geom::Point> const &p,
117 Geom::Point const &pointer,
118 Geom::Coord const &s,
119 Geom::Point const &o,
120 Geom::Dim2 d,
121 bool uniform) const;
123 Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
124 std::vector<Geom::Point> const &p,
125 Geom::Point const &pointer,
126 Inkscape::Snapper::ConstraintLine const &constraint,
127 Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
128 Geom::Point const &o,
129 Geom::Dim2 d) const;
131 Inkscape::GuideSnapper guide; ///< guide snapper
132 Inkscape::ObjectSnapper object; ///< snapper to other objects
133 Inkscape::SnapPreferences snapprefs;
135 SnapperList getSnappers() const;
136 SnapperList getGridSnappers() const;
138 SPDesktop const *getDesktop() const {return _desktop;}
139 SPNamedView const *getNamedView() const {return _named_view;}
140 SPDocument *getDocument() const;
142 protected:
143 SPNamedView const *_named_view;
145 private:
146 std::vector<SPItem const *> *_items_to_ignore;
147 SPItem const *_item_to_ignore;
148 SPDesktop const *_desktop;
149 bool _snapindicator;
150 std::vector<Geom::Point> *_unselected_nodes;
152 Inkscape::SnappedPoint _snapTransformed(Inkscape::SnapPreferences::PointType type,
153 std::vector<Geom::Point> const &points,
154 Geom::Point const &pointer,
155 bool constrained,
156 Inkscape::Snapper::ConstraintLine const &constraint,
157 Transformation transformation_type,
158 Geom::Point const &transformation,
159 Geom::Point const &origin,
160 Geom::Dim2 dim,
161 bool uniform) const;
163 Geom::Point _transformPoint(Geom::Point const &p,
164 Transformation const transformation_type,
165 Geom::Point const &transformation,
166 Geom::Point const &origin,
167 Geom::Dim2 const dim,
168 bool const uniform) const;
170 void _displaySnapsource(Inkscape::SnapPreferences::PointType point_type, Geom::Point const &p) const;
172 Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const;
173 };
175 #endif /* !SEEN_SNAP_H */
177 /*
178 Local Variables:
179 mode:c++
180 c-file-style:"stroustrup"
181 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
182 indent-tabs-mode:nil
183 fill-column:99
184 End:
185 */
186 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :