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 "desktop-handles.h"
32 #include "selection.h"
33 // clipboard support
34 #include "ui/clipboard.h"
35 // required for linking to other paths
36 #include "uri.h"
37 #include "sp-shape.h"
38 #include "sp-text.h"
39 #include "display/curve.h"
41 #include "ui/tool/node-tool.h"
42 #include "ui/tool/multi-path-manipulator.h"
43 #include "ui/tool/shape-record.h"
46 namespace Inkscape {
48 namespace LivePathEffect {
50 PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,
51 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
52 Effect* effect, const gchar * default_value)
53 : Parameter(label, tip, key, wr, effect),
54 changed(true),
55 _pathvector(),
56 _pwd2(),
57 must_recalculate_pwd2(false),
58 href(NULL),
59 ref( (SPObject*)effect->getLPEObj() )
60 {
61 defvalue = g_strdup(default_value);
62 param_readSVGValue(defvalue);
63 oncanvas_editable = true;
65 ref_changed_connection = ref.changedSignal().connect(sigc::mem_fun(*this, &PathParam::ref_changed));
66 }
68 PathParam::~PathParam()
69 {
70 remove_link();
72 g_free(defvalue);
73 }
75 std::vector<Geom::Path> const &
76 PathParam::get_pathvector()
77 {
78 return _pathvector;
79 }
81 Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
82 PathParam::get_pwd2()
83 {
84 ensure_pwd2();
85 return _pwd2;
86 }
88 void
89 PathParam::param_set_default()
90 {
91 param_readSVGValue(defvalue);
92 }
94 void
95 PathParam::param_set_and_write_default()
96 {
97 param_write_to_repr(defvalue);
98 }
100 bool
101 PathParam::param_readSVGValue(const gchar * strvalue)
102 {
103 if (strvalue) {
104 _pathvector.clear();
105 remove_link();
106 must_recalculate_pwd2 = true;
108 if (strvalue[0] == '#') {
109 if (href)
110 g_free(href);
111 href = g_strdup(strvalue);
113 // Now do the attaching, which emits the changed signal.
114 try {
115 ref.attach(Inkscape::URI(href));
116 } catch (Inkscape::BadURIException &e) {
117 g_warning("%s", e.what());
118 ref.detach();
119 _pathvector = sp_svg_read_pathv(defvalue);
120 }
121 } else {
122 _pathvector = sp_svg_read_pathv(strvalue);
123 }
125 emit_changed();
126 return true;
127 }
129 return false;
130 }
132 gchar *
133 PathParam::param_getSVGValue() const
134 {
135 if (href) {
136 return href;
137 } else {
138 gchar * svgd = sp_svg_write_path( _pathvector );
139 return svgd;
140 }
141 }
143 Gtk::Widget *
144 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
145 {
146 Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
148 Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
149 static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
150 tooltips->set_tip(*pLabel, param_tooltip);
152 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "tool-node-editor", Inkscape::ICON_SIZE_BUTTON) );
153 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
154 pButton->set_relief(Gtk::RELIEF_NONE);
155 pIcon->show();
156 pButton->add(*pIcon);
157 pButton->show();
158 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
159 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
160 tooltips->set_tip(*pButton, _("Edit on-canvas"));
162 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
163 pButton = Gtk::manage(new Gtk::Button());
164 pButton->set_relief(Gtk::RELIEF_NONE);
165 pIcon->show();
166 pButton->add(*pIcon);
167 pButton->show();
168 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
169 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
170 tooltips->set_tip(*pButton, _("Copy path"));
172 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
173 pButton = Gtk::manage(new Gtk::Button());
174 pButton->set_relief(Gtk::RELIEF_NONE);
175 pIcon->show();
176 pButton->add(*pIcon);
177 pButton->show();
178 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
179 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
180 tooltips->set_tip(*pButton, _("Paste path"));
182 pIcon = Gtk::manage( sp_icon_get_icon( "edit-clone", Inkscape::ICON_SIZE_BUTTON) );
183 pButton = Gtk::manage(new Gtk::Button());
184 pButton->set_relief(Gtk::RELIEF_NONE);
185 pIcon->show();
186 pButton->add(*pIcon);
187 pButton->show();
188 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_link_button_click));
189 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
190 tooltips->set_tip(*pButton, _("Link to path"));
192 static_cast<Gtk::HBox*>(_widget)->show_all_children();
194 return dynamic_cast<Gtk::Widget *> (_widget);
195 }
197 void
198 PathParam::param_editOncanvas(SPItem * /*item*/, SPDesktop * dt)
199 {
200 using namespace Inkscape::UI;
202 // TODO remove the tools_switch atrocity.
203 if (!tools_isactive(dt, TOOLS_NODES)) {
204 tools_switch(dt, TOOLS_NODES);
205 }
207 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
208 std::set<ShapeRecord> shapes;
209 ShapeRecord r;
211 r.role = SHAPE_ROLE_LPE_PARAM;
212 r.edit_transform = Geom::identity(); // TODO this is almost certainly wrong
213 if (!href) {
214 r.item = reinterpret_cast<SPItem*>(param_effect->getLPEObj());
215 r.lpe_key = param_key;
216 } else {
217 r.item = ref.getObject();
218 }
219 shapes.insert(r);
220 nt->_multipath->setItems(shapes);
221 }
223 void
224 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *)
225 {
226 // TODO this method should not exist at all!
227 }
229 void
230 PathParam::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
231 {
232 hp_vec.push_back(_pathvector);
233 }
235 /*
236 * Only applies transform when not referring to other path!
237 */
238 void
239 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
240 {
241 // only apply transform when not referring to other path
242 if (!href) {
243 set_new_value( _pathvector * postmul, true );
244 }
245 }
247 /*
248 * See comments for set_new_value(std::vector<Geom::Path>).
249 */
250 void
251 PathParam::set_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath, bool write_to_svg)
252 {
253 remove_link();
254 _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
256 if (write_to_svg) {
257 gchar * svgd = sp_svg_write_path( _pathvector );
258 param_write_to_repr(svgd);
259 g_free(svgd);
261 // After the whole "writing to svg avalanche of function calling": force value upon pwd2 and don't recalculate.
262 _pwd2 = newpath;
263 must_recalculate_pwd2 = false;
264 } else {
265 _pwd2 = newpath;
266 must_recalculate_pwd2 = false;
267 emit_changed();
268 }
269 }
271 /*
272 * This method sets new path data.
273 * If this PathParam refers to another path, this link is removed (and replaced with explicit path data).
274 *
275 * If write_to_svg = true :
276 * The new path data is written to SVG. In this case the signal_path_changed signal
277 * is not directly emited in this method, because writing to SVG
278 * triggers the LPEObject to which this belongs to call Effect::setParameter which calls
279 * PathParam::readSVGValue, which finally emits the signal_path_changed signal.
280 * If write_to_svg = false :
281 * The new path data is not written to SVG. This method will emit the signal_path_changed signal.
282 */
283 void
284 PathParam::set_new_value (std::vector<Geom::Path> const &newpath, bool write_to_svg)
285 {
286 remove_link();
287 _pathvector = newpath;
288 must_recalculate_pwd2 = true;
290 if (write_to_svg) {
291 gchar * svgd = sp_svg_write_path( _pathvector );
292 param_write_to_repr(svgd);
293 g_free(svgd);
294 } else {
295 emit_changed();
296 }
297 }
299 void
300 PathParam::ensure_pwd2()
301 {
302 if (must_recalculate_pwd2) {
303 _pwd2.clear();
304 for (unsigned int i=0; i < _pathvector.size(); i++) {
305 _pwd2.concat( _pathvector[i].toPwSb() );
306 }
308 must_recalculate_pwd2 = false;
309 }
310 }
312 void
313 PathParam::emit_changed()
314 {
315 changed = true;
316 signal_path_changed.emit();
317 }
319 void
320 PathParam::start_listening(SPObject * to)
321 {
322 if ( to == NULL ) {
323 return;
324 }
325 linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
326 linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
327 linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
328 }
330 void
331 PathParam::quit_listening(void)
332 {
333 linked_modified_connection.disconnect();
334 linked_delete_connection.disconnect();
335 }
337 void
338 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
339 {
340 quit_listening();
341 if ( new_ref ) {
342 start_listening(new_ref);
343 }
344 }
346 void
347 PathParam::remove_link()
348 {
349 if (href) {
350 ref.detach();
351 g_free(href);
352 href = NULL;
353 }
354 }
356 void
357 PathParam::linked_delete(SPObject */*deleted*/)
358 {
359 quit_listening();
360 remove_link();
361 set_new_value (_pathvector, true);
362 }
364 void
365 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
366 {
367 SPCurve *curve = NULL;
368 if (SP_IS_SHAPE(linked_obj)) {
369 curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
370 }
371 if (SP_IS_TEXT(linked_obj)) {
372 curve = SP_TEXT(linked_obj)->getNormalizedBpath();
373 }
375 if (curve == NULL) {
376 // curve invalid, set default value
377 _pathvector = sp_svg_read_pathv(defvalue);
378 } else {
379 _pathvector = curve->get_pathvector();
380 curve->unref();
381 }
383 must_recalculate_pwd2 = true;
384 emit_changed();
385 SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
386 }
388 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
389 void
390 PathParam::on_edit_button_click()
391 {
392 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
393 if (item != NULL) {
394 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
395 }
396 }
398 void
399 PathParam::paste_param_path(const char *svgd)
400 {
401 // only recognize a non-null, non-empty string
402 if (svgd && *svgd) {
403 // remove possible link to path
404 remove_link();
406 param_write_to_repr(svgd);
407 signal_path_pasted.emit();
408 }
409 }
411 void
412 PathParam::on_paste_button_click()
413 {
414 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
415 Glib::ustring svgd = cm->getPathParameter();
416 paste_param_path(svgd.data());
417 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
418 _("Paste path parameter"));
419 }
421 void
422 PathParam::on_copy_button_click()
423 {
424 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
425 cm->copyPathParameter(this);
426 }
428 void
429 PathParam::on_link_button_click()
430 {
431 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
432 Glib::ustring pathid = cm->getShapeOrTextObjectId();
434 if (pathid == "") {
435 return;
436 }
438 // add '#' at start to make it an uri.
439 pathid.insert(pathid.begin(), '#');
440 if ( href && strcmp(pathid.c_str(), href) == 0 ) {
441 // no change, do nothing
442 return;
443 } else {
444 // TODO:
445 // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
446 // check if linking to object to which LPE is applied (maybe delegated to PathReference
448 param_write_to_repr(pathid.c_str());
449 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
450 _("Link path parameter to path"));
451 }
452 }
454 } /* namespace LivePathEffect */
456 } /* namespace Inkscape */
458 /*
459 Local Variables:
460 mode:c++
461 c-file-style:"stroustrup"
462 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
463 indent-tabs-mode:nil
464 fill-column:99
465 End:
466 */
467 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :