Code

From trunk
[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, Geom::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     Geom::Matrix additional_affine;  
43 };
45 class ObjectSnapper : public Snapper
46 {
48 public:
49     ObjectSnapper(SnapManager const *sm, Geom::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 guideSnap(SnappedConstraints &sc,
70                    Geom::Point const &p,
71                  Geom::Point const &guide_normal) const;
72   
73       bool ThisSnapperMightSnap() const;
74       bool GuidesMightSnap() const;
75   
76       void freeSnap(SnappedConstraints &sc,
77                       Inkscape::SnapPreferences::PointType const &t,
78                       Geom::Point const &p,
79                       bool const &first_point,
80                       boost::optional<Geom::Rect> const &bbox_to_snap,
81                       std::vector<SPItem const *> const *it,
82                       std::vector<Geom::Point> *unselected_nodes) const;
84       void constrainedSnap(SnappedConstraints &sc,
85                       Inkscape::SnapPreferences::PointType const &t,
86                       Geom::Point const &p,
87                       bool const &first_point,                                                                   
88                       boost::optional<Geom::Rect> const &bbox_to_snap,
89                       ConstraintLine const &c,
90                       std::vector<SPItem const *> const *it) const;
91   
92 private:
93     //store some lists of candidates, points and paths, so we don't have to rebuild them for each point we want to snap
94     std::vector<SnapCandidate> *_candidates; 
95     std::vector<Geom::Point> *_points_to_snap_to;
96     std::vector<Geom::PathVector*> *_paths_to_snap_to;
97     
98     void _findCandidates(SPObject* parent,
99                        std::vector<SPItem const *> const *it,
100                        bool const &first_point,
101                        Geom::Rect const &bbox_to_snap,
102                        DimensionToSnap snap_dim,
103                        bool const _clip_or_mask,
104                        Geom::Matrix const additional_affine) const;
105   
106     void _snapNodes(SnappedConstraints &sc,
107                       Inkscape::SnapPreferences::PointType const &t,
108                       Geom::Point const &p, // in desktop coordinates
109                       bool const &first_point,
110                       std::vector<Geom::Point> *unselected_nodes) const; // in desktop coordinates
111                       
112     void _snapTranslatingGuideToNodes(SnappedConstraints &sc,
113                      Inkscape::SnapPreferences::PointType const &t,
114                      Geom::Point const &p,
115                      Geom::Point const &guide_normal) const;
116                      
117     void _collectNodes(Inkscape::SnapPreferences::PointType const &t,
118                   bool const &first_point) const;
119   
120     void _snapPaths(SnappedConstraints &sc,
121                       Inkscape::SnapPreferences::PointType const &t, 
122                       Geom::Point const &p,     // in desktop coordinates
123                       bool const &first_point,
124                       std::vector<Geom::Point> *unselected_nodes, // in desktop coordinates
125                       SPPath const *selected_path) const;
126                       
127     void _snapPathsConstrained(SnappedConstraints &sc,
128                  Inkscape::SnapPreferences::PointType const &t,
129                  Geom::Point const &p, // in desktop coordinates
130                  bool const &first_point,
131                  ConstraintLine const &c) const;
132   
133     bool isUnselectedNode(Geom::Point const &point, std::vector<Geom::Point> const *unselected_nodes) const;
134   
135     void _collectPaths(Inkscape::SnapPreferences::PointType const &t, 
136                   bool const &first_point) const;
137                   
138     void _clear_paths() const;
139     Geom::PathVector* _getBorderPathv() const;
140     Geom::PathVector* _getPathvFromRect(Geom::Rect const rect) const;
141     void _getBorderNodes(std::vector<Geom::Point> *points) const;
142   
143     bool _snap_to_itemnode;
144     bool _snap_to_itempath;
145     bool _snap_to_bboxnode;
146     bool _snap_to_bboxpath;
147     bool _snap_to_page_border;
148   
149     //If enabled, then bbox corners will only snap to bboxes, 
150     //and nodes will only snap to nodes and paths. We will not
151     //snap bbox corners to nodes, or nodes to bboxes.
152     //(snapping to grids and guides is not affected by this)
153     bool _strict_snapping; 
154 };
158 #endif