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"
31 class SPNamedView;
33 /// Class to coordinate snapping operations
35 /**
36 * Each SPNamedView has one of these. It offers methods to snap points to whatever
37 * snappers are defined (e.g. grid, guides etc.). It also allows callers to snap
38 * points which have undergone some transformation (e.g. translation, scaling etc.)
39 */
41 class SnapManager
42 {
43 public:
44 SnapManager(SPNamedView const *v);
46 typedef std::list<const Inkscape::Snapper*> SnapperList;
48 bool SomeSnapperMightSnap() const;
50 void setup(SPDesktop const *desktop_for_snapindicator = NULL, SPItem const *item_to_ignore = NULL, std::vector<Geom::Point> *unselected_nodes = NULL);
51 void setup(SPDesktop const *desktop_for_snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<Geom::Point> *unselected_nodes = NULL);
53 // freeSnapReturnByRef() is preferred over freeSnap(), because it only returns a
54 // point if snapping has occured (by overwriting p); otherwise p is untouched
55 void freeSnapReturnByRef(Inkscape::Snapper::PointType point_type,
56 Geom::Point &p,
57 bool first_point = true,
58 boost::optional<Geom::Rect> const &bbox_to_snap = boost::optional<Geom::Rect>()) const;
60 Inkscape::SnappedPoint freeSnap(Inkscape::Snapper::PointType point_type,
61 Geom::Point const &p,
62 bool first_point = true,
63 boost::optional<Geom::Rect> const &bbox_to_snap = boost::optional<Geom::Rect>() ) const;
65 Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
67 // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
68 // point, by overwriting p, if snapping has occured; otherwise p is untouched
69 void constrainedSnapReturnByRef(Inkscape::Snapper::PointType point_type,
70 Geom::Point &p,
71 Inkscape::Snapper::ConstraintLine const &constraint,
72 bool first_point = true,
73 boost::optional<Geom::Rect> const &bbox_to_snap = boost::optional<Geom::Rect>()) const;
75 Inkscape::SnappedPoint constrainedSnap(Inkscape::Snapper::PointType point_type,
76 Geom::Point const &p,
77 Inkscape::Snapper::ConstraintLine const &constraint,
78 bool first_point = true,
79 boost::optional<Geom::Rect> const &bbox_to_snap = boost::optional<Geom::Rect>()) const;
81 void guideSnap(Geom::Point &p, Geom::Point const &guide_normal) const;
83 Inkscape::SnappedPoint freeSnapTranslation(Inkscape::Snapper::PointType point_type,
84 std::vector<Geom::Point> const &p,
85 Geom::Point const &tr) const;
87 Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::Snapper::PointType point_type,
88 std::vector<Geom::Point> const &p,
89 Inkscape::Snapper::ConstraintLine const &constraint,
90 Geom::Point const &tr) const;
92 Inkscape::SnappedPoint freeSnapScale(Inkscape::Snapper::PointType point_type,
93 std::vector<Geom::Point> const &p,
94 Geom::Scale const &s,
95 Geom::Point const &o) const;
97 Inkscape::SnappedPoint constrainedSnapScale(Inkscape::Snapper::PointType point_type,
98 std::vector<Geom::Point> const &p,
99 Geom::Scale const &s,
100 Geom::Point const &o) const;
102 Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::Snapper::PointType point_type,
103 std::vector<Geom::Point> const &p,
104 Geom::Coord const &s,
105 Geom::Point const &o,
106 Geom::Dim2 d,
107 bool uniform) const;
109 Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::Snapper::PointType point_type,
110 std::vector<Geom::Point> const &p,
111 Inkscape::Snapper::ConstraintLine const &constraint,
112 Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
113 Geom::Point const &o,
114 Geom::Dim2 d) const;
116 Inkscape::GuideSnapper guide; ///< guide snapper
117 Inkscape::ObjectSnapper object; ///< snapper to other objects
119 SnapperList getSnappers() const;
120 SnapperList getGridSnappers() const;
122 void setSnapModeBBox(bool enabled);
123 void setSnapModeNode(bool enabled);
124 void setSnapModeGuide(bool enabled);
125 bool getSnapModeBBox() const;
126 bool getSnapModeNode() const;
127 bool getSnapModeGuide() const;
129 void setSnapIntersectionGG(bool enabled) {_intersectionGG = enabled;}
130 void setSnapIntersectionCS(bool enabled) {_intersectionCS = enabled;}
131 bool getSnapIntersectionGG() {return _intersectionGG;}
132 bool getSnapIntersectionCS() {return _intersectionCS;}
134 void setIncludeItemCenter(bool enabled) {
135 _include_item_center = enabled;
136 // also store a local copy in the object-snapper instead of passing it through many functions
137 object.setIncludeItemCenter(enabled);
138 }
140 bool getIncludeItemCenter() const {
141 return _include_item_center;
142 }
144 void setSnapEnabledGlobally(bool enabled) {
145 _snap_enabled_globally = enabled;
146 }
148 bool getSnapEnabledGlobally() const {
149 return _snap_enabled_globally;
150 }
152 void toggleSnapEnabledGlobally() {
153 _snap_enabled_globally = !_snap_enabled_globally;
154 }
156 protected:
157 SPNamedView const *_named_view;
159 private:
161 enum Transformation {
162 TRANSLATION,
163 SCALE,
164 STRETCH,
165 SKEW
166 };
168 bool _include_item_center; //If true, snapping nodes will also snap the item's center
169 bool _intersectionGG;
170 bool _intersectionCS;
171 bool _snap_enabled_globally; //Toggles ALL snapping
173 std::vector<SPItem const *> *_items_to_ignore;
174 SPItem const *_item_to_ignore;
175 SPDesktop const *_desktop_for_snapindicator;
176 std::vector<Geom::Point> *_unselected_nodes;
178 Inkscape::SnappedPoint _snapTransformed(Inkscape::Snapper::PointType type,
179 std::vector<Geom::Point> const &points,
180 bool constrained,
181 Inkscape::Snapper::ConstraintLine const &constraint,
182 Transformation transformation_type,
183 Geom::Point const &transformation,
184 Geom::Point const &origin,
185 Geom::Dim2 dim,
186 bool uniform) const;
188 Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const;
189 };
191 #endif /* !SEEN_SNAP_H */
193 /*
194 Local Variables:
195 mode:c++
196 c-file-style:"stroustrup"
197 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
198 indent-tabs-mode:nil
199 fill-column:99
200 End:
201 */
202 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :