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;
58 void setup(SPDesktop const *desktop, bool snapindicator = true, SPItem const *item_to_ignore = NULL, std::vector<Geom::Point> *unselected_nodes = NULL);
59 void setup(SPDesktop const *desktop, bool snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<Geom::Point> *unselected_nodes = NULL);
61 // freeSnapReturnByRef() is preferred over freeSnap(), because it only returns a
62 // point if snapping has occured (by overwriting p); otherwise p is untouched
63 void freeSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
64 Geom::Point &p,
65 bool first_point = true,
66 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
68 Inkscape::SnappedPoint freeSnap(Inkscape::SnapPreferences::PointType point_type,
69 Geom::Point const &p,
70 bool first_point = true,
71 Geom::OptRect const &bbox_to_snap = Geom::OptRect() ) const;
73 Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
75 // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
76 // point, by overwriting p, if snapping has occured; otherwise p is untouched
77 void constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
78 Geom::Point &p,
79 Inkscape::Snapper::ConstraintLine const &constraint,
80 bool first_point = true,
81 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
83 Inkscape::SnappedPoint constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
84 Geom::Point const &p,
85 Inkscape::Snapper::ConstraintLine const &constraint,
86 bool first_point = true,
87 Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
89 void guideSnap(Geom::Point &p, Geom::Point const &guide_normal) const;
91 Inkscape::SnappedPoint freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
92 std::vector<Geom::Point> const &p,
93 Geom::Point const &pointer,
94 Geom::Point const &tr) const;
96 Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
97 std::vector<Geom::Point> const &p,
98 Geom::Point const &pointer,
99 Inkscape::Snapper::ConstraintLine const &constraint,
100 Geom::Point const &tr) const;
102 Inkscape::SnappedPoint freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
103 std::vector<Geom::Point> const &p,
104 Geom::Point const &pointer,
105 Geom::Scale const &s,
106 Geom::Point const &o) const;
108 Inkscape::SnappedPoint constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
109 std::vector<Geom::Point> const &p,
110 Geom::Point const &pointer,
111 Geom::Scale const &s,
112 Geom::Point const &o) const;
114 Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
115 std::vector<Geom::Point> const &p,
116 Geom::Point const &pointer,
117 Geom::Coord const &s,
118 Geom::Point const &o,
119 Geom::Dim2 d,
120 bool uniform) const;
122 Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
123 std::vector<Geom::Point> const &p,
124 Geom::Point const &pointer,
125 Inkscape::Snapper::ConstraintLine const &constraint,
126 Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
127 Geom::Point const &o,
128 Geom::Dim2 d) const;
130 Inkscape::GuideSnapper guide; ///< guide snapper
131 Inkscape::ObjectSnapper object; ///< snapper to other objects
132 Inkscape::SnapPreferences snapprefs;
134 SnapperList getSnappers() const;
135 SnapperList getGridSnappers() const;
137 SPDesktop const *getDesktop() const {return _desktop;}
138 SPNamedView const *getNamedView() const {return _named_view;}
139 SPDocument *getDocument() const;
141 protected:
142 SPNamedView const *_named_view;
144 private:
145 std::vector<SPItem const *> *_items_to_ignore;
146 SPItem const *_item_to_ignore;
147 SPDesktop const *_desktop;
148 bool _snapindicator;
149 std::vector<Geom::Point> *_unselected_nodes;
151 Inkscape::SnappedPoint _snapTransformed(Inkscape::SnapPreferences::PointType type,
152 std::vector<Geom::Point> const &points,
153 Geom::Point const &pointer,
154 bool constrained,
155 Inkscape::Snapper::ConstraintLine const &constraint,
156 Transformation transformation_type,
157 Geom::Point const &transformation,
158 Geom::Point const &origin,
159 Geom::Dim2 dim,
160 bool uniform) const;
162 Geom::Point _transformPoint(Geom::Point const &p,
163 Transformation const transformation_type,
164 Geom::Point const &transformation,
165 Geom::Point const &origin,
166 Geom::Dim2 const dim,
167 bool const uniform) const;
169 void _displaySnapsource(Inkscape::SnapPreferences::PointType point_type, Geom::Point const &p) const;
171 Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const;
172 };
174 #endif /* !SEEN_SNAP_H */
176 /*
177 Local Variables:
178 mode:c++
179 c-file-style:"stroustrup"
180 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
181 indent-tabs-mode:nil
182 fill-column:99
183 End:
184 */
185 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :