Code

fix pasting style after copying a text span
[inkscape.git] / src / knot-holder-entity.cpp
index bd12cde886c695ec13ebed228b97096cffe6e09c..36a072c444b92d2767c611b5845dcd3ce1d05057 100644 (file)
@@ -19,7 +19,7 @@
 #include "knotholder.h"
 #include "sp-item.h"
 #include "style.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "macros.h"
 #include <libnr/nr-matrix-ops.h>
 #include "sp-pattern.h"
@@ -77,7 +77,7 @@ KnotHolderEntity::~KnotHolderEntity()
 void
 KnotHolderEntity::update_knot()
 {
-    NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
+    Geom::Matrix const i2d(sp_item_i2d_affine(item));
 
     Geom::Point dp(knot_get() * i2d);
 
@@ -89,27 +89,28 @@ KnotHolderEntity::update_knot()
 Geom::Point
 KnotHolderEntity::snap_knot_position(Geom::Point const &p)
 {
-    NR::Matrix const i2d (from_2geom(sp_item_i2d_affine(item)));
-    NR::Point s = p * i2d;
+    Geom::Matrix const i2d (sp_item_i2d_affine(item));
+    Geom::Point s = p * i2d;
     SnapManager &m = desktop->namedview->snap_manager;
-    m.setup(desktop, item);
-    m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, s);
+    m.setup(desktop, true, item);
+    m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, s);
     return s * i2d.inverse();
 }
 
 
 /* Pattern manipulation */
 
-static gdouble sp_pattern_extract_theta(SPPattern *pat, Geom::Point scale)
+/*  TODO: this pattern manipulation is not able to handle general transformation matrices. Only matrices that are the result of a pure scale times a pure rotation. */
+
+static gdouble sp_pattern_extract_theta(SPPattern *pat)
 {
-    gdouble theta = asin(pat->patternTransform[1] / scale[NR::X]);
-    if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
-    return theta;
+    Geom::Matrix transf = pat->patternTransform;
+    return Geom::atan2(transf.xAxis());
 }
 
 static Geom::Point sp_pattern_extract_scale(SPPattern *pat)
 {
-    Geom::Matrix transf = to_2geom(pat->patternTransform);
+    Geom::Matrix transf = pat->patternTransform;
     return Geom::Point( transf.expansionX(), transf.expansionY() );
 }
 
@@ -123,19 +124,20 @@ PatternKnotHolderEntityXY::knot_set(Geom::Point const &p, Geom::Point const &ori
 {
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
+    // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
     Geom::Point p_snapped = snap_knot_position(p);
 
     if ( state & GDK_CONTROL_MASK ) {
-        if (fabs((p - origin)[NR::X]) > fabs((p - origin)[NR::Y])) {
-            p_snapped[NR::Y] = origin[NR::Y];
+        if (fabs((p - origin)[Geom::X]) > fabs((p - origin)[Geom::Y])) {
+            p_snapped[Geom::Y] = origin[Geom::Y];
         } else {
-            p_snapped[NR::X] = origin[NR::X];
+            p_snapped[Geom::X] = origin[Geom::X];
         }
     }
 
     if (state)  {
         Geom::Point const q = p_snapped - sp_pattern_extract_trans(pat);
-        sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
+        sp_item_adjust_pattern(item, Geom::Matrix(Geom::Translate(q)));
     }
 
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
@@ -157,8 +159,8 @@ PatternKnotHolderEntityAngle::knot_get()
     gdouble y = 0;
     Geom::Point delta = Geom::Point(x,y);
     Geom::Point scale = sp_pattern_extract_scale(pat);
-    gdouble theta = sp_pattern_extract_theta(pat, scale);
-    delta = delta * NR::Matrix(NR::scale(scale))*NR::Matrix(NR::rotate(theta));
+    gdouble theta = sp_pattern_extract_theta(pat);
+    delta = delta * Geom::Matrix(Geom::Scale(scale))*Geom::Matrix(Geom::Rotate(theta));
     delta = delta + sp_pattern_extract_trans(pat);
     return delta;
 }
@@ -166,7 +168,8 @@ PatternKnotHolderEntityAngle::knot_get()
 void
 PatternKnotHolderEntityAngle::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
 {
-    int const snaps = prefs_get_int_attribute("options.rotationsnapsperpi", "value", 12);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
 
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
@@ -180,37 +183,44 @@ PatternKnotHolderEntityAngle::knot_set(Geom::Point const &p, Geom::Point const &
 
     // get the scale from the current transform so we can keep it.
     Geom::Point scl = sp_pattern_extract_scale(pat);
-    NR::Matrix rot = NR::Matrix(NR::scale(scl)) * NR::Matrix(NR::rotate(theta));
+    Geom::Matrix rot = Geom::Matrix(Geom::Scale(scl)) * Geom::Matrix(Geom::Rotate(theta));
     Geom::Point const t = sp_pattern_extract_trans(pat);
-    rot[4] = t[NR::X];
-    rot[5] = t[NR::Y];
+    rot[4] = t[Geom::X];
+    rot[5] = t[Geom::Y];
     sp_item_adjust_pattern(item, rot, true);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
 void
-PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/)
+PatternKnotHolderEntityScale::knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint state)
 {
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
+    // FIXME: this snapping should be done together with knowing whether control was pressed. If GDK_CONTROL_MASK, then constrained snapping should be used.
     Geom::Point p_snapped = snap_knot_position(p);
 
-    // get angle from current transform, (need get current scale first to calculate angle)
-    Geom::Point oldscale = sp_pattern_extract_scale(pat);
-    gdouble theta = sp_pattern_extract_theta(pat,oldscale);
+    // get angle from current transform
+    gdouble theta = sp_pattern_extract_theta(pat);
 
     // Get the new scale from the position of the knotholder
     Geom::Point d = p_snapped - sp_pattern_extract_trans(pat);
-    d *= Geom::Rotate(-theta);
     gdouble pat_x = pattern_width(pat);
     gdouble pat_y = pattern_height(pat);
-    Geom::Point scl (d[NR::X] / pat_x, d[NR::Y] / pat_y);
+    Geom::Scale scl(1);
+    if ( state & GDK_CONTROL_MASK ) {
+        // if ctrl is pressed: use 1:1 scaling
+        gdouble pat_h = hypot(pat_x, pat_y);
+        scl = Geom::Scale(d.length() / pat_h);
+    } else {
+        d *= Geom::Rotate(-theta);
+        scl = Geom::Scale(d[Geom::X] / pat_x, d[Geom::Y] / pat_y);
+    }
 
-    NR::Matrix rot =  NR::Matrix(NR::scale(scl)) * NR::Matrix(NR::rotate(theta));
+    Geom::Matrix rot = (Geom::Matrix)scl * Geom::Rotate(theta);
 
     Geom::Point const t = sp_pattern_extract_trans(pat);
-    rot[4] = t[NR::X];
-    rot[5] = t[NR::Y];
+    rot[4] = t[Geom::X];
+    rot[5] = t[Geom::Y];
     sp_item_adjust_pattern(item, rot, true);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
@@ -224,7 +234,7 @@ PatternKnotHolderEntityScale::knot_get()
     gdouble x = pattern_width(pat);
     gdouble y = pattern_height(pat);
     Geom::Point delta = Geom::Point(x,y);
-    NR::Matrix a = pat->patternTransform;
+    Geom::Matrix a = pat->patternTransform;
     a[4] = 0;
     a[5] = 0;
     delta = delta * a;