Code

Improving the performance of the object snapper
[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>
25 #include "guide-snapper.h"
26 #include "object-snapper.h"
28 class SPNamedView;
30 /// Class to coordinate snapping operations
32 /**
33  *  Each SPNamedView has one of these.  It offers methods to snap points to whatever
34  *  snappers are defined (e.g. grid, guides etc.).  It also allows callers to snap
35  *  points which have undergone some transformation (e.g. translation, scaling etc.)
36  */
38 class SnapManager
39 {
40 public:
41     SnapManager(SPNamedView const *v);
43     typedef std::list<const Inkscape::Snapper*> SnapperList;
45     bool SomeSnapperMightSnap() const;
47     Inkscape::SnappedPoint freeSnap(Inkscape::Snapper::PointType t,
48                                     NR::Point const &p,
49                                     SPItem const *it) const;
51     Inkscape::SnappedPoint freeSnap(Inkscape::Snapper::PointType t,
52                                     NR::Point const &p,
53                                     bool const &first_point,
54                                     std::vector<NR::Point> &points_to_snap,
55                                     std::list<SPItem const *> const &it) const;
57         Inkscape::SnappedPoint freeSnap( Inkscape::Snapper::PointType t,
58                                       NR::Point const &p,
59                                       bool const &first_point,
60                                       std::vector<NR::Point> &points_to_snap,
61                                       std::list<SPItem const *> const &it,
62                                       SnapperList const &snappers ) const;
64     Inkscape::SnappedPoint freeSnapAlways( Inkscape::Snapper::PointType t,
65                                            NR::Point const &p,
66                                            SPItem const *it,
67                                            SnapperList &snappers );
69     Inkscape::SnappedPoint freeSnapAlways( Inkscape::Snapper::PointType t,
70                                            NR::Point const &p,
71                                            std::list<SPItem const *> const &it,
72                                            SnapperList &snappers );
74     Inkscape::SnappedPoint constrainedSnap(Inkscape::Snapper::PointType t,
75                                            NR::Point const &p,
76                                            Inkscape::Snapper::ConstraintLine const &c,
77                                            SPItem const *it) const;
78     
79     Inkscape::SnappedPoint constrainedSnap(Inkscape::Snapper::PointType t,
80                                            NR::Point const &p,
81                                            bool const &first_point,
82                                            std::vector<NR::Point> &points_to_snap,
83                                            Inkscape::Snapper::ConstraintLine const &c,
84                                            std::list<SPItem const *> const &it) const;
85                                            
86         Inkscape::SnappedPoint guideSnap(NR::Point const &p,
87                                                                          SPGuide const &guide) const;
89     std::pair<NR::Point, bool> freeSnapTranslation(Inkscape::Snapper::PointType t,
90                                                    std::vector<NR::Point> const &p,
91                                                    std::list<SPItem const *> const &it,
92                                                    NR::Point const &tr) const;
94     std::pair<NR::Point, bool> constrainedSnapTranslation(Inkscape::Snapper::PointType t,
95                                                           std::vector<NR::Point> const &p,
96                                                           std::list<SPItem const *> const &it,
97                                                           Inkscape::Snapper::ConstraintLine const &c,
98                                                           NR::Point const &tr) const;
100     std::pair<NR::scale, bool> freeSnapScale(Inkscape::Snapper::PointType t,
101                                              std::vector<NR::Point> const &p,
102                                              std::list<SPItem const *> const &it,
103                                              NR::scale const &s,
104                                              NR::Point const &o) const;
106     std::pair<NR::scale, bool> constrainedSnapScale(Inkscape::Snapper::PointType t,
107                                                     std::vector<NR::Point> const &p,
108                                                     std::list<SPItem const *> const &it,
109                                                     Inkscape::Snapper::ConstraintLine const &c,
110                                                     NR::scale const &s,
111                                                     NR::Point const &o) const;
113     std::pair<NR::Coord, bool> freeSnapStretch(Inkscape::Snapper::PointType t,
114                                                std::vector<NR::Point> const &p,
115                                                std::list<SPItem const *> const &it,
116                                                NR::Coord const &s,
117                                                NR::Point const &o,
118                                                NR::Dim2 d,
119                                                bool uniform) const;
121     std::pair<NR::Coord, bool> freeSnapSkew(Inkscape::Snapper::PointType t,
122                                             std::vector<NR::Point> const &p,
123                                             std::list<SPItem const *> const &it,
124                                             NR::Coord const &s,
125                                             NR::Point const &o,
126                                             NR::Dim2 d) const;
128     Inkscape::GuideSnapper guide;      ///< guide snapper
129     Inkscape::ObjectSnapper object;    ///< snapper to other objects
131     SnapperList getSnappers() const;
132     SnapperList getGridSnappers() const;
133     
134     void setSnapModeBBox(bool enabled);
135     void setSnapModeNode(bool enabled);
136     void setSnapModeGuide(bool enabled);
137     bool getSnapModeBBox() const;
138     bool getSnapModeNode() const;
139     bool getSnapModeGuide() const;
141         void setIncludeItemCenter(bool enabled) {
142                 _include_item_center = enabled;
143                 object.setIncludeItemCenter(enabled);   //store a local copy in the object-snapper
144                                                                                                 //instead of passing it through many functions 
145         }
146         
147         bool getIncludeItemCenter()     const {
148                 return _include_item_center;
149         }
150                 
151 protected:
152     SPNamedView const *_named_view;
154 private:
156     enum Transformation {
157         TRANSLATION,
158         SCALE,
159         STRETCH,
160         SKEW
161     };
162     
163     bool _include_item_center; //If true, snapping nodes will also snap the item's center
164     
165     std::pair<NR::Point, bool> _snapTransformed(Inkscape::Snapper::PointType type,
166                                                 std::vector<NR::Point> const &points,
167                                                 std::list<SPItem const *> const &ignore,
168                                                 bool constrained,
169                                                 Inkscape::Snapper::ConstraintLine const &constraint,
170                                                 Transformation transformation_type,
171                                                 NR::Point const &transformation,
172                                                 NR::Point const &origin,
173                                                 NR::Dim2 dim,
174                                                 bool uniform) const;
175 };
177 #endif /* !SEEN_SNAP_H */
179 /*
180   Local Variables:
181   mode:c++
182   c-file-style:"stroustrup"
183   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
184   indent-tabs-mode:nil
185   fill-column:99
186   End:
187 */
188 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :