Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / live_effects / parameter / path.cpp
index f24fa933a053da5736ec95fca3742b7bb8abdf4e..8d9b2376ff586770d7b5b8da664c629fa6b770f0 100644 (file)
-#define INKSCAPE_LIVEPATHEFFECT_PARAMETER_PATH_CPP\r
-\r
-/*\r
- * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>\r
- *\r
- * Released under GNU GPL, read the file 'COPYING' for more information\r
- */\r
-\r
-#include "live_effects/parameter/path.h"\r
-#include "live_effects/effect.h"\r
-#include "live_effects/n-art-bpath-2geom.h"\r
-#include "svg/svg.h"\r
-#include <2geom/svg-path-parser.h>\r
-#include <2geom/sbasis-to-bezier.h>\r
-#include <2geom/d2.h>\r
-\r
-#include "ui/widget/point.h"\r
-#include "widgets/icon.h"\r
-#include <gtk/gtkstock.h>\r
-#include "selection-chemistry.h"\r
-\r
-#include "desktop.h"\r
-#include "inkscape.h"\r
-#include "message-stack.h"\r
-#include "verbs.h"\r
-#include "document.h"\r
-\r
-#define noLPEPATHPARAM_DEBUG\r
-\r
-namespace Inkscape {\r
-\r
-namespace LivePathEffect {\r
-\r
-PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,\r
-                      const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,\r
-                      Effect* effect, const gchar * default_value)\r
-    : Parameter(label, tip, key, wr, effect)\r
-{\r
-    _widget = NULL;\r
-    _tooltips = NULL;\r
-    defvalue = g_strdup(default_value);\r
-    param_readSVGValue(defvalue);\r
-}\r
-\r
-PathParam::~PathParam()\r
-{\r
-    if (_tooltips)\r
-        delete _tooltips;\r
-    // _widget is managed by GTK so do not delete!\r
-\r
-    g_free(defvalue);\r
-}\r
-\r
-void\r
-PathParam::param_set_default()\r
-{\r
-    param_readSVGValue(defvalue);\r
-}\r
-\r
-bool\r
-PathParam::param_readSVGValue(const gchar * strvalue)\r
-{\r
-    if (strvalue) {\r
-        Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;\r
-        std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);\r
-        for (unsigned int i=0; i < temppath.size(); i++) {\r
-            newpath.concat( temppath[i].toPwSb() );\r
-        }\r
-        *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;\r
-        return true;\r
-    }\r
-\r
-    return false;\r
-}\r
-\r
-gchar *\r
-PathParam::param_writeSVGValue() const\r
-{\r
-    const std::vector<Geom::Path> temppath =\r
-        Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);\r
-    gchar * svgd = SVGD_from_2GeomPath( temppath );\r
-    return svgd;\r
-}\r
-\r
-Gtk::Widget *\r
-PathParam::param_getWidget()\r
-{\r
-    if (!_widget) {\r
-        _widget = Gtk::manage(new Gtk::HBox());\r
-        _tooltips = new Gtk::Tooltips();\r
-\r
-        Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));\r
-        static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);\r
-        _tooltips->set_tip(*pLabel, param_tooltip);\r
-\r
-        Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );\r
-        Gtk::Button * pButton = Gtk::manage(new Gtk::Button());\r
-        pButton->set_relief(Gtk::RELIEF_NONE);\r
-        pIcon->show();\r
-        pButton->add(*pIcon);\r
-        pButton->show();\r
-        pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));\r
-        static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);\r
-        _tooltips->set_tip(*pButton, _("Edit on-canvas"));\r
-#ifndef LPEPATHPARAM_DEBUG\r
-        pButton->set_sensitive(false);\r
-#endif\r
-\r
-        pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );\r
-        pButton = Gtk::manage(new Gtk::Button());\r
-        pButton->set_relief(Gtk::RELIEF_NONE);\r
-        pIcon->show();\r
-        pButton->add(*pIcon);\r
-        pButton->show();\r
-        pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));\r
-        static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);\r
-        _tooltips->set_tip(*pButton, _("Paste path"));\r
-\r
-        static_cast<Gtk::HBox*>(_widget)->show_all_children();\r
-\r
-    }\r
-    return dynamic_cast<Gtk::Widget *> (_widget);\r
-}\r
-\r
-void\r
-PathParam::on_edit_button_click()\r
-{\r
-    g_message("give this path to edit on canvas!");\r
-}\r
-\r
-void\r
-PathParam::on_paste_button_click()\r
-{\r
-    // check if something is in the clipboard\r
-    GSList * clipboard = sp_selection_get_clipboard();\r
-    if (clipboard == NULL || clipboard->data == NULL) {\r
-        SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));\r
-        return;\r
-    }\r
-\r
-    Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;\r
-    if (!strcmp (repr->name(), "svg:path")) {\r
-        const char * svgd = repr->attribute("d");\r
-        if (svgd) {\r
-            param_write_to_repr(svgd);\r
-            signal_path_pasted.emit();\r
-            sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, \r
-                             _("Paste path parameter"));\r
-        }\r
-    } else {\r
-        SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));\r
-    }\r
-\r
-}\r
-\r
-void\r
-PathParam::param_write_to_repr(const char * svgd)\r
-{\r
-    param_effect->getRepr()->setAttribute(param_key.c_str(), svgd);\r
-}\r
-\r
-\r
-} /* namespace LivePathEffect */\r
-\r
-} /* namespace Inkscape */\r
-\r
-/*\r
-  Local Variables:\r
-  mode:c++\r
-  c-file-style:"stroustrup"\r
-  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
-  indent-tabs-mode:nil\r
-  fill-column:99\r
-  End:\r
-*/\r
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r
+/*
+ * 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 "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"
+#include "widgets/icon.h"
+#include <gtk/gtkstock.h>
+#include "selection-chemistry.h"
+#include "xml/repr.h"
+#include "desktop.h"
+#include "inkscape.h"
+#include "message-stack.h"
+#include "verbs.h"
+#include "document.h"
+
+// needed for on-canvas editting:
+#include "tools-switch.h"
+#include "shape-editor.h"
+#include "desktop-handles.h"
+#include "selection.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"
+
+#include "ui/tool/node-tool.h"
+#include "ui/tool/multi-path-manipulator.h"
+#include "ui/tool/shape-record.h"
+
+
+namespace Inkscape {
+
+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),
+      changed(true),
+      _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()
+{
+    param_readSVGValue(defvalue);
+}
+
+void
+PathParam::param_set_and_write_default()
+{
+    param_write_to_repr(defvalue);
+}
+
+bool
+PathParam::param_readSVGValue(const gchar * strvalue)
+{
+    if (strvalue) {
+        _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 = sp_svg_read_pathv(defvalue);
+            }
+        } else {
+            _pathvector = sp_svg_read_pathv(strvalue);
+        }
+
+        emit_changed();
+        return true;
+    }
+
+    return false;
+}
+
+gchar *
+PathParam::param_getSVGValue() const
+{
+    if (href) {
+        return href;
+    } else {
+        gchar * svgd = sp_svg_write_path( _pathvector );
+        return svgd;
+    }
+}
+
+Gtk::Widget *
+PathParam::param_newWidget(Gtk::Tooltips * tooltips)
+{
+    Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
+
+    Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
+    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( "tool-node-editor", Inkscape::ICON_SIZE_BUTTON) );
+    Gtk::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_edit_button_click));
+    static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
+    tooltips->set_tip(*pButton, _("Edit on-canvas"));
+
+    pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, 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_copy_button_click));
+    static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
+    tooltips->set_tip(*pButton, _("Copy path"));
+
+    pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, 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_paste_button_click));
+    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);
+}
+
+void
+PathParam::param_editOncanvas(SPItem *item, SPDesktop * dt)
+{
+    using namespace Inkscape::UI;
+
+    // TODO remove the tools_switch atrocity.
+    if (!tools_isactive(dt, TOOLS_NODES)) {
+        tools_switch(dt, TOOLS_NODES);
+    }
+
+    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) {
+        r.item = reinterpret_cast<SPItem*>(param_effect->getLPEObj());
+        r.lpe_key = param_key;
+    } else {
+        r.item = ref.getObject();
+    }
+    shapes.insert(r);
+    nt->_multipath->setItems(shapes);
+}
+
+void
+PathParam::param_setup_nodepath(Inkscape::NodePath::Path *)
+{
+    // 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) {
+        set_new_value( _pathvector * postmul, true );
+    }
+}
+
+/*
+ * See comments for set_new_value(std::vector<Geom::Path>).
+ */
+void
+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);
+
+    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::set_new_value (std::vector<Geom::Path> const &newpath, bool write_to_svg)
+{
+    remove_link();
+    _pathvector = newpath;
+    must_recalculate_pwd2 = true;
+
+    if (write_to_svg) {
+        gchar * svgd = sp_svg_write_path( _pathvector );
+        param_write_to_repr(svgd);
+        g_free(svgd);
+    } else {
+        emit_changed();
+    }
+}
+
+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::emit_changed()
+{
+    changed = true;
+    signal_path_changed.emit();
+}
+
+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();
+    set_new_value (_pathvector, true);
+}
+
+void
+PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
+{
+    SPCurve *curve = NULL;
+    if (SP_IS_SHAPE(linked_obj)) {
+        curve = SP_SHAPE(linked_obj)->getCurve();
+    }
+    if (SP_IS_TEXT(linked_obj)) {
+        curve = SP_TEXT(linked_obj)->getNormalizedBpath();
+    }
+
+    if (curve == NULL) {
+        // curve invalid, set default value
+        _pathvector = sp_svg_read_pathv(defvalue);
+    } else {
+        _pathvector = curve->get_pathvector();
+        curve->unref();
+    }
+
+    must_recalculate_pwd2 = true;
+    emit_changed();
+    SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
+}
+
+/* CALLBACK FUNCTIONS FOR THE BUTTONS */
+void
+PathParam::on_edit_button_click()
+{
+    SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
+    if (item != NULL) {
+        param_editOncanvas(item, SP_ACTIVE_DESKTOP);
+    }
+}
+
+void
+PathParam::paste_param_path(const char *svgd)
+{
+    // only recognize a non-null, non-empty string
+    if (svgd && *svgd) {
+        // remove possible link to path
+        remove_link();
+
+        param_write_to_repr(svgd);
+        signal_path_pasted.emit();
+    }
+}
+
+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
+PathParam::on_copy_button_click()
+{
+    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(SP_ACTIVE_DESKTOP);
+
+    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());
+        DocumentUndo::done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
+                           _("Link path parameter to path"));
+    }
+}
+
+} /* namespace LivePathEffect */
+
+} /* namespace Inkscape */
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :