1 /*
2 * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
3 * Abhishek Sharma
4 *
5 * Released under GNU GPL, read the file 'COPYING' for more information
6 */
8 #include "live_effects/parameter/path.h"
9 #include "live_effects/effect.h"
10 #include "svg/svg.h"
11 #include <2geom/svg-path-parser.h>
12 #include <2geom/sbasis-to-bezier.h>
13 #include <2geom/pathvector.h>
14 #include <2geom/d2.h>
16 #include "ui/widget/point.h"
17 #include "widgets/icon.h"
18 #include <gtk/gtkstock.h>
19 #include "selection-chemistry.h"
20 #include "xml/repr.h"
21 #include "desktop.h"
22 #include "inkscape.h"
23 #include "message-stack.h"
24 #include "verbs.h"
25 #include "document.h"
27 // needed for on-canvas editting:
28 #include "tools-switch.h"
29 #include "shape-editor.h"
30 #include "desktop-handles.h"
31 #include "selection.h"
32 // clipboard support
33 #include "ui/clipboard.h"
34 // required for linking to other paths
35 #include "uri.h"
36 #include "sp-shape.h"
37 #include "sp-text.h"
38 #include "display/curve.h"
40 #include "ui/tool/node-tool.h"
41 #include "ui/tool/multi-path-manipulator.h"
42 #include "ui/tool/shape-record.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 changed(true),
54 _pathvector(),
55 _pwd2(),
56 must_recalculate_pwd2(false),
57 href(NULL),
58 ref( (SPObject*)effect->getLPEObj() )
59 {
60 defvalue = g_strdup(default_value);
61 param_readSVGValue(defvalue);
62 oncanvas_editable = true;
64 ref_changed_connection = ref.changedSignal().connect(sigc::mem_fun(*this, &PathParam::ref_changed));
65 }
67 PathParam::~PathParam()
68 {
69 remove_link();
71 g_free(defvalue);
72 }
74 std::vector<Geom::Path> const &
75 PathParam::get_pathvector()
76 {
77 return _pathvector;
78 }
80 Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
81 PathParam::get_pwd2()
82 {
83 ensure_pwd2();
84 return _pwd2;
85 }
87 void
88 PathParam::param_set_default()
89 {
90 param_readSVGValue(defvalue);
91 }
93 void
94 PathParam::param_set_and_write_default()
95 {
96 param_write_to_repr(defvalue);
97 }
99 bool
100 PathParam::param_readSVGValue(const gchar * strvalue)
101 {
102 if (strvalue) {
103 _pathvector.clear();
104 remove_link();
105 must_recalculate_pwd2 = true;
107 if (strvalue[0] == '#') {
108 if (href)
109 g_free(href);
110 href = g_strdup(strvalue);
112 // Now do the attaching, which emits the changed signal.
113 try {
114 ref.attach(Inkscape::URI(href));
115 } catch (Inkscape::BadURIException &e) {
116 g_warning("%s", e.what());
117 ref.detach();
118 _pathvector = sp_svg_read_pathv(defvalue);
119 }
120 } else {
121 _pathvector = sp_svg_read_pathv(strvalue);
122 }
124 emit_changed();
125 return true;
126 }
128 return false;
129 }
131 gchar *
132 PathParam::param_getSVGValue() const
133 {
134 if (href) {
135 return href;
136 } else {
137 gchar * svgd = sp_svg_write_path( _pathvector );
138 return svgd;
139 }
140 }
142 Gtk::Widget *
143 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
144 {
145 Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
147 Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
148 static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
149 tooltips->set_tip(*pLabel, param_tooltip);
151 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "tool-node-editor", Inkscape::ICON_SIZE_BUTTON) );
152 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
153 pButton->set_relief(Gtk::RELIEF_NONE);
154 pIcon->show();
155 pButton->add(*pIcon);
156 pButton->show();
157 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
158 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
159 tooltips->set_tip(*pButton, _("Edit on-canvas"));
161 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
162 pButton = Gtk::manage(new Gtk::Button());
163 pButton->set_relief(Gtk::RELIEF_NONE);
164 pIcon->show();
165 pButton->add(*pIcon);
166 pButton->show();
167 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
168 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
169 tooltips->set_tip(*pButton, _("Copy path"));
171 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
172 pButton = Gtk::manage(new Gtk::Button());
173 pButton->set_relief(Gtk::RELIEF_NONE);
174 pIcon->show();
175 pButton->add(*pIcon);
176 pButton->show();
177 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
178 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
179 tooltips->set_tip(*pButton, _("Paste path"));
181 pIcon = Gtk::manage( sp_icon_get_icon( "edit-clone", Inkscape::ICON_SIZE_BUTTON) );
182 pButton = Gtk::manage(new Gtk::Button());
183 pButton->set_relief(Gtk::RELIEF_NONE);
184 pIcon->show();
185 pButton->add(*pIcon);
186 pButton->show();
187 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_link_button_click));
188 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
189 tooltips->set_tip(*pButton, _("Link to path"));
191 static_cast<Gtk::HBox*>(_widget)->show_all_children();
193 return dynamic_cast<Gtk::Widget *> (_widget);
194 }
196 void
197 PathParam::param_editOncanvas(SPItem *item, SPDesktop * dt)
198 {
199 using namespace Inkscape::UI;
201 // TODO remove the tools_switch atrocity.
202 if (!tools_isactive(dt, TOOLS_NODES)) {
203 tools_switch(dt, TOOLS_NODES);
204 }
206 InkNodeTool *nt = static_cast<InkNodeTool*>(dt->event_context);
207 std::set<ShapeRecord> shapes;
208 ShapeRecord r;
210 r.role = SHAPE_ROLE_LPE_PARAM;
211 r.edit_transform = item->i2d_affine(); // TODO is it right?
212 if (!href) {
213 r.item = reinterpret_cast<SPItem*>(param_effect->getLPEObj());
214 r.lpe_key = param_key;
215 } else {
216 r.item = ref.getObject();
217 }
218 shapes.insert(r);
219 nt->_multipath->setItems(shapes);
220 }
222 void
223 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *)
224 {
225 // TODO this method should not exist at all!
226 }
228 void
229 PathParam::addCanvasIndicators(SPLPEItem */*lpeitem*/, std::vector<Geom::PathVector> &hp_vec)
230 {
231 hp_vec.push_back(_pathvector);
232 }
234 /*
235 * Only applies transform when not referring to other path!
236 */
237 void
238 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
239 {
240 // only apply transform when not referring to other path
241 if (!href) {
242 set_new_value( _pathvector * postmul, true );
243 }
244 }
246 /*
247 * See comments for set_new_value(std::vector<Geom::Path>).
248 */
249 void
250 PathParam::set_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath, bool write_to_svg)
251 {
252 remove_link();
253 _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
255 if (write_to_svg) {
256 gchar * svgd = sp_svg_write_path( _pathvector );
257 param_write_to_repr(svgd);
258 g_free(svgd);
260 // After the whole "writing to svg avalanche of function calling": force value upon pwd2 and don't recalculate.
261 _pwd2 = newpath;
262 must_recalculate_pwd2 = false;
263 } else {
264 _pwd2 = newpath;
265 must_recalculate_pwd2 = false;
266 emit_changed();
267 }
268 }
270 /*
271 * This method sets new path data.
272 * If this PathParam refers to another path, this link is removed (and replaced with explicit path data).
273 *
274 * If write_to_svg = true :
275 * The new path data is written to SVG. In this case the signal_path_changed signal
276 * is not directly emited in this method, because writing to SVG
277 * triggers the LPEObject to which this belongs to call Effect::setParameter which calls
278 * PathParam::readSVGValue, which finally emits the signal_path_changed signal.
279 * If write_to_svg = false :
280 * The new path data is not written to SVG. This method will emit the signal_path_changed signal.
281 */
282 void
283 PathParam::set_new_value (std::vector<Geom::Path> const &newpath, bool write_to_svg)
284 {
285 remove_link();
286 _pathvector = newpath;
287 must_recalculate_pwd2 = true;
289 if (write_to_svg) {
290 gchar * svgd = sp_svg_write_path( _pathvector );
291 param_write_to_repr(svgd);
292 g_free(svgd);
293 } else {
294 emit_changed();
295 }
296 }
298 void
299 PathParam::ensure_pwd2()
300 {
301 if (must_recalculate_pwd2) {
302 _pwd2.clear();
303 for (unsigned int i=0; i < _pathvector.size(); i++) {
304 _pwd2.concat( _pathvector[i].toPwSb() );
305 }
307 must_recalculate_pwd2 = false;
308 }
309 }
311 void
312 PathParam::emit_changed()
313 {
314 changed = true;
315 signal_path_changed.emit();
316 }
318 void
319 PathParam::start_listening(SPObject * to)
320 {
321 if ( to == NULL ) {
322 return;
323 }
324 linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
325 linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
326 linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
327 }
329 void
330 PathParam::quit_listening(void)
331 {
332 linked_modified_connection.disconnect();
333 linked_delete_connection.disconnect();
334 }
336 void
337 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
338 {
339 quit_listening();
340 if ( new_ref ) {
341 start_listening(new_ref);
342 }
343 }
345 void
346 PathParam::remove_link()
347 {
348 if (href) {
349 ref.detach();
350 g_free(href);
351 href = NULL;
352 }
353 }
355 void
356 PathParam::linked_delete(SPObject */*deleted*/)
357 {
358 quit_listening();
359 remove_link();
360 set_new_value (_pathvector, true);
361 }
363 void
364 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
365 {
366 SPCurve *curve = NULL;
367 if (SP_IS_SHAPE(linked_obj)) {
368 curve = SP_SHAPE(linked_obj)->getCurve();
369 }
370 if (SP_IS_TEXT(linked_obj)) {
371 curve = SP_TEXT(linked_obj)->getNormalizedBpath();
372 }
374 if (curve == NULL) {
375 // curve invalid, set default value
376 _pathvector = sp_svg_read_pathv(defvalue);
377 } else {
378 _pathvector = curve->get_pathvector();
379 curve->unref();
380 }
382 must_recalculate_pwd2 = true;
383 emit_changed();
384 SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
385 }
387 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
388 void
389 PathParam::on_edit_button_click()
390 {
391 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
392 if (item != NULL) {
393 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
394 }
395 }
397 void
398 PathParam::paste_param_path(const char *svgd)
399 {
400 // only recognize a non-null, non-empty string
401 if (svgd && *svgd) {
402 // remove possible link to path
403 remove_link();
405 param_write_to_repr(svgd);
406 signal_path_pasted.emit();
407 }
408 }
410 void
411 PathParam::on_paste_button_click()
412 {
413 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
414 Glib::ustring svgd = cm->getPathParameter(SP_ACTIVE_DESKTOP);
415 paste_param_path(svgd.data());
416 DocumentUndo::done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
417 _("Paste path parameter"));
418 }
420 void
421 PathParam::on_copy_button_click()
422 {
423 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
424 cm->copyPathParameter(this);
425 }
427 void
428 PathParam::on_link_button_click()
429 {
430 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
431 Glib::ustring pathid = cm->getShapeOrTextObjectId(SP_ACTIVE_DESKTOP);
433 if (pathid == "") {
434 return;
435 }
437 // add '#' at start to make it an uri.
438 pathid.insert(pathid.begin(), '#');
439 if ( href && strcmp(pathid.c_str(), href) == 0 ) {
440 // no change, do nothing
441 return;
442 } else {
443 // TODO:
444 // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
445 // check if linking to object to which LPE is applied (maybe delegated to PathReference
447 param_write_to_repr(pathid.c_str());
448 DocumentUndo::done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
449 _("Link path parameter to path"));
450 }
451 }
453 } /* namespace LivePathEffect */
455 } /* namespace Inkscape */
457 /*
458 Local Variables:
459 mode:c++
460 c-file-style:"stroustrup"
461 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
462 indent-tabs-mode:nil
463 fill-column:99
464 End:
465 */
466 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :