Code

Documenting of the snapping API
[inkscape.git] / src / snap.h
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 snap_projection, //try snapping the projection of p onto the constraint line, not p itself
97                                                                         bool first_point = true,
98                                                                         Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
100     Inkscape::SnappedPoint constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
101                                                                                    Geom::Point const &p,
102                                                                                    Inkscape::SnapSourceType const &source_type,
103                                                                                    Inkscape::Snapper::ConstraintLine const &constraint,
104                                                                                    bool const snap_projection,
105                                            bool first_point = true,
106                                            Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
108     void guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal) const;
109     void guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const;
111     Inkscape::SnappedPoint freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
112                                                std::vector<std::pair<Geom::Point, int> > const &p,
113                                                Geom::Point const &pointer,
114                                                Geom::Point const &tr) const;
116     Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
117                                                       std::vector<std::pair<Geom::Point, int> > const &p,
118                                                       Geom::Point const &pointer,
119                                                       Inkscape::Snapper::ConstraintLine const &constraint,
120                                                       Geom::Point const &tr) const;
122     Inkscape::SnappedPoint freeSnapScale(Inkscape::SnapPreferences::PointType point_type,
123                                          std::vector<std::pair<Geom::Point, int> > const &p,
124                                          Geom::Point const &pointer,
125                                          Geom::Scale const &s,
126                                          Geom::Point const &o) const;
128     Inkscape::SnappedPoint constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
129                                                 std::vector<std::pair<Geom::Point, int> > const &p,
130                                                 Geom::Point const &pointer,
131                                                 Geom::Scale const &s,
132                                                 Geom::Point const &o) const;
134     Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
135                                                   std::vector<std::pair<Geom::Point, int> > const &p,
136                                                   Geom::Point const &pointer,
137                                                   Geom::Coord const &s,
138                                                   Geom::Point const &o,
139                                                   Geom::Dim2 d,
140                                                   bool uniform) const;
142     Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
143                                                std::vector<std::pair<Geom::Point, int> > const &p,
144                                                Geom::Point const &pointer,
145                                                Inkscape::Snapper::ConstraintLine const &constraint,
146                                                Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
147                                                Geom::Point const &o,
148                                                Geom::Dim2 d) const;
150     Inkscape::GuideSnapper guide;      ///< guide snapper
151     Inkscape::ObjectSnapper object;    ///< snapper to other objects
152     Inkscape::SnapPreferences snapprefs;
154     SnapperList getSnappers() const;
155     SnapperList getGridSnappers() const;
157     SPDesktop const *getDesktop() const {return _desktop;}
158     SPNamedView const *getNamedView() const {return _named_view;}
159     SPDocument *getDocument() const;
160     SPGuide const *getGuideToIgnore() const {return _guide_to_ignore;}
162     bool getSnapIndicator() const {return _snapindicator;}
164 protected:
165     SPNamedView const *_named_view;
167 private:
168     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
169     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
170     SPGuide *_guide_to_ignore; ///< A guide that should not be snapped to, e.g. the guide that is currently being dragged
171     SPDesktop const *_desktop;
172     bool _snapindicator; ///< When true, an indicator will be drawn at the position that was being snapped to
173     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
174     //TODO: Make _unselected_nodes type safe; in the line above int is used for Inkscape::SnapTargetType, but if I remember
175     //correctly then in other cases the int is being used for Inkscape::SnapSourceType, or for both. How to make
176     //this type safe?
178     Inkscape::SnappedPoint _snapTransformed(Inkscape::SnapPreferences::PointType type,
179                                             std::vector<std::pair<Geom::Point, int> > const &points,
180                                             Geom::Point const &pointer,
181                                             bool constrained,
182                                             Inkscape::Snapper::ConstraintLine const &constraint,
183                                             Transformation transformation_type,
184                                             Geom::Point const &transformation,
185                                             Geom::Point const &origin,
186                                             Geom::Dim2 dim,
187                                             bool uniform) const;
189     Geom::Point _transformPoint(std::pair<Geom::Point, int> const &p,
190                                             Transformation const transformation_type,
191                                             Geom::Point const &transformation,
192                                             Geom::Point const &origin,
193                                             Geom::Dim2 const dim,
194                                             bool const uniform) const;
196     void _displaySnapsource(Inkscape::SnapPreferences::PointType point_type, std::pair<Geom::Point, int> const &p) const;
198     Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, Inkscape::SnapSourceType const source_type, SnappedConstraints &sc, bool constrained) const;
199 };
201 #endif /* !SEEN_SNAP_H */
203 /*
204   Local Variables:
205   mode:c++
206   c-file-style:"stroustrup"
207   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
208   indent-tabs-mode:nil
209   fill-column:99
210   End:
211 */
212 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :