Code

prepare PathParam for linking to other object
[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 "live_effects/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"
36 namespace Inkscape {
38 namespace LivePathEffect {
40 PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,
41                       const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
42                       Effect* effect, const gchar * default_value)
43     : Parameter(label, tip, key, wr, effect),
44       _pathvector(),
45       must_recalculate_pwd2(false),
46       _pwd2(),
47       href(NULL)
48 {
49     defvalue = g_strdup(default_value);
50     param_readSVGValue(defvalue);
51     oncanvas_editable = true;
52 }
54 PathParam::~PathParam()
55 {
56     g_free(defvalue);
57 }
59 std::vector<Geom::Path> const &
60 PathParam::get_pathvector()
61 {
62     return _pathvector;
63 }
65 Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
66 PathParam::get_pwd2()
67 {
68     ensure_pwd2();
69     return _pwd2;
70 }
72 void
73 PathParam::param_set_default()
74 {
75     param_readSVGValue(defvalue);
76 }
78 void
79 PathParam::param_set_and_write_default()
80 {
81     param_write_to_repr(defvalue);
82 }
84 bool
85 PathParam::param_readSVGValue(const gchar * strvalue)
86 {
87     if (strvalue) {
88         _pathvector.clear();
89         if (href) {
90             g_free(href);
91             href = NULL;
92         }
93         must_recalculate_pwd2 = true;
95         if (false /*if strvalue is xlink*/) {
96             href = g_strdup(strvalue);
97             update_from_referred();
98             // TODO: add listener, because we must update when referred updates. we must always be up-to-date with referred path data
99         } else {
100             _pathvector = SVGD_to_2GeomPath(strvalue);
101         }
103         signal_path_changed.emit();
104         return true;
105     }
107     return false;
110 gchar *
111 PathParam::param_writeSVGValue() const
113     gchar * svgd = SVGD_from_2GeomPath( _pathvector );
114     return svgd;
117 Gtk::Widget *
118 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
120     Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
122     Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
123     static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
124     tooltips->set_tip(*pLabel, param_tooltip);
126     Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
127     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
128     pButton->set_relief(Gtk::RELIEF_NONE);
129     pIcon->show();
130     pButton->add(*pIcon);
131     pButton->show();
132     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
133     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
134     tooltips->set_tip(*pButton, _("Edit on-canvas"));
136     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
137     pButton = Gtk::manage(new Gtk::Button());
138     pButton->set_relief(Gtk::RELIEF_NONE);
139     pIcon->show();
140     pButton->add(*pIcon);
141     pButton->show();
142     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
143     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
144     tooltips->set_tip(*pButton, _("Copy path"));
146     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
147     pButton = Gtk::manage(new Gtk::Button());
148     pButton->set_relief(Gtk::RELIEF_NONE);
149     pIcon->show();
150     pButton->add(*pIcon);
151     pButton->show();
152     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
153     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
154     tooltips->set_tip(*pButton, _("Paste path"));
156     static_cast<Gtk::HBox*>(_widget)->show_all_children();
158     return dynamic_cast<Gtk::Widget *> (_widget);
161 void
162 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
164     // If not already in nodecontext, goto it!
165     if (!tools_isactive(dt, TOOLS_NODES)) {
166         tools_switch_current(TOOLS_NODES);
167     }
169     ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
170     if (!href) {
171         shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
172     } else {
173         // set referred item for editing
174     }
177 void
178 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
180     np->show_helperpath = true;
181     np->helperpath_rgba = 0x009000ff;
182     np->helperpath_width = 1.0;
185 void
186 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
188     // TODO: recode this to apply transform to _pathvector instead?
189     if (!href) {
190         // only apply transform when not referring to other path
191         ensure_pwd2();
192         param_set_and_write_new_value( _pwd2 * postmul );
193     }
196 void
197 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
199     _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
200     gchar * svgd = SVGD_from_2GeomPath( _pathvector );
201     param_write_to_repr(svgd);
202     g_free(svgd);
203     // force value upon pwd2 and don't recalculate.
204     _pwd2 = newpath;
205     must_recalculate_pwd2 = false;
208 void
209 PathParam::ensure_pwd2()
211     if (must_recalculate_pwd2) {
212         _pwd2.clear();
213         for (unsigned int i=0; i < _pathvector.size(); i++) {
214             _pwd2.concat( _pathvector[i].toPwSb() );
215         }
217         must_recalculate_pwd2 = false;
218     }
221 void
222 PathParam::update_from_referred()
224     if (!href) {
225         g_warning("PathParam::update_from_referred - logical error, this should not possible");
226         return;
227     }
229     // TODO: implement!
230     
231     // optimize, only update from referred when referred changed.
234 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
235 void
236 PathParam::on_edit_button_click()
238     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
239     if (item != NULL) {
240         param_editOncanvas(item, SP_ACTIVE_DESKTOP);
241     }
244 void
245 PathParam::on_paste_button_click()
247     // check if something is in the clipboard
248     GSList * clipboard = sp_selection_get_clipboard();
249     if (clipboard == NULL || clipboard->data == NULL) {
250         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
251         return;
252     }
254     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
255     if (!strcmp (repr->name(), "svg:path")) {
256         const char * svgd = repr->attribute("d");
257         if (svgd) {
258             if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD
259                 SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
260                             _("This effect does not support arcs yet, try to convert to path.") );
261                 return;
262             } else {
263                 param_write_to_repr(svgd);
264                 signal_path_pasted.emit();
265                 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
266                                  _("Paste path parameter"));
267             }
268         }
269     } else {
270         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));
271         return;
272     }
275 void
276 PathParam::on_copy_button_click()
278     sp_selection_copy_lpe_pathparam(this);
281 } /* namespace LivePathEffect */
283 } /* namespace Inkscape */
285 /*
286   Local Variables:
287   mode:c++
288   c-file-style:"stroustrup"
289   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
290   indent-tabs-mode:nil
291   fill-column:99
292   End:
293 */
294 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :