Code

Snap to page corners too (you won't notice the difference yet because it will snap...
[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"
22 struct SPNamedView;
23 struct SPItem;
24 struct SPObject;
26 namespace Inkscape
27 {
29 class SnapCandidate
30         
31 {
32 public:
33         SnapCandidate(SPItem* item, bool clip_or_mask, NR::Matrix _additional_affine);
34         ~SnapCandidate();
35         
36         SPItem* item;           // An item that is to be considered for snapping to
37         bool clip_or_mask;      // If true, then item refers to a clipping path or a mask
38         
39         /* To find out the absolute position of a clipping path or mask, we not only need to know 
40          * the transformation of the clipping path or mask itself, but also the transformation of 
41          * the object to which the clip or mask is being applied; that transformation is stored here
42          */
43         NR::Matrix additional_affine;  
44 };
46 class ObjectSnapper : public Snapper
47 {
49 public:
50         ObjectSnapper(SPNamedView const *nv, NR::Coord const d);
51         ~ObjectSnapper();
53         enum DimensionToSnap {
54                 GUIDE_TRANSL_SNAP_X, // For snapping a vertical guide (normal in the X-direction) to objects, 
55                 GUIDE_TRANSL_SNAP_Y, // For snapping a horizontal guide (normal in the Y-direction) to objects
56                 ANGLED_GUIDE_TRANSL_SNAP, // For snapping an angled guide, while translating it accross the desktop
57                 ANGLED_GUIDE_ROT_SNAP, // For snapping an angled guide, while rotating it around some pivot point
58                 TRANSL_SNAP_XY}; // All other cases; for snapping to objects, other than guides
60     void setSnapToItemNode(bool s) {_snap_to_itemnode = s;}
61         bool getSnapToItemNode() const {return _snap_to_itemnode;}
62         void setSnapToItemPath(bool s) {_snap_to_itempath = s;}
63         bool getSnapToItemPath() const {return _snap_to_itempath;}
64         void setSnapToBBoxNode(bool s) {_snap_to_bboxnode = s;}
65         bool getSnapToBBoxNode() const {return _snap_to_bboxnode;}
66         void setSnapToBBoxPath(bool s) {_snap_to_bboxpath = s;}
67         bool getSnapToBBoxPath() const {return _snap_to_bboxpath;}
68         void setSnapToPageBorder(bool s) {_snap_to_page_border = s;}
69         bool getSnapToPageBorder() const {return _snap_to_page_border;}
70         void setIncludeItemCenter(bool s) {_include_item_center = s;}
71         bool getIncludeItemCenter() const {return _include_item_center;}
72         void setStrictSnapping(bool enabled) {_strict_snapping = enabled;}
73         void guideSnap(SnappedConstraints &sc,
74                                  NR::Point const &p,
75                  NR::Point const &guide_normal) const;
76   
77         bool ThisSnapperMightSnap() const;
78         bool GuidesMightSnap() const;
79   
80         void freeSnap(SnappedConstraints &sc,
81                       Inkscape::Snapper::PointType const &t,
82                       NR::Point const &p,
83                       bool const &first_point,
84                       NR::Maybe<NR::Rect> const &bbox_to_snap,
85                       std::vector<SPItem const *> const *it,
86                       std::vector<NR::Point> *unselected_nodes) const;
88         void constrainedSnap(SnappedConstraints &sc,
89                       Inkscape::Snapper::PointType const &t,
90                       NR::Point const &p,
91                       bool const &first_point,                                                                   
92                       NR::Maybe<NR::Rect> const &bbox_to_snap,
93                       ConstraintLine const &c,
94                       std::vector<SPItem const *> const *it) const;
95   
96 private:
97         //store some lists of candidates, points and paths, so we don't have to rebuild them for each point we want to snap
98         std::vector<SnapCandidate> *_candidates; 
99         std::vector<NR::Point> *_points_to_snap_to;
100         std::vector<NArtBpath*> *_bpaths_to_snap_to;
101         std::vector<Path*> *_paths_to_snap_to;
102   
103         void _findCandidates(SPObject* parent,
104                        std::vector<SPItem const *> const *it,
105                        bool const &first_point,
106                        NR::Rect const &bbox_to_snap,
107                        DimensionToSnap snap_dim,
108                        bool const _clip_or_mask,
109                        NR::Matrix const additional_affine) const;
110   
111         void _snapNodes(SnappedConstraints &sc,
112                       Inkscape::Snapper::PointType const &t,
113                       NR::Point const &p, 
114                       bool const &first_point,
115                       std::vector<NR::Point> *unselected_nodes) const;
116                       
117         void _snapTranslatingGuideToNodes(SnappedConstraints &sc,
118                      Inkscape::Snapper::PointType const &t,
119                      NR::Point const &p,
120                      NR::Point const &guide_normal) const;
121                      
122         void _collectNodes(Inkscape::Snapper::PointType const &t,
123                   bool const &first_point) const;
124   
125         void _snapPaths(SnappedConstraints &sc,
126                       Inkscape::Snapper::PointType const &t, 
127                       NR::Point const &p,
128                       bool const &first_point,
129                       std::vector<NR::Point> *unselected_nodes,
130                       SPPath const *selected_path) const;
131                       
132         void _snapPathsConstrained(SnappedConstraints &sc,
133                  Inkscape::Snapper::PointType const &t,
134                  NR::Point const &p,
135                  bool const &first_point,
136                  ConstraintLine const &c) const;
137   
138         bool isUnselectedNode(NR::Point const &point, std::vector<NR::Point> const *unselected_nodes) const;
139   
140         void _collectPaths(Inkscape::Snapper::PointType const &t, 
141                   bool const &first_point) const;
142                   
143         void _clear_paths() const;
144         NArtBpath const* _getBorderBPath() const;
145         void _getBorderNodes(std::vector<NR::Point> *points) const;
146   
147         bool _snap_to_itemnode;
148         bool _snap_to_itempath;
149         bool _snap_to_bboxnode;
150         bool _snap_to_bboxpath;
151         bool _snap_to_page_border;
152   
153         //If enabled, then bbox corners will only snap to bboxes, 
154         //and nodes will only snap to nodes and paths. We will not
155         //snap bbox corners to nodes, or nodes to bboxes.
156         //(snapping to grids and guides is not affected by this)
157         bool _strict_snapping; 
158         bool _include_item_center;
159 };
163 #endif