Code

Snap to intersections of line segments
[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  *
13  * Copyright (C) 2006-2007 Johan Engelen <johan@shouraizou.nl>
14  * Copyright (C) 2000-2002 Lauris Kaplinski
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #include <vector>
21 #include <libnr/nr-coord.h>
22 #include <libnr/nr-dim2.h>
23 #include <libnr/nr-forward.h>
24 #include <libnr/nr-scale.h>
26 #include "guide-snapper.h"
27 #include "object-snapper.h"
29 class SPNamedView;
31 /// Class to coordinate snapping operations
33 /**
34  *  Each SPNamedView has one of these.  It offers methods to snap points to whatever
35  *  snappers are defined (e.g. grid, guides etc.).  It also allows callers to snap
36  *  points which have undergone some transformation (e.g. translation, scaling etc.)
37  */
39 class SnapManager
40 {
41 public:
42     SnapManager(SPNamedView const *v);
44     typedef std::list<const Inkscape::Snapper*> SnapperList;
46     bool SomeSnapperMightSnap() const;
48     Inkscape::SnappedPoint freeSnap(Inkscape::Snapper::PointType t,
49                                     NR::Point const &p,
50                                     SPItem const *it) const;
52     Inkscape::SnappedPoint freeSnap( Inkscape::Snapper::PointType t,
53                                       NR::Point const &p,
54                                       bool const &first_point,
55                                       std::vector<NR::Point> &points_to_snap,
56                                       std::list<SPItem const *> const &it) const;
58     Inkscape::SnappedPoint freeSnapAlways( Inkscape::Snapper::PointType t,
59                                            NR::Point const &p,
60                                            SPItem const *it,
61                                            SnapperList &snappers );
63     Inkscape::SnappedPoint freeSnapAlways( Inkscape::Snapper::PointType t,
64                                            NR::Point const &p,
65                                            std::list<SPItem const *> const &it,
66                                            SnapperList &snappers );
68     Inkscape::SnappedPoint constrainedSnap(Inkscape::Snapper::PointType t,
69                                            NR::Point const &p,
70                                            Inkscape::Snapper::ConstraintLine const &c,
71                                            SPItem const *it) const;
72     
73     Inkscape::SnappedPoint constrainedSnap(Inkscape::Snapper::PointType t,
74                                            NR::Point const &p,
75                                            bool const &first_point,
76                                            std::vector<NR::Point> &points_to_snap,
77                                            Inkscape::Snapper::ConstraintLine const &c,
78                                            std::list<SPItem const *> const &it) const;
79                                            
80     Inkscape::SnappedPoint guideSnap(NR::Point const &p,
81                                      NR::Point const &guide_normal) const;
83     std::pair<NR::Point, bool> freeSnapTranslation(Inkscape::Snapper::PointType t,
84                                                    std::vector<NR::Point> const &p,
85                                                    std::list<SPItem const *> const &it,
86                                                    NR::Point const &tr) const;
88     std::pair<NR::Point, bool> constrainedSnapTranslation(Inkscape::Snapper::PointType t,
89                                                           std::vector<NR::Point> const &p,
90                                                           std::list<SPItem const *> const &it,
91                                                           Inkscape::Snapper::ConstraintLine const &c,
92                                                           NR::Point const &tr) const;
94     std::pair<NR::scale, bool> freeSnapScale(Inkscape::Snapper::PointType t,
95                                              std::vector<NR::Point> const &p,
96                                              std::list<SPItem const *> const &it,
97                                              NR::scale const &s,
98                                              NR::Point const &o) const;
100     std::pair<NR::scale, bool> constrainedSnapScale(Inkscape::Snapper::PointType t,
101                                                     std::vector<NR::Point> const &p,
102                                                     std::list<SPItem const *> const &it,
103                                                     Inkscape::Snapper::ConstraintLine const &c,
104                                                     NR::scale const &s,
105                                                     NR::Point const &o) const;
107     std::pair<NR::Coord, bool> freeSnapStretch(Inkscape::Snapper::PointType t,
108                                                std::vector<NR::Point> const &p,
109                                                std::list<SPItem const *> const &it,
110                                                NR::Coord const &s,
111                                                NR::Point const &o,
112                                                NR::Dim2 d,
113                                                bool uniform) const;
115     std::pair<NR::Coord, bool> freeSnapSkew(Inkscape::Snapper::PointType t,
116                                             std::vector<NR::Point> const &p,
117                                             std::list<SPItem const *> const &it,
118                                             NR::Coord const &s,
119                                             NR::Point const &o,
120                                             NR::Dim2 d) const;
121                                             
122         Inkscape::SnappedPoint guideSnap(NR::Point const &p,
123                            Inkscape::ObjectSnapper::DimensionToSnap const snap_dim) const;
124   
126     Inkscape::GuideSnapper guide;      ///< guide snapper
127     Inkscape::ObjectSnapper object;    ///< snapper to other objects
129     SnapperList getSnappers() const;
130     SnapperList getGridSnappers() const;
131     
132     void setSnapModeBBox(bool enabled);
133     void setSnapModeNode(bool enabled);
134     void setSnapModeGuide(bool enabled);
135     bool getSnapModeBBox() const;
136     bool getSnapModeNode() const;
137     bool getSnapModeGuide() const;
138     
139     void setSnapIntersectionGG(bool enabled) {_intersectionGG = enabled;}
140     void setSnapIntersectionLS(bool enabled) {_intersectionLS = enabled;}
141     bool getSnapIntersectionGG() { return _intersectionGG;}
142     bool getSnapIntersectionLS() { return _intersectionLS;}    
144     void setIncludeItemCenter(bool enabled)    {
145         _include_item_center = enabled;
146         // also store a local copy in the object-snapper instead of passing it through many functions
147         object.setIncludeItemCenter(enabled);
148         }
149     
150     bool getIncludeItemCenter() const {
151         return _include_item_center;
152     }
153         
154 protected:
155     SPNamedView const *_named_view;
157 private:
159     enum Transformation {
160         TRANSLATION,
161         SCALE,
162         STRETCH,
163         SKEW
164     };
165     
166     bool _include_item_center; //If true, snapping nodes will also snap the item's center
167     bool _intersectionGG;
168     bool _intersectionLS;
169     
170     std::pair<NR::Point, bool> _snapTransformed(Inkscape::Snapper::PointType type,
171                                                 std::vector<NR::Point> const &points,
172                                                 std::list<SPItem const *> const &ignore,
173                                                 bool constrained,
174                                                 Inkscape::Snapper::ConstraintLine const &constraint,
175                                                 Transformation transformation_type,
176                                                 NR::Point const &transformation,
177                                                 NR::Point const &origin,
178                                                 NR::Dim2 dim,
179                                                 bool uniform) const;
180                                                 
181     Inkscape::SnappedPoint findBestSnap(NR::Point const &p, SnappedConstraints &sc) const;
182 };
184 #endif /* !SEEN_SNAP_H */
186 /*
187   Local Variables:
188   mode:c++
189   c-file-style:"stroustrup"
190   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
191   indent-tabs-mode:nil
192   fill-column:99
193   End:
194 */
195 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :