Code

Commit LivePathEffect branch to trunk!
[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 )\r
37     : Parameter(label, tip, key, wr, effect)\r
38 {\r
39     _widget = NULL;\r
40     _tooltips = NULL;\r
41 }\r
42 \r
43 PathParam::~PathParam()\r
44 {\r
45     if (_tooltips)\r
46         delete _tooltips;\r
47     // _widget is managed by GTK so do not delete!\r
48 }\r
49 \r
50 bool\r
51 PathParam::param_readSVGValue(const gchar * strvalue)\r
52 {\r
53     if (strvalue) {\r
54         Geom::Piecewise<Geom::D2<Geom::SBasis> > newpath;\r
55         std::vector<Geom::Path> temppath = SVGD_to_2GeomPath(strvalue);\r
56         for (unsigned int i=0; i < temppath.size(); i++) {\r
57             newpath.concat( temppath[i].toPwSb() );\r
58         }\r
59         *( dynamic_cast<Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this) ) = newpath;\r
60         return true;\r
61     }\r
62 \r
63     return false;\r
64 }\r
65 \r
66 gchar *\r
67 PathParam::param_writeSVGValue() const\r
68 {\r
69     const std::vector<Geom::Path> temppath =\r
70         Geom::path_from_piecewise(* dynamic_cast<const Geom::Piecewise<Geom::D2<Geom::SBasis> > *> (this), LPE_CONVERSION_TOLERANCE);\r
71     gchar * svgd = SVGD_from_2GeomPath( temppath );\r
72     return svgd;\r
73 }\r
74 \r
75 Gtk::Widget *\r
76 PathParam::param_getWidget()\r
77 {\r
78     if (!_widget) {\r
79         _widget = Gtk::manage(new Gtk::HBox());\r
80         _tooltips = new Gtk::Tooltips();\r
81 \r
82         Gtk::Label* pLabel = Gtk::manage(new Gtk::Label(param_label));\r
83         static_cast<Gtk::HBox*>(_widget)->pack_start(*pLabel, true, true);\r
84         _tooltips->set_tip(*pLabel, param_tooltip);\r
85 \r
86         Gtk::Widget*  pIcon = Gtk::manage( sp_icon_get_icon( "draw_node", Inkscape::ICON_SIZE_BUTTON) );\r
87         Gtk::Button * pButton = Gtk::manage(new Gtk::Button());\r
88         pButton->set_relief(Gtk::RELIEF_NONE);\r
89         pIcon->show();\r
90         pButton->add(*pIcon);\r
91         pButton->show();\r
92         pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_edit_button_click));\r
93         static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);\r
94         _tooltips->set_tip(*pButton, _("Edit on-canvas"));\r
95 #ifndef LPEPATHPARAM_DEBUG\r
96         pButton->set_sensitive(false);\r
97 #endif\r
98 \r
99         pIcon = Gtk::manage( sp_icon_get_icon( GTK_STOCK_PASTE, Inkscape::ICON_SIZE_BUTTON) );\r
100         pButton = Gtk::manage(new Gtk::Button());\r
101         pButton->set_relief(Gtk::RELIEF_NONE);\r
102         pIcon->show();\r
103         pButton->add(*pIcon);\r
104         pButton->show();\r
105         pButton->signal_clicked().connect(sigc::mem_fun(*this, &PathParam::on_paste_button_click));\r
106         static_cast<Gtk::HBox*>(_widget)->pack_start(*pButton, true, true);\r
107         _tooltips->set_tip(*pButton, _("Paste path"));\r
108 \r
109         static_cast<Gtk::HBox*>(_widget)->show_all_children();\r
110 \r
111     }\r
112     return dynamic_cast<Gtk::Widget *> (_widget);\r
113 }\r
114 \r
115 void\r
116 PathParam::on_edit_button_click()\r
117 {\r
118     g_message("give this path to edit on canvas!");\r
119 }\r
120 \r
121 void\r
122 PathParam::on_paste_button_click()\r
123 {\r
124     // check if something is in the clipboard\r
125     GSList * clipboard = sp_selection_get_clipboard();\r
126     if (clipboard == NULL || clipboard->data == NULL) {\r
127         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Nothing on the clipboard."));\r
128         return;\r
129     }\r
130 \r
131     Inkscape::XML::Node *repr = (Inkscape::XML::Node *) clipboard->data;\r
132     if (!strcmp (repr->name(), "svg:path")) {\r
133         const char * svgd = repr->attribute("d");\r
134         if (svgd) {\r
135             param_write_to_repr(svgd);\r
136             signal_path_pasted.emit();\r
137             sp_document_done(param_effect->getSPDoc(), SP_VERB_DIALOG_LIVE_PATH_EFFECT, \r
138                              _("Paste path parameter"));\r
139         }\r
140     } else {\r
141         SP_ACTIVE_DESKTOP->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Clipboard does not contain a path."));\r
142     }\r
143 \r
144 }\r
145 \r
146 void\r
147 PathParam::param_write_to_repr(const char * svgd)\r
148 {\r
149     param_effect->getRepr()->setAttribute(param_key.c_str(), svgd);\r
150 }\r
151 \r
152 \r
153 }; /* namespace LivePathEffect */\r
154 \r
155 }; /* namespace Inkscape */\r
156 \r
157 /*\r
158   Local Variables:\r
159   mode:c++\r
160   c-file-style:"stroustrup"\r
161   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
162   indent-tabs-mode:nil\r
163   fill-column:99\r
164   End:\r
165 */\r
166 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :\r