Code

GSoC node tool
[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 "svg/svg.h"
12 #include <2geom/svg-path-parser.h>
13 #include <2geom/sbasis-to-bezier.h>
14 #include <2geom/pathvector.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       changed(true),
53       _pathvector(),
54       _pwd2(),
55       must_recalculate_pwd2(false),
56       href(NULL),
57       ref( (SPObject*)effect->getLPEObj() )
58 {
59     defvalue = g_strdup(default_value);
60     param_readSVGValue(defvalue);
61     oncanvas_editable = true;
63     ref_changed_connection = ref.changedSignal().connect(sigc::mem_fun(*this, &PathParam::ref_changed));
64 }
66 PathParam::~PathParam()
67 {
68     remove_link();
70     g_free(defvalue);
71 }
73 std::vector<Geom::Path> const &
74 PathParam::get_pathvector()
75 {
76     return _pathvector;
77 }
79 Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
80 PathParam::get_pwd2()
81 {
82     ensure_pwd2();
83     return _pwd2;
84 }
86 void
87 PathParam::param_set_default()
88 {
89     param_readSVGValue(defvalue);
90 }
92 void
93 PathParam::param_set_and_write_default()
94 {
95     param_write_to_repr(defvalue);
96 }
98 bool
99 PathParam::param_readSVGValue(const gchar * strvalue)
101     if (strvalue) {
102         _pathvector.clear();
103         remove_link();
104         must_recalculate_pwd2 = true;
106         if (strvalue[0] == '#') {
107             if (href)
108                 g_free(href);
109             href = g_strdup(strvalue);
111             // Now do the attaching, which emits the changed signal.
112             try {
113                 ref.attach(Inkscape::URI(href));
114             } catch (Inkscape::BadURIException &e) {
115                 g_warning("%s", e.what());
116                 ref.detach();
117                 _pathvector = sp_svg_read_pathv(defvalue);
118             }
119         } else {
120             _pathvector = sp_svg_read_pathv(strvalue);
121         }
123         emit_changed();
124         return true;
125     }
127     return false;
130 gchar *
131 PathParam::param_getSVGValue() const
133     if (href) {
134         return href;
135     } else {
136         gchar * svgd = sp_svg_write_path( _pathvector );
137         return svgd;
138     }
141 Gtk::Widget *
142 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
144     Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
146     Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
147     static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
148     tooltips->set_tip(*pLabel, param_tooltip);
150     Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "tool-node-editor", Inkscape::ICON_SIZE_BUTTON) );
151     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
152     pButton->set_relief(Gtk::RELIEF_NONE);
153     pIcon->show();
154     pButton->add(*pIcon);
155     pButton->show();
156     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
157     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
158     tooltips->set_tip(*pButton, _("Edit on-canvas"));
160     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
161     pButton = Gtk::manage(new Gtk::Button());
162     pButton->set_relief(Gtk::RELIEF_NONE);
163     pIcon->show();
164     pButton->add(*pIcon);
165     pButton->show();
166     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
167     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
168     tooltips->set_tip(*pButton, _("Copy path"));
170     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
171     pButton = Gtk::manage(new Gtk::Button());
172     pButton->set_relief(Gtk::RELIEF_NONE);
173     pIcon->show();
174     pButton->add(*pIcon);
175     pButton->show();
176     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
177     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
178     tooltips->set_tip(*pButton, _("Paste path"));
180     pIcon = Gtk::manage( sp_icon_get_icon( "edit-clone", Inkscape::ICON_SIZE_BUTTON) );
181     pButton = Gtk::manage(new Gtk::Button());
182     pButton->set_relief(Gtk::RELIEF_NONE);
183     pIcon->show();
184     pButton->add(*pIcon);
185     pButton->show();
186     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_link_button_click));
187     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
188     tooltips->set_tip(*pButton, _("Link to path"));
190     static_cast<Gtk::HBox*>(_widget)->show_all_children();
192     return dynamic_cast<Gtk::Widget *> (_widget);
195 void
196 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
198     // TODO this whole method is broken!
201 void
202 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
203 {   
204     // TODO this too!
205     np->show_helperpath = true;
206     np->helperpath_rgba = 0x009000ff;
207     np->helperpath_width = 1.0;
210 void
211 PathParam::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
213     hp_vec.push_back(_pathvector);
216 /*
217  * Only applies transform when not referring to other path!
218  */
219 void
220 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
222     // only apply transform when not referring to other path
223     if (!href) {
224         set_new_value( _pathvector * postmul, true );
225     }
228 /*
229  * See comments for set_new_value(std::vector<Geom::Path>).
230  */
231 void
232 PathParam::set_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath, bool write_to_svg)
234     remove_link();
235     _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
237     if (write_to_svg) {
238         gchar * svgd = sp_svg_write_path( _pathvector );
239         param_write_to_repr(svgd);
240         g_free(svgd);
242         // After the whole "writing to svg avalanche of function calling": force value upon pwd2 and don't recalculate.
243         _pwd2 = newpath;
244         must_recalculate_pwd2 = false;
245     } else {
246         _pwd2 = newpath;
247         must_recalculate_pwd2 = false;
248         emit_changed();
249     }
252 /*
253  * This method sets new path data.
254  * If this PathParam refers to another path, this link is removed (and replaced with explicit path data).
255  *
256  * If write_to_svg = true :
257  *          The new path data is written to SVG. In this case the signal_path_changed signal
258  *          is not directly emited in this method, because writing to SVG
259  *          triggers the LPEObject to which this belongs to call Effect::setParameter which calls
260  *          PathParam::readSVGValue, which finally emits the signal_path_changed signal.
261  * If write_to_svg = false :
262  *          The new path data is not written to SVG. This method will emit the signal_path_changed signal.
263  */
264 void
265 PathParam::set_new_value (std::vector<Geom::Path> const &newpath, bool write_to_svg)
267     remove_link();
268     _pathvector = newpath;
269     must_recalculate_pwd2 = true;
271     if (write_to_svg) {
272         gchar * svgd = sp_svg_write_path( _pathvector );
273         param_write_to_repr(svgd);
274         g_free(svgd);
275     } else {
276         emit_changed();
277     }
280 void
281 PathParam::ensure_pwd2()
283     if (must_recalculate_pwd2) {
284         _pwd2.clear();
285         for (unsigned int i=0; i < _pathvector.size(); i++) {
286             _pwd2.concat( _pathvector[i].toPwSb() );
287         }
289         must_recalculate_pwd2 = false;
290     }
293 void
294 PathParam::emit_changed()
296     changed = true;
297     signal_path_changed.emit();
300 void
301 PathParam::start_listening(SPObject * to)
303     if ( to == NULL ) {
304         return;
305     }
306     linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
307     linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
308     linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
311 void
312 PathParam::quit_listening(void)
314     linked_modified_connection.disconnect();
315     linked_delete_connection.disconnect();
318 void
319 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
321     quit_listening();
322     if ( new_ref ) {
323         start_listening(new_ref);
324     }
327 void
328 PathParam::remove_link()
330     if (href) {
331         ref.detach();
332         g_free(href);
333         href = NULL;
334     }
337 void
338 PathParam::linked_delete(SPObject */*deleted*/)
340     quit_listening();
341     remove_link();
342     set_new_value (_pathvector, true);
345 void
346 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
348     SPCurve *curve = NULL;
349     if (SP_IS_SHAPE(linked_obj)) {
350         curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
351     }
352     if (SP_IS_TEXT(linked_obj)) {
353         curve = SP_TEXT(linked_obj)->getNormalizedBpath();
354     }
356     if (curve == NULL) {
357         // curve invalid, set default value
358         _pathvector = sp_svg_read_pathv(defvalue);
359     } else {
360         _pathvector = curve->get_pathvector();
361         curve->unref();
362     }
364     must_recalculate_pwd2 = true;
365     emit_changed();
366     SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
369 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
370 void
371 PathParam::on_edit_button_click()
373     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
374     if (item != NULL) {
375         param_editOncanvas(item, SP_ACTIVE_DESKTOP);
376     }
379 void
380 PathParam::paste_param_path(const char *svgd)
382     // only recognize a non-null, non-empty string
383     if (svgd && *svgd) {
384         // remove possible link to path
385         remove_link();
387         param_write_to_repr(svgd);
388         signal_path_pasted.emit();
389     }
392 void
393 PathParam::on_paste_button_click()
395     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
396     Glib::ustring svgd = cm->getPathParameter();
397     paste_param_path(svgd.data());
398     sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
399                      _("Paste path parameter"));
402 void
403 PathParam::on_copy_button_click()
405     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
406     cm->copyPathParameter(this);
409 void
410 PathParam::on_link_button_click()
412     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
413     Glib::ustring pathid = cm->getShapeOrTextObjectId();
415     if (pathid == "") {
416         return;
417     }
419     // add '#' at start to make it an uri.
420     pathid.insert(pathid.begin(), '#');
421     if ( href && strcmp(pathid.c_str(), href) == 0 ) {
422         // no change, do nothing
423         return;
424     } else {
425         // TODO:
426         // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
427         // check if linking to object to which LPE is applied (maybe delegated to PathReference
429         param_write_to_repr(pathid.c_str());
430         sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
431                          _("Link path parameter to path"));
432     }
435 } /* namespace LivePathEffect */
437 } /* namespace Inkscape */
439 /*
440   Local Variables:
441   mode:c++
442   c-file-style:"stroustrup"
443   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
444   indent-tabs-mode:nil
445   fill-column:99
446   End:
447 */
448 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :