From f38d1b851669bf0fa14a2ca67d9bde7d3fb2dc9b Mon Sep 17 00:00:00 2001 From: dvlierop2 Date: Sun, 7 Sep 2008 15:12:57 +0000 Subject: [PATCH] If a snapper needs a reference to a SPNamedView or a SPDesktop, then get that reference via the SnapManager (instead of storing references in the snapper and accessing them directly) --- src/display/canvas-axonomgrid.cpp | 4 +-- src/display/canvas-axonomgrid.h | 2 +- src/display/canvas-grid.cpp | 6 ++-- src/display/canvas-grid.h | 2 +- src/guide-snapper.cpp | 8 ++--- src/guide-snapper.h | 2 +- src/line-snapper.cpp | 2 +- src/line-snapper.h | 2 +- src/object-snapper.cpp | 50 ++++++++++++++----------------- src/object-snapper.h | 2 +- src/snap.cpp | 9 ++++-- src/snap.h | 2 ++ src/snapper.cpp | 7 ++--- src/snapper.h | 10 +++---- 14 files changed, 55 insertions(+), 53 deletions(-) diff --git a/src/display/canvas-axonomgrid.cpp b/src/display/canvas-axonomgrid.cpp index 0c2c627c9..0294f7420 100644 --- a/src/display/canvas-axonomgrid.cpp +++ b/src/display/canvas-axonomgrid.cpp @@ -211,7 +211,7 @@ CanvasAxonomGrid::CanvasAxonomGrid (SPNamedView * nv, Inkscape::XML::Node * in_r angle_rad[Z] = deg_to_rad(angle_deg[Z]); tan_angle[Z] = tan(angle_rad[Z]); - snapper = new CanvasAxonomGridSnapper(this, namedview, 0); + snapper = new CanvasAxonomGridSnapper(this, &namedview->snap_manager, 0); if (repr) readRepr(); } @@ -654,7 +654,7 @@ CanvasAxonomGrid::Render (SPCanvasBuf *buf) } } -CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, Geom::Coord const d) : LineSnapper(nv, d) +CanvasAxonomGridSnapper::CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SnapManager const *sm, Geom::Coord const d) : LineSnapper(sm, d) { this->grid = grid; } diff --git a/src/display/canvas-axonomgrid.h b/src/display/canvas-axonomgrid.h index f5f9019d2..f8dc8e25a 100644 --- a/src/display/canvas-axonomgrid.h +++ b/src/display/canvas-axonomgrid.h @@ -71,7 +71,7 @@ private: class CanvasAxonomGridSnapper : public LineSnapper { public: - CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SPNamedView const *nv, Geom::Coord const d); + CanvasAxonomGridSnapper(CanvasAxonomGrid *grid, SnapManager const *sm, Geom::Coord const d); private: LineList _getSnapLines(Geom::Point const &p) const; diff --git a/src/display/canvas-grid.cpp b/src/display/canvas-grid.cpp index 47cff6d44..6d6b5d89f 100644 --- a/src/display/canvas-grid.cpp +++ b/src/display/canvas-grid.cpp @@ -427,7 +427,7 @@ CanvasXYGrid::CanvasXYGrid (SPNamedView * nv, Inkscape::XML::Node * in_repr, SPD spacing[NR::Y] = sp_units_get_pixels( prefs_get_double_attribute ("options.grids.xy", "spacing_y", 0.0), *(gridunit) ); render_dotted = prefs_get_int_attribute ("options.grids.xy", "dotted", 0) == 1; - snapper = new CanvasXYGridSnapper(this, namedview, 0); + snapper = new CanvasXYGridSnapper(this, &namedview->snap_manager, 0); if (repr) readRepr(); } @@ -947,7 +947,7 @@ CanvasXYGrid::Render (SPCanvasBuf *buf) } } -CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d) +CanvasXYGridSnapper::CanvasXYGridSnapper(CanvasXYGrid *grid, SnapManager const *sm, NR::Coord const d) : LineSnapper(sm, d) { this->grid = grid; } @@ -998,7 +998,7 @@ void CanvasXYGridSnapper::_addSnappedLine(SnappedConstraints &sc, Geom::Point co */ bool CanvasXYGridSnapper::ThisSnapperMightSnap() const { - return _named_view == NULL ? false : (_snap_enabled && _snap_from != 0); + return _snap_enabled && _snap_from != 0; } diff --git a/src/display/canvas-grid.h b/src/display/canvas-grid.h index a734cac84..20cb31473 100644 --- a/src/display/canvas-grid.h +++ b/src/display/canvas-grid.h @@ -161,7 +161,7 @@ private: class CanvasXYGridSnapper : public LineSnapper { public: - CanvasXYGridSnapper(CanvasXYGrid *grid, SPNamedView const *nv, Geom::Coord const d); + CanvasXYGridSnapper(CanvasXYGrid *grid, SnapManager const *sm, Geom::Coord const d); bool ThisSnapperMightSnap() const; private: diff --git a/src/guide-snapper.cpp b/src/guide-snapper.cpp index 059d99a19..d1991e75d 100644 --- a/src/guide-snapper.cpp +++ b/src/guide-snapper.cpp @@ -17,7 +17,7 @@ #include "sp-namedview.h" #include "sp-guide.h" -Inkscape::GuideSnapper::GuideSnapper(SPNamedView const *nv, Geom::Coord const d) : LineSnapper(nv, d) +Inkscape::GuideSnapper::GuideSnapper(SnapManager const *sm, Geom::Coord const d) : LineSnapper(sm, d) { } @@ -26,11 +26,11 @@ Inkscape::GuideSnapper::LineList Inkscape::GuideSnapper::_getSnapLines(Geom::Poi { LineList s; - if ( NULL == _named_view || ThisSnapperMightSnap() == false) { + if ( NULL == _snapmanager->getNamedView() || ThisSnapperMightSnap() == false) { return s; } - for (GSList const *l = _named_view->guides; l != NULL; l = l->next) { + for (GSList const *l = _snapmanager->getNamedView()->guides; l != NULL; l = l->next) { SPGuide const *g = SP_GUIDE(l->data); s.push_back(std::make_pair(g->normal_to_line, g->point_on_line)); } @@ -43,7 +43,7 @@ Inkscape::GuideSnapper::LineList Inkscape::GuideSnapper::_getSnapLines(Geom::Poi */ bool Inkscape::GuideSnapper::ThisSnapperMightSnap() const { - return _named_view == NULL ? false : (_snap_enabled && _snap_from != 0 && _named_view->showguides); + return _snapmanager->getNamedView() == NULL ? false : (_snap_enabled && _snap_from != 0 && _snapmanager->getNamedView()->showguides); } void Inkscape::GuideSnapper::_addSnappedLine(SnappedConstraints &sc, Geom::Point const snapped_point, Geom::Coord const snapped_distance, Geom::Point const normal_to_line, Geom::Point const point_on_line) const diff --git a/src/guide-snapper.h b/src/guide-snapper.h index df6086ccf..a7798c72f 100644 --- a/src/guide-snapper.h +++ b/src/guide-snapper.h @@ -28,7 +28,7 @@ namespace Inkscape class GuideSnapper : public LineSnapper { public: - GuideSnapper(SPNamedView const *nv, Geom::Coord const d); + GuideSnapper(SnapManager const *sm, Geom::Coord const d); bool ThisSnapperMightSnap() const; private: diff --git a/src/line-snapper.cpp b/src/line-snapper.cpp index db7a20e4f..5453c1cf2 100644 --- a/src/line-snapper.cpp +++ b/src/line-snapper.cpp @@ -18,7 +18,7 @@ #include "snapped-line.h" #include -Inkscape::LineSnapper::LineSnapper(SPNamedView const *nv, Geom::Coord const d) : Snapper(nv, d) +Inkscape::LineSnapper::LineSnapper(SnapManager const *sm, Geom::Coord const d) : Snapper(sm, d) { } diff --git a/src/line-snapper.h b/src/line-snapper.h index 6af2e87d4..7767fe2ea 100644 --- a/src/line-snapper.h +++ b/src/line-snapper.h @@ -19,7 +19,7 @@ namespace Inkscape class LineSnapper : public Snapper { public: - LineSnapper(SPNamedView const *nv, Geom::Coord const d); + LineSnapper(SnapManager const *sm, Geom::Coord const d); void freeSnap(SnappedConstraints &sc, Inkscape::Snapper::PointType const &t, diff --git a/src/object-snapper.cpp b/src/object-snapper.cpp index c620e91cd..884b561b1 100644 --- a/src/object-snapper.cpp +++ b/src/object-snapper.cpp @@ -44,8 +44,8 @@ Inkscape::SnapCandidate::~SnapCandidate() { } -Inkscape::ObjectSnapper::ObjectSnapper(SPNamedView const *nv, Geom::Coord const d) - : Snapper(nv, d), _snap_to_itemnode(true), _snap_to_itempath(true), +Inkscape::ObjectSnapper::ObjectSnapper(SnapManager const *sm, Geom::Coord const d) + : Snapper(sm, d), _snap_to_itemnode(true), _snap_to_itempath(true), _snap_to_bboxnode(true), _snap_to_bboxpath(true), _snap_to_page_border(false), _strict_snapping(true), _include_item_center(false) { @@ -98,8 +98,8 @@ void Inkscape::ObjectSnapper::_findCandidates(SPObject* parent, bbox_to_snap_incl.expandBy(getSnapperTolerance()); // see? for (SPObject* o = sp_object_first_child(parent); o != NULL; o = SP_OBJECT_NEXT(o)) { - g_assert(_named_view->snap_manager.getDesktop() != NULL); - if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(_named_view->snap_manager.getDesktop()->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) { + g_assert(_snapmanager->getDesktop() != NULL); + if (SP_IS_ITEM(o) && !SP_ITEM(o)->isLocked() && !(_snapmanager->getDesktop()->itemIsHidden(SP_ITEM(o)) && !clip_or_mask)) { // Don't snap to locked items, and // don't snap to hidden objects, unless they're a clipped path or a mask /* See if this item is on the ignore list */ @@ -413,8 +413,8 @@ void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc, _collectPaths(t, first_point); // Now we can finally do the real snapping, using the paths collected above - g_assert(_named_view->snap_manager.getDesktop() != NULL); - Geom::Point const p_doc = _named_view->snap_manager.getDesktop()->dt2doc(p); + g_assert(_snapmanager->getDesktop() != NULL); + Geom::Point const p_doc = _snapmanager->getDesktop()->dt2doc(p); bool const node_tool_active = _snap_to_itempath && selected_path != NULL; @@ -463,13 +463,13 @@ void Inkscape::ObjectSnapper::_snapPaths(SnappedConstraints &sc, * piece are unselected; if they are then this piece must be stationary */ g_assert(unselected_nodes != NULL); - Geom::Point start_pt = _named_view->snap_manager.getDesktop()->doc2dt(curve->pointAt(0)); - Geom::Point end_pt = _named_view->snap_manager.getDesktop()->doc2dt(curve->pointAt(1)); + Geom::Point start_pt = _snapmanager->getDesktop()->doc2dt(curve->pointAt(0)); + Geom::Point end_pt = _snapmanager->getDesktop()->doc2dt(curve->pointAt(1)); c1 = isUnselectedNode(start_pt, unselected_nodes); c2 = isUnselectedNode(end_pt, unselected_nodes); } - Geom::Point const sp_dt = _named_view->snap_manager.getDesktop()->doc2dt(sp_doc); + Geom::Point const sp_dt = _snapmanager->getDesktop()->doc2dt(sp_doc); if (!being_edited || (c1 && c2)) { Geom::Coord const dist = Geom::distance(sp_doc, p_doc); if (dist < getSnapperTolerance()) { @@ -512,8 +512,8 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc, // Now we can finally do the real snapping, using the paths collected above - g_assert(_named_view->snap_manager.getDesktop() != NULL); - Geom::Point const p_doc = _named_view->snap_manager.getDesktop()->dt2doc(p); + g_assert(_snapmanager->getDesktop() != NULL); + Geom::Point const p_doc = _snapmanager->getDesktop()->dt2doc(p); Geom::Point direction_vector = c.getDirection(); if (!is_zero(direction_vector)) { @@ -527,8 +527,8 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc, // must lie within two points on the constraintline: p_min_on_cl and p_max_on_cl // The distance between those points is twice the snapping tolerance Geom::Point const p_proj_on_cl = project_on_linesegment(p, p1_on_cl, p2_on_cl); - Geom::Point const p_min_on_cl = _named_view->snap_manager.getDesktop()->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector); - Geom::Point const p_max_on_cl = _named_view->snap_manager.getDesktop()->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector); + Geom::Point const p_min_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl - getSnapperTolerance() * direction_vector); + Geom::Point const p_max_on_cl = _snapmanager->getDesktop()->dt2doc(p_proj_on_cl + getSnapperTolerance() * direction_vector); Geom::Path cl; std::vector clv; @@ -548,8 +548,8 @@ void Inkscape::ObjectSnapper::_snapPathsConstrained(SnappedConstraints &sc, Geom::Point p_inters = p_min_on_cl + ((*m).ta) * (p_max_on_cl - p_min_on_cl); // When it's within snapping range, then return it // (within snapping range == between p_min_on_cl and p_max_on_cl == 0 < ta < 1) - Geom::Coord dist = Geom::L2(_named_view->snap_manager.getDesktop()->dt2doc(p_proj_on_cl) - p_inters); - SnappedPoint s(_named_view->snap_manager.getDesktop()->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap()); + Geom::Coord dist = Geom::L2(_snapmanager->getDesktop()->dt2doc(p_proj_on_cl) - p_inters); + SnappedPoint s(_snapmanager->getDesktop()->doc2dt(p_inters), SNAPTARGET_PATH, dist, getSnapperTolerance(), getSnapperAlwaysSnap()); sc.points.push_back(s); } } @@ -567,14 +567,14 @@ void Inkscape::ObjectSnapper::freeSnap(SnappedConstraints &sc, std::vector const *it, std::vector *unselected_nodes) const { - if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) { + if (_snap_enabled == false || getSnapFrom(t) == false ) { return; } /* Get a list of all the SPItems that we will try to snap to */ if (first_point) { Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p); - _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity()); + _findCandidates(sp_document_root(_snapmanager->getDocument()), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity()); } if (_snap_to_itemnode || _snap_to_bboxnode || _snap_to_page_border) { @@ -610,14 +610,14 @@ void Inkscape::ObjectSnapper::constrainedSnap( SnappedConstraints &sc, ConstraintLine const &c, std::vector const *it) const { - if (_snap_enabled == false || getSnapFrom(t) == false || _named_view == NULL) { + if (_snap_enabled == false || getSnapFrom(t) == false) { return; } /* Get a list of all the SPItems that we will try to snap to */ if (first_point) { Geom::Rect const local_bbox_to_snap = bbox_to_snap ? *bbox_to_snap : Geom::Rect(p, p); - _findCandidates(sp_document_root(_named_view->document), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity()); + _findCandidates(sp_document_root(_snapmanager->getDocument()), it, first_point, local_bbox_to_snap, TRANSL_SNAP_XY, false, Geom::identity()); } // A constrained snap, is a snap in only one degree of freedom (specified by the constraint line). @@ -640,10 +640,6 @@ void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc, Geom::Point const &p, Geom::Point const &guide_normal) const { - if ( NULL == _named_view ) { - return; - } - /* Get a list of all the SPItems that we will try to snap to */ std::vector cand; std::vector const it; //just an empty list @@ -667,7 +663,7 @@ void Inkscape::ObjectSnapper::guideSnap(SnappedConstraints &sc, // second time on an object; but should this point then be constrained to the // line, or can it be located anywhere?) - _findCandidates(sp_document_root(_named_view->document), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity()); + _findCandidates(sp_document_root(_snapmanager->getDocument()), &it, true, Geom::Rect(p, p), snap_dim, false, Geom::identity()); _snapTranslatingGuideToNodes(sc, Inkscape::Snapper::SNAPPOINT_GUIDE, p, guide_normal); // _snapRotatingGuideToNodes has not been implemented yet. } @@ -697,7 +693,7 @@ void Inkscape::ObjectSnapper::_clear_paths() const Geom::PathVector* Inkscape::ObjectSnapper::_getBorderPathv() const { - Geom::Rect const border_rect = Geom::Rect(Geom::Point(0,0), Geom::Point(sp_document_width(_named_view->document),sp_document_height(_named_view->document))); + Geom::Rect const border_rect = Geom::Rect(Geom::Point(0,0), Geom::Point(sp_document_width(_snapmanager->getDocument()),sp_document_height(_snapmanager->getDocument()))); return _getPathvFromRect(border_rect); } @@ -714,8 +710,8 @@ Geom::PathVector* Inkscape::ObjectSnapper::_getPathvFromRect(Geom::Rect const re void Inkscape::ObjectSnapper::_getBorderNodes(std::vector *points) const { - Geom::Coord w = sp_document_width(_named_view->document); - Geom::Coord h = sp_document_height(_named_view->document); + Geom::Coord w = sp_document_width(_snapmanager->getDocument()); + Geom::Coord h = sp_document_height(_snapmanager->getDocument()); points->push_back(Geom::Point(0,0)); points->push_back(Geom::Point(0,h)); points->push_back(Geom::Point(w,h)); diff --git a/src/object-snapper.h b/src/object-snapper.h index 02b4b422b..c567e115b 100644 --- a/src/object-snapper.h +++ b/src/object-snapper.h @@ -46,7 +46,7 @@ class ObjectSnapper : public Snapper { public: - ObjectSnapper(SPNamedView const *nv, Geom::Coord const d); + ObjectSnapper(SnapManager const *sm, Geom::Coord const d); ~ObjectSnapper(); enum DimensionToSnap { diff --git a/src/snap.cpp b/src/snap.cpp index 15bc8658d..c4ed536e5 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -44,8 +44,8 @@ using std::vector; */ SnapManager::SnapManager(SPNamedView const *v) : - guide(v, 0), - object(v, 0), + guide(this, 0), + object(this, 0), _named_view(v), _include_item_center(false), _snap_enabled_globally(true) @@ -903,6 +903,11 @@ void SnapManager::setup(SPDesktop const *desktop, bool snapindicator, std::vecto _unselected_nodes = unselected_nodes; } +SPDocument *SnapManager::getDocument() const +{ + return _named_view->document; +} + /* Local Variables: mode:c++ diff --git a/src/snap.h b/src/snap.h index 480ca9869..df0860274 100644 --- a/src/snap.h +++ b/src/snap.h @@ -120,6 +120,8 @@ public: SnapperList getGridSnappers() const; SPDesktop const *getDesktop() const {return _desktop;} + SPNamedView const *getNamedView() const {return _named_view;} + SPDocument *getDocument() const; void setSnapModeBBox(bool enabled); void setSnapModeNode(bool enabled); diff --git a/src/snapper.cpp b/src/snapper.cpp index 35aa012dc..51ffaa42d 100644 --- a/src/snapper.cpp +++ b/src/snapper.cpp @@ -23,11 +23,10 @@ Inkscape::Snapper::PointType const Inkscape::Snapper::SNAPPOINT_GUIDE = 0x4; * \param nv Named view. * \param d Snap tolerance. */ -Inkscape::Snapper::Snapper(SPNamedView const *nv, Geom::Coord const t) : _named_view(nv), _snap_enabled(true), _snapper_tolerance(t) +Inkscape::Snapper::Snapper(SnapManager const *sm, Geom::Coord const t) : _snapmanager(sm), _snap_enabled(true), _snapper_tolerance(t) { - g_assert(_named_view != NULL); - g_assert(SP_IS_NAMEDVIEW(_named_view)); - + g_assert(_snapmanager != NULL); + setSnapFrom(SNAPPOINT_BBOX | SNAPPOINT_NODE, true); //Snap any point. In v0.45 and earlier, this was controlled in the preferences tab } diff --git a/src/snapper.h b/src/snapper.h index 18967ebd9..97068bf10 100644 --- a/src/snapper.h +++ b/src/snapper.h @@ -30,7 +30,7 @@ struct SnappedConstraints { std::list curves; }; -struct SPNamedView; +class SnapManager; struct SPItem; namespace Inkscape @@ -41,7 +41,7 @@ class Snapper { public: Snapper() {} - Snapper(SPNamedView const *nv, ::Geom::Coord const d); + Snapper(SnapManager const *sm, ::Geom::Coord const d); virtual ~Snapper() {} /// Point types to snap. @@ -112,12 +112,12 @@ public: std::vector const */*it*/) const {}; protected: - SPNamedView const *_named_view; - int _snap_from; ///< bitmap of point types that we will snap from + SnapManager const *_snapmanager; + int _snap_from; ///< bitmap of point types that we will snap from bool _snap_enabled; ///< true if this snapper is enabled, otherwise false private: - Geom::Coord _snapper_tolerance; ///< snap tolerance in desktop coordinates + Geom::Coord _snapper_tolerance; ///< snap tolerance in desktop coordinates // must be private to enforce the usage of getTolerance(), which retrieves // the tolerance in screen pixels (making it zoom independent) -- 2.30.2