Code

fix 198818
[inkscape.git] / src / sp-path.cpp
index 53cbb16374a51d8feaa260f4e3a7a99ec3d52118..e401d0a2085414b05af36d077bb21937149df29c 100644 (file)
 #include "attributes.h"
 
 #include "sp-path.h"
+#include "sp-guide.h"
 
 #include "document.h"
+#include "desktop.h"
+#include "desktop-handles.h"
+#include "desktop-style.h"
+#include "event-context.h"
+#include "inkscape.h"
+#include "style.h"
+#include "message-stack.h"
+#include "prefs-utils.h"
+#include "selection.h"
 
 #define noPATH_VERBOSE
 
@@ -46,6 +56,7 @@ static void sp_path_set(SPObject *object, unsigned key, gchar const *value);
 static Inkscape::XML::Node *sp_path_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
 static NR::Matrix sp_path_set_transform(SPItem *item, NR::Matrix const &xform);
 static gchar * sp_path_description(SPItem *item);
+static void sp_path_convert_to_guides(SPItem *item);
 
 static void sp_path_update(SPObject *object, SPCtx *ctx, guint flags);
 static void sp_path_update_patheffect(SPShape *shape, bool write);
@@ -101,6 +112,7 @@ sp_path_class_init(SPPathClass * klass)
 
     item_class->description = sp_path_description;
     item_class->set_transform = sp_path_set_transform;
+    item_class->convert_to_guides = sp_path_convert_to_guides;
 
     shape_class->update_patheffect = sp_path_update_patheffect;
 }
@@ -124,8 +136,44 @@ static gchar *
 sp_path_description(SPItem * item)
 {
     int count = sp_nodes_in_path(SP_PATH(item));
-    return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
-                                    "<b>Path</b> (%i nodes)",count), count);
+    if (SP_SHAPE(item)->path_effect_href) {
+        return g_strdup_printf(ngettext("<b>Path</b> (%i node, path effect)",
+                                        "<b>Path</b> (%i nodes, path effect)",count), count);
+    } else {
+        return g_strdup_printf(ngettext("<b>Path</b> (%i node)",
+                                        "<b>Path</b> (%i nodes)",count), count);
+    }
+}
+
+static void
+sp_path_convert_to_guides(SPItem *item)
+{
+    SPPath *path = SP_PATH(item);
+
+    SPDocument *doc = SP_OBJECT_DOCUMENT(path);
+    std::list<std::pair<Geom::Point, Geom::Point> > pts;
+
+    NR::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(path)));
+
+    SPCurve *curve = SP_SHAPE(path)->curve;
+    if (!curve) return;
+    NArtBpath *bpath = SP_CURVE_BPATH(curve);
+
+    NR::Point last_pt;
+    NR::Point pt;
+    for (int i = 0; bpath[i].code != NR_END; i++){
+        if (bpath[i].code == NR_LINETO) {
+            /* we only convert straight line segments (converting curve segments would be unintuitive) */
+            pt = bpath[i].c(3) * i2d;
+            pts.push_back(std::make_pair(last_pt.to_2geom(), pt.to_2geom()));
+        }
+
+        /* remember current point for potential reuse in the next step
+           (e.g., in case this was an NR_MOVETO or NR_MOVETO_OPEN) */
+        last_pt = bpath[i].c(3) * i2d;
+    }
+
+    sp_guide_pt_pairs_to_guides(doc, pts);
 }
 
 /**
@@ -358,6 +406,9 @@ sp_path_set_transform(SPItem *item, NR::Matrix const &xform)
     // Adjust gradient fill
     sp_item_adjust_gradient(item, xform);
 
+    // Adjust LPE
+    sp_item_adjust_livepatheffect(item, xform);
+
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
 
     // nothing remains - we've written all of the transform, so return identity
@@ -447,6 +498,80 @@ sp_path_get_curve_for_edit (SPPath *path)
     }
 }
 
+/**
+ * Return a reference to original_curve if it exists or
+ * shape->curve if not.
+ */
+const SPCurve*
+sp_path_get_curve_reference (SPPath *path)
+{
+    if (path->original_curve) {
+        return path->original_curve;
+    } else {
+        return path->curve;
+    }
+}
+
+/* Create a single dot represented by a circle */
+void freehand_create_single_dot(SPEventContext *ec, NR::Point const &pt, char const *tool, guint event_state) {
+    g_return_if_fail(!strcmp(tool, "tools.freehand.pen") || !strcmp(tool, "tools.freehand.pencil"));
+
+    SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+    Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
+    repr->setAttribute("sodipodi:type", "arc");
+    SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
+    Inkscape::GC::release(repr);
+
+    /* apply the tool's current style */
+    sp_desktop_apply_style_tool(desktop, repr, tool, false);
+
+    /* find out stroke width (TODO: is there an easier way??) */
+    double stroke_width = 3.0;
+    gchar const *style_str = NULL;
+    style_str = repr->attribute("style");
+    if (style_str) {
+        SPStyle *style = sp_style_new(SP_ACTIVE_DOCUMENT);
+        sp_style_merge_from_style_string(style, style_str);
+        stroke_width = style->stroke_width.computed;
+        style->stroke_width.computed = 0;
+        sp_style_unref(style);
+    }
+    /* unset stroke and set fill color to former stroke color */
+    gchar * str;
+    str = g_strdup_printf("fill:#%06x;stroke:none;", sp_desktop_get_color_tool(desktop, tool, false) >> 8);
+    repr->setAttribute("style", str);
+    g_free(str);
+
+    /* put the circle where the mouse click occurred and set the diameter to the
+       current stroke width, multiplied by the amount specified in the preferences */
+    NR::Matrix const i2d (sp_item_i2d_affine (item));
+    NR::Point pp = pt * i2d;
+    double rad = 0.5 * prefs_get_double_attribute(tool, "dot-size", 3.0);
+    if (event_state & GDK_MOD1_MASK) {
+        /* TODO: We vary the dot size between 0.5*rad and 1.5*rad, where rad is the dot size
+           as specified in prefs. Very simple, but it might be sufficient in practice. If not,
+           we need to devise something more sophisticated. */
+        double s = g_random_double_range(-0.5, 0.5);
+        rad *= (1 + s);
+    }
+    if (event_state & GDK_SHIFT_MASK) {
+        // double the point size
+        rad *= 2;
+    }
+
+    sp_repr_set_svg_double (repr, "sodipodi:cx", pp[NR::X]);
+    sp_repr_set_svg_double (repr, "sodipodi:cy", pp[NR::Y]);
+    sp_repr_set_svg_double (repr, "sodipodi:rx", rad * stroke_width);
+    sp_repr_set_svg_double (repr, "sodipodi:ry", rad * stroke_width);
+    item->updateRepr();
+
+    sp_desktop_selection(desktop)->set(item);
+
+    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating single dot"));
+    sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, _("Create single dot"));
+}
+
 /*
   Local Variables:
   mode:c++