Code

move live_effects/n-art-bpath-2geom.* to libnr/n-art-bpath-2geom.*
[inkscape.git] / src / live_effects / parameter / path.cpp
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));
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)
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 = SVGD_to_2GeomPath(defvalue);
118             }
119         } else {
120             _pathvector = SVGD_to_2GeomPath(strvalue);
121         }
123         signal_path_changed.emit();
124         return true;
125     }
127     return false;
130 gchar *
131 PathParam::param_writeSVGValue() const
133     if (href) {
134         return href;
135     } else {
136         gchar * svgd = SVGD_from_2GeomPath( _pathvector );
137         return svgd;
138     }
141 Gtk::Widget *
142 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
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);
195 void
196 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
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());
209     }
212 void
213 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
215     np->show_helperpath = true;
216     np->helperpath_rgba = 0x009000ff;
217     np->helperpath_width = 1.0;
220 void
221 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
223     if (!href) {
224         // TODO: recode this to apply transform to _pathvector instead?
226         // only apply transform when not referring to other path
227         ensure_pwd2();
228         param_set_and_write_new_value( _pwd2 * postmul );
229     }
232 void
233 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
235     remove_link();
236     _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
237     gchar * svgd = SVGD_from_2GeomPath( _pathvector );
238     param_write_to_repr(svgd);
239     g_free(svgd);
240     // force value upon pwd2 and don't recalculate.
241     _pwd2 = newpath;
242     must_recalculate_pwd2 = false;
245 void
246 PathParam::param_set_and_write_new_value (std::vector<Geom::Path> const & newpath)
248     remove_link();
249     _pathvector = newpath;
250     must_recalculate_pwd2 = true;
252     gchar * svgd = SVGD_from_2GeomPath( _pathvector );
253     param_write_to_repr(svgd);
254     g_free(svgd);
257 void
258 PathParam::ensure_pwd2()
260     if (must_recalculate_pwd2) {
261         _pwd2.clear();
262         for (unsigned int i=0; i < _pathvector.size(); i++) {
263             _pwd2.concat( _pathvector[i].toPwSb() );
264         }
266         must_recalculate_pwd2 = false;
267     }
270 void
271 PathParam::start_listening(SPObject * to)
273     if ( to == NULL ) {
274         return;
275     }
276     linked_delete_connection = to->connectDelete(sigc::mem_fun(*this, &PathParam::linked_delete));
277     linked_modified_connection = to->connectModified(sigc::mem_fun(*this, &PathParam::linked_modified));
278     linked_modified(to, SP_OBJECT_MODIFIED_FLAG); // simulate linked_modified signal, so that path data is updated
281 void
282 PathParam::quit_listening(void)
284     linked_modified_connection.disconnect();
285     linked_delete_connection.disconnect();
288 void
289 PathParam::ref_changed(SPObject */*old_ref*/, SPObject *new_ref)
291     quit_listening();
292     if ( new_ref ) {
293         start_listening(new_ref);
294     }
297 void
298 PathParam::remove_link()
300     if (href) {
301         ref.detach();
302         g_free(href);
303         href = NULL;
304     }
307 void
308 PathParam::linked_delete(SPObject */*deleted*/)
310     quit_listening();
311     remove_link();
312     param_set_and_write_new_value (_pathvector);
315 void
316 PathParam::linked_modified(SPObject *linked_obj, guint /*flags*/)
318     SPCurve *curve = NULL;
319     if (SP_IS_SHAPE(linked_obj)) {
320         curve = sp_shape_get_curve(SP_SHAPE(linked_obj));
321     }
322     if (SP_IS_TEXT(linked_obj)) {
323         curve = SP_TEXT(linked_obj)->getNormalizedBpath();
324     }
326     if (curve == NULL) {
327         // curve invalid, set default value
328         _pathvector = SVGD_to_2GeomPath(defvalue);
329     } else {
330         _pathvector = BPath_to_2GeomPath(SP_CURVE_BPATH(curve));
331         sp_curve_unref(curve);
332     }
334     must_recalculate_pwd2 = true;
335     signal_path_changed.emit();
336     SP_OBJECT(param_effect->getLPEObj())->requestModified(SP_OBJECT_MODIFIED_FLAG);
339 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
340 void
341 PathParam::on_edit_button_click()
343     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
344     if (item != NULL) {
345         param_editOncanvas(item, SP_ACTIVE_DESKTOP);
346     }
349 void
350 PathParam::on_paste_button_click()
352     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
353     Glib::ustring svgd = cm->getPathParameter();
354     
355     if (svgd == "")
356         return;
358     // Temporary hack until 2Geom supports arcs in SVGD
359     if (svgd.find('A') != Glib::ustring::npos) {
360         SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
361                     _("This effect does not support arcs yet, try to convert to path.") );
362         return;
363     } else {
364         // remove possible link to path
365         remove_link();
367         param_write_to_repr(svgd.data());
368         signal_path_pasted.emit();
369         sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
370                          _("Paste path parameter"));
371     }
374 void
375 PathParam::on_copy_button_click()
377     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
378     cm->copyPathParameter(this);
381 void
382 PathParam::on_link_button_click()
384     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
385     Glib::ustring pathid = cm->getShapeOrTextObjectId();
387     if (pathid == "") {
388         return;
389     }
391     // add '#' at start to make it an uri.
392     pathid.insert(pathid.begin(), '#');
393     if ( href && strcmp(pathid.c_str(), href) == 0 ) {
394         // no change, do nothing
395         return;
396     } else {
397         // TODO:
398         // check if id really exists in document, or only in clipboard document: if only in clipboard then invalid
399         // check if linking to object to which LPE is applied (maybe delegated to PathReference
401         param_write_to_repr(pathid.c_str());
402         sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
403                          _("Link path parameter to path"));
404     }
407 } /* namespace LivePathEffect */
409 } /* namespace Inkscape */
411 /*
412   Local Variables:
413   mode:c++
414   c-file-style:"stroustrup"
415   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
416   indent-tabs-mode:nil
417   fill-column:99
418   End:
419 */
420 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :