Code

add linking to other paths to lpe-PathParam
[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 "live_effects/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));
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 = SVGD_to_2GeomPath(defvalue);
118             }
119         } else {
120             _pathvector = SVGD_to_2GeomPath(strvalue);
121         }
123         signal_path_changed.emit();
124         return true;
125     }
127     return false;
130 gchar *
131 PathParam::param_writeSVGValue() const
133     if (href) {
134         return href;
135     } else {
136         gchar * svgd = SVGD_from_2GeomPath( _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( "draw_node", 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     // If not already in nodecontext, goto it!
199     if (!tools_isactive(dt, TOOLS_NODES)) {
200         tools_switch_current(TOOLS_NODES);
201     }
203     ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
204     if (!href) {
205         shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
206     } else {
207         // set referred item for editing
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 = SVGD_from_2GeomPath( _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::ensure_pwd2()
247     if (must_recalculate_pwd2) {
248         _pwd2.clear();
249         for (unsigned int i=0; i < _pathvector.size(); i++) {
250             _pwd2.concat( _pathvector[i].toPwSb() );
251         }
253         must_recalculate_pwd2 = false;
254     }
257 void
258 PathParam::start_listening(SPObject * to)
260     if ( to == NULL ) {
261         return;
262     }
263     linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
264     linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
265     linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
268 void
269 PathParam::quit_listening(void)
271     linked_modified_connection.disconnect();
272     linked_delete_connection.disconnect();
275 void
276 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
278     quit_listening();
279     if ( new_ref ) {
280         start_listening(new_ref);
281     }
284 void
285 PathParam::remove_link()
287     if (href) {
288         ref.detach();
289         g_free(href);
290         href = NULL;
291     }
294 void
295 PathParam::linked_delete(SPObject */*deleted*/)
297 // don't know what to do yet. not acting probably crashes inkscape.
298     g_message("PathParam::linked_delete");
301 void
302 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
304     SPCurve *curve = NULL;
305     if (SP_IS_SHAPE(linked_obj)) {
306         curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
307     }
308     if (SP_IS_TEXT(linked_obj)) {
309         curve = SP_TEXT(linked_obj)->getNormalizedBpath();
310     }
312     if (curve == NULL) {
313         // curve invalid, set default value
314         _pathvector = SVGD_to_2GeomPath(defvalue);
315     } else {
316         _pathvector = BPath_to_2GeomPath(SP_CURVE_BPATH(curve));
317         sp_curve_unref(curve);
318     }
320     must_recalculate_pwd2 = true;
321     signal_path_changed.emit();
322     SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
325 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
326 void
327 PathParam::on_edit_button_click()
329     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
330     if (item != NULL) {
331         param_editOncanvas(item, SP_ACTIVE_DESKTOP);
332     }
335 void
336 PathParam::on_paste_button_click()
338     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
339     Glib::ustring svgd = cm->getPathParameter();
340     
341     if (svgd == "")
342         return;
344     // Temporary hack until 2Geom supports arcs in SVGD
345     if (svgd.find('A') != Glib::ustring::npos) {
346         SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
347                     _("This effect does not support arcs yet, try to convert to path.") );
348         return;
349     } else {
350         // remove possible link to path
351         remove_link();
353         param_write_to_repr(svgd.data());
354         signal_path_pasted.emit();
355         sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
356                          _("Paste path parameter"));
357     }
360 void
361 PathParam::on_copy_button_click()
363     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
364     cm->copyPathParameter(this);
367 void
368 PathParam::on_link_button_click()
370     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
371     Glib::ustring pathid = cm->getShapeOrTextObjectId();
373     if (pathid == "") {
374         return;
375     }
377     // add '#' at start to make it an uri.
378     pathid.insert(pathid.begin(), '#');
379     if ( href && strcmp(pathid.c_str(), href) == 0 ) {
380         // no change, do nothing
381         return;
382     } else {
383         // TODO:
384         // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
385         // check if linking to object to which LPE is applied (maybe delegated to PathReference
387         param_write_to_repr(pathid.c_str());
388         sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
389                          _("Link path parameter to path"));
390     }
393 } /* namespace LivePathEffect */
395 } /* namespace Inkscape */
397 /*
398   Local Variables:
399   mode:c++
400   c-file-style:"stroustrup"
401   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
402   indent-tabs-mode:nil
403   fill-column:99
404   End:
405 */
406 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :