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"
35 // clipboard support
36 #include "ui/clipboard.h"
38 namespace Inkscape {
40 namespace LivePathEffect {
42 PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,
43 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
44 Effect* effect, const gchar * default_value)
45 : Parameter(label, tip, key, wr, effect),
46 _pathvector(),
47 _pwd2(),
48 must_recalculate_pwd2(false),
49 href(NULL)
50 {
51 defvalue = g_strdup(default_value);
52 param_readSVGValue(defvalue);
53 oncanvas_editable = true;
54 }
56 PathParam::~PathParam()
57 {
58 g_free(defvalue);
59 }
61 std::vector<Geom::Path> const &
62 PathParam::get_pathvector()
63 {
64 return _pathvector;
65 }
67 Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
68 PathParam::get_pwd2()
69 {
70 ensure_pwd2();
71 return _pwd2;
72 }
74 void
75 PathParam::param_set_default()
76 {
77 param_readSVGValue(defvalue);
78 }
80 void
81 PathParam::param_set_and_write_default()
82 {
83 param_write_to_repr(defvalue);
84 }
86 bool
87 PathParam::param_readSVGValue(const gchar * strvalue)
88 {
89 if (strvalue) {
90 _pathvector.clear();
91 if (href) {
92 g_free(href);
93 href = NULL;
94 }
95 must_recalculate_pwd2 = true;
97 if (false /*if strvalue is xlink*/) {
98 href = g_strdup(strvalue);
99 update_from_referred();
100 // TODO: add listener, because we must update when referred updates. we must always be up-to-date with referred path data
101 } else {
102 _pathvector = SVGD_to_2GeomPath(strvalue);
103 }
105 signal_path_changed.emit();
106 return true;
107 }
109 return false;
110 }
112 gchar *
113 PathParam::param_writeSVGValue() const
114 {
115 gchar * svgd = SVGD_from_2GeomPath( _pathvector );
116 return svgd;
117 }
119 Gtk::Widget *
120 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
121 {
122 Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
124 Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
125 static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
126 tooltips->set_tip(*pLabel, param_tooltip);
128 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
129 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
130 pButton->set_relief(Gtk::RELIEF_NONE);
131 pIcon->show();
132 pButton->add(*pIcon);
133 pButton->show();
134 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
135 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
136 tooltips->set_tip(*pButton, _("Edit on-canvas"));
138 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
139 pButton = Gtk::manage(new Gtk::Button());
140 pButton->set_relief(Gtk::RELIEF_NONE);
141 pIcon->show();
142 pButton->add(*pIcon);
143 pButton->show();
144 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
145 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
146 tooltips->set_tip(*pButton, _("Copy path"));
148 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
149 pButton = Gtk::manage(new Gtk::Button());
150 pButton->set_relief(Gtk::RELIEF_NONE);
151 pIcon->show();
152 pButton->add(*pIcon);
153 pButton->show();
154 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
155 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
156 tooltips->set_tip(*pButton, _("Paste path"));
158 static_cast<Gtk::HBox*>(_widget)->show_all_children();
160 return dynamic_cast<Gtk::Widget *> (_widget);
161 }
163 void
164 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
165 {
166 // If not already in nodecontext, goto it!
167 if (!tools_isactive(dt, TOOLS_NODES)) {
168 tools_switch_current(TOOLS_NODES);
169 }
171 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
172 if (!href) {
173 shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
174 } else {
175 // set referred item for editing
176 }
177 }
179 void
180 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
181 {
182 np->show_helperpath = true;
183 np->helperpath_rgba = 0x009000ff;
184 np->helperpath_width = 1.0;
185 }
187 void
188 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
189 {
190 // TODO: recode this to apply transform to _pathvector instead?
191 if (!href) {
192 // only apply transform when not referring to other path
193 ensure_pwd2();
194 param_set_and_write_new_value( _pwd2 * postmul );
195 }
196 }
198 void
199 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
200 {
201 _pathvector = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
202 gchar * svgd = SVGD_from_2GeomPath( _pathvector );
203 param_write_to_repr(svgd);
204 g_free(svgd);
205 // force value upon pwd2 and don't recalculate.
206 _pwd2 = newpath;
207 must_recalculate_pwd2 = false;
208 }
210 void
211 PathParam::ensure_pwd2()
212 {
213 if (must_recalculate_pwd2) {
214 _pwd2.clear();
215 for (unsigned int i=0; i < _pathvector.size(); i++) {
216 _pwd2.concat( _pathvector[i].toPwSb() );
217 }
219 must_recalculate_pwd2 = false;
220 }
221 }
223 void
224 PathParam::update_from_referred()
225 {
226 if (!href) {
227 g_warning("PathParam::update_from_referred - logical error, this should not possible");
228 return;
229 }
231 // TODO: implement!
233 // optimize, only update from referred when referred changed.
234 }
236 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
237 void
238 PathParam::on_edit_button_click()
239 {
240 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
241 if (item != NULL) {
242 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
243 }
244 }
246 void
247 PathParam::on_paste_button_click()
248 {
249 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
250 Glib::ustring svgd = cm->getPathParameter();
252 if (svgd == "") return;
254 // Temporary hack until 2Geom supports arcs in SVGD
255 if (svgd.find('A') != Glib::ustring::npos) {
256 SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
257 _("This effect does not support arcs yet, try to convert to path.") );
258 return;
259 } else {
260 param_write_to_repr(svgd.data());
261 signal_path_pasted.emit();
262 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
263 _("Paste path parameter"));
264 }
265 }
267 void
268 PathParam::on_copy_button_click()
269 {
270 Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
271 cm->copyPathParameter(this);
272 }
274 } /* namespace LivePathEffect */
276 } /* namespace Inkscape */
278 /*
279 Local Variables:
280 mode:c++
281 c-file-style:"stroustrup"
282 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
283 indent-tabs-mode:nil
284 fill-column:99
285 End:
286 */
287 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :