Code

NR::Maybe => boost::optional
[inkscape.git] / src / object-snapper.h
1 #ifndef SEEN_OBJECT_SNAPPER_H
2 #define SEEN_OBJECT_SNAPPER_H
4 /**
5  *  \file object-snapper.h
6  *  \brief Snapping things to objects.
7  *
8  * Authors:
9  *   Carl Hetherington <inkscape@carlh.net>
10  *   Diederik van Lierop <mail@diedenrezi.nl>
11  *
12  * Copyright (C) 2005 - 2008 Authors 
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #include "snapper.h"
18 #include "sp-path.h"
19 #include "splivarot.h"
21 struct SPNamedView;
22 struct SPItem;
23 struct SPObject;
25 namespace Inkscape
26 {
28 class SnapCandidate
29     
30 {
31 public:
32     SnapCandidate(SPItem* item, bool clip_or_mask, NR::Matrix _additional_affine);
33     ~SnapCandidate();
34     
35     SPItem* item;        // An item that is to be considered for snapping to
36     bool clip_or_mask;    // If true, then item refers to a clipping path or a mask
37     
38     /* To find out the absolute position of a clipping path or mask, we not only need to know 
39      * the transformation of the clipping path or mask itself, but also the transformation of 
40      * the object to which the clip or mask is being applied; that transformation is stored here
41      */
42     NR::Matrix additional_affine;  
43 };
45 class ObjectSnapper : public Snapper
46 {
48 public:
49     ObjectSnapper(SPNamedView const *nv, NR::Coord const d);
50     ~ObjectSnapper();
52       enum DimensionToSnap {
53           GUIDE_TRANSL_SNAP_X, // For snapping a vertical guide (normal in the X-direction) to objects, 
54           GUIDE_TRANSL_SNAP_Y, // For snapping a horizontal guide (normal in the Y-direction) to objects
55           ANGLED_GUIDE_TRANSL_SNAP, // For snapping an angled guide, while translating it accross the desktop
56           ANGLED_GUIDE_ROT_SNAP, // For snapping an angled guide, while rotating it around some pivot point
57           TRANSL_SNAP_XY}; // All other cases; for snapping to objects, other than guides
59     void setSnapToItemNode(bool s) {_snap_to_itemnode = s;}
60       bool getSnapToItemNode() const {return _snap_to_itemnode;}
61       void setSnapToItemPath(bool s) {_snap_to_itempath = s;}
62       bool getSnapToItemPath() const {return _snap_to_itempath;}
63       void setSnapToBBoxNode(bool s) {_snap_to_bboxnode = s;}
64       bool getSnapToBBoxNode() const {return _snap_to_bboxnode;}
65       void setSnapToBBoxPath(bool s) {_snap_to_bboxpath = s;}
66       bool getSnapToBBoxPath() const {return _snap_to_bboxpath;}
67       void setSnapToPageBorder(bool s) {_snap_to_page_border = s;}
68       bool getSnapToPageBorder() const {return _snap_to_page_border;}
69       void setIncludeItemCenter(bool s) {_include_item_center = s;}
70       bool getIncludeItemCenter() const {return _include_item_center;}
71       void setStrictSnapping(bool enabled) {_strict_snapping = enabled;}
72       void guideSnap(SnappedConstraints &sc,
73                    NR::Point const &p,
74                  NR::Point const &guide_normal) const;
75   
76       bool ThisSnapperMightSnap() const;
77       bool GuidesMightSnap() const;
78   
79       void freeSnap(SnappedConstraints &sc,
80                       Inkscape::Snapper::PointType const &t,
81                       NR::Point const &p,
82                       bool const &first_point,
83                       boost::optional<NR::Rect> const &bbox_to_snap,
84                       std::vector<SPItem const *> const *it,
85                       std::vector<NR::Point> *unselected_nodes) const;
87       void constrainedSnap(SnappedConstraints &sc,
88                       Inkscape::Snapper::PointType const &t,
89                       NR::Point const &p,
90                       bool const &first_point,                                                                   
91                       boost::optional<NR::Rect> const &bbox_to_snap,
92                       ConstraintLine const &c,
93                       std::vector<SPItem const *> const *it) const;
94   
95 private:
96     //store some lists of candidates, points and paths, so we don't have to rebuild them for each point we want to snap
97     std::vector<SnapCandidate> *_candidates; 
98     std::vector<NR::Point> *_points_to_snap_to;
99     std::vector<Geom::PathVector*> *_paths_to_snap_to;
100     
101     void _findCandidates(SPObject* parent,
102                        std::vector<SPItem const *> const *it,
103                        bool const &first_point,
104                        NR::Rect const &bbox_to_snap,
105                        DimensionToSnap snap_dim,
106                        bool const _clip_or_mask,
107                        NR::Matrix const additional_affine) const;
108   
109     void _snapNodes(SnappedConstraints &sc,
110                       Inkscape::Snapper::PointType const &t,
111                       NR::Point const &p, 
112                       bool const &first_point,
113                       std::vector<NR::Point> *unselected_nodes) const;
114                       
115     void _snapTranslatingGuideToNodes(SnappedConstraints &sc,
116                      Inkscape::Snapper::PointType const &t,
117                      NR::Point const &p,
118                      NR::Point const &guide_normal) const;
119                      
120     void _collectNodes(Inkscape::Snapper::PointType const &t,
121                   bool const &first_point) const;
122   
123     void _snapPaths(SnappedConstraints &sc,
124                       Inkscape::Snapper::PointType const &t, 
125                       NR::Point const &p,
126                       bool const &first_point,
127                       std::vector<NR::Point> *unselected_nodes,
128                       SPPath const *selected_path) const;
129                       
130     void _snapPathsConstrained(SnappedConstraints &sc,
131                  Inkscape::Snapper::PointType const &t,
132                  NR::Point const &p,
133                  bool const &first_point,
134                  ConstraintLine const &c) const;
135   
136     bool isUnselectedNode(NR::Point const &point, std::vector<NR::Point> const *unselected_nodes) const;
137   
138     void _collectPaths(Inkscape::Snapper::PointType const &t, 
139                   bool const &first_point) const;
140                   
141     void _clear_paths() const;
142     Geom::PathVector* _getBorderPathv() const;
143     Geom::PathVector* _getPathvFromRect(Geom::Rect const rect) const;
144     void _getBorderNodes(std::vector<NR::Point> *points) const;
145   
146     bool _snap_to_itemnode;
147     bool _snap_to_itempath;
148     bool _snap_to_bboxnode;
149     bool _snap_to_bboxpath;
150     bool _snap_to_page_border;
151   
152     //If enabled, then bbox corners will only snap to bboxes, 
153     //and nodes will only snap to nodes and paths. We will not
154     //snap bbox corners to nodes, or nodes to bboxes.
155     //(snapping to grids and guides is not affected by this)
156     bool _strict_snapping; 
157     bool _include_item_center;
158 };
162 #endif