Code

correctly transform LPE path and point parameters with the SPItem's transform
[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"
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     _widget = NULL;
46     _tooltips = NULL;
47     edit_button = NULL;
48     defvalue = g_strdup(default_value);
49     param_readSVGValue(defvalue);
50     oncanvas_editable = true;
51 }
53 PathParam::~PathParam()
54 {
55     if (_tooltips)
56         delete _tooltips;
57     // _widget is managed by GTK so do not delete!
59     g_free(defvalue);
60 }
62 void
63 PathParam::param_set_default()
64 {
65     param_readSVGValue(defvalue);
66 }
68 bool
69 PathParam::param_readSVGValue(const gchar * strvalue)
70 {
71     if (strvalue) {
72         Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;
73         std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);
74         for (unsigned int i=0; i < temppath.size(); i++) {
75             newpath.concat( temppath[i].toPwSb() );
76         }
77         *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;
78         signal_path_changed.emit();
79         return true;
80     }
82     return false;
83 }
85 gchar *
86 PathParam::param_writeSVGValue() const
87 {
88     const std::vector<Geom::Path> temppath =
89         Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);
90     gchar * svgd = SVGD_from_2GeomPath( temppath );
91     return svgd;
92 }
94 Gtk::Widget *
95 PathParam::param_getWidget()
96 {
97     if (!_widget) {
98         _widget = Gtk::manage(new Gtk::HBox());
99         _tooltips = new Gtk::Tooltips();
101         Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
102         static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
103         _tooltips->set_tip(*pLabel, param_tooltip);
105         Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
106         Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
107         pButton->set_relief(Gtk::RELIEF_NONE);
108         pIcon->show();
109         pButton->add(*pIcon);
110         pButton->show();
111         pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
112         static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
113         _tooltips->set_tip(*pButton, _("Edit on-canvas"));
114         edit_button = pButton;
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     }
129     return dynamic_cast<Gtk::Widget *> (_widget);
132 void
133 PathParam::param_editOncanvas(SPItem * item, SPDesktop * dt)
135     // If not already in nodecontext, goto it!
136     if (!tools_isactive(dt, TOOLS_NODES)) {
137         tools_switch_current(TOOLS_NODES);
138     }
140     ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
141     shape_editor->set_item_livepatheffect_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
144 void
145 PathParam::param_setup_nodepath(Inkscape::NodePath::Path *np)
147     np->show_helperpath = true;
148     np->helperpath_rgba = 0x009000ff;
149     np->helperpath_width = 1.0;
152 void
153 PathParam::param_transform_multiply(Geom::Matrix const& postmul, bool /*set*/)
155     param_set_and_write_new_value( (*this) * postmul );
158 void
159 PathParam::param_set_and_write_new_value (Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath)
161     const std::vector<Geom::Path> temppath = Geom::path_from_piecewise(newpath, LPE_CONVERSION_TOLERANCE);
162     gchar * svgd = SVGD_from_2GeomPath( temppath );
163     param_write_to_repr(svgd);
164     g_free(svgd);
167 /* CALLBACK FUNCTIONS FOR THE BUTTONS */
168 void
169 PathParam::on_edit_button_click()
171     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
172     if (item != NULL) {
173         param_editOncanvas(item, SP_ACTIVE_DESKTOP);
174     }
177 void
178 PathParam::on_paste_button_click()
180     // check if something is in the clipboard
181     GSList * clipboard = sp_selection_get_clipboard();
182     if (clipboard == NULL || clipboard->data == NULL) {
183         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
184         return;
185     }
187     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
188     if (!strcmp (repr->name(), "svg:path")) {
189         const char * svgd = repr->attribute("d");
190         if (svgd) {
191             if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD
192                 SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
193                             _("This effect does not support arcs yet, try to convert to path.") );
194                 return;
195             } else {
196                 param_write_to_repr(svgd);
197                 signal_path_pasted.emit();
198                 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
199                                  _("Paste path parameter"));
200             }
201         }
202     } else {
203         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));
204         return;
205     }
209 } /* namespace LivePathEffect */
211 } /* namespace Inkscape */
213 /*
214   Local Variables:
215   mode:c++
216   c-file-style:"stroustrup"
217   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
218   indent-tabs-mode:nil
219   fill-column:99
220   End:
221 */
222 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :