Code

Enable simultaneous knotholder and nodepath
[inkscape.git] / src / live_effects / parameter / path.cpp
1 #define INKSCAPE_LIVEPATHEFFECT_PARAMETER_PATH_CPP
3 /*
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
5  *
6  * Released under GNU GPL, read the file 'COPYING' for more information
7  */
9 #include "live_effects/parameter/path.h"
10 #include "live_effects/effect.h"
11 #include "libnr/n-art-bpath-2geom.h"
12 #include "svg/svg.h"
13 #include <2geom/svg-path-parser.h>
14 #include <2geom/sbasis-to-bezier.h>
15 #include <2geom/d2.h>
17 #include "ui/widget/point.h"
18 #include "widgets/icon.h"
19 #include <gtk/gtkstock.h>
20 #include "selection-chemistry.h"
21 #include "xml/repr.h"
22 #include "desktop.h"
23 #include "inkscape.h"
24 #include "message-stack.h"
25 #include "verbs.h"
26 #include "document.h"
28 // needed for on-canvas editting:
29 #include "tools-switch.h"
30 #include "shape-editor.h"
31 #include "node-context.h"
32 #include "desktop-handles.h"
33 #include "selection.h"
34 #include "nodepath.h"
35 // clipboard support
36 #include "ui/clipboard.h"
37 // required for linking to other paths
38 #include "uri.h"
39 #include "sp-shape.h"
40 #include "sp-text.h"
41 #include "display/curve.h"
44 namespace Inkscape {
46 namespace LivePathEffect {
48 PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,
49                       const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
50                       Effect* effect, const gchar * default_value)
51     : Parameter(label, tip, key, wr, effect),
52       _pathvector(),
53       _pwd2(),
54       must_recalculate_pwd2(false),
55       href(NULL),
56       ref( (SPObject*)effect->getLPEObj() )
57 {
58     defvalue = g_strdup(default_value);
59     param_readSVGValue(defvalue);
60     oncanvas_editable = true;
62     ref_changed_connection = ref.changedSignal().connect(sigc::mem_fun(*this, &PathParam::ref_changed));
63 }
65 PathParam::~PathParam()
66 {
67     remove_link();
69     g_free(defvalue);
70 }
72 std::vector<Geom::Path> const &
73 PathParam::get_pathvector()
74 {
75     return _pathvector;
76 }
78 Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
79 PathParam::get_pwd2()
80 {
81     ensure_pwd2();
82     return _pwd2;
83 }
85 void
86 PathParam::param_set_default()
87 {
88     param_readSVGValue(defvalue);
89 }
91 void
92 PathParam::param_set_and_write_default()
93 {
94     param_write_to_repr(defvalue);
95 }
97 bool
98 PathParam::param_readSVGValue(const gchar * strvalue)
99 {
100     if (strvalue) {
101         _pathvector.clear();
102         remove_link();
103         must_recalculate_pwd2 = true;
105         if (strvalue[0] == '#') {
106             if (href)
107                 g_free(href);
108             href = g_strdup(strvalue);
110             // Now do the attaching, which emits the changed signal.
111             try {
112                 ref.attach(Inkscape::URI(href));
113             } catch (Inkscape::BadURIException &e) {
114                 g_warning("%s", e.what());
115                 ref.detach();
116                 _pathvector = sp_svg_read_pathv(defvalue);
117             }
118         } else {
119             _pathvector = sp_svg_read_pathv(strvalue);
120         }
122         signal_path_changed.emit();
123         return true;
124     }
126     return false;
129 gchar *
130 PathParam::param_getSVGValue() const
132     if (href) {
133         return href;
134     } else {
135         gchar * svgd = sp_svg_write_path( _pathvector );
136         return svgd;
137     }
140 Gtk::Widget *
141 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
143     Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
145     Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
146     static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
147     tooltips->set_tip(*pLabel, param_tooltip);
149     Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
150     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
151     pButton->set_relief(Gtk::RELIEF_NONE);
152     pIcon->show();
153     pButton->add(*pIcon);
154     pButton->show();
155     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
156     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
157     tooltips->set_tip(*pButton, _("Edit on-canvas"));
159     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
160     pButton = Gtk::manage(new Gtk::Button());
161     pButton->set_relief(Gtk::RELIEF_NONE);
162     pIcon->show();
163     pButton->add(*pIcon);
164     pButton->show();
165     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
166     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
167     tooltips->set_tip(*pButton, _("Copy path"));
169     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
170     pButton = Gtk::manage(new Gtk::Button());
171     pButton->set_relief(Gtk::RELIEF_NONE);
172     pIcon->show();
173     pButton->add(*pIcon);
174     pButton->show();
175     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
176     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
177     tooltips->set_tip(*pButton, _("Paste path"));
179     pIcon = Gtk::manage( sp_icon_get_icon( "edit_clone", Inkscape::ICON_SIZE_BUTTON) );
180     pButton = Gtk::manage(new Gtk::Button());
181     pButton->set_relief(Gtk::RELIEF_NONE);
182     pIcon->show();
183     pButton->add(*pIcon);
184     pButton->show();
185     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_link_button_click));
186     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
187     tooltips->set_tip(*pButton, _("Link to path"));
189     static_cast<Gtk::HBox*>(_widget)->show_all_children();
191     return dynamic_cast<Gtk::Widget *> (_widget);
194 void
195 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
197     // If not already in nodecontext, goto it!
198     if (!tools_isactive(dt, TOOLS_NODES)) {
199         tools_switch_current(TOOLS_NODES);
200     }
202     ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
203     if (!href) {
204         shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
205     } else {
206         // set referred item for editing
207         shape_editor->set_item(ref.getObject(), SH_NODEPATH);
208     }
211 void
212 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
214     np->show_helperpath = true;
215     np->helperpath_rgba = 0x009000ff;
216     np->helperpath_width = 1.0;
219 void
220 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
222     if (!href) {
223         // TODO: recode this to apply transform to _pathvector instead?
225         // only apply transform when not referring to other path
226         ensure_pwd2();
227         param_set_and_write_new_value( _pwd2 * postmul );
228     }
231 void
232 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
234     remove_link();
235     _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
236     gchar * svgd = sp_svg_write_path( _pathvector );
237     param_write_to_repr(svgd);
238     g_free(svgd);
239     // force value upon pwd2 and don't recalculate.
240     _pwd2 = newpath;
241     must_recalculate_pwd2 = false;
244 void
245 PathParam::param_set_and_write_new_value (std::vector<Geom::Path> const & newpath)
247     remove_link();
248     _pathvector = newpath;
249     must_recalculate_pwd2 = true;
251     gchar * svgd = sp_svg_write_path( _pathvector );
252     param_write_to_repr(svgd);
253     g_free(svgd);
256 void
257 PathParam::ensure_pwd2()
259     if (must_recalculate_pwd2) {
260         _pwd2.clear();
261         for (unsigned int i=0; i < _pathvector.size(); i++) {
262             _pwd2.concat( _pathvector[i].toPwSb() );
263         }
265         must_recalculate_pwd2 = false;
266     }
269 void
270 PathParam::start_listening(SPObject * to)
272     if ( to == NULL ) {
273         return;
274     }
275     linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
276     linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
277     linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
280 void
281 PathParam::quit_listening(void)
283     linked_modified_connection.disconnect();
284     linked_delete_connection.disconnect();
287 void
288 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
290     quit_listening();
291     if ( new_ref ) {
292         start_listening(new_ref);
293     }
296 void
297 PathParam::remove_link()
299     if (href) {
300         ref.detach();
301         g_free(href);
302         href = NULL;
303     }
306 void
307 PathParam::linked_delete(SPObject */*deleted*/)
309     quit_listening();
310     remove_link();
311     param_set_and_write_new_value (_pathvector);
314 void
315 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
317     SPCurve *curve = NULL;
318     if (SP_IS_SHAPE(linked_obj)) {
319         curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
320     }
321     if (SP_IS_TEXT(linked_obj)) {
322         curve = SP_TEXT(linked_obj)->getNormalizedBpath();
323     }
325     if (curve == NULL) {
326         // curve invalid, set default value
327         _pathvector = sp_svg_read_pathv(defvalue);
328     } else {
329         _pathvector = curve->get_pathvector();
330         curve->unref();
331     }
333     must_recalculate_pwd2 = true;
334     signal_path_changed.emit();
335     SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
338 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
339 void
340 PathParam::on_edit_button_click()
342     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
343     if (item != NULL) {
344         param_editOncanvas(item, SP_ACTIVE_DESKTOP);
345     }
348 void
349 PathParam::paste_param_path(const char *svgd)
351     if (svgd == "")
352         return;
354     // remove possible link to path
355     remove_link();
357     param_write_to_repr(svgd);
358     signal_path_pasted.emit();
361 void
362 PathParam::on_paste_button_click()
364     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
365     Glib::ustring svgd = cm->getPathParameter();
366     paste_param_path(svgd.data());
367     sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
368                      _("Paste path parameter"));
371 void
372 PathParam::on_copy_button_click()
374     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
375     cm->copyPathParameter(this);
378 void
379 PathParam::on_link_button_click()
381     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
382     Glib::ustring pathid = cm->getShapeOrTextObjectId();
384     if (pathid == "") {
385         return;
386     }
388     // add '#' at start to make it an uri.
389     pathid.insert(pathid.begin(), '#');
390     if ( href && strcmp(pathid.c_str(), href) == 0 ) {
391         // no change, do nothing
392         return;
393     } else {
394         // TODO:
395         // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
396         // check if linking to object to which LPE is applied (maybe delegated to PathReference
398         param_write_to_repr(pathid.c_str());
399         sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
400                          _("Link path parameter to path"));
401     }
404 } /* namespace LivePathEffect */
406 } /* namespace Inkscape */
408 /*
409   Local Variables:
410   mode:c++
411   c-file-style:"stroustrup"
412   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
413   indent-tabs-mode:nil
414   fill-column:99
415   End:
416 */
417 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :