Code

LPE: add on-canvas editing of path parameters!
[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 #define LPEPATHPARAM_DEBUG
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 }
51 PathParam::~PathParam()
52 {
53     if (_tooltips)
54         delete _tooltips;
55     // _widget is managed by GTK so do not delete!
57     g_free(defvalue);
58 }
60 void
61 PathParam::param_set_default()
62 {
63     param_readSVGValue(defvalue);
64 }
66 bool
67 PathParam::param_readSVGValue(const gchar * strvalue)
68 {
69     if (strvalue) {
70         Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;
71         std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);
72         for (unsigned int i=0; i < temppath.size(); i++) {
73             newpath.concat( temppath[i].toPwSb() );
74         }
75         *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;
76         signal_path_changed.emit();
77         return true;
78     }
80     return false;
81 }
83 gchar *
84 PathParam::param_writeSVGValue() const
85 {
86     const std::vector<Geom::Path> temppath =
87         Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);
88     gchar * svgd = SVGD_from_2GeomPath( temppath );
89     return svgd;
90 }
92 Gtk::Widget *
93 PathParam::param_getWidget()
94 {
95     if (!_widget) {
96         _widget = Gtk::manage(new Gtk::HBox());
97         _tooltips = new Gtk::Tooltips();
99         Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));
100         static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);
101         _tooltips->set_tip(*pLabel, param_tooltip);
103         Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );
104         Gtk::Button * pButton = Gtk::manage(new Gtk::Button());
105         pButton->set_relief(Gtk::RELIEF_NONE);
106         pIcon->show();
107         pButton->add(*pIcon);
108         pButton->show();
109         pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));
110         static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
111         _tooltips->set_tip(*pButton, _("Edit on-canvas"));
112         edit_button = pButton;
113 #ifndef LPEPATHPARAM_DEBUG
114         edit_button->set_sensitive(false);
115 #endif
117         pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );
118         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_paste_button_click));
124         static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);
125         _tooltips->set_tip(*pButton, _("Paste path"));
127         static_cast<Gtk::HBox*>(_widget)->show_all_children();
129     }
130     return dynamic_cast<Gtk::Widget *> (_widget);
133 void
134 PathParam::on_edit_button_click()
136     // Switch to node edit tool:
137     tools_switch_current(TOOLS_NODES);
139     // set this parameter to edit:
140     ShapeEditor * shape_editor = SP_NODE_CONTEXT( SP_ACTIVE_DESKTOP->event_context )->shape_editor;
141     SPItem * item = sp_desktop_selection(SP_ACTIVE_DESKTOP)->singleItem();
142     shape_editor->set_item_livepatheffect_parameter(item, SP_OBJECT(param_effect->getLPEObj()), param_key.c_str());
145 void
146 PathParam::on_paste_button_click()
148     // check if something is in the clipboard
149     GSList * clipboard = sp_selection_get_clipboard();
150     if (clipboard == NULL || clipboard->data == NULL) {
151         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));
152         return;
153     }
155     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;
156     if (!strcmp (repr->name(), "svg:path")) {
157         const char * svgd = repr->attribute("d");
158         if (svgd) {
159             if (strchr(svgd,'A')) { // FIXME: temporary hack until 2Geom supports arcs in SVGD
160                 SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE,
161                             _("This effect does not support arcs yet, try to convert to path.") );
162                 return;
163             } else {
164                 param_write_to_repr(svgd);
165                 signal_path_pasted.emit();
166                 sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
167                                  _("Paste path parameter"));
168             }
169         }
170     } else {
171         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));
172         return;
173     }
176 void
177 PathParam::param_write_to_repr(const char * svgd)
179     param_effect->getRepr()->setAttribute(param_key.c_str(), svgd);
183 } /* namespace LivePathEffect */
185 } /* namespace Inkscape */
187 /*
188   Local Variables:
189   mode:c++
190   c-file-style:"stroustrup"
191   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
192   indent-tabs-mode:nil
193   fill-column:99
194   End:
195 */
196 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :