1 /**
2 * \brief LivePathEffect dialog
3 *
4 * Authors:
5 * Johan Engelen <j.b.c.engelen@utwente.nl>
6 *
7 * Copyright (C) 2007 Author
8 *
9 * Released under GNU GPL. Read the file 'COPYING' for more information.
10 */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #include <glibmm/i18n.h>
17 #include "livepatheffect-editor.h"
18 #include "verbs.h"
19 #include "selection.h"
20 #include "sp-shape.h"
21 #include "sp-path.h"
22 #include "live_effects/effect.h"
23 #include "live_effects/lpeobject.h"
24 #include "gtkmm/widget.h"
25 #include <vector>
26 #include "inkscape.h"
27 #include "desktop-handles.h"
28 #include "desktop.h"
29 #include "document-private.h"
30 #include "xml/node.h"
31 #include "xml/document.h"
33 namespace Inkscape {
34 class Application;
36 namespace UI {
37 namespace Dialog {
40 /*####################
41 * Callback functions
42 */
43 static void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
44 {
45 LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
46 lpeeditor->onSelectionChanged(selection);
47 }
49 static void lpeeditor_selection_modified (Inkscape::Selection *selection, guint flags, gpointer data)
50 {
51 lpeeditor_selection_changed (selection, data);
52 }
55 static void lpeeditor_desktop_change(Inkscape::Application*, SPDesktop* desktop, void *data)
56 {
57 if (!desktop) {
58 return;
59 }
60 LivePathEffectEditor* editor = reinterpret_cast<LivePathEffectEditor*>(data);
61 editor->setDesktop(desktop);
62 }
66 /*#######################
67 * LivePathEffectEditor
68 */
69 LivePathEffectEditor::LivePathEffectEditor(Behavior::BehaviorFactory behavior_factory)
70 : Dialog (behavior_factory, "dialogs.livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
71 combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
72 button_apply(_("_Apply"), _("Apply chosen effect to selection")),
73 button_remove(_("_Remove"), _("Remove effect from selection")),
74 effectwidget(NULL),
75 explain_label("", Gtk::ALIGN_CENTER),
76 effectapplication_frame(_("Apply new effect")),
77 effectcontrol_frame(_("Current effect")),
78 current_desktop(NULL)
79 {
80 // Top level vbox
81 Gtk::VBox *vbox = get_vbox();
82 vbox->set_spacing(4);
84 effectapplication_hbox.set_spacing(4);
85 effectcontrol_vbox.set_spacing(4);
87 effectapplication_hbox.pack_start(combo_effecttype, true, true);
88 effectapplication_hbox.pack_start(button_apply, true, true);
89 effectapplication_frame.add(effectapplication_hbox);
91 effectcontrol_vbox.pack_start(explain_label, true, true);
92 effectcontrol_vbox.pack_end(button_remove, true, true);
93 effectcontrol_frame.add(effectcontrol_vbox);
95 vbox->pack_start(effectapplication_frame, true, true);
96 vbox->pack_start(effectcontrol_frame, true, true);
98 // connect callback functions to buttons
99 button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
100 button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
102 // connect callback functions to changes in selected desktop.
103 g_signal_connect( G_OBJECT(INKSCAPE), "activate_desktop",
104 G_CALLBACK(lpeeditor_desktop_change), this);
106 g_signal_connect( G_OBJECT(INKSCAPE), "deactivate_desktop",
107 G_CALLBACK(lpeeditor_desktop_change), this);
109 setDesktop(SP_ACTIVE_DESKTOP);
110 show_all_children();
111 button_remove.hide();
112 }
114 LivePathEffectEditor::~LivePathEffectEditor()
115 {
116 if (effectwidget) {
117 effectcontrol_vbox.remove(*effectwidget);
118 effectwidget = NULL;
119 }
121 if (current_desktop) {
122 selection_changed_connection.disconnect();
123 selection_modified_connection.disconnect();
124 }
125 }
127 void
128 LivePathEffectEditor::showParams(LivePathEffect::Effect* effect)
129 {
130 if (effectwidget) {
131 effectcontrol_vbox.remove(*effectwidget);
132 effectwidget = NULL;
133 }
135 explain_label.set_markup("<b>" + effect->getName() + "</b>");
136 effectwidget = effect->getWidget();
137 if (effectwidget) {
138 effectcontrol_vbox.pack_start(*effectwidget, true, true);
139 }
140 button_remove.show();
142 effectcontrol_vbox.show_all_children();
143 // fixme: do resizing of dialog
144 }
146 void
147 LivePathEffectEditor::showText(Glib::ustring const &str)
148 {
149 if (effectwidget) {
150 effectcontrol_vbox.remove(*effectwidget);
151 effectwidget = NULL;
152 }
154 explain_label.set_label(str);
155 button_remove.hide();
157 // fixme: do resizing of dialog ?
158 }
160 void
161 LivePathEffectEditor::set_sensitize_all(bool sensitive)
162 {
163 combo_effecttype.set_sensitive(sensitive);
164 button_apply.set_sensitive(sensitive);
165 button_remove.set_sensitive(sensitive);
166 }
168 void
169 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
170 {
171 if ( sel && !sel->isEmpty() ) {
172 SPItem *item = sel->singleItem();
173 if ( item ) {
174 if ( SP_IS_SHAPE(item) ) {
175 SPShape *shape = SP_SHAPE(item);
176 LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
177 set_sensitize_all(true);
178 if (lpeobj) {
179 if (lpeobj->lpe) {
180 showParams(lpeobj->lpe);
181 } else {
182 showText(_("Unknown effect is applied"));
183 }
184 } else {
185 showText(_("No effect applied"));
186 button_remove.set_sensitive(false);
187 }
188 } else {
189 showText(_("Item is not a shape or path"));
190 set_sensitize_all(false);
191 }
192 } else {
193 showText(_("Only one item can be selected"));
194 set_sensitize_all(false);
195 }
196 } else {
197 showText(_("Empty selection"));
198 set_sensitize_all(false);
199 }
200 }
202 void
203 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
204 {
206 if ( desktop == current_desktop ) {
207 return;
208 }
210 if (current_desktop) {
211 selection_changed_connection.disconnect();
212 selection_modified_connection.disconnect();
213 }
215 current_desktop = desktop;
216 if (desktop) {
217 Inkscape::Selection *selection = sp_desktop_selection(desktop);
218 selection_changed_connection = selection->connectChanged(
219 sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
220 selection_modified_connection = selection->connectModified(
221 sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
223 onSelectionChanged(selection);
224 } else {
225 onSelectionChanged(NULL);
226 }
227 }
232 /*########################################################################
233 # BUTTON CLICK HANDLERS (callbacks)
234 ########################################################################*/
236 // TODO: factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
238 void
239 LivePathEffectEditor::onApply()
240 {
241 Inkscape::Selection *sel = _getSelection();
242 if ( sel && !sel->isEmpty() ) {
243 SPItem *item = sel->singleItem();
244 if ( item && SP_IS_SHAPE(item) ) {
245 SPDocument *doc = current_desktop->doc();
247 const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
248 if (!data) return;
250 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
251 Inkscape::XML::Node *repr = xml_doc->createElement("inkscape:path-effect");
252 repr->setAttribute("effect", data->key.c_str() );
254 SP_OBJECT_REPR(SP_DOCUMENT_DEFS(doc))->addChild(repr, NULL); // adds to <defs> and assigns the 'id' attribute
255 const gchar * repr_id = repr->attribute("id");
256 Inkscape::GC::release(repr);
258 gchar *href = g_strdup_printf("#%s", repr_id);
259 sp_shape_set_path_effect(SP_SHAPE(item), href);
260 g_free(href);
262 // make sure there is an original-d for paths!!!
263 if ( SP_IS_PATH(item) ) {
264 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
265 if ( ! pathrepr->attribute("inkscape:original-d") ) {
266 pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
267 }
268 }
270 sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
271 _("Create and apply path effect"));
272 }
273 }
274 }
276 void
277 LivePathEffectEditor::onRemove()
278 {
279 Inkscape::Selection *sel = _getSelection();
280 if ( sel && !sel->isEmpty() ) {
281 SPItem *item = sel->singleItem();
282 if ( item && SP_IS_SHAPE(item) ) {
283 sp_shape_remove_path_effect(SP_SHAPE(item));
284 sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
285 _("Remove path effect") );
286 }
287 }
288 }
292 } // namespace Dialog
293 } // namespace UI
294 } // namespace Inkscape