Code

don't update LPE dialog when selection is modified.
[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 /*#######################
56  * LivePathEffectEditor
57  */
59 LivePathEffectEditor::LivePathEffectEditor() 
60     : UI::Widget::Panel("", "dialogs.livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
61       combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
62       button_apply(_("_Apply"), _("Apply chosen effect to selection")),
63       button_remove(_("_Remove"), _("Remove effect from selection")),
64       effectwidget(NULL),
65       explain_label("", Gtk::ALIGN_CENTER),
66       effectapplication_frame(_("Apply new effect")),
67       effectcontrol_frame(_("Current effect")),
68       current_desktop(NULL),
69       currect_effect(NULL)
70 {
71     Gtk::Box *contents = _getContents();
72     contents->set_spacing(4);
74     effectapplication_hbox.set_spacing(4);
75     effectcontrol_vbox.set_spacing(4);
77     effectapplication_hbox.pack_start(combo_effecttype, true, true);
78     effectapplication_hbox.pack_start(button_apply, true, true);
79     effectapplication_frame.add(effectapplication_hbox);
81     effectcontrol_vbox.pack_start(explain_label, true, true);
82     effectcontrol_vbox.pack_end(button_remove, true, true);
83     effectcontrol_frame.add(effectcontrol_vbox);
85     contents->pack_start(effectapplication_frame, true, true);
86     contents->pack_start(effectcontrol_frame, true, true);
88     // connect callback functions to buttons
89     button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
90     button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
92     show_all_children();
94     button_remove.hide();
95 }
97 LivePathEffectEditor::~LivePathEffectEditor() 
98 {
99     if (effectwidget) {
100         effectcontrol_vbox.remove(*effectwidget);
101         delete effectwidget;
102         effectwidget = NULL;
103     }
105     if (current_desktop) {
106         selection_changed_connection.disconnect();
107         selection_modified_connection.disconnect();
108     }
111 void
112 LivePathEffectEditor::showParams(LivePathEffect::Effect* effect)
114     if (currect_effect != effect) {
115         currect_effect = effect;
117         if (effectwidget) {
118             effectcontrol_vbox.remove(*effectwidget);
119             delete effectwidget;
120             effectwidget = NULL;
121         }
123         explain_label.set_markup("<b>" + effect->getName() + "</b>");
124         effectwidget = effect->newWidget(&tooltips);
125         if (effectwidget) {
126             effectcontrol_vbox.pack_start(*effectwidget, true, true);
127         }
128         button_remove.show();
130         effectcontrol_vbox.show_all_children();
131         // fixme: add resizing of dialog
132     }
135 void
136 LivePathEffectEditor::showText(Glib::ustring const &str)
138     if (effectwidget) {
139         effectcontrol_vbox.remove(*effectwidget);
140         delete effectwidget;
141         effectwidget = NULL;
142     }
144     explain_label.set_label(str);
145     button_remove.hide();
147     // fixme: do resizing of dialog ?
150 void
151 LivePathEffectEditor::set_sensitize_all(bool sensitive)
153     combo_effecttype.set_sensitive(sensitive);
154     button_apply.set_sensitive(sensitive);
155     button_remove.set_sensitive(sensitive);
158 void
159 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
161     if ( sel && !sel->isEmpty() ) {
162         SPItem *item = sel->singleItem();
163         if ( item ) {
164             if ( SP_IS_SHAPE(item) ) {
165                 SPShape *shape = SP_SHAPE(item);
166                 LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
167                 set_sensitize_all(true);
168                 if (lpeobj) {
169                     if (lpeobj->lpe) {
170                         showParams(lpeobj->lpe);
171                     } else {
172                         showText(_("Unknown effect is applied"));
173                     }
174                 } else {
175                     showText(_("No effect applied"));
176                     button_remove.set_sensitive(false);
177                 }
178             } else {
179                 showText(_("Item is not a shape or path"));
180                 set_sensitize_all(false);
181             }
182         } else {
183             showText(_("Only one item can be selected"));
184             set_sensitize_all(false);
185         }
186     } else {
187         showText(_("Empty selection"));
188         set_sensitize_all(false);
189     }
192 void 
193 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
195     Panel::setDesktop(desktop);
197     if ( desktop == current_desktop ) {
198         return;
199     }
201     if (current_desktop) {
202         selection_changed_connection.disconnect();
203         selection_modified_connection.disconnect();
204     }
206     current_desktop = desktop;
207     if (desktop) {
208         Inkscape::Selection *selection = sp_desktop_selection(desktop);
209         selection_changed_connection = selection->connectChanged(
210             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
211         selection_modified_connection = selection->connectModified(
212             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
214         onSelectionChanged(selection);
215     } else {
216         onSelectionChanged(NULL);
217     }
223 /*########################################################################
224 # BUTTON CLICK HANDLERS    (callbacks)
225 ########################################################################*/
227 // TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
229 void
230 LivePathEffectEditor::onApply()
232     Inkscape::Selection *sel = _getSelection();
233     if ( sel && !sel->isEmpty() ) {
234         SPItem *item = sel->singleItem();
235         if ( item && SP_IS_SHAPE(item) ) {
236             SPDocument *doc = current_desktop->doc();
238             const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
239             if (!data) return;
241             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
242             Inkscape::XML::Node *repr = xml_doc->createElement("inkscape:path-effect");
243             repr->setAttribute("effect", data->key.c_str() );
245             SP_OBJECT_REPR(SP_DOCUMENT_DEFS(doc))->addChild(repr, NULL); // adds to <defs> and assigns the 'id' attribute
246             const gchar * repr_id = repr->attribute("id");
247             Inkscape::GC::release(repr);
249             gchar *href = g_strdup_printf("#%s", repr_id);
250             sp_shape_set_path_effect(SP_SHAPE(item), href);
251             g_free(href);
253             // make sure there is an original-d for paths!!!
254             if ( SP_IS_PATH(item) ) {
255                 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
256                 if ( ! pathrepr->attribute("inkscape:original-d") ) {
257                     pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
258                 }
259             }
261             LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(SP_SHAPE(item));
262             if (lpeobj && lpeobj->lpe) {
263                 lpeobj->lpe->resetDefaults(item);
264             }
266             sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
267                              _("Create and apply path effect"));
268         }
269     }
272 void
273 LivePathEffectEditor::onRemove()
275     Inkscape::Selection *sel = _getSelection();
276     if ( sel && !sel->isEmpty() ) {
277         SPItem *item = sel->singleItem();
278         if ( item && SP_IS_SHAPE(item) ) {
279             sp_shape_remove_path_effect(SP_SHAPE(item));
280             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
281                                _("Remove path effect") );
282         }
283     }
288 } // namespace Dialog
289 } // namespace UI
290 } // namespace Inkscape