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/pathvector.h>
16 #include <2geom/d2.h>
18 #include "ui/widget/point.h"
19 #include "widgets/icon.h"
20 #include <gtk/gtkstock.h>
21 #include "selection-chemistry.h"
22 #include "xml/repr.h"
23 #include "desktop.h"
24 #include "inkscape.h"
25 #include "message-stack.h"
26 #include "verbs.h"
27 #include "document.h"
29 // needed for on-canvas editting:
30 #include "tools-switch.h"
31 #include "shape-editor.h"
32 #include "node-context.h"
33 #include "desktop-handles.h"
34 #include "selection.h"
35 #include "nodepath.h"
36 // clipboard support
37 #include "ui/clipboard.h"
38 // required for linking to other paths
39 #include "uri.h"
40 #include "sp-shape.h"
41 #include "sp-text.h"
42 #include "display/curve.h"
45 namespace Inkscape {
47 namespace LivePathEffect {
49 PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,
50 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
51 Effect* effect, const gchar * default_value)
52 : Parameter(label, tip, key, wr, effect),
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)
100 {
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 signal_path_changed.emit();
124 return true;
125 }
127 return false;
128 }
130 gchar *
131 PathParam::param_getSVGValue() const
132 {
133 if (href) {
134 return href;
135 } else {
136 gchar * svgd = sp_svg_write_path( _pathvector );
137 return svgd;
138 }
139 }
141 Gtk::Widget *
142 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
143 {
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);
193 }
195 void
196 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
197 {
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 shape_editor->set_item(ref.getObject(), SH_NODEPATH);
209 }
210 }
212 void
213 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
214 {
215 np->show_helperpath = true;
216 np->helperpath_rgba = 0x009000ff;
217 np->helperpath_width = 1.0;
218 }
220 /*
221 * Only applies transform when not referring to other path!
222 */
223 void
224 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
225 {
226 // only apply transform when not referring to other path
227 if (!href) {
228 set_new_value( _pathvector * postmul, true );
229 }
230 }
232 /*
233 * See comments for set_new_value(std::vector<Geom::Path>).
234 */
235 void
236 PathParam::set_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath, bool write_to_svg)
237 {
238 remove_link();
239 _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
241 if (write_to_svg) {
242 gchar * svgd = sp_svg_write_path( _pathvector );
243 param_write_to_repr(svgd);
244 g_free(svgd);
246 // After the whole "writing to svg avalanche of function calling": force value upon pwd2 and don't recalculate.
247 _pwd2 = newpath;
248 must_recalculate_pwd2 = false;
249 } else {
250 _pwd2 = newpath;
251 must_recalculate_pwd2 = false;
252 signal_path_changed.emit();
253 }
254 }
256 /*
257 * This method sets new path data.
258 * If this PathParam refers to another path, this link is removed (and replaced with explicit path data).
259 *
260 * If write_to_svg = true :
261 * The new path data is written to SVG. In this case the signal_path_changed signal
262 * is not directly emited in this method, because writing to SVG
263 * triggers the LPEObject to which this belongs to call Effect::setParameter which calls
264 * PathParam::readSVGValue, which finally emits the signal_path_changed signal.
265 * If write_to_svg = false :
266 * The new path data is not written to SVG. This method will emit the signal_path_changed signal.
267 */
268 void
269 PathParam::set_new_value (std::vector<Geom::Path> const &newpath, bool write_to_svg)
270 {
271 remove_link();
272 _pathvector = newpath;
273 must_recalculate_pwd2 = true;
275 if (write_to_svg) {
276 gchar * svgd = sp_svg_write_path( _pathvector );
277 param_write_to_repr(svgd);
278 g_free(svgd);
279 } else {
280 signal_path_changed.emit();
281 }
282 }
284 void
285 PathParam::ensure_pwd2()
286 {
287 if (must_recalculate_pwd2) {
288 _pwd2.clear();
289 for (unsigned int i=0; i < _pathvector.size(); i++) {
290 _pwd2.concat( _pathvector[i].toPwSb() );
291 }
293 must_recalculate_pwd2 = false;
294 }
295 }
297 void
298 PathParam::start_listening(SPObject * to)
299 {
300 if ( to == NULL ) {
301 return;
302 }
303 linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
304 linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
305 linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
306 }
308 void
309 PathParam::quit_listening(void)
310 {
311 linked_modified_connection.disconnect();
312 linked_delete_connection.disconnect();
313 }
315 void
316 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
317 {
318 quit_listening();
319 if ( new_ref ) {
320 start_listening(new_ref);
321 }
322 }
324 void
325 PathParam::remove_link()
326 {
327 if (href) {
328 ref.detach();
329 g_free(href);
330 href = NULL;
331 }
332 }
334 void
335 PathParam::linked_delete(SPObject */*deleted*/)
336 {
337 quit_listening();
338 remove_link();
339 set_new_value (_pathvector, true);
340 }
342 void
343 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
344 {
345 SPCurve *curve = NULL;
346 if (SP_IS_SHAPE(linked_obj)) {
347 curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
348 }
349 if (SP_IS_TEXT(linked_obj)) {
350 curve = SP_TEXT(linked_obj)->getNormalizedBpath();
351 }
353 if (curve == NULL) {
354 // curve invalid, set default value
355 _pathvector = sp_svg_read_pathv(defvalue);
356 } else {
357 _pathvector = curve->get_pathvector();
358 curve->unref();
359 }
361 must_recalculate_pwd2 = true;
362 signal_path_changed.emit();
363 SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
364 }
366 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
367 void
368 PathParam::on_edit_button_click()
369 {
370 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
371 if (item != NULL) {
372 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
373 }
374 }
376 void
377 PathParam::paste_param_path(const char *svgd)
378 {
379 if (svgd == "")
380 return;
382 // remove possible link to path
383 remove_link();
385 param_write_to_repr(svgd);
386 signal_path_pasted.emit();
387 }
389 void
390 PathParam::on_paste_button_click()
391 {
392 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
393 Glib::ustring svgd = cm->getPathParameter();
394 paste_param_path(svgd.data());
395 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
396 _("Paste path parameter"));
397 }
399 void
400 PathParam::on_copy_button_click()
401 {
402 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
403 cm->copyPathParameter(this);
404 }
406 void
407 PathParam::on_link_button_click()
408 {
409 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
410 Glib::ustring pathid = cm->getShapeOrTextObjectId();
412 if (pathid == "") {
413 return;
414 }
416 // add '#' at start to make it an uri.
417 pathid.insert(pathid.begin(), '#');
418 if ( href && strcmp(pathid.c_str(), href) == 0 ) {
419 // no change, do nothing
420 return;
421 } else {
422 // TODO:
423 // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
424 // check if linking to object to which LPE is applied (maybe delegated to PathReference
426 param_write_to_repr(pathid.c_str());
427 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
428 _("Link path parameter to path"));
429 }
430 }
432 } /* namespace LivePathEffect */
434 } /* namespace Inkscape */
436 /*
437 Local Variables:
438 mode:c++
439 c-file-style:"stroustrup"
440 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
441 indent-tabs-mode:nil
442 fill-column:99
443 End:
444 */
445 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :