Code

Fix fallback icon loading order for icons with legacy names.
[inkscape.git] / src / knot-holder-entity.cpp
index a587a3af0cef1c4fe19bcc73102a0e1842a57da6..005ca24c71c1fbe2e1b8bbb8112c5c5401f4ed26 100644 (file)
@@ -1,11 +1,10 @@
-#define __KNOT_HOLDER_ENTITY_C__
-
-/** \file 
- * KnotHolderEntity definition. 
- * 
+/** \file
+ * KnotHolderEntity definition.
+ *
  * Authors:
  *   Mitsuru Oka <oka326@parkcity.ne.jp>
  *   Maximilian Albert <maximilian.albert@gmail.com>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2001 Lauris Kaplinski
  * Copyright (C) 2000-2001 Ximian, Inc.
 #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"
-
+#include "snap.h"
+#include "desktop.h"
+#include "sp-namedview.h"
+#include <2geom/matrix.h>
+#include <2geom/transforms.h>
 
 int KnotHolderEntity::counter = 0;
 
@@ -35,6 +38,7 @@ KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, c
 
     this->parent_holder = parent;
     this->item = item; // TODO: remove the item either from here or from knotholder.cpp
+    this->desktop = desktop;
 
     my_counter = KnotHolderEntity::counter++;
 
@@ -55,98 +59,143 @@ KnotHolderEntity::create(SPDesktop *desktop, SPItem *item, KnotHolder *parent, c
 
 KnotHolderEntity::~KnotHolderEntity()
 {
+    _moved_connection.disconnect();
+    _click_connection.disconnect();
+    _ungrabbed_connection.disconnect();
+
     /* unref should call destroy */
-    g_object_unref(knot);
+    if (knot) {
+        g_object_unref(knot);
+    } else {
+        // FIXME: This shouldn't occur. Perhaps it is caused by LPE PointParams being knotholder entities, too
+        //        If so, it will likely be fixed with upcoming refactoring efforts.
+        g_return_if_fail(knot);
+    }
 }
 
 void
 KnotHolderEntity::update_knot()
 {
-    NR::Matrix const i2d(from_2geom(sp_item_i2d_affine(item)));
+    Geom::Matrix const i2d(item->i2d_affine());
 
-    NR::Point dp(knot_get() * i2d);
+    Geom::Point dp(knot_get() * i2d);
 
     _moved_connection.block();
-    sp_knot_set_position(knot, &dp, SP_KNOT_STATE_NORMAL); 
+    sp_knot_set_position(knot, dp, SP_KNOT_STATE_NORMAL);
     _moved_connection.unblock();
 }
 
+Geom::Point
+KnotHolderEntity::snap_knot_position(Geom::Point const &p)
+{
+    Geom::Matrix const i2d (item->i2d_affine());
+    Geom::Point s = p * i2d;
+
+    SnapManager &m = desktop->namedview->snap_manager;
+    m.setup(desktop, true, item);
+    m.freeSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE);
+    m.unSetup();
+
+    return s * i2d.inverse();
+}
+
+Geom::Point
+KnotHolderEntity::snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::SnapConstraint const &constraint)
+{
+    Geom::Matrix const i2d (item->i2d_affine());
+    Geom::Point s = p * i2d;
+
+    SnapManager &m = desktop->namedview->snap_manager;
+    m.setup(desktop, true, item);
+
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    // constrainedSnap() will first project the point p onto the constraint line and then try to snap along that line.
+    // This way the constraint is already enforced, no need to worry about that later on
+    Inkscape::Snapper::SnapConstraint transformed_constraint = Inkscape::Snapper::SnapConstraint(constraint.getPoint() * i2d, (constraint.getPoint() + constraint.getDirection()) * i2d - constraint.getPoint() * i2d);
+    m.constrainedSnapReturnByRef(s, Inkscape::SNAPSOURCE_NODE_HANDLE, transformed_constraint);
+    m.unSetup();
+
+    return s * i2d.inverse();
+}
+
+
 /* Pattern manipulation */
 
-static gdouble sp_pattern_extract_theta(SPPattern *pat, gdouble 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);
-    if (pat->patternTransform[0] < 0) theta = M_PI - theta ;
-    return theta;
+    Geom::Matrix transf = pat->patternTransform;
+    return Geom::atan2(transf.xAxis());
 }
 
-static gdouble sp_pattern_extract_scale(SPPattern *pat)
+static Geom::Point sp_pattern_extract_scale(SPPattern *pat)
 {
-    gdouble s = pat->patternTransform[1];
-    gdouble c = pat->patternTransform[0];
-    gdouble xscale = sqrt(c * c + s * s);
-    return xscale;
+    Geom::Matrix transf = pat->patternTransform;
+    return Geom::Point( transf.expansionX(), transf.expansionY() );
 }
 
-static NR::Point sp_pattern_extract_trans(SPPattern const *pat)
+static Geom::Point sp_pattern_extract_trans(SPPattern const *pat)
 {
-    return NR::Point(pat->patternTransform[4], pat->patternTransform[5]);
+    return Geom::Point(pat->patternTransform[4], pat->patternTransform[5]);
 }
 
 void
-PatternKnotHolderEntityXY::knot_set(NR::Point const &p, NR::Point const &origin, guint state)
+PatternKnotHolderEntityXY::knot_set(Geom::Point const &p, Geom::Point const &origin, guint state)
 {
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
-    NR::Point p_snapped = p;
+    // 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)  {
-        NR::Point const q = p_snapped - sp_pattern_extract_trans(pat);
-        sp_item_adjust_pattern(item, NR::Matrix(NR::translate(q)));
+        Geom::Point const q = p_snapped - sp_pattern_extract_trans(pat);
+        item->adjust_pattern(Geom::Matrix(Geom::Translate(q)));
     }
 
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
-NR::Point
+Geom::Point
 PatternKnotHolderEntityXY::knot_get()
 {
     SPPattern const *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
     return sp_pattern_extract_trans(pat);
 }
 
-NR::Point
+Geom::Point
 PatternKnotHolderEntityAngle::knot_get()
 {
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
-    gdouble x = (pattern_width(pat)*0.5);
+    gdouble x = (pattern_width(pat));
     gdouble y = 0;
-    NR::Point delta = NR::Point(x,y);
-    gdouble scale = sp_pattern_extract_scale(pat);
-    gdouble theta = sp_pattern_extract_theta(pat, scale);
-    delta = delta * NR::Matrix(NR::rotate(theta))*NR::Matrix(NR::scale(scale,scale));
+    Geom::Point delta = Geom::Point(x,y);
+    Geom::Point scale = sp_pattern_extract_scale(pat);
+    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;
 }
 
 void
-PatternKnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*origin*/, guint state)
+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));
 
     // get the angle from pattern 0,0 to the cursor pos
-    NR::Point delta = p - sp_pattern_extract_trans(pat);
+    Geom::Point delta = p - sp_pattern_extract_trans(pat);
     gdouble theta = atan2(delta);
 
     if ( state & GDK_CONTROL_MASK ) {
@@ -154,50 +203,59 @@ PatternKnotHolderEntityAngle::knot_set(NR::Point const &p, NR::Point const &/*or
     }
 
     // get the scale from the current transform so we can keep it.
-    gdouble scl = sp_pattern_extract_scale(pat);
-    NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
-    NR::Point const t = sp_pattern_extract_trans(pat);
-    rot[4] = t[NR::X];
-    rot[5] = t[NR::Y];
-    sp_item_adjust_pattern(item, rot, true);
+    Geom::Point scl = sp_pattern_extract_scale(pat);
+    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[Geom::X];
+    rot[5] = t[Geom::Y];
+    item->adjust_pattern(rot, true);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
 void
-PatternKnotHolderEntityScale::knot_set(NR::Point const &p, NR::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));
 
-    // Get the scale from the position of the knotholder,
-    NR::Point d = p - sp_pattern_extract_trans(pat);
-    gdouble s = NR::L2(d);
-    gdouble pat_x = pattern_width(pat) * 0.5;
-    gdouble pat_y = pattern_height(pat) * 0.5;
-    gdouble pat_h = hypot(pat_x, pat_y);
-    gdouble scl = s / pat_h;
-
-    // get angle from current transform, (need get current scale first to calculate angle)
-    gdouble oldscale = sp_pattern_extract_scale(pat);
-    gdouble theta = sp_pattern_extract_theta(pat,oldscale);
-
-    NR::Matrix rot =  NR::Matrix(NR::rotate(theta)) * NR::Matrix(NR::scale(scl,scl));
-    NR::Point const t = sp_pattern_extract_trans(pat);
-    rot[4] = t[NR::X];
-    rot[5] = t[NR::Y];
-    sp_item_adjust_pattern(item, rot, true);
+    // 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
+    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);
+    gdouble pat_x = pattern_width(pat);
+    gdouble pat_y = pattern_height(pat);
+    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);
+    }
+
+    Geom::Matrix rot = (Geom::Matrix)scl * Geom::Rotate(theta);
+
+    Geom::Point const t = sp_pattern_extract_trans(pat);
+    rot[4] = t[Geom::X];
+    rot[5] = t[Geom::Y];
+    item->adjust_pattern(rot, true);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
 
-NR::Point
+Geom::Point
 PatternKnotHolderEntityScale::knot_get()
 {
     SPPattern *pat = SP_PATTERN(SP_STYLE_FILL_SERVER(SP_OBJECT(item)->style));
 
-    gdouble x = pattern_width(pat)*0.5;
-    gdouble y = pattern_height(pat)*0.5;
-    NR::Point delta = NR::Point(x,y);
-    NR::Matrix a = pat->patternTransform;
+    gdouble x = pattern_width(pat);
+    gdouble y = pattern_height(pat);
+    Geom::Point delta = Geom::Point(x,y);
+    Geom::Matrix a = pat->patternTransform;
     a[4] = 0;
     a[5] = 0;
     delta = delta * a;
@@ -214,4 +272,4 @@ PatternKnotHolderEntityScale::knot_get()
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :