7d7679496ba489b39174095ca3656cfa535ad7af
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"
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 {
45 defvalue = g_strdup(default_value);
46 param_readSVGValue(defvalue);
47 oncanvas_editable = true;
48 }
50 PathParam::~PathParam()
51 {
52 g_free(defvalue);
53 }
55 void
56 PathParam::param_set_default()
57 {
58 param_readSVGValue(defvalue);
59 }
61 bool
62 PathParam::param_readSVGValue(const gchar * strvalue)
63 {
64 if (strvalue) {
65 Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;
66 std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);
67 for (unsigned int i=0; i < temppath.size(); i++) {
68 newpath.concat( temppath[i].toPwSb() );
69 }
70 *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;
71 signal_path_changed.emit();
72 return true;
73 }
75 return false;
76 }
78 gchar *
79 PathParam::param_writeSVGValue() const
80 {
81 const std::vector<Geom::Path> temppath =
82 Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);
83 gchar * svgd = SVGD_from_2GeomPath( temppath );
84 return svgd;
85 }
87 Gtk::Widget *
88 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
89 {
90 Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
92 Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
93 static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
94 tooltips->set_tip(*pLabel, param_tooltip);
96 Gtk::Widget* pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
97 Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
98 pButton->set_relief(Gtk::RELIEF_NONE);
99 pIcon->show();
100 pButton->add(*pIcon);
101 pButton->show();
102 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
103 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
104 tooltips->set_tip(*pButton, _("Edit on-canvas"));
106 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
107 pButton = Gtk::manage(new Gtk::Button());
108 pButton->set_relief(Gtk::RELIEF_NONE);
109 pIcon->show();
110 pButton->add(*pIcon);
111 pButton->show();
112 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
113 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
114 tooltips->set_tip(*pButton, _("Copy path"));
116 pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
117 pButton = Gtk::manage(new Gtk::Button());
118 pButton->set_relief(Gtk::RELIEF_NONE);
119 pIcon->show();
120 pButton->add(*pIcon);
121 pButton->show();
122 pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
123 static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
124 tooltips->set_tip(*pButton, _("Paste path"));
126 static_cast<Gtk::HBox*>(_widget)->show_all_children();
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_setup_nodepath(Inkscape::NodePath::Path *np)
145 {
146 np->show_helperpath = true;
147 np->helperpath_rgba = 0x009000ff;
148 np->helperpath_width = 1.0;
149 }
151 void
152 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
153 {
154 param_set_and_write_new_value( (*this) * postmul );
155 }
157 void
158 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath)
159 {
160 const std::vector<Geom::Path> temppath = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
161 gchar * svgd = SVGD_from_2GeomPath( temppath );
162 param_write_to_repr(svgd);
163 g_free(svgd);
164 }
166 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
167 void
168 PathParam::on_edit_button_click()
169 {
170 SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
171 if (item != NULL) {
172 param_editOncanvas(item, SP_ACTIVE_DESKTOP);
173 }
174 }
176 void
177 PathParam::on_paste_button_click()
178 {
179 // check if something is in the clipboard
180 GSList * clipboard = sp_selection_get_clipboard();
181 if (clipboard == NULL || clipboard->data == NULL) {
182 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
183 return;
184 }
186 Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
187 if (!strcmp (repr->name(), "svg:path")) {
188 const char * svgd = repr->attribute("d");
189 if (svgd) {
190 if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD
191 SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
192 _("This effect does not support arcs yet, try to convert to path.") );
193 return;
194 } else {
195 param_write_to_repr(svgd);
196 signal_path_pasted.emit();
197 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
198 _("Paste path parameter"));
199 }
200 }
201 } else {
202 SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));
203 return;
204 }
205 }
207 void
208 PathParam::on_copy_button_click()
209 {
210 sp_selection_copy_lpe_pathparam(this);
211 }
213 } /* namespace LivePathEffect */
215 } /* namespace Inkscape */
217 /*
218 Local Variables:
219 mode:c++
220 c-file-style:"stroustrup"
221 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
222 indent-tabs-mode:nil
223 fill-column:99
224 End:
225 */
226 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :