Code

Fixed signed/unsigned problem with precision calc. Fixes bug #399604.
[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 /* 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<std::pair<Geom::Point, int> > *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<std::pair<Geom::Point, int> > *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                                                         bool first_point = true,
87                                                         Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
90     Inkscape::SnappedPoint freeSnap(Inkscape::SnapPreferences::PointType point_type,
91                                                                         Geom::Point const &p,
92                                                             Inkscape::SnapSourceType const &source_type,
93                                         bool first_point = true,
94                                     Geom::OptRect const &bbox_to_snap = Geom::OptRect() ) const;
96     Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
98     // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
99     // point, by overwriting p, if snapping has occurred; otherwise p is untouched
100     void constrainedSnapReturnByRef(Inkscape::SnapPreferences::PointType point_type,
101                                                                         Geom::Point &p,
102                                                                         Inkscape::SnapSourceType const source_type,
103                                                                         Inkscape::Snapper::ConstraintLine const &constraint,
104                                                                         bool first_point = true,
105                                                                         Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
107     Inkscape::SnappedPoint constrainedSnap(Inkscape::SnapPreferences::PointType point_type,
108                                                                                    Geom::Point const &p,
109                                                                                    Inkscape::SnapSourceType const &source_type,
110                                                                                    Inkscape::Snapper::ConstraintLine const &constraint,
111                                                                                    bool first_point = true,
112                                            Geom::OptRect const &bbox_to_snap = Geom::OptRect()) const;
114     void guideFreeSnap(Geom::Point &p, Geom::Point const &guide_normal, SPGuideDragType drag_type) const;
115     void guideConstrainedSnap(Geom::Point &p, SPGuide const &guideline) const;
117     Inkscape::SnappedPoint freeSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
118                                                std::vector<std::pair<Geom::Point, int> > const &p,
119                                                Geom::Point const &pointer,
120                                                Geom::Point const &tr) const;
122     Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::SnapPreferences::PointType point_type,
123                                                       std::vector<std::pair<Geom::Point, int> > const &p,
124                                                       Geom::Point const &pointer,
125                                                       Inkscape::Snapper::ConstraintLine const &constraint,
126                                                       Geom::Point const &tr) const;
128     Inkscape::SnappedPoint freeSnapScale(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 constrainedSnapScale(Inkscape::SnapPreferences::PointType point_type,
135                                                 std::vector<std::pair<Geom::Point, int> > const &p,
136                                                 Geom::Point const &pointer,
137                                                 Geom::Scale const &s,
138                                                 Geom::Point const &o) const;
140     Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::SnapPreferences::PointType point_type,
141                                                   std::vector<std::pair<Geom::Point, int> > const &p,
142                                                   Geom::Point const &pointer,
143                                                   Geom::Coord const &s,
144                                                   Geom::Point const &o,
145                                                   Geom::Dim2 d,
146                                                   bool uniform) const;
148     Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::SnapPreferences::PointType point_type,
149                                                std::vector<std::pair<Geom::Point, int> > const &p,
150                                                Geom::Point const &pointer,
151                                                Inkscape::Snapper::ConstraintLine const &constraint,
152                                                Geom::Point const &s, // s[0] = skew factor, s[1] = scale factor
153                                                Geom::Point const &o,
154                                                Geom::Dim2 d) const;
156     Inkscape::GuideSnapper guide;      ///< guide snapper
157     Inkscape::ObjectSnapper object;    ///< snapper to other objects
158     Inkscape::SnapPreferences snapprefs;
160     SnapperList getSnappers() const;
161     SnapperList getGridSnappers() const;
163     SPDesktop const *getDesktop() const {return _desktop;}
164     SPNamedView const *getNamedView() const {return _named_view;}
165     SPDocument *getDocument() const;
166     SPGuide const *getGuideToIgnore() const {return _guide_to_ignore;}
168     bool getSnapIndicator() const {return _snapindicator;}
170 protected:
171     SPNamedView const *_named_view;
173 private:
174     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
175     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
176     SPGuide *_guide_to_ignore; ///< A guide that should not be snapped to, e.g. the guide that is currently being dragged
177     SPDesktop const *_desktop;
178     bool _snapindicator; ///< When true, an indicator will be drawn at the position that was being snapped to
179     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
180     //TODO: Make _unselected_nodes type safe; in the line above int is used for Inkscape::SnapTargetType, but if I remember
181     //correctly then in other cases the int is being used for Inkscape::SnapSourceType, or for both. How to make
182     //this type safe?
184     Inkscape::SnappedPoint _snapTransformed(Inkscape::SnapPreferences::PointType type,
185                                             std::vector<std::pair<Geom::Point, int> > const &points,
186                                             Geom::Point const &pointer,
187                                             bool constrained,
188                                             Inkscape::Snapper::ConstraintLine const &constraint,
189                                             Transformation transformation_type,
190                                             Geom::Point const &transformation,
191                                             Geom::Point const &origin,
192                                             Geom::Dim2 dim,
193                                             bool uniform) const;
195     Geom::Point _transformPoint(std::pair<Geom::Point, int> const &p,
196                                             Transformation const transformation_type,
197                                             Geom::Point const &transformation,
198                                             Geom::Point const &origin,
199                                             Geom::Dim2 const dim,
200                                             bool const uniform) const;
202     void _displaySnapsource(Inkscape::SnapPreferences::PointType point_type, std::pair<Geom::Point, int> const &p) const;
204     Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, Inkscape::SnapSourceType const source_type, SnappedConstraints &sc, bool constrained, bool noCurves = false) const;
205 };
207 #endif /* !SEEN_SNAP_H */
209 /*
210   Local Variables:
211   mode:c++
212   c-file-style:"stroustrup"
213   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
214   indent-tabs-mode:nil
215   fill-column:99
216   End:
217 */
218 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :