Code

warning cleanup
[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 }
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();
112     button_remove.hide();
115 LivePathEffectEditor::~LivePathEffectEditor() 
117     if (effectwidget) {
118         effectcontrol_vbox.remove(*effectwidget);
119         effectwidget = NULL;
120     }
122     if (current_desktop) {
123         selection_changed_connection.disconnect();
124         selection_modified_connection.disconnect();
125     }
128 void
129 LivePathEffectEditor::showParams(LivePathEffect::Effect* effect)
131     if (effectwidget) {
132         effectcontrol_vbox.remove(*effectwidget);
133         effectwidget = NULL;
134     }
136     explain_label.set_markup("<b>" + effect->getName() + "</b>");
137     effectwidget = effect->getWidget();
138     if (effectwidget) {
139         effectcontrol_vbox.pack_start(*effectwidget, true, true);
140     }
141     button_remove.show();
143     effectcontrol_vbox.show_all_children();
144     // fixme: do resizing of dialog 
147 void
148 LivePathEffectEditor::showText(Glib::ustring const &str)
150     if (effectwidget) {
151         effectcontrol_vbox.remove(*effectwidget);
152         effectwidget = NULL;
153     }
155     explain_label.set_label(str);
156     button_remove.hide();
158     // fixme: do resizing of dialog ?
161 void
162 LivePathEffectEditor::set_sensitize_all(bool sensitive)
164     combo_effecttype.set_sensitive(sensitive);
165     button_apply.set_sensitive(sensitive);
166     button_remove.set_sensitive(sensitive);
169 void
170 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
172     if ( sel && !sel->isEmpty() ) {
173         SPItem *item = sel->singleItem();
174         if ( item ) {
175             if ( SP_IS_SHAPE(item) ) {
176                 SPShape *shape = SP_SHAPE(item);
177                 LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
178                 set_sensitize_all(true);
179                 if (lpeobj) {
180                     if (lpeobj->lpe) {
181                         showParams(lpeobj->lpe);
182                     } else {
183                         showText(_("Unknown effect is applied"));
184                     }
185                 } else {
186                     showText(_("No effect applied"));
187                     button_remove.set_sensitive(false);
188                 }
189             } else {
190                 showText(_("Item is not a shape or path"));
191                 set_sensitize_all(false);
192             }
193         } else {
194             showText(_("Only one item can be selected"));
195             set_sensitize_all(false);
196         }
197     } else {
198         showText(_("Empty selection"));
199         set_sensitize_all(false);
200     }
203 void 
204 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
207     if ( desktop == current_desktop ) {
208         return;
209     }
211     if (current_desktop) {
212         selection_changed_connection.disconnect();
213         selection_modified_connection.disconnect();
214     }
216     current_desktop = desktop;
217     if (desktop) {
218         Inkscape::Selection *selection = sp_desktop_selection(desktop);
219         selection_changed_connection = selection->connectChanged(
220             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
221         selection_modified_connection = selection->connectModified(
222             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
224         onSelectionChanged(selection);
225     } else {
226         onSelectionChanged(NULL);
227     }
233 /*########################################################################
234 # BUTTON CLICK HANDLERS    (callbacks)
235 ########################################################################*/
237 // TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
239 void
240 LivePathEffectEditor::onApply()
242     Inkscape::Selection *sel = _getSelection();
243     if ( sel && !sel->isEmpty() ) {
244         SPItem *item = sel->singleItem();
245         if ( item && SP_IS_SHAPE(item) ) {
246             SPDocument *doc = current_desktop->doc();
248             const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
249             if (!data) return;
251             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
252             Inkscape::XML::Node *repr = xml_doc->createElement("inkscape:path-effect");
253             repr->setAttribute("effect", data->key.c_str() );
255             SP_OBJECT_REPR(SP_DOCUMENT_DEFS(doc))->addChild(repr, NULL); // adds to <defs> and assigns the 'id' attribute
256             const gchar * repr_id = repr->attribute("id");
257             Inkscape::GC::release(repr);
259             gchar *href = g_strdup_printf("#%s", repr_id);
260             sp_shape_set_path_effect(SP_SHAPE(item), href);
261             g_free(href);
263             // make sure there is an original-d for paths!!!
264             if ( SP_IS_PATH(item) ) {
265                 Inkscape::XML::Node *pathrepr = SP_OBJECT_REPR(item);
266                 if ( ! pathrepr->attribute("inkscape:original-d") ) {
267                     pathrepr->setAttribute("inkscape:original-d", pathrepr->attribute("d"));
268                 }
269             }
271             LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(SP_SHAPE(item));
272             if (lpeobj && lpeobj->lpe) {
273                 lpeobj->lpe->resetDefaults(item);
274             }
276             sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
277                              _("Create and apply path effect"));
278         }
279     }
282 void
283 LivePathEffectEditor::onRemove()
285     Inkscape::Selection *sel = _getSelection();
286     if ( sel && !sel->isEmpty() ) {
287         SPItem *item = sel->singleItem();
288         if ( item && SP_IS_SHAPE(item) ) {
289             sp_shape_remove_path_effect(SP_SHAPE(item));
290             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
291                                _("Remove path effect") );
292         }
293     }
298 } // namespace Dialog
299 } // namespace UI
300 } // namespace Inkscape