Code

2 LPE things:
[inkscape.git] / src / live_effects / parameter / path.cpp
1 #define INKSCAPE_LIVEPATHEFFECT_PARAMETER_PATH_CPP\r
2 \r
3 /*\r
4  * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>\r
5  *\r
6  * Released under GNU GPL, read the file 'COPYING' for more information\r
7  */\r
8 \r
9 #include "live_effects/parameter/path.h"\r
10 #include "live_effects/effect.h"\r
11 #include "live_effects/n-art-bpath-2geom.h"\r
12 #include "svg/svg.h"\r
13 #include <2geom/svg-path-parser.h>\r
14 #include <2geom/sbasis-to-bezier.h>\r
15 #include <2geom/d2.h>\r
16 \r
17 #include "ui/widget/point.h"\r
18 #include "widgets/icon.h"\r
19 #include <gtk/gtkstock.h>\r
20 #include "selection-chemistry.h"\r
21 \r
22 #include "desktop.h"\r
23 #include "inkscape.h"\r
24 #include "message-stack.h"\r
25 #include "verbs.h"\r
26 #include "document.h"\r
27 \r
28 #define noLPEPATHPARAM_DEBUG\r
29 \r
30 namespace Inkscape {\r
31 \r
32 namespace LivePathEffect {\r
33 \r
34 PathParam::PathParam( const Glib::ustring& label, const Glib::ustring& tip,\r
35                       const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,\r
36                       Effect* effect, const gchar * default_value)\r
37     : Parameter(label, tip, key, wr, effect)\r
38 {\r
39     _widget = NULL;\r
40     _tooltips = NULL;\r
41     defvalue = g_strdup(default_value);\r
42     param_readSVGValue(defvalue);\r
43 }\r
44 \r
45 PathParam::~PathParam()\r
46 {\r
47     if (_tooltips)\r
48         delete _tooltips;\r
49     // _widget is managed by GTK so do not delete!\r
50 \r
51     g_free(defvalue);\r
52 }\r
53 \r
54 void\r
55 PathParam::param_set_default()\r
56 {\r
57     param_readSVGValue(defvalue);\r
58 }\r
59 \r
60 bool\r
61 PathParam::param_readSVGValue(const gchar * strvalue)\r
62 {\r
63     if (strvalue) {\r
64         Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;\r
65         std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);\r
66         for (unsigned int i=0; i < temppath.size(); i++) {\r
67             newpath.concat( temppath[i].toPwSb() );\r
68         }\r
69         *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;\r
70         signal_path_changed.emit();\r
71         return true;\r
72     }\r
73 \r
74     return false;\r
75 }\r
76 \r
77 gchar *\r
78 PathParam::param_writeSVGValue() const\r
79 {\r
80     const std::vector<Geom::Path> temppath =\r
81         Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);\r
82     gchar * svgd = SVGD_from_2GeomPath( temppath );\r
83     return svgd;\r
84 }\r
85 \r
86 Gtk::Widget *\r
87 PathParam::param_getWidget()\r
88 {\r
89     if (!_widget) {\r
90         _widget = Gtk::manage(new Gtk::HBox());\r
91         _tooltips = new Gtk::Tooltips();\r
92 \r
93         Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));\r
94         static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);\r
95         _tooltips->set_tip(*pLabel, param_tooltip);\r
96 \r
97         Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );\r
98         Gtk::Button * pButton = Gtk::manage(new Gtk::Button());\r
99         pButton->set_relief(Gtk::RELIEF_NONE);\r
100         pIcon->show();\r
101         pButton->add(*pIcon);\r
102         pButton->show();\r
103         pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));\r
104         static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);\r
105         _tooltips->set_tip(*pButton, _("Edit on-canvas"));\r
106 #ifndef LPEPATHPARAM_DEBUG\r
107         pButton->set_sensitive(false);\r
108 #endif\r
109 \r
110         pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );\r
111         pButton = Gtk::manage(new Gtk::Button());\r
112         pButton->set_relief(Gtk::RELIEF_NONE);\r
113         pIcon->show();\r
114         pButton->add(*pIcon);\r
115         pButton->show();\r
116         pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));\r
117         static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);\r
118         _tooltips->set_tip(*pButton, _("Paste path"));\r
119 \r
120         static_cast<Gtk::HBox*>(_widget)->show_all_children();\r
121 \r
122     }\r
123     return dynamic_cast<Gtk::Widget *> (_widget);\r
124 }\r
125 \r
126 void\r
127 PathParam::on_edit_button_click()\r
128 {\r
129     g_message("give this path to edit on canvas!");\r
130 }\r
131 \r
132 void\r
133 PathParam::on_paste_button_click()\r
134 {\r
135     // check if something is in the clipboard\r
136     GSList * clipboard = sp_selection_get_clipboard();\r
137     if (clipboard == NULL || clipboard->data == NULL) {\r
138         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));\r
139         return;\r
140     }\r
141 \r
142     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;\r
143     if (!strcmp (repr->name(), "svg:path")) {\r
144         const char * svgd = repr->attribute("d");\r
145         if (svgd) {\r
146             param_write_to_repr(svgd);\r
147             signal_path_pasted.emit();\r
148             sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, \r
149                              _("Paste path parameter"));\r
150         }\r
151     } else {\r
152         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));\r
153     }\r
154 \r
155 }\r
156 \r
157 void\r
158 PathParam::param_write_to_repr(const char * svgd)\r
159 {\r
160     param_effect->getRepr()->setAttribute(param_key.c_str(), svgd);\r
161 }\r
162 \r
163 \r
164 } /* namespace LivePathEffect */\r
165 \r
166 } /* namespace Inkscape */\r
167 \r
168 /*\r
169   Local Variables:\r
170   mode:c++\r
171   c-file-style:"stroustrup"\r
172   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
173   indent-tabs-mode:nil\r
174   fill-column:99\r
175   End:\r
176 */\r
177 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r