Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / live_effects / parameter / path.cpp
index 759e9f788c148083a306a1d9c54939ef3b0a1c98..8d9b2376ff586770d7b5b8da664c629fa6b770f0 100644 (file)
@@ -1,17 +1,16 @@
-#define INKSCAPE_LIVEPATHEFFECT_PARAMETER_PATH_CPP
-
 /*
  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
+ *   Abhishek Sharma
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
 #include "live_effects/parameter/path.h"
 #include "live_effects/effect.h"
-#include "libnr/n-art-bpath-2geom.h"
 #include "svg/svg.h"
 #include <2geom/svg-path-parser.h>
 #include <2geom/sbasis-to-bezier.h>
+#include <2geom/pathvector.h>
 #include <2geom/d2.h>
 
 #include "ui/widget/point.h"
 // needed for on-canvas editting:
 #include "tools-switch.h"
 #include "shape-editor.h"
-#include "node-context.h"
 #include "desktop-handles.h"
 #include "selection.h"
-#include "nodepath.h"
 // clipboard support
 #include "ui/clipboard.h"
 // required for linking to other paths
 #include "sp-text.h"
 #include "display/curve.h"
 
+#include "ui/tool/node-tool.h"
+#include "ui/tool/multi-path-manipulator.h"
+#include "ui/tool/shape-record.h"
+
 
 namespace Inkscape {
 
@@ -49,6 +50,7 @@ PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,
                       const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
                       Effect* effect, const gchar * default_value)
     : Parameter(label, tip, key, wr, effect),
+      changed(true),
       _pathvector(),
       _pwd2(),
       must_recalculate_pwd2(false),
@@ -119,7 +121,7 @@ PathParam::param_readSVGValue(const gchar * strvalue)
             _pathvector = sp_svg_read_pathv(strvalue);
         }
 
-        signal_path_changed.emit();
+        emit_changed();
         return true;
     }
 
@@ -146,7 +148,7 @@ PathParam::param_newWidget(Gtk::Tooltips * tooltips)
     static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
     tooltips->set_tip(*pLabel, param_tooltip);
 
-    Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
+    Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "tool-node-editor", Inkscape::ICON_SIZE_BUTTON) );
     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
     pButton->set_relief(Gtk::RELIEF_NONE);
     pIcon->show();
@@ -176,7 +178,7 @@ PathParam::param_newWidget(Gtk::Tooltips * tooltips)
     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
     tooltips->set_tip(*pButton, _("Paste path"));
 
-    pIcon = Gtk::manage( sp_icon_get_icon( "edit_clone", Inkscape::ICON_SIZE_BUTTON) );
+    pIcon = Gtk::manage( sp_icon_get_icon( "edit-clone", Inkscape::ICON_SIZE_BUTTON) );
     pButton = Gtk::manage(new Gtk::Button());
     pButton->set_relief(Gtk::RELIEF_NONE);
     pIcon->show();
@@ -192,65 +194,105 @@ PathParam::param_newWidget(Gtk::Tooltips * tooltips)
 }
 
 void
-PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
+PathParam::param_editOncanvas(SPItem *item, SPDesktop * dt)
 {
-    // If not already in nodecontext, goto it!
+    using namespace Inkscape::UI;
+
+    // TODO remove the tools_switch atrocity.
     if (!tools_isactive(dt, TOOLS_NODES)) {
-        tools_switch_current(TOOLS_NODES);
+        tools_switch(dt, TOOLS_NODES);
     }
 
-    ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
+    InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
+    std::set<ShapeRecord> shapes;
+    ShapeRecord r;
+
+    r.role = SHAPE_ROLE_LPE_PARAM;
+    r.edit_transform = item->i2d_affine(); // TODO is it right?
     if (!href) {
-        shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
+        r.item = reinterpret_cast<SPItem*>(param_effect->getLPEObj());
+        r.lpe_key = param_key;
     } else {
-        // set referred item for editing
-        shape_editor->set_item(ref.getObject());
+        r.item = ref.getObject();
     }
+    shapes.insert(r);
+    nt->_multipath->setItems(shapes);
 }
 
 void
-PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
+PathParam::param_setup_nodepath(Inkscape::NodePath::Path *)
 {
-    np->show_helperpath = true;
-    np->helperpath_rgba = 0x009000ff;
-    np->helperpath_width = 1.0;
+    // TODO this method should not exist at all!
 }
 
+void
+PathParam::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
+{
+    hp_vec.push_back(_pathvector);
+}
+
+/*
+ * Only applies transform when not referring to other path!
+ */
 void
 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
 {
+    // only apply transform when not referring to other path
     if (!href) {
-        // TODO: recode this to apply transform to _pathvector instead?
-
-        // only apply transform when not referring to other path
-        ensure_pwd2();
-        param_set_and_write_new_value( _pwd2 * postmul );
+        set_new_value( _pathvector * postmul, true );
     }
 }
 
+/*
+ * See comments for set_new_value(std::vector<Geom::Path>).
+ */
 void
-PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
+PathParam::set_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath, bool write_to_svg)
 {
     remove_link();
     _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
-    gchar * svgd = sp_svg_write_path( _pathvector );
-    param_write_to_repr(svgd);
-    g_free(svgd);
-    // force value upon pwd2 and don't recalculate.
-    _pwd2 = newpath;
-    must_recalculate_pwd2 = false;
+
+    if (write_to_svg) {
+        gchar * svgd = sp_svg_write_path( _pathvector );
+        param_write_to_repr(svgd);
+        g_free(svgd);
+
+        // After the whole "writing to svg avalanche of function calling": force value upon pwd2 and don't recalculate.
+        _pwd2 = newpath;
+        must_recalculate_pwd2 = false;
+    } else {
+        _pwd2 = newpath;
+        must_recalculate_pwd2 = false;
+        emit_changed();
+    }
 }
 
+/*
+ * This method sets new path data.
+ * If this PathParam refers to another path, this link is removed (and replaced with explicit path data).
+ *
+ * If write_to_svg = true :
+ *          The new path data is written to SVG. In this case the signal_path_changed signal
+ *          is not directly emited in this method, because writing to SVG
+ *          triggers the LPEObject to which this belongs to call Effect::setParameter which calls
+ *          PathParam::readSVGValue, which finally emits the signal_path_changed signal.
+ * If write_to_svg = false :
+ *          The new path data is not written to SVG. This method will emit the signal_path_changed signal.
+ */
 void
-PathParam::param_set_and_write_new_value (std::vector<Geom::Path> const & newpath)
+PathParam::set_new_value (std::vector<Geom::Path> const &newpath, bool write_to_svg)
 {
     remove_link();
     _pathvector = newpath;
     must_recalculate_pwd2 = true;
 
-    gchar * svgd = sp_svg_write_path( _pathvector );
-    param_write_to_repr(svgd);
-    g_free(svgd);
+    if (write_to_svg) {
+        gchar * svgd = sp_svg_write_path( _pathvector );
+        param_write_to_repr(svgd);
+        g_free(svgd);
+    } else {
+        emit_changed();
+    }
 }
 
 void
@@ -266,6 +308,13 @@ PathParam::ensure_pwd2()
     }
 }
 
+void
+PathParam::emit_changed()
+{
+    changed = true;
+    signal_path_changed.emit();
+}
+
 void
 PathParam::start_listening(SPObject * to)
 {
@@ -308,7 +357,7 @@ PathParam::linked_delete(SPObject */*deleted*/)
 {
     quit_listening();
     remove_link();
-    param_set_and_write_new_value (_pathvector);
+    set_new_value (_pathvector, true);
 }
 
 void
@@ -316,7 +365,7 @@ PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
 {
     SPCurve *curve = NULL;
     if (SP_IS_SHAPE(linked_obj)) {
-        curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
+        curve = SP_SHAPE(linked_obj)->getCurve();
     }
     if (SP_IS_TEXT(linked_obj)) {
         curve = SP_TEXT(linked_obj)->getNormalizedBpath();
@@ -326,12 +375,12 @@ PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
         // curve invalid, set default value
         _pathvector = sp_svg_read_pathv(defvalue);
     } else {
-        _pathvector = BPath_to_2GeomPath(SP_CURVE_BPATH(curve));
+        _pathvector = curve->get_pathvector();
         curve->unref();
     }
 
     must_recalculate_pwd2 = true;
-    signal_path_changed.emit();
+    emit_changed();
     SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
 }
 
@@ -346,21 +395,26 @@ PathParam::on_edit_button_click()
 }
 
 void
-PathParam::on_paste_button_click()
+PathParam::paste_param_path(const char *svgd)
 {
-    Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
-    Glib::ustring svgd = cm->getPathParameter();
-    
-    if (svgd == "")
-        return;
+    // only recognize a non-null, non-empty string
+    if (svgd && *svgd) {
+        // remove possible link to path
+        remove_link();
 
-    // remove possible link to path
-    remove_link();
+        param_write_to_repr(svgd);
+        signal_path_pasted.emit();
+    }
+}
 
-    param_write_to_repr(svgd.data());
-    signal_path_pasted.emit();
-    sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
-                     _("Paste path parameter"));
+void
+PathParam::on_paste_button_click()
+{
+    Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
+    Glib::ustring svgd = cm->getPathParameter(SP_ACTIVE_DESKTOP);
+    paste_param_path(svgd.data());
+    DocumentUndo::done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
+                       _("Paste path parameter"));
 }
 
 void
@@ -374,7 +428,7 @@ void
 PathParam::on_link_button_click()
 {
     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
-    Glib::ustring pathid = cm->getShapeOrTextObjectId();
+    Glib::ustring pathid = cm->getShapeOrTextObjectId(SP_ACTIVE_DESKTOP);
 
     if (pathid == "") {
         return;
@@ -391,8 +445,8 @@ PathParam::on_link_button_click()
         // check if linking to object to which LPE is applied (maybe delegated to PathReference
 
         param_write_to_repr(pathid.c_str());
-        sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
-                         _("Link path parameter to path"));
+        DocumentUndo::done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
+                           _("Link path parameter to path"));
     }
 }