Code

fix typo
[inkscape.git] / src / live_effects / parameter / path.cpp
index ded004eee7da9ba07709285b17675604b535a7fa..429cf82d1a78d672eb04d346208001d92b5f4eff 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "live_effects/parameter/path.h"
 #include "live_effects/effect.h"
-#include "live_effects/n-art-bpath-2geom.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 "desktop-handles.h"
 #include "selection.h"
 #include "nodepath.h"
+// clipboard support
+#include "ui/clipboard.h"
+// required for linking to other paths
+#include "uri.h"
+#include "sp-shape.h"
+#include "sp-text.h"
+#include "display/curve.h"
+
 
 namespace Inkscape {
 
@@ -40,18 +48,40 @@ namespace LivePathEffect {
 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)
+    : Parameter(label, tip, key, wr, effect),
+      _pathvector(),
+      _pwd2(),
+      must_recalculate_pwd2(false),
+      href(NULL),
+      ref( (SPObject*)effect->getLPEObj() )
 {
     defvalue = g_strdup(default_value);
     param_readSVGValue(defvalue);
     oncanvas_editable = true;
+
+    ref_changed_connection = ref.changedSignal().connect(sigc::mem_fun(*this, &PathParam::ref_changed));
 }
 
 PathParam::~PathParam()
 {
+    remove_link();
+
     g_free(defvalue);
 }
 
+std::vector<Geom::Path> const &
+PathParam::get_pathvector()
+{
+    return _pathvector;
+}
+
+Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
+PathParam::get_pwd2()
+{
+    ensure_pwd2();
+    return _pwd2;
+}
+
 void
 PathParam::param_set_default()
 {
@@ -68,12 +98,27 @@ bool
 PathParam::param_readSVGValue(const gchar * strvalue)
 {
     if (strvalue) {
-        Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;
-        std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);
-        for (unsigned int i=0; i < temppath.size(); i++) {
-            newpath.concat( temppath[i].toPwSb() );
+        _pathvector.clear();
+        remove_link();
+        must_recalculate_pwd2 = true;
+
+        if (strvalue[0] == '#') {
+            if (href)
+                g_free(href);
+            href = g_strdup(strvalue);
+
+            // Now do the attaching, which emits the changed signal.
+            try {
+                ref.attach(Inkscape::URI(href));
+            } catch (Inkscape::BadURIException &e) {
+                g_warning("%s", e.what());
+                ref.detach();
+                _pathvector = SVGD_to_2GeomPath(defvalue);
+            }
+        } else {
+            _pathvector = SVGD_to_2GeomPath(strvalue);
         }
-        *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;
+
         signal_path_changed.emit();
         return true;
     }
@@ -84,10 +129,12 @@ PathParam::param_readSVGValue(const gchar * strvalue)
 gchar *
 PathParam::param_writeSVGValue() const
 {
-    const std::vector<Geom::Path> temppath =
-        Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);
-    gchar * svgd = SVGD_from_2GeomPath( temppath );
-    return svgd;
+    if (href) {
+        return href;
+    } else {
+        gchar * svgd = SVGD_from_2GeomPath( _pathvector );
+        return svgd;
+    }
 }
 
 Gtk::Widget *
@@ -129,6 +176,16 @@ 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) );
+    pButton = Gtk::manage(new Gtk::Button());
+    pButton->set_relief(Gtk::RELIEF_NONE);
+    pIcon->show();
+    pButton->add(*pIcon);
+    pButton->show();
+    pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_link_button_click));
+    static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
+    tooltips->set_tip(*pButton, _("Link to path"));
+
     static_cast<Gtk::HBox*>(_widget)->show_all_children();
 
     return dynamic_cast<Gtk::Widget *> (_widget);
@@ -143,7 +200,12 @@ PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
     }
 
     ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
-    shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
+    if (!href) {
+        shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
+    } else {
+        // set referred item for editing
+        shape_editor->set_item(ref.getObject());
+    }
 }
 
 void
@@ -157,18 +219,122 @@ PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
 void
 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
 {
-    param_set_and_write_new_value( (*this) * postmul );
+    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 );
+    }
+}
+
+void
+PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
+{
+    remove_link();
+    _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
+    gchar * svgd = SVGD_from_2GeomPath( _pathvector );
+    param_write_to_repr(svgd);
+    g_free(svgd);
+    // force value upon pwd2 and don't recalculate.
+    _pwd2 = newpath;
+    must_recalculate_pwd2 = false;
 }
 
 void
-PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath)
+PathParam::param_set_and_write_new_value (std::vector<Geom::Path> const & newpath)
 {
-    const std::vector<Geom::Path> temppath = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
-    gchar * svgd = SVGD_from_2GeomPath( temppath );
+    remove_link();
+    _pathvector = newpath;
+    must_recalculate_pwd2 = true;
+
+    gchar * svgd = SVGD_from_2GeomPath( _pathvector );
     param_write_to_repr(svgd);
     g_free(svgd);
 }
 
+void
+PathParam::ensure_pwd2()
+{
+    if (must_recalculate_pwd2) {
+        _pwd2.clear();
+        for (unsigned int i=0; i < _pathvector.size(); i++) {
+            _pwd2.concat( _pathvector[i].toPwSb() );
+        }
+
+        must_recalculate_pwd2 = false;
+    }
+}
+
+void
+PathParam::start_listening(SPObject * to)
+{
+    if ( to == NULL ) {
+        return;
+    }
+    linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
+    linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
+    linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
+}
+
+void
+PathParam::quit_listening(void)
+{
+    linked_modified_connection.disconnect();
+    linked_delete_connection.disconnect();
+}
+
+void
+PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
+{
+    quit_listening();
+    if ( new_ref ) {
+        start_listening(new_ref);
+    }
+}
+
+void
+PathParam::remove_link()
+{
+    if (href) {
+        ref.detach();
+        g_free(href);
+        href = NULL;
+    }
+}
+
+void
+PathParam::linked_delete(SPObject */*deleted*/)
+{
+    quit_listening();
+    remove_link();
+    param_set_and_write_new_value (_pathvector);
+}
+
+void
+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));
+    }
+    if (SP_IS_TEXT(linked_obj)) {
+        curve = SP_TEXT(linked_obj)->getNormalizedBpath();
+    }
+
+    if (curve == NULL) {
+        // curve invalid, set default value
+        _pathvector = SVGD_to_2GeomPath(defvalue);
+    } else {
+        _pathvector = BPath_to_2GeomPath(SP_CURVE_BPATH(curve));
+        sp_curve_unref(curve);
+    }
+
+    must_recalculate_pwd2 = true;
+    signal_path_changed.emit();
+    SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
+}
+
 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
 void
 PathParam::on_edit_button_click()
@@ -182,38 +348,52 @@ PathParam::on_edit_button_click()
 void
 PathParam::on_paste_button_click()
 {
-    // check if something is in the clipboard
-    GSList * clipboard = sp_selection_get_clipboard();
-    if (clipboard == NULL || clipboard->data == NULL) {
-        SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
+    Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
+    Glib::ustring svgd = cm->getPathParameter();
+    
+    if (svgd == "")
         return;
-    }
 
-    Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
-    if (!strcmp (repr->name(), "svg:path")) {
-        const char * svgd = repr->attribute("d");
-        if (svgd) {
-            if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD
-                SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
-                            _("This effect does not support arcs yet, try to convert to path.") );
-                return;
-            } else {
-                param_write_to_repr(svgd);
-                signal_path_pasted.emit();
-                sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
-                                 _("Paste path parameter"));
-            }
-        }
-    } else {
-        SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));
-        return;
-    }
+    // remove possible link to path
+    remove_link();
+
+    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_copy_button_click()
 {
-    sp_selection_copy_lpe_pathparam(this);
+    Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
+    cm->copyPathParameter(this);
+}
+
+void
+PathParam::on_link_button_click()
+{
+    Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
+    Glib::ustring pathid = cm->getShapeOrTextObjectId();
+
+    if (pathid == "") {
+        return;
+    }
+
+    // add '#' at start to make it an uri.
+    pathid.insert(pathid.begin(), '#');
+    if ( href && strcmp(pathid.c_str(), href) == 0 ) {
+        // no change, do nothing
+        return;
+    } else {
+        // TODO:
+        // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
+        // 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"));
+    }
 }
 
 } /* namespace LivePathEffect */