1 #ifndef SEEN_SNAP_H
2 #define SEEN_SNAP_H
4 /**
5 * \file snap.h
6 * \brief SnapManager class.
7 *
8 * The SnapManager class handles most (if not all) of the interfacing of the snapping mechanisms with the
9 * other parts of the code base. It stores the references to the various types of snappers for grid, guides
10 * and objects, and it stores most of the snapping preferences. Besides that it provides methods to setup
11 * the snapping environment (e.g. keeps a list of the items to ignore when looking for snap target candidates,
12 * and toggling of the snap indicator), and it provides many different methods for the snapping itself (free
13 * snapping vs. constrained snapping, returning the result by reference or through a return statement, etc.)
14 *
15 * Authors:
16 * Lauris Kaplinski <lauris@kaplinski.com>
17 * Frank Felfe <innerspace@iname.com>
18 * Carl Hetherington <inkscape@carlh.net>
19 * Diederik van Lierop <mail@diedenrezi.nl>
20 *
21 * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
22 * Copyright (C) 2000-2002 Lauris Kaplinski
23 * Copyright (C) 2000-2009 Authors
24 *
25 * Released under GNU GPL, read the file 'COPYING' for more information
26 */
28 #include <vector>
30 #include "guide-snapper.h"
31 #include "object-snapper.h"
32 #include "snap-preferences.h"
34 class SPNamedView;
36 /// Class to coordinate snapping operations
38 /**
39 * Each SPNamedView has one of these. It offers methods to snap points to whatever
40 * snappers are defined (e.g. grid, guides etc.). It also allows callers to snap
41 * points which have undergone some transformation (e.g. translation, scaling etc.)
42 */
44 class SnapManager
45 {
46 public:
47 enum Transformation {
48 TRANSLATION,
49 SCALE,
50 STRETCH,
51 SKEW
52 };
54 SnapManager(SPNamedView const *v);
56 typedef std::list<const Inkscape::Snapper*> SnapperList;
58 bool someSnapperMightSnap() const;
59 bool gridSnapperMightSnap() const;
61 void setup(SPDesktop const *desktop,
62 bool snapindicator = true,
63 SPItem const *item_to_ignore = NULL,
64 std::vector<std::pair<Geom::Point, int> > *unselected_nodes = NULL,
65 SPGuide *guide_to_ignore = NULL);
67 void setup(SPDesktop const *desktop,
68 bool snapindicator,
69 std::vector<SPItem const *> &items_to_ignore,
70 std::vector<std::pair<Geom::Point, int> > *unselected_nodes = NULL,
71 SPGuide *guide_to_ignore = NULL);
73 // freeSnapReturnByRef() is preferred over freeSnap(), because it only returns a
74 // point if snapping has occurred (by overwriting p); otherwise p is untouched
75 void freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
76 Geom::Point &p,
77 Inkscape::SnapSourceType const source_type,
78 bool first_point = true,
79 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
82 Inkscape::SnappedPoint freeSnap(Inkscape::SnapPreferences::PointType point_type,
83 Geom::Point const &p,
84 Inkscape::SnapSourceType const &source_type,
85 bool first_point = true,
86 Geom::OptRect const &bbox_to_snap = Geom::OptRect() ) const;
88 Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
90 // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
91 // point, by overwriting p, if snapping has occurred; otherwise p is untouched
92 void constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
93 Geom::Point &p,
94 Inkscape::SnapSourceType const source_type,
95 Inkscape::Snapper::ConstraintLine const &constraint,
96 bool first_point = true,
97 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
99 Inkscape::SnappedPoint constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
100 Geom::Point const &p,
101 Inkscape::SnapSourceType const &source_type,
102 Inkscape::Snapper::ConstraintLine const &constraint,
103 bool first_point = true,
104 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
106 void guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal) const;
107 void guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const;
109 Inkscape::SnappedPoint freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
110 std::vector<std::pair<Geom::Point, int> > const &p,
111 Geom::Point const &pointer,
112 Geom::Point const &tr) const;
114 Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
115 std::vector<std::pair<Geom::Point, int> > const &p,
116 Geom::Point const &pointer,
117 Inkscape::Snapper::ConstraintLine const &constraint,
118 Geom::Point const &tr) const;
120 Inkscape::SnappedPoint freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
121 std::vector<std::pair<Geom::Point, int> > const &p,
122 Geom::Point const &pointer,
123 Geom::Scale const &s,
124 Geom::Point const &o) const;
126 Inkscape::SnappedPoint constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
127 std::vector<std::pair<Geom::Point, int> > const &p,
128 Geom::Point const &pointer,
129 Geom::Scale const &s,
130 Geom::Point const &o) const;
132 Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
133 std::vector<std::pair<Geom::Point, int> > const &p,
134 Geom::Point const &pointer,
135 Geom::Coord const &s,
136 Geom::Point const &o,
137 Geom::Dim2 d,
138 bool uniform) const;
140 Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
141 std::vector<std::pair<Geom::Point, int> > const &p,
142 Geom::Point const &pointer,
143 Inkscape::Snapper::ConstraintLine const &constraint,
144 Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
145 Geom::Point const &o,
146 Geom::Dim2 d) const;
148 Inkscape::GuideSnapper guide; ///< guide snapper
149 Inkscape::ObjectSnapper object; ///< snapper to other objects
150 Inkscape::SnapPreferences snapprefs;
152 SnapperList getSnappers() const;
153 SnapperList getGridSnappers() const;
155 SPDesktop const *getDesktop() const {return _desktop;}
156 SPNamedView const *getNamedView() const {return _named_view;}
157 SPDocument *getDocument() const;
158 SPGuide const *getGuideToIgnore() const {return _guide_to_ignore;}
160 bool getSnapIndicator() const {return _snapindicator;}
162 protected:
163 SPNamedView const *_named_view;
165 private:
166 std::vector<SPItem const *> *_items_to_ignore; ///< Items that should not be snapped to, for example the items that are currently being dragged. Set using the setup() method
167 SPItem const *_item_to_ignore; ///< Single item that should not be snapped to. If not NULL then this takes precedence over _items_to_ignore. Set using the setup() method
168 SPGuide *_guide_to_ignore; ///< A guide that should not be snapped to, e.g. the guide that is currently being dragged
169 SPDesktop const *_desktop;
170 bool _snapindicator; ///< When true, an indicator will be drawn at the position that was being snapped to
171 std::vector<std::pair<Geom::Point, int> > *_unselected_nodes; ///< Nodes of the path that is currently being edited and which have not been selected and which will therefore be stationary. Only these nodes will be considered for snapping to. Of each unselected node both the position (Geom::Point) and the type (Inkscape::SnapTargetType) will be stored
172 //TODO: Make _unselected_nodes type safe; in the line above int is used for Inkscape::SnapTargetType, but if I remember
173 //correctly then in other cases the int is being used for Inkscape::SnapSourceType, or for both. How to make
174 //this type safe?
176 Inkscape::SnappedPoint _snapTransformed(Inkscape::SnapPreferences::PointType type,
177 std::vector<std::pair<Geom::Point, int> > const &points,
178 Geom::Point const &pointer,
179 bool constrained,
180 Inkscape::Snapper::ConstraintLine const &constraint,
181 Transformation transformation_type,
182 Geom::Point const &transformation,
183 Geom::Point const &origin,
184 Geom::Dim2 dim,
185 bool uniform) const;
187 Geom::Point _transformPoint(std::pair<Geom::Point, int> const &p,
188 Transformation const transformation_type,
189 Geom::Point const &transformation,
190 Geom::Point const &origin,
191 Geom::Dim2 const dim,
192 bool const uniform) const;
194 void _displaySnapsource(Inkscape::SnapPreferences::PointType point_type, std::pair<Geom::Point, int> const &p) const;
196 Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, Inkscape::SnapSourceType const source_type, SnappedConstraints &sc, bool constrained) const;
197 };
199 #endif /* !SEEN_SNAP_H */
201 /*
202 Local Variables:
203 mode:c++
204 c-file-style:"stroustrup"
205 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
206 indent-tabs-mode:nil
207 fill-column:99
208 End:
209 */
210 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :