40bf0996eab577f9833f073fcd0e90330d667d61
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-2010 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 /* Guides */
35 enum SPGuideDragType { // used both here and in desktop-events.cpp
36 SP_DRAG_TRANSLATE,
37 SP_DRAG_ROTATE,
38 SP_DRAG_MOVE_ORIGIN,
39 SP_DRAG_NONE
40 };
42 class SPNamedView;
44 /// Class to coordinate snapping operations
46 /**
47 * Each SPNamedView has one of these. It offers methods to snap points to whatever
48 * snappers are defined (e.g. grid, guides etc.). It also allows callers to snap
49 * points which have undergone some transformation (e.g. translation, scaling etc.)
50 */
52 class SnapManager
53 {
54 public:
55 enum Transformation {
56 TRANSLATION,
57 SCALE,
58 STRETCH,
59 SKEW
60 };
62 SnapManager(SPNamedView const *v);
64 typedef std::list<const Inkscape::Snapper*> SnapperList;
66 bool someSnapperMightSnap() const;
67 bool gridSnapperMightSnap() const;
69 void setup(SPDesktop const *desktop,
70 bool snapindicator = true,
71 SPItem const *item_to_ignore = NULL,
72 std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes = NULL,
73 SPGuide *guide_to_ignore = NULL);
75 void setup(SPDesktop const *desktop,
76 bool snapindicator,
77 std::vector<SPItem const *> &items_to_ignore,
78 std::vector<Inkscape::SnapCandidatePoint> *unselected_nodes = NULL,
79 SPGuide *guide_to_ignore = NULL);
81 // freeSnapReturnByRef() is preferred over freeSnap(), because it only returns a
82 // point if snapping has occurred (by overwriting p); otherwise p is untouched
83 void freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
84 Geom::Point &p,
85 Inkscape::SnapSourceType const source_type,
86 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
88 Inkscape::SnappedPoint freeSnap(Inkscape::SnapPreferences::PointType const point_type,
89 Inkscape::SnapCandidatePoint const &p,
90 Geom::OptRect const &bbox_to_snap = Geom::OptRect() ) const;
92 Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
94 // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
95 // point, by overwriting p, if snapping has occurred; otherwise p is untouched
96 void constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
97 Geom::Point &p,
98 Inkscape::SnapSourceType const source_type,
99 Inkscape::Snapper::ConstraintLine const &constraint,
100 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
102 Inkscape::SnappedPoint constrainedSnap(Inkscape::SnapPreferences::PointType const point_type,
103 Inkscape::SnapCandidatePoint const &p,
104 Inkscape::Snapper::ConstraintLine const &constraint,
105 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
107 void guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, SPGuideDragType drag_type) const;
108 void guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const;
110 Inkscape::SnappedPoint freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
111 std::vector<Inkscape::SnapCandidatePoint> const &p,
112 Geom::Point const &pointer,
113 Geom::Point const &tr) const;
115 Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
116 std::vector<Inkscape::SnapCandidatePoint> const &p,
117 Geom::Point const &pointer,
118 Inkscape::Snapper::ConstraintLine const &constraint,
119 Geom::Point const &tr) const;
121 Inkscape::SnappedPoint freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
122 std::vector<Inkscape::SnapCandidatePoint> const &p,
123 Geom::Point const &pointer,
124 Geom::Scale const &s,
125 Geom::Point const &o) const;
127 Inkscape::SnappedPoint constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
128 std::vector<Inkscape::SnapCandidatePoint> const &p,
129 Geom::Point const &pointer,
130 Geom::Scale const &s,
131 Geom::Point const &o) const;
133 Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
134 std::vector<Inkscape::SnapCandidatePoint> const &p,
135 Geom::Point const &pointer,
136 Geom::Coord const &s,
137 Geom::Point const &o,
138 Geom::Dim2 d,
139 bool uniform) const;
141 Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
142 std::vector<Inkscape::SnapCandidatePoint> const &p,
143 Geom::Point const &pointer,
144 Inkscape::Snapper::ConstraintLine const &constraint,
145 Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
146 Geom::Point const &o,
147 Geom::Dim2 d) const;
149 Inkscape::GuideSnapper guide; ///< guide snapper
150 Inkscape::ObjectSnapper object; ///< snapper to other objects
151 Inkscape::SnapPreferences snapprefs;
153 SnapperList getSnappers() const;
154 SnapperList getGridSnappers() const;
156 SPDesktop const *getDesktop() const {return _desktop;}
157 SPNamedView const *getNamedView() const {return _named_view;}
158 SPDocument *getDocument() const;
159 SPGuide const *getGuideToIgnore() const {return _guide_to_ignore;}
161 bool getSnapIndicator() const {return _snapindicator;}
163 Inkscape::SnappedPoint findBestSnap(Inkscape::SnapCandidatePoint const &p, SnappedConstraints const &sc, bool constrained, bool noCurves = false) const;
165 protected:
166 SPNamedView const *_named_view;
168 private:
169 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
170 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
171 SPGuide *_guide_to_ignore; ///< A guide that should not be snapped to, e.g. the guide that is currently being dragged
172 SPDesktop const *_desktop;
173 bool _snapindicator; ///< When true, an indicator will be drawn at the position that was being snapped to
174 std::vector<Inkscape::SnapCandidatePoint> *_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
175 //TODO: Make _unselected_nodes type safe; in the line above int is used for Inkscape::SnapTargetType, but if I remember
176 //correctly then in other cases the int is being used for Inkscape::SnapSourceType, or for both. How to make
177 //this type safe?
179 Inkscape::SnappedPoint _snapTransformed(Inkscape::SnapPreferences::PointType type,
180 std::vector<Inkscape::SnapCandidatePoint> const &points,
181 Geom::Point const &pointer,
182 bool constrained,
183 Inkscape::Snapper::ConstraintLine const &constraint,
184 Transformation transformation_type,
185 Geom::Point const &transformation,
186 Geom::Point const &origin,
187 Geom::Dim2 dim,
188 bool uniform) const;
190 Geom::Point _transformPoint(Inkscape::SnapCandidatePoint const &p,
191 Transformation const transformation_type,
192 Geom::Point const &transformation,
193 Geom::Point const &origin,
194 Geom::Dim2 const dim,
195 bool const uniform) const;
197 void _displaySnapsource(Inkscape::SnapPreferences::PointType point_type, Inkscape::SnapCandidatePoint const &p) const;
198 };
200 #endif /* !SEEN_SNAP_H */
202 /*
203 Local Variables:
204 mode:c++
205 c-file-style:"stroustrup"
206 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
207 indent-tabs-mode:nil
208 fill-column:99
209 End:
210 */
211 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :