summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDiederik van Lierop <mail@diedenrezi.nl>2008-08-06 21:28:34 +0000
committerdvlierop2 <dvlierop2@users.sourceforge.net>2008-08-06 21:28:34 +0000
commitf46cc1179e44ec373cfaa02c7140fda8757660cf (patch)
treee9fb08e0305016e391d6426076110b5271295e3d /src
parentsrc/helper 2geomified (diff)
downloadinkscape-f46cc1179e44ec373cfaa02c7140fda8757660cf.tar.gz
inkscape-f46cc1179e44ec373cfaa02c7140fda8757660cf.zip
1) When pasting, use an offset that is a multiple of the grid pitch (got lost during a previous round of refactoring)
2) Refactor some of the pasting code (bzr r6580)
Diffstat (limited to 'src')
-rw-r--r--src/display/canvas-axonomgrid.h9
-rw-r--r--src/display/canvas-grid.h13
-rw-r--r--src/snap.cpp54
-rw-r--r--src/snap.h2
-rw-r--r--src/ui/clipboard.cpp80
5 files changed, 118 insertions, 40 deletions
diff --git a/src/display/canvas-axonomgrid.h b/src/display/canvas-axonomgrid.h
index 668c59649..564d266a6 100644
--- a/src/display/canvas-axonomgrid.h
+++ b/src/display/canvas-axonomgrid.h
@@ -41,18 +41,13 @@ public:
void readRepr();
void onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive);
- SPUnit const* gridunit;
-
- NR::Point origin; /**< Origin of the grid */
double lengthy; /**< The lengths of the primary y-axis */
double angle_deg[3]; /**< Angle of each axis (note that angle[2] == 0) */
double angle_rad[3]; /**< Angle of each axis (note that angle[2] == 0) */
double tan_angle[3]; /**< tan(angle[.]) */
- guint32 color; /**< Color for normal lines */
- guint32 empcolor; /**< Color for emphasis lines */
- gint empspacing; /**< Spacing between emphasis lines */
+
bool scaled; /**< Whether the grid is in scaled mode */
-
+
NR::Point ow; /**< Transformed origin by the affine for the zoom */
double lyw; /**< Transformed length y by the affine for the zoom */
double lxw_x;
diff --git a/src/display/canvas-grid.h b/src/display/canvas-grid.h
index a9e317534..5d674bb2d 100644
--- a/src/display/canvas-grid.h
+++ b/src/display/canvas-grid.h
@@ -89,6 +89,13 @@ public:
Gtk::Widget * newWidget();
+ NR::Point origin; /**< Origin of the grid */
+ guint32 color; /**< Color for normal lines */
+ guint32 empcolor; /**< Color for emphasis lines */
+ gint empspacing; /**< Spacing between emphasis lines */
+
+ SPUnit const* gridunit;
+
Inkscape::XML::Node * repr;
SPDocument *doc;
@@ -130,12 +137,6 @@ public:
void readRepr();
void onReprAttrChanged (Inkscape::XML::Node * repr, const gchar *key, const gchar *oldval, const gchar *newval, bool is_interactive);
- NR::Point origin;
- guint32 color;
- guint32 empcolor;
- gint empspacing;
- SPUnit const* gridunit;
-
NR::Point spacing; /**< Spacing between elements of the grid */
bool scaled[2]; /**< Whether the grid is in scaled mode, which can
be different in the X or Y direction, hense two
diff --git a/src/snap.cpp b/src/snap.cpp
index 68f4c8465..46b9ecad2 100644
--- a/src/snap.cpp
+++ b/src/snap.cpp
@@ -232,6 +232,60 @@ Inkscape::SnappedPoint SnapManager::freeSnap(Inkscape::Snapper::PointType point_
return findBestSnap(p, sc, false);
}
+// When pasting, we would like to snap to the grid. Problem is that we don't know which nodes were
+// aligned to the grid at the time of copying, so we don't know which nodes to snap. If we'd snap an
+// unaligned node to the grid, previously aligned nodes would become unaligned. That's undesirable.
+// Instead we will make sure that the offset between the source and the copy is a multiple of the grid
+// pitch. If the source was aligned, then the copy will therefore also be aligned
+// PS: Wether we really find a multiple also depends on the snapping range!
+Geom::Point SnapManager::multipleOfGridPitch(Geom::Point const &t) const
+{
+ if (!_snap_enabled_globally)
+ return t;
+
+ //FIXME: this code should actually do this: add new grid snappers that are active for this desktop. now it just adds all gridsnappers
+ SPDesktop* desktop = SP_ACTIVE_DESKTOP;
+
+ if (desktop && desktop->gridsEnabled()) {
+ bool success = false;
+ NR::Point nearest_multiple;
+ NR::Coord nearest_distance = NR_HUGE;
+
+ // It will snap to the grid for which we find the closest snap. This might be a different
+ // grid than to which the objects were initially aligned. I don't see an easy way to fix
+ // this, so when using multiple grids one can get unexpected results
+
+ // Cannot use getGridSnappers() because we need both the grids AND their snappers
+ // Therefor we iterate through all grids manually
+ for (GSList const *l = _named_view->grids; l != NULL; l = l->next) {
+ Inkscape::CanvasGrid *grid = (Inkscape::CanvasGrid*) l->data;
+ const Inkscape::Snapper* snapper = grid->snapper;
+ if (snapper && snapper->ThisSnapperMightSnap()) {
+ // To find the nearest multiple of the grid pitch for a given translation t, we
+ // will use the grid snapper. Simply snapping the value t to the grid will do, but
+ // only if the origin of the grid is at (0,0). If it's not then compensate for this
+ // in the translation t
+ NR::Point const t_offset = from_2geom(t) + grid->origin;
+ SnappedConstraints sc;
+ // Only the first three parameters are being used for grid snappers
+ snapper->freeSnap(sc, Inkscape::Snapper::SNAPPOINT_NODE, t_offset, TRUE, boost::optional<NR::Rect>(), NULL, NULL);
+ // Find the best snap for this grid, including intersections of the grid-lines
+ Inkscape::SnappedPoint s = findBestSnap(t_offset, sc, false);
+ if (s.getSnapped() && (s.getDistance() < nearest_distance)) {
+ success = true;
+ nearest_multiple = s.getPoint() - grid->origin;
+ nearest_distance = s.getDistance();
+ }
+ }
+ }
+
+ if (success)
+ return to_2geom(nearest_multiple);
+ }
+
+ return t;
+}
+
/**
* Try to snap a point to any interested snappers. A snap will only occur along
* a line described by a Inkscape::Snapper::ConstraintLine.
diff --git a/src/snap.h b/src/snap.h
index 966d2a543..981e91ecd 100644
--- a/src/snap.h
+++ b/src/snap.h
@@ -62,6 +62,8 @@ public:
bool first_point = true,
boost::optional<NR::Rect> const &bbox_to_snap = boost::optional<NR::Rect>() ) const;
+ Geom::Point multipleOfGridPitch(Geom::Point const &t) const;
+
// constrainedSnapReturnByRef() is preferred over constrainedSnap(), because it only returns a
// point, by overwriting p, if snapping has occured; otherwise p is untouched
void constrainedSnapReturnByRef(Inkscape::Snapper::PointType point_type,
diff --git a/src/ui/clipboard.cpp b/src/ui/clipboard.cpp
index 013882327..e7f2d82ee 100644
--- a/src/ui/clipboard.cpp
+++ b/src/ui/clipboard.cpp
@@ -78,6 +78,8 @@
#include "unit-constants.h"
#include "helper/png-write.h"
#include "svg/svg-color.h"
+#include "sp-namedview.h"
+#include "snap.h"
/// @brief Made up mimetype to represent Gdk::Pixbuf clipboard contents
#define CLIPBOARD_GDK_PIXBUF_TARGET "image/x-gdk-pixbuf"
@@ -753,36 +755,60 @@ void ClipboardManagerImpl::_pasteDocument(SPDocument *clipdoc, bool in_place)
Inkscape::XML::Node *obj_copy = _copyNode(obj, target_xmldoc, target_parent);
pasted_objects = g_slist_prepend(pasted_objects, (gpointer) obj_copy);
}
-
+
+ /* The pasted objects are at the origin of the document coordinates (i.e. the upper left corner
+ * of the bounding box of the pasted selection is aligned to the upper left page corner).
+ * Now we will move the pasted objects to where we want them to be, which is either at the
+ * original position ("in place") or at the location of the mouse pointer (optionaly with snapping
+ * to the grid)
+ */
+
+ Geom::Point rel_pos_original, rel_pos_mouse;
+
+ // Calculate the relative location of the original objects
+ Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
+ if (clipnode) {
+ Geom::Point min, max;
+ // Get two bounding box corners of the data in the clipboard (still in it's original position)
+ sp_repr_get_point(clipnode, "min", &min); //In desktop coordinates
+ sp_repr_get_point(clipnode, "max", &max);
+ // Calculate the upper-left page corner in desktop coordinates (where the pasted objects are located)
+ Geom::Point ul_page_corner = desktop->doc2dt(Geom::Point(0,0));
+ // Calculate the upper-left bbox corner of the original objects
+ Geom::Point ul_sel_corner = Geom::Point(min[Geom::X], max[Geom::Y]);
+ // Now calculate how far we would have to move the pasted objects to get them
+ // at the location of the original
+ rel_pos_original = ul_sel_corner - ul_page_corner; // in desktop coordinates
+ }
+
+ // Calculate the relative location of the mouse pointer
Inkscape::Selection *selection = sp_desktop_selection(desktop);
- selection->setReprList(pasted_objects);
-
- // move the selection to the right position
- if(in_place)
- {
- Inkscape::XML::Node *clipnode = sp_repr_lookup_name(root, "inkscape:clipboard", 1);
- if (clipnode) {
- Geom::Point min, max;
- sp_repr_get_point(clipnode, "min", &min);
- sp_repr_get_point(clipnode, "max", &max);
-
- // this formula was discovered empyrically
- min[Geom::Y] += ((max[Geom::Y] - min[Geom::Y]) - sp_document_height(target_document));
- sp_selection_move_relative(selection, Geom::Point(min));
- }
+ selection->setReprList(pasted_objects); // Change the selection to the freshly pasted objects
+ sp_document_ensure_up_to_date(target_document); // What does this do?
+
+ boost::optional<NR::Rect> sel_bbox = selection->bounds(); //In desktop coordinates
+ // PS: We could also have used the min/max corners calculated above, because we know that
+ // after pasting the upper left corner of the selection will be aligend to the corresponding page corner
+ // Using the boundingbox of the selection is more foolproof though
+ if (sel_bbox) {
+ Geom::Point pos_mouse = to_2geom(desktop->point()); //Location of mouse pointer in desktop coordinates
+ // Now calculate how far we would have to move the pasted objects to get their
+ // midpoint at the location of the mouse pointer
+ rel_pos_mouse = pos_mouse - to_2geom(sel_bbox->midpoint());
}
- // copied from former sp_selection_paste in selection-chemistry.cpp
- else {
- sp_document_ensure_up_to_date(target_document);
- boost::optional<NR::Rect> sel_size = selection->bounds();
-
- Geom::Point m( desktop->point() );
- if (sel_size) {
- m -= sel_size->midpoint();
- }
- sp_selection_move_relative(selection, m);
+
+ // Determine which offset we need to apply to the pasted objects
+ Geom::Point offset;
+ if (in_place) { // Align the pasted objects with their originals
+ offset = rel_pos_original;
+ } else { // Stick to the grid if snapping is enabled, otherwise paste at mouse position;
+ SnapManager &m = desktop->namedview->snap_manager;
+ m.setup(NULL, NULL); //Don't display snapindicator
+ offset = rel_pos_original + m.multipleOfGridPitch(rel_pos_mouse - rel_pos_original);
}
-
+
+ // Apply the offset to the pasted objects
+ sp_selection_move_relative(selection, offset);
g_slist_free(pasted_objects);
}