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"
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"
35 namespace Inkscape {
37 namespace LivePathEffect {
39 PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,
40 const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
41 Effect* effect, const gchar * default_value)
42 : Parameter(label, tip, key, wr, effect)
43 {
44 _widget = NULL;
45 _tooltips = NULL;
46 edit_button = NULL;
47 defvalue = g_strdup(default_value);
48 param_readSVGValue(defvalue);
49 oncanvas_editable = true;
50 }
52 PathParam::~PathParam()
53 {
54 if (_tooltips)
55 delete _tooltips;
56 // _widget is managed by GTK so do not delete!
58 g_free(defvalue);
59 }
61 void
62 PathParam::param_set_default()
63 {
64 param_readSVGValue(defvalue);
65 }
67 bool
68 PathParam::param_readSVGValue(const gchar * strvalue)
69 {
70 if (strvalue) {
71 Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;
72 std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);
73 for (unsigned int i=0; i < temppath.size(); i++) {
74 newpath.concat( temppath[i].toPwSb() );
75 }
76 *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;
77 signal_path_changed.emit();
78 return true;
79 }
81 return false;
82 }
84 gchar *
85 PathParam::param_writeSVGValue() const
86 {
87 const std::vector<Geom::Path> temppath =
88 Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);
89 gchar * svgd = SVGD_from_2GeomPath( temppath );
90 return svgd;
91 }
93 Gtk::Widget *
94 PathParam::param_getWidget()
95 {
96 if (!_widget) {
97 _widget = Gtk::manage(new Gtk::HBox());
98 _tooltips = new Gtk::Tooltips();
100 Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
101 static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
102 _tooltips->set_tip(*pLabel, param_tooltip);
104 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
105 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
106 pButton->set_relief(Gtk::RELIEF_NONE);
107 pIcon->show();
108 pButton->add(*pIcon);
109 pButton->show();
110 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
111 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
112 _tooltips->set_tip(*pButton, _("Edit on-canvas"));
113 edit_button = pButton;
115 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
116 pButton = Gtk::manage(new Gtk::Button());
117 pButton->set_relief(Gtk::RELIEF_NONE);
118 pIcon->show();
119 pButton->add(*pIcon);
120 pButton->show();
121 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
122 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
123 _tooltips->set_tip(*pButton, _("Paste path"));
125 static_cast<Gtk::HBox*>(_widget)->show_all_children();
127 }
128 return dynamic_cast<Gtk::Widget *> (_widget);
129 }
131 void
132 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
133 {
134 // If not already in nodecontext, goto it!
135 if (!tools_isactive(dt, TOOLS_NODES)) {
136 tools_switch_current(TOOLS_NODES);
137 }
139 ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
140 shape_editor->set_item_livepatheffect_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
141 }
143 void
144 PathParam::param_write_to_repr(const char * svgd)
145 {
146 param_effect->getRepr()->setAttribute(param_key.c_str(), svgd);
147 }
150 void
151 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath)
152 {
153 const std::vector<Geom::Path> temppath = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
154 gchar * svgd = SVGD_from_2GeomPath( temppath );
155 param_write_to_repr(svgd);
156 g_free(svgd);
157 }
159 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
160 void
161 PathParam::on_edit_button_click()
162 {
163 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
164 if (item != NULL) {
165 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
166 }
167 }
169 void
170 PathParam::on_paste_button_click()
171 {
172 // check if something is in the clipboard
173 GSList * clipboard = sp_selection_get_clipboard();
174 if (clipboard == NULL || clipboard->data == NULL) {
175 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
176 return;
177 }
179 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
180 if (!strcmp (repr->name(), "svg:path")) {
181 const char * svgd = repr->attribute("d");
182 if (svgd) {
183 if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD
184 SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
185 _("This effect does not support arcs yet, try to convert to path.") );
186 return;
187 } else {
188 param_write_to_repr(svgd);
189 signal_path_pasted.emit();
190 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
191 _("Paste path parameter"));
192 }
193 }
194 } else {
195 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));
196 return;
197 }
198 }
201 } /* namespace LivePathEffect */
203 } /* namespace Inkscape */
205 /*
206 Local Variables:
207 mode:c++
208 c-file-style:"stroustrup"
209 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
210 indent-tabs-mode:nil
211 fill-column:99
212 End:
213 */
214 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :