Code

1) When pasting, use an offset that is a multiple of the grid pitch (got lost during...
[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_for_snapindicator = NULL, SPItem const *item_to_ignore = NULL, std::vector<NR::Point> *unselected_nodes = NULL);
51     void setup(SPDesktop const *desktop_for_snapindicator, std::vector<SPItem const *> &items_to_ignore, std::vector<NR::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                       NR::Point &p,
57                       bool first_point = true,
58                       boost::optional<NR::Rect> const &bbox_to_snap = boost::optional<NR::Rect>()) const;
59     
60     Inkscape::SnappedPoint freeSnap(Inkscape::Snapper::PointType point_type,
61                                     NR::Point const &p,
62                                     bool first_point = true,
63                                     boost::optional<NR::Rect> const &bbox_to_snap = boost::optional<NR::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                              NR::Point &p,
71                              Inkscape::Snapper::ConstraintLine const &constraint,
72                              bool first_point = true,
73                              boost::optional<NR::Rect> const &bbox_to_snap = boost::optional<NR::Rect>()) const;
74     
75     Inkscape::SnappedPoint constrainedSnap(Inkscape::Snapper::PointType point_type,
76                                            NR::Point const &p,
77                                            Inkscape::Snapper::ConstraintLine const &constraint,
78                                            bool first_point = true,
79                                            boost::optional<NR::Rect> const &bbox_to_snap = boost::optional<NR::Rect>()) const;
80                                            
81     void guideSnap(NR::Point &p, NR::Point const &guide_normal) const;
83     Inkscape::SnappedPoint freeSnapTranslation(Inkscape::Snapper::PointType point_type,
84                                                std::vector<NR::Point> const &p,
85                                                NR::Point const &tr) const;
87     Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::Snapper::PointType point_type,
88                                                       std::vector<NR::Point> const &p,
89                                                       Inkscape::Snapper::ConstraintLine const &constraint,
90                                                       NR::Point const &tr) const;
92     Inkscape::SnappedPoint freeSnapScale(Inkscape::Snapper::PointType point_type,
93                                          std::vector<NR::Point> const &p,
94                                          NR::scale const &s,
95                                          NR::Point const &o) const;
97     Inkscape::SnappedPoint constrainedSnapScale(Inkscape::Snapper::PointType point_type,
98                                                 std::vector<NR::Point> const &p,
99                                                 NR::scale const &s,
100                                                 NR::Point const &o) const;
102     Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::Snapper::PointType point_type,
103                                                   std::vector<NR::Point> const &p,
104                                                   NR::Coord const &s,
105                                                   NR::Point const &o,
106                                                   NR::Dim2 d,
107                                                   bool uniform) const;
109     Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::Snapper::PointType point_type,
110                                                std::vector<NR::Point> const &p,
111                                                Inkscape::Snapper::ConstraintLine const &constraint,
112                                                NR::Point const &s, // s[0] = skew factor, s[1] = scale factor
113                                                NR::Point const &o,
114                                                NR::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     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;
128     
129     void setSnapIntersectionGG(bool enabled) {_intersectionGG = enabled;}
130     void setSnapIntersectionLS(bool enabled) {_intersectionLS = enabled;}
131     bool getSnapIntersectionGG() {return _intersectionGG;}
132     bool getSnapIntersectionLS() {return _intersectionLS;}    
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         }
139     
140     bool getIncludeItemCenter() const {
141         return _include_item_center;
142     }
143     
144     void setSnapEnabledGlobally(bool enabled) {
145         _snap_enabled_globally = enabled;   
146     }
147         
148     bool getSnapEnabledGlobally() const {
149         return _snap_enabled_globally;   
150     }
151     
152     void toggleSnapEnabledGlobally() {
153         _snap_enabled_globally = !_snap_enabled_globally;   
154     }
155         
156 protected:
157     SPNamedView const *_named_view;
159 private:
161     enum Transformation {
162         TRANSLATION,
163         SCALE,
164         STRETCH,
165         SKEW
166     };
167     
168     bool _include_item_center; //If true, snapping nodes will also snap the item's center
169     bool _intersectionGG;
170     bool _intersectionLS;
171     bool _snap_enabled_globally; //Toggles ALL snapping
172     
173     std::vector<SPItem const *> *_items_to_ignore;
174     SPItem const *_item_to_ignore;
175     SPDesktop const *_desktop_for_snapindicator;    
176     std::vector<NR::Point> *_unselected_nodes;                                    
177     
178     Inkscape::SnappedPoint _snapTransformed(Inkscape::Snapper::PointType type,
179                                             std::vector<NR::Point> const &points,
180                                             bool constrained,
181                                             Inkscape::Snapper::ConstraintLine const &constraint,
182                                             Transformation transformation_type,
183                                             NR::Point const &transformation,
184                                             NR::Point const &origin,
185                                             NR::Dim2 dim,
186                                             bool uniform) const;
187                                                 
188     Inkscape::SnappedPoint findBestSnap(NR::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 :