Code

6ec078ce034ad940a313b642b1a70b838e9d7ba4
[inkscape.git] / src / ui / dialog / livepatheffect-editor.cpp
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 }
65 /*#######################
66  * LivePathEffectEditor
67  */
69 LivePathEffectEditor::LivePathEffectEditor() 
70     : UI::Widget::Panel("", "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     Gtk::Box *contents = _getContents();
81     contents->set_spacing(4);
83     effectapplication_hbox.set_spacing(4);
84     effectcontrol_vbox.set_spacing(4);
86     effectapplication_hbox.pack_start(combo_effecttype, true, true);
87     effectapplication_hbox.pack_start(button_apply, true, true);
88     effectapplication_frame.add(effectapplication_hbox);
90     effectcontrol_vbox.pack_start(explain_label, true, true);
91     effectcontrol_vbox.pack_end(button_remove, true, true);
92     effectcontrol_frame.add(effectcontrol_vbox);
94     contents->pack_start(effectapplication_frame, true, true);
95     contents->pack_start(effectcontrol_frame, true, true);
97     // connect callback functions to buttons
98     button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
99     button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
101     // connect callback functions to changes in selected desktop.
102     g_signal_connect( G_OBJECT(INKSCAPE), "activate_desktop",
103                        G_CALLBACK(lpeeditor_desktop_change), this);
105     g_signal_connect( G_OBJECT(INKSCAPE), "deactivate_desktop",
106                        G_CALLBACK(lpeeditor_desktop_change), this);
108     setDesktop(SP_ACTIVE_DESKTOP);
109     show_all_children();
111     button_remove.hide();
114 LivePathEffectEditor::~LivePathEffectEditor() 
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     }
127 void
128 LivePathEffectEditor::showParams(LivePathEffect::Effect* effect)
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 
146 void
147 LivePathEffectEditor::showText(Glib::ustring const &str)
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 ?
160 void
161 LivePathEffectEditor::set_sensitize_all(bool sensitive)
163     combo_effecttype.set_sensitive(sensitive);
164     button_apply.set_sensitive(sensitive);
165     button_remove.set_sensitive(sensitive);
168 void
169 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
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     }
202 void 
203 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
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     }
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()
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             LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(SP_SHAPE(item));
271             if (lpeobj && lpeobj->lpe) {
272                 lpeobj->lpe->resetDefaults(item);
273             }
275             sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
276                              _("Create and apply path effect"));
277         }
278     }
281 void
282 LivePathEffectEditor::onRemove()
284     Inkscape::Selection *sel = _getSelection();
285     if ( sel && !sel->isEmpty() ) {
286         SPItem *item = sel->singleItem();
287         if ( item && SP_IS_SHAPE(item) ) {
288             sp_shape_remove_path_effect(SP_SHAPE(item));
289             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
290                                _("Remove path effect") );
291         }
292     }
297 } // namespace Dialog
298 } // namespace UI
299 } // namespace Inkscape