summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e3b47ca)
raw | patch | inline | side by side (parent: e3b47ca)
author | dvlierop2 <dvlierop2@users.sourceforge.net> | |
Wed, 6 Aug 2008 21:28:34 +0000 (21:28 +0000) | ||
committer | dvlierop2 <dvlierop2@users.sourceforge.net> | |
Wed, 6 Aug 2008 21:28:34 +0000 (21:28 +0000) |
2) Refactor some of the pasting code
index 668c59649b08bdb736a3e1a208bde677ade85d67..564d266a64e0d4157173d162d84e2eb1e10e0492 100644 (file)
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;
index a9e317534b90dd12740b80b49aef5681741f8786..5d674bb2d08c13d7f6b9156eb975972213d745df 100644 (file)
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;
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 68f4c8465acb1c6592909a9e707d9c19c67734cf..46b9ecad297e4c3154ff6dca23cb6a4256b22e2a 100644 (file)
--- 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 966d2a5431127968a7de6089d16ed1d3bdf79024..981e91ecdfaba1e7fcf665622753633d058c5145 100644 (file)
--- a/src/snap.h
+++ b/src/snap.h
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 0138823277bafebc4540c00b34d6d079cf9c3aaf..e7f2d82ee23d40e0179aa5dc02de69517f5bc616 100644 (file)
--- a/src/ui/clipboard.cpp
+++ b/src/ui/clipboard.cpp
#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"
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);
}