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 _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;
127 }
129 gchar *
130 PathParam::param_getSVGValue() const
131 {
132 if (href) {
133 return href;
134 } else {
135 gchar * svgd = sp_svg_write_path( _pathvector );
136 return svgd;
137 }
138 }
140 Gtk::Widget *
141 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
142 {
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);
192 }
194 void
195 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
196 {
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 }
209 }
211 void
212 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
213 {
214 np->show_helperpath = true;
215 np->helperpath_rgba = 0x009000ff;
216 np->helperpath_width = 1.0;
217 }
219 void
220 PathParam::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
221 {
222 hp_vec.push_back(_pathvector);
223 }
225 /*
226 * Only applies transform when not referring to other path!
227 */
228 void
229 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
230 {
231 // only apply transform when not referring to other path
232 if (!href) {
233 set_new_value( _pathvector * postmul, true );
234 }
235 }
237 /*
238 * See comments for set_new_value(std::vector<Geom::Path>).
239 */
240 void
241 PathParam::set_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath, bool write_to_svg)
242 {
243 remove_link();
244 _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
246 if (write_to_svg) {
247 gchar * svgd = sp_svg_write_path( _pathvector );
248 param_write_to_repr(svgd);
249 g_free(svgd);
251 // After the whole "writing to svg avalanche of function calling": force value upon pwd2 and don't recalculate.
252 _pwd2 = newpath;
253 must_recalculate_pwd2 = false;
254 } else {
255 _pwd2 = newpath;
256 must_recalculate_pwd2 = false;
257 signal_path_changed.emit();
258 }
259 }
261 /*
262 * This method sets new path data.
263 * If this PathParam refers to another path, this link is removed (and replaced with explicit path data).
264 *
265 * If write_to_svg = true :
266 * The new path data is written to SVG. In this case the signal_path_changed signal
267 * is not directly emited in this method, because writing to SVG
268 * triggers the LPEObject to which this belongs to call Effect::setParameter which calls
269 * PathParam::readSVGValue, which finally emits the signal_path_changed signal.
270 * If write_to_svg = false :
271 * The new path data is not written to SVG. This method will emit the signal_path_changed signal.
272 */
273 void
274 PathParam::set_new_value (std::vector<Geom::Path> const &newpath, bool write_to_svg)
275 {
276 remove_link();
277 _pathvector = newpath;
278 must_recalculate_pwd2 = true;
280 if (write_to_svg) {
281 gchar * svgd = sp_svg_write_path( _pathvector );
282 param_write_to_repr(svgd);
283 g_free(svgd);
284 } else {
285 signal_path_changed.emit();
286 }
287 }
289 void
290 PathParam::ensure_pwd2()
291 {
292 if (must_recalculate_pwd2) {
293 _pwd2.clear();
294 for (unsigned int i=0; i < _pathvector.size(); i++) {
295 _pwd2.concat( _pathvector[i].toPwSb() );
296 }
298 must_recalculate_pwd2 = false;
299 }
300 }
302 void
303 PathParam::start_listening(SPObject * to)
304 {
305 if ( to == NULL ) {
306 return;
307 }
308 linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
309 linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
310 linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
311 }
313 void
314 PathParam::quit_listening(void)
315 {
316 linked_modified_connection.disconnect();
317 linked_delete_connection.disconnect();
318 }
320 void
321 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
322 {
323 quit_listening();
324 if ( new_ref ) {
325 start_listening(new_ref);
326 }
327 }
329 void
330 PathParam::remove_link()
331 {
332 if (href) {
333 ref.detach();
334 g_free(href);
335 href = NULL;
336 }
337 }
339 void
340 PathParam::linked_delete(SPObject */*deleted*/)
341 {
342 quit_listening();
343 remove_link();
344 set_new_value (_pathvector, true);
345 }
347 void
348 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
349 {
350 SPCurve *curve = NULL;
351 if (SP_IS_SHAPE(linked_obj)) {
352 curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
353 }
354 if (SP_IS_TEXT(linked_obj)) {
355 curve = SP_TEXT(linked_obj)->getNormalizedBpath();
356 }
358 if (curve == NULL) {
359 // curve invalid, set default value
360 _pathvector = sp_svg_read_pathv(defvalue);
361 } else {
362 _pathvector = curve->get_pathvector();
363 curve->unref();
364 }
366 must_recalculate_pwd2 = true;
367 signal_path_changed.emit();
368 SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
369 }
371 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
372 void
373 PathParam::on_edit_button_click()
374 {
375 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
376 if (item != NULL) {
377 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
378 }
379 }
381 void
382 PathParam::paste_param_path(const char *svgd)
383 {
384 // only recognize a non-null, non-empty string
385 if (svgd && *svgd) {
386 // remove possible link to path
387 remove_link();
389 param_write_to_repr(svgd);
390 signal_path_pasted.emit();
391 }
392 }
394 void
395 PathParam::on_paste_button_click()
396 {
397 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
398 Glib::ustring svgd = cm->getPathParameter();
399 paste_param_path(svgd.data());
400 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
401 _("Paste path parameter"));
402 }
404 void
405 PathParam::on_copy_button_click()
406 {
407 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
408 cm->copyPathParameter(this);
409 }
411 void
412 PathParam::on_link_button_click()
413 {
414 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
415 Glib::ustring pathid = cm->getShapeOrTextObjectId();
417 if (pathid == "") {
418 return;
419 }
421 // add '#' at start to make it an uri.
422 pathid.insert(pathid.begin(), '#');
423 if ( href && strcmp(pathid.c_str(), href) == 0 ) {
424 // no change, do nothing
425 return;
426 } else {
427 // TODO:
428 // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
429 // check if linking to object to which LPE is applied (maybe delegated to PathReference
431 param_write_to_repr(pathid.c_str());
432 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
433 _("Link path parameter to path"));
434 }
435 }
437 } /* namespace LivePathEffect */
439 } /* namespace Inkscape */
441 /*
442 Local Variables:
443 mode:c++
444 c-file-style:"stroustrup"
445 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
446 indent-tabs-mode:nil
447 fill-column:99
448 End:
449 */
450 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :