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"
20 #include "desktop.h"
22 struct SPNamedView;
23 struct SPItem;
24 struct SPObject;
26 namespace Inkscape
27 {
29 class SnapCandidate
31 {
32 public:
33 SnapCandidate(SPItem* item, bool clip_or_mask, Geom::Matrix _additional_affine);
34 ~SnapCandidate();
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
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 Geom::Matrix additional_affine;
44 };
46 class ObjectSnapper : public Snapper
47 {
49 public:
50 ObjectSnapper(SPNamedView const *nv, Geom::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 setDesktop(SPDesktop const *desktop) {_desktop = desktop;}
74 void guideSnap(SnappedConstraints &sc,
75 Geom::Point const &p,
76 Geom::Point const &guide_normal) const;
78 bool ThisSnapperMightSnap() const;
79 bool GuidesMightSnap() const;
81 void freeSnap(SnappedConstraints &sc,
82 Inkscape::Snapper::PointType const &t,
83 Geom::Point const &p,
84 bool const &first_point,
85 boost::optional<Geom::Rect> const &bbox_to_snap,
86 std::vector<SPItem const *> const *it,
87 std::vector<Geom::Point> *unselected_nodes) const;
89 void constrainedSnap(SnappedConstraints &sc,
90 Inkscape::Snapper::PointType const &t,
91 Geom::Point const &p,
92 bool const &first_point,
93 boost::optional<Geom::Rect> const &bbox_to_snap,
94 ConstraintLine const &c,
95 std::vector<SPItem const *> const *it) const;
97 private:
98 //store some lists of candidates, points and paths, so we don't have to rebuild them for each point we want to snap
99 std::vector<SnapCandidate> *_candidates;
100 std::vector<Geom::Point> *_points_to_snap_to;
101 std::vector<Geom::PathVector*> *_paths_to_snap_to;
103 void _findCandidates(SPObject* parent,
104 std::vector<SPItem const *> const *it,
105 bool const &first_point,
106 Geom::Rect const &bbox_to_snap,
107 DimensionToSnap snap_dim,
108 bool const _clip_or_mask,
109 Geom::Matrix const additional_affine) const;
111 void _snapNodes(SnappedConstraints &sc,
112 Inkscape::Snapper::PointType const &t,
113 Geom::Point const &p, // in desktop coordinates
114 bool const &first_point,
115 std::vector<Geom::Point> *unselected_nodes) const; // in desktop coordinates
117 void _snapTranslatingGuideToNodes(SnappedConstraints &sc,
118 Inkscape::Snapper::PointType const &t,
119 Geom::Point const &p,
120 Geom::Point const &guide_normal) const;
122 void _collectNodes(Inkscape::Snapper::PointType const &t,
123 bool const &first_point) const;
125 void _snapPaths(SnappedConstraints &sc,
126 Inkscape::Snapper::PointType const &t,
127 Geom::Point const &p, // in desktop coordinates
128 bool const &first_point,
129 std::vector<Geom::Point> *unselected_nodes, // in desktop coordinates
130 SPPath const *selected_path) const;
132 void _snapPathsConstrained(SnappedConstraints &sc,
133 Inkscape::Snapper::PointType const &t,
134 Geom::Point const &p, // in desktop coordinates
135 bool const &first_point,
136 ConstraintLine const &c) const;
138 bool isUnselectedNode(Geom::Point const &point, std::vector<Geom::Point> const *unselected_nodes) const;
140 void _collectPaths(Inkscape::Snapper::PointType const &t,
141 bool const &first_point) const;
143 void _clear_paths() const;
144 Geom::PathVector* _getBorderPathv() const;
145 Geom::PathVector* _getPathvFromRect(Geom::Rect const rect) const;
146 void _getBorderNodes(std::vector<Geom::Point> *points) const;
148 bool _snap_to_itemnode;
149 bool _snap_to_itempath;
150 bool _snap_to_bboxnode;
151 bool _snap_to_bboxpath;
152 bool _snap_to_page_border;
154 //If enabled, then bbox corners will only snap to bboxes,
155 //and nodes will only snap to nodes and paths. We will not
156 //snap bbox corners to nodes, or nodes to bboxes.
157 //(snapping to grids and guides is not affected by this)
158 bool _strict_snapping;
159 bool _include_item_center;
160 SPDesktop const *_desktop;
161 };
163 }
165 #endif