Code

NR::Maybe => boost::optional
[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     // constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a 
66     // point, by overwriting p, if snapping has occured; otherwise p is untouched
67     void constrainedSnapReturnByRef(Inkscape::Snapper::PointType point_type,
68                              NR::Point &p,
69                              Inkscape::Snapper::ConstraintLine const &constraint,
70                              bool first_point = true,
71                              boost::optional<NR::Rect> const &bbox_to_snap = boost::optional<NR::Rect>()) const;
72     
73     Inkscape::SnappedPoint constrainedSnap(Inkscape::Snapper::PointType point_type,
74                                            NR::Point const &p,
75                                            Inkscape::Snapper::ConstraintLine const &constraint,
76                                            bool first_point = true,
77                                            boost::optional<NR::Rect> const &bbox_to_snap = boost::optional<NR::Rect>()) const;
78                                            
79     void guideSnap(NR::Point &p, NR::Point const &guide_normal) const;
81     Inkscape::SnappedPoint freeSnapTranslation(Inkscape::Snapper::PointType point_type,
82                                                std::vector<NR::Point> const &p,
83                                                NR::Point const &tr) const;
85     Inkscape::SnappedPoint constrainedSnapTranslation(Inkscape::Snapper::PointType point_type,
86                                                       std::vector<NR::Point> const &p,
87                                                       Inkscape::Snapper::ConstraintLine const &constraint,
88                                                       NR::Point const &tr) const;
90     Inkscape::SnappedPoint freeSnapScale(Inkscape::Snapper::PointType point_type,
91                                          std::vector<NR::Point> const &p,
92                                          NR::scale const &s,
93                                          NR::Point const &o) const;
95     Inkscape::SnappedPoint constrainedSnapScale(Inkscape::Snapper::PointType point_type,
96                                                 std::vector<NR::Point> const &p,
97                                                 NR::scale const &s,
98                                                 NR::Point const &o) const;
100     Inkscape::SnappedPoint constrainedSnapStretch(Inkscape::Snapper::PointType point_type,
101                                                   std::vector<NR::Point> const &p,
102                                                   NR::Coord const &s,
103                                                   NR::Point const &o,
104                                                   NR::Dim2 d,
105                                                   bool uniform) const;
107     Inkscape::SnappedPoint constrainedSnapSkew(Inkscape::Snapper::PointType point_type,
108                                                std::vector<NR::Point> const &p,
109                                                Inkscape::Snapper::ConstraintLine const &constraint,
110                                                NR::Point const &s, // s[0] = skew factor, s[1] = scale factor
111                                                NR::Point const &o,
112                                                NR::Dim2 d) const;
113                                         
114     Inkscape::GuideSnapper guide;      ///< guide snapper
115     Inkscape::ObjectSnapper object;    ///< snapper to other objects
117     SnapperList getSnappers() const;
118     SnapperList getGridSnappers() const;
119     
120     void setSnapModeBBox(bool enabled);
121     void setSnapModeNode(bool enabled);
122     void setSnapModeGuide(bool enabled);
123     bool getSnapModeBBox() const;
124     bool getSnapModeNode() const;
125     bool getSnapModeGuide() const;
126     
127     void setSnapIntersectionGG(bool enabled) {_intersectionGG = enabled;}
128     void setSnapIntersectionLS(bool enabled) {_intersectionLS = enabled;}
129     bool getSnapIntersectionGG() {return _intersectionGG;}
130     bool getSnapIntersectionLS() {return _intersectionLS;}    
132     void setIncludeItemCenter(bool enabled)    {
133         _include_item_center = enabled;
134         // also store a local copy in the object-snapper instead of passing it through many functions
135         object.setIncludeItemCenter(enabled);
136         }
137     
138     bool getIncludeItemCenter() const {
139         return _include_item_center;
140     }
141     
142     void setSnapEnabledGlobally(bool enabled) {
143         _snap_enabled_globally = enabled;   
144     }
145         
146     bool getSnapEnabledGlobally() const {
147         return _snap_enabled_globally;   
148     }
149     
150     void toggleSnapEnabledGlobally() {
151         _snap_enabled_globally = !_snap_enabled_globally;   
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     bool _snap_enabled_globally; //Toggles ALL snapping
170     
171     std::vector<SPItem const *> *_items_to_ignore;
172     SPItem const *_item_to_ignore;
173     SPDesktop const *_desktop_for_snapindicator;    
174     std::vector<NR::Point> *_unselected_nodes;                                    
175     
176     Inkscape::SnappedPoint _snapTransformed(Inkscape::Snapper::PointType type,
177                                             std::vector<NR::Point> const &points,
178                                             bool constrained,
179                                             Inkscape::Snapper::ConstraintLine const &constraint,
180                                             Transformation transformation_type,
181                                             NR::Point const &transformation,
182                                             NR::Point const &origin,
183                                             NR::Dim2 dim,
184                                             bool uniform) const;
185                                                 
186     Inkscape::SnappedPoint findBestSnap(NR::Point const &p, SnappedConstraints &sc, bool constrained) const;
187 };
189 #endif /* !SEEN_SNAP_H */
191 /*
192   Local Variables:
193   mode:c++
194   c-file-style:"stroustrup"
195   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
196   indent-tabs-mode:nil
197   fill-column:99
198   End:
199 */
200 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :