Code

Remove pointer to SPDesktop from ObjectSnapper
[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  * 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;
49     
50     void setup(SPDesktop const *desktop, bool snapindicator = true, SPItem const *item_to_ignore = NULL, std::vector<Geom::Point> *unselected_nodes = NULL);
51     void setup(SPDesktop const *desktop, bool 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;
59     
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;
64     
65     Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
66     
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;
74     
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;
80                                            
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;
115                                         
116     Inkscape::GuideSnapper guide;      ///< guide snapper
117     Inkscape::ObjectSnapper object;    ///< snapper to other objects
119     SnapperList getSnappers() const;
120     SnapperList getGridSnappers() const;
121     
122     SPDesktop const *getDesktop() const {return _desktop;}
123     
124     void setSnapModeBBox(bool enabled);
125     void setSnapModeNode(bool enabled);
126     void setSnapModeGuide(bool enabled);
127     bool getSnapModeBBox() const;
128     bool getSnapModeNode() const;
129     bool getSnapModeGuide() const;
130     
131     void setSnapIntersectionGG(bool enabled) {_intersectionGG = enabled;}
132     void setSnapIntersectionCS(bool enabled) {_intersectionCS = enabled;}
133     bool getSnapIntersectionGG() {return _intersectionGG;}
134     bool getSnapIntersectionCS() {return _intersectionCS;}    
136     void setIncludeItemCenter(bool enabled)    {
137         _include_item_center = enabled;
138         // also store a local copy in the object-snapper instead of passing it through many functions
139         object.setIncludeItemCenter(enabled);
140         }
141     
142     bool getIncludeItemCenter() const {
143         return _include_item_center;
144     }
145     
146     void setSnapEnabledGlobally(bool enabled) {
147         _snap_enabled_globally = enabled;   
148     }
149         
150     bool getSnapEnabledGlobally() const {
151         return _snap_enabled_globally;   
152     }
153     
154     void toggleSnapEnabledGlobally() {
155         _snap_enabled_globally = !_snap_enabled_globally;   
156     }
157         
158 protected:
159     SPNamedView const *_named_view;
161 private:
163     enum Transformation {
164         TRANSLATION,
165         SCALE,
166         STRETCH,
167         SKEW
168     };
169     
170     bool _include_item_center; //If true, snapping nodes will also snap the item's center
171     bool _intersectionGG;
172     bool _intersectionCS;
173     bool _snap_enabled_globally; //Toggles ALL snapping
174     
175     std::vector<SPItem const *> *_items_to_ignore;
176     SPItem const *_item_to_ignore;
177     SPDesktop const *_desktop;
178     bool _snapindicator;
179     std::vector<Geom::Point> *_unselected_nodes;                                    
180     
181     Inkscape::SnappedPoint _snapTransformed(Inkscape::Snapper::PointType type,
182                                             std::vector<Geom::Point> const &points,
183                                             bool constrained,
184                                             Inkscape::Snapper::ConstraintLine const &constraint,
185                                             Transformation transformation_type,
186                                             Geom::Point const &transformation,
187                                             Geom::Point const &origin,
188                                             Geom::Dim2 dim,
189                                             bool uniform) const;
190                                                 
191     Inkscape::SnappedPoint findBestSnap(Geom::Point const &p, SnappedConstraints &sc, bool constrained) const;
192 };
194 #endif /* !SEEN_SNAP_H */
196 /*
197   Local Variables:
198   mode:c++
199   c-file-style:"stroustrup"
200   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
201   indent-tabs-mode:nil
202   fill-column:99
203   End:
204 */
205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :