Code

return const reference when getting path data from lpe-PathParam!
[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       _pwd2(),
45       referring(false)
46 {
47     defvalue = g_strdup(default_value);
48     param_readSVGValue(defvalue);
49     oncanvas_editable = true;
50 }
52 PathParam::~PathParam()
53 {
54     g_free(defvalue);
55 }
57 Geom::Piecewise<Geom::D2<Geom::SBasis> > const &
58 PathParam::get_pwd2()
59 {
60     if (!referring) {
61         return _pwd2;
62     } else {
63         /* update own pwd2 with data from path referred to
64          when this works, optimize to only update own pwd2 when referred path changed. */
65         //_pwd2 = ...;
66         return _pwd2;
67     }
68 }
70 void
71 PathParam::param_set_default()
72 {
73     param_readSVGValue(defvalue);
74 }
76 void
77 PathParam::param_set_and_write_default()
78 {
79     param_write_to_repr(defvalue);
80 }
82 bool
83 PathParam::param_readSVGValue(const gchar * strvalue)
84 {
85     if (strvalue) {
86         Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;
87         std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);
88         for (unsigned int i=0; i < temppath.size(); i++) {
89             newpath.concat( temppath[i].toPwSb() );
90         }
91         _pwd2 = newpath;
92         signal_path_changed.emit();
93         return true;
94     }
96     return false;
97 }
99 gchar *
100 PathParam::param_writeSVGValue() const
102     const std::vector<Geom::Path> temppath =
103         Geom::path_from_piecewise(_pwd2, LPE_CONVERSION_TOLERANCE);
104     gchar * svgd = SVGD_from_2GeomPath( temppath );
105     return svgd;
108 Gtk::Widget *
109 PathParam::param_newWidget(Gtk::Tooltips * tooltips)
111     Gtk::HBox * _widget = Gtk::manage(new Gtk::HBox());
113     Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
114     static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
115     tooltips->set_tip(*pLabel, param_tooltip);
117     Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
118     Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
119     pButton->set_relief(Gtk::RELIEF_NONE);
120     pIcon->show();
121     pButton->add(*pIcon);
122     pButton->show();
123     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
124     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
125     tooltips->set_tip(*pButton, _("Edit on-canvas"));
127     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_COPY, Inkscape::ICON_SIZE_BUTTON) );
128     pButton = Gtk::manage(new Gtk::Button());
129     pButton->set_relief(Gtk::RELIEF_NONE);
130     pIcon->show();
131     pButton->add(*pIcon);
132     pButton->show();
133     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_copy_button_click));
134     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
135     tooltips->set_tip(*pButton, _("Copy path"));
137     pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
138     pButton = Gtk::manage(new Gtk::Button());
139     pButton->set_relief(Gtk::RELIEF_NONE);
140     pIcon->show();
141     pButton->add(*pIcon);
142     pButton->show();
143     pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));
144     static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
145     tooltips->set_tip(*pButton, _("Paste path"));
147     static_cast<Gtk::HBox*>(_widget)->show_all_children();
149     return dynamic_cast<Gtk::Widget *> (_widget);
152 void
153 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
155     // If not already in nodecontext, goto it!
156     if (!tools_isactive(dt, TOOLS_NODES)) {
157         tools_switch_current(TOOLS_NODES);
158     }
160     ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
161     shape_editor->set_item_lpe_path_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
164 void
165 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
167     np->show_helperpath = true;
168     np->helperpath_rgba = 0x009000ff;
169     np->helperpath_width = 1.0;
172 void
173 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
175     // only apply transform when not referring to other path
176     if (!referring)
177         param_set_and_write_new_value( _pwd2 * postmul );
180 void
181 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & newpath)
183     const std::vector<Geom::Path> temppath = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
184     gchar * svgd = SVGD_from_2GeomPath( temppath );
185     param_write_to_repr(svgd);
186     g_free(svgd);
189 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
190 void
191 PathParam::on_edit_button_click()
193     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
194     if (item != NULL) {
195         param_editOncanvas(item, SP_ACTIVE_DESKTOP);
196     }
199 void
200 PathParam::on_paste_button_click()
202     // check if something is in the clipboard
203     GSList * clipboard = sp_selection_get_clipboard();
204     if (clipboard == NULL || clipboard->data == NULL) {
205         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
206         return;
207     }
209     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
210     if (!strcmp (repr->name(), "svg:path")) {
211         const char * svgd = repr->attribute("d");
212         if (svgd) {
213             if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD
214                 SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
215                             _("This effect does not support arcs yet, try to convert to path.") );
216                 return;
217             } else {
218                 param_write_to_repr(svgd);
219                 signal_path_pasted.emit();
220                 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
221                                  _("Paste path parameter"));
222             }
223         }
224     } else {
225         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));
226         return;
227     }
230 void
231 PathParam::on_copy_button_click()
233     sp_selection_copy_lpe_pathparam(this);
236 } /* namespace LivePathEffect */
238 } /* namespace Inkscape */
240 /*
241   Local Variables:
242   mode:c++
243   c-file-style:"stroustrup"
244   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
245   indent-tabs-mode:nil
246   fill-column:99
247   End:
248 */
249 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :