Code

Factor out 'create and apply' code for LPEs so that it can be called from everywhere
[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  *   Steren Giannini <steren.giannini@gmail.com>
7  *   Bastien Bouclet <bgkweb@gmail.com>
8  *
9  * Copyright (C) 2007 Author
10  *
11  * Released under GNU GPL.  Read the file 'COPYING' for more information.
12  */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include <glibmm/i18n.h>
19 #include "livepatheffect-editor.h"
20 #include "verbs.h"
21 #include "selection.h"
22 #include "sp-shape.h"
23 #include "sp-item-group.h"
24 #include "sp-path.h"
25 #include "sp-rect.h"
26 #include "sp-lpe-item.h"
27 #include "path-chemistry.h"
28 #include "live_effects/effect.h"
29 #include "live_effects/lpeobject.h"
30 #include "gtkmm/widget.h"
31 #include <vector>
32 #include "inkscape.h"
33 #include "desktop-handles.h"
34 #include "desktop.h"
35 #include "document.h"
36 #include "xml/node.h"
37 #include <gtkmm/stock.h>
38 #include <gtkmm/toolbar.h>
40 #include "live_effects/lpeobject-reference.h"
42 namespace Inkscape {
43 class Application;
45 namespace UI {
46 namespace Dialog {
49 /*####################
50  * Callback functions
51  */
52 static void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
53 {
54     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
55     lpeeditor->onSelectionChanged(selection);
56 }
59 /*#######################
60  * LivePathEffectEditor
61  */
63 LivePathEffectEditor::LivePathEffectEditor() 
64     : UI::Widget::Panel("", "dialogs.livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
65       combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
66       effectwidget(NULL),
67       explain_label("", Gtk::ALIGN_CENTER),
68       effectapplication_frame(_("Apply new effect")),
69       effectcontrol_frame(_("Current effect")),
70       effectlist_frame(_("Effect list")),
71       button_up(Gtk::Stock::GO_UP),
72       button_down(Gtk::Stock::GO_DOWN),
73       button_apply(Gtk::Stock::ADD),
74       button_remove(Gtk::Stock::REMOVE),
75       current_desktop(NULL),
76       current_lpeitem(NULL)
77 {
78     Gtk::Box *contents = _getContents();
79     contents->set_spacing(4);
81     //Add the TreeView, inside a ScrolledWindow, with the button underneath:
82     scrolled_window.add(effectlist_view);
83     //Only show the scrollbars when they are necessary:
84     scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
86     effectapplication_hbox.set_spacing(4);
87     effectcontrol_vbox.set_spacing(4);
88     effectlist_vbox.set_spacing(4);
90     effectapplication_hbox.pack_start(combo_effecttype, true, true);
91     effectapplication_hbox.pack_start(button_apply, true, true);
92     effectapplication_frame.add(effectapplication_hbox);
94     effectlist_vbox.pack_start(scrolled_window, Gtk::PACK_EXPAND_WIDGET);
95     effectlist_vbox.pack_end(toolbar, Gtk::PACK_SHRINK);
96    // effectlist_vbox.pack_end(button_hbox, Gtk::PACK_SHRINK);
97     effectlist_frame.add(effectlist_vbox);
99     effectcontrol_vbox.pack_start(explain_label, true, true);
100     effectcontrol_frame.add(effectcontrol_vbox);
102  //   button_hbox.pack_start(button_up, true, true);
103  //   button_hbox.pack_start(button_down, true, true);
104  //   button_hbox.pack_end(button_remove, true, true);
105         toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);  
106  // Add toolbar items to toolbar
107   toolbar.append(button_up);
108   toolbar.append(button_down);
109   toolbar.append(button_remove);
111   
112   // Add toolbar
113   //add_toolbar(toolbar);
114   toolbar.show_all(); //Show the toolbar and all its child widgets.
116       
117     //Create the Tree model:
118     effectlist_store = Gtk::ListStore::create(columns);
119     effectlist_view.set_model(effectlist_store);
121     effectlist_view.set_rules_hint();
122     effectlist_view.set_headers_clickable(true);
123     effectlist_view.set_headers_visible(true);
125     // Handle tree selections
126     effectlist_selection = effectlist_view.get_selection();
127     effectlist_selection->signal_changed().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_effect_selection_changed) );
129     effectlist_view.set_headers_visible(false);
130     //Add the TreeView's view columns:
131     effectlist_view.append_column("Effect", columns.col_name);
134     contents->pack_start(effectapplication_frame, false, false);
135     contents->pack_start(effectlist_frame, true, true);
136     contents->pack_start(effectcontrol_frame, false, false);
138     // connect callback functions to buttons
139     button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
140     button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
142     button_up.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onUp));
143     button_down.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onDown));
145     show_all_children();
147     //button_remove.hide();
150 LivePathEffectEditor::~LivePathEffectEditor() 
152     if (effectwidget) {
153         effectcontrol_vbox.remove(*effectwidget);
154         delete effectwidget;
155         effectwidget = NULL;
156     }
158     if (current_desktop) {
159         selection_changed_connection.disconnect();
160     }
163 void
164 LivePathEffectEditor::showParams(LivePathEffect::Effect* effect)
166     if (effectwidget) {
167         effectcontrol_vbox.remove(*effectwidget);
168         delete effectwidget;
169         effectwidget = NULL;
170     }
172     explain_label.set_markup("<b>" + effect->getName() + "</b>");
173     effectwidget = effect->newWidget(&tooltips);
174     if (effectwidget) {
175         effectcontrol_vbox.pack_start(*effectwidget, true, true);
176     }
177     button_remove.show();
179     effectcontrol_vbox.show_all_children();
180     // fixme: add resizing of dialog
183 void
184 LivePathEffectEditor::showText(Glib::ustring const &str)
186     if (effectwidget) {
187         effectcontrol_vbox.remove(*effectwidget);
188         delete effectwidget;
189         effectwidget = NULL;
190     }
192     explain_label.set_label(str);
193     //button_remove.hide();
195     // fixme: do resizing of dialog ?
198 void
199 LivePathEffectEditor::set_sensitize_all(bool sensitive)
201     combo_effecttype.set_sensitive(sensitive);
202     button_apply.set_sensitive(sensitive);
203     button_remove.set_sensitive(sensitive);
204     effectlist_view.set_sensitive(sensitive);
205     button_up.set_sensitive(sensitive);
206     button_down.set_sensitive(sensitive);
210 void
211 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
213     effectlist_store->clear();
214     current_lpeitem = NULL;
216     if ( sel && !sel->isEmpty() ) {
217         SPItem *item = sel->singleItem();
218         if ( item ) {
219             if ( SP_IS_LPE_ITEM(item) ) {
220                 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
222                 effect_list_update(lpeitem);
224                 current_lpeitem = lpeitem;
225                 
226                 set_sensitize_all(true);
227                 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
228                     Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
229                     if (lpe) {
230                         showParams(lpe);
231                     } else {
232                         showText(_("Unknown effect is applied"));
233                     }
234                 } else {
235                     showText(_("No effect applied"));
236                     button_remove.set_sensitive(false);
237                 }
238             }
239               else
240             {
241                 showText(_("Item is not compound by paths"));
242                 set_sensitize_all(false);
243             }
244         } else {
245             showText(_("Only one item can be selected"));
246             set_sensitize_all(false);
247         }
248     } else {
249         showText(_("Empty selection"));
250         set_sensitize_all(false);
251     }
254 void
255 LivePathEffectEditor::effect_list_update(SPLPEItem *lpeitem)
257     effectlist_store->clear();
258     
259     PathEffectList effectlist = sp_lpe_item_get_effect_list(lpeitem);
260     PathEffectList::iterator it;
261     for( it = effectlist.begin() ; it!=effectlist.end(); it++ )
262     {
263          Gtk::TreeModel::Row row = *(effectlist_store->append());
264          row[columns.col_name] = (*it)->lpeobject->lpe->getName();
265          row[columns.lperef] = *it;
266     }
270 void 
271 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
273     Panel::setDesktop(desktop);
275     if ( desktop == current_desktop ) {
276         return;
277     }
279     if (current_desktop) {
280         selection_changed_connection.disconnect();
281     }
283     current_desktop = desktop;
284     if (desktop) {
285         Inkscape::Selection *selection = sp_desktop_selection(desktop);
286         selection_changed_connection = selection->connectChanged(
287             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
289         onSelectionChanged(selection);
290     } else {
291         onSelectionChanged(NULL);
292     }
298 /*########################################################################
299 # BUTTON CLICK HANDLERS    (callbacks)
300 ########################################################################*/
302 // TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
304 void
305 LivePathEffectEditor::onApply()
307     Inkscape::Selection *sel = _getSelection();
308     if ( sel && !sel->isEmpty() ) {
309         SPItem *item = sel->singleItem();
310         if ( item && SP_IS_LPE_ITEM(item) ) {
311             SPDocument *doc = current_desktop->doc();
313             const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
314             if (!data) return;
316             // If item is a SPRect, convert it to path first:
317             if ( SP_IS_RECT(item) ) {
318                 sp_selected_path_to_curves(false);
319                 item = sel->singleItem(); // get new item
320             }
322             LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
324             onSelectionChanged(sel);
325         }
326     }
329 void
330 LivePathEffectEditor::onRemove()
332     Inkscape::Selection *sel = _getSelection();
333     if ( sel && !sel->isEmpty() ) {
334         SPItem *item = sel->singleItem();
335         if ( item && SP_IS_LPE_ITEM(item) ) {
336             sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
337             
338             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
339                                _("Remove path effect") );
341             effect_list_update(SP_LPE_ITEM(item));
342         }
343     }
346 void LivePathEffectEditor::onUp()
348         Inkscape::Selection *sel = _getSelection();
349     if ( sel && !sel->isEmpty() ) {
350         SPItem *item = sel->singleItem();
351         if ( item && SP_IS_LPE_ITEM(item) ) {
352             
353                         sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
354             
355             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
356                                _("Remove path effect") );
358             effect_list_update(SP_LPE_ITEM(item));
359         }
360     }
361         
364 void LivePathEffectEditor::onDown()
366         Inkscape::Selection *sel = _getSelection();
367     if ( sel && !sel->isEmpty() ) {
368         SPItem *item = sel->singleItem();
369         if ( item && SP_IS_LPE_ITEM(item) ) {
370             
371                         sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
372             
373             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT, 
374                                _("Remove path effect") );
376             effect_list_update(SP_LPE_ITEM(item));
377         }
378     }
381 void LivePathEffectEditor::on_effect_selection_changed()
383     Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
384     if (sel->count_selected_rows () == 0)
385         return;
387     Gtk::TreeModel::iterator it = sel->get_selected();
388     LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
390     if (lperef && current_lpeitem) {
391         sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
392         showParams(lperef->lpeobject->lpe);
393     }
397 } // namespace Dialog
398 } // namespace UI
399 } // namespace Inkscape
401 /*
402   Local Variables:
403   mode:c++
404   c-file-style:"stroustrup"
405   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
406   indent-tabs-mode:nil
407   fill-column:99
408   End:
409 */
410 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :