429cf82d1a78d672eb04d346208001d92b5f4eff
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/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 = SVGD_to_2GeomPath(defvalue);
117 }
118 } else {
119 _pathvector = SVGD_to_2GeomPath(strvalue);
120 }
122 signal_path_changed.emit();
123 return true;
124 }
126 return false;
127 }
129 gchar *
130 PathParam::param_writeSVGValue() const
131 {
132 if (href) {
133 return href;
134 } else {
135 gchar * svgd = SVGD_from_2GeomPath( _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());
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::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
221 {
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 }
229 }
231 void
232 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
233 {
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;
242 }
244 void
245 PathParam::param_set_and_write_new_value (std::vector<Geom::Path> const & newpath)
246 {
247 remove_link();
248 _pathvector = newpath;
249 must_recalculate_pwd2 = true;
251 gchar * svgd = SVGD_from_2GeomPath( _pathvector );
252 param_write_to_repr(svgd);
253 g_free(svgd);
254 }
256 void
257 PathParam::ensure_pwd2()
258 {
259 if (must_recalculate_pwd2) {
260 _pwd2.clear();
261 for (unsigned int i=0; i < _pathvector.size(); i++) {
262 _pwd2.concat( _pathvector[i].toPwSb() );
263 }
265 must_recalculate_pwd2 = false;
266 }
267 }
269 void
270 PathParam::start_listening(SPObject * to)
271 {
272 if ( to == NULL ) {
273 return;
274 }
275 linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
276 linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
277 linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
278 }
280 void
281 PathParam::quit_listening(void)
282 {
283 linked_modified_connection.disconnect();
284 linked_delete_connection.disconnect();
285 }
287 void
288 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
289 {
290 quit_listening();
291 if ( new_ref ) {
292 start_listening(new_ref);
293 }
294 }
296 void
297 PathParam::remove_link()
298 {
299 if (href) {
300 ref.detach();
301 g_free(href);
302 href = NULL;
303 }
304 }
306 void
307 PathParam::linked_delete(SPObject */*deleted*/)
308 {
309 quit_listening();
310 remove_link();
311 param_set_and_write_new_value (_pathvector);
312 }
314 void
315 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
316 {
317 SPCurve *curve = NULL;
318 if (SP_IS_SHAPE(linked_obj)) {
319 curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
320 }
321 if (SP_IS_TEXT(linked_obj)) {
322 curve = SP_TEXT(linked_obj)->getNormalizedBpath();
323 }
325 if (curve == NULL) {
326 // curve invalid, set default value
327 _pathvector = SVGD_to_2GeomPath(defvalue);
328 } else {
329 _pathvector = BPath_to_2GeomPath(SP_CURVE_BPATH(curve));
330 sp_curve_unref(curve);
331 }
333 must_recalculate_pwd2 = true;
334 signal_path_changed.emit();
335 SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
336 }
338 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
339 void
340 PathParam::on_edit_button_click()
341 {
342 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
343 if (item != NULL) {
344 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
345 }
346 }
348 void
349 PathParam::on_paste_button_click()
350 {
351 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
352 Glib::ustring svgd = cm->getPathParameter();
354 if (svgd == "")
355 return;
357 // remove possible link to path
358 remove_link();
360 param_write_to_repr(svgd.data());
361 signal_path_pasted.emit();
362 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
363 _("Paste path parameter"));
364 }
366 void
367 PathParam::on_copy_button_click()
368 {
369 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
370 cm->copyPathParameter(this);
371 }
373 void
374 PathParam::on_link_button_click()
375 {
376 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
377 Glib::ustring pathid = cm->getShapeOrTextObjectId();
379 if (pathid == "") {
380 return;
381 }
383 // add '#' at start to make it an uri.
384 pathid.insert(pathid.begin(), '#');
385 if ( href && strcmp(pathid.c_str(), href) == 0 ) {
386 // no change, do nothing
387 return;
388 } else {
389 // TODO:
390 // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
391 // check if linking to object to which LPE is applied (maybe delegated to PathReference
393 param_write_to_repr(pathid.c_str());
394 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
395 _("Link path parameter to path"));
396 }
397 }
399 } /* namespace LivePathEffect */
401 } /* namespace Inkscape */
403 /*
404 Local Variables:
405 mode:c++
406 c-file-style:"stroustrup"
407 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
408 indent-tabs-mode:nil
409 fill-column:99
410 End:
411 */
412 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :