Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / ui / dialog / livepatheffect-editor.cpp
1 /** @file
2  * @brief Live Path Effect editing dialog - implementation
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  *   Abhishek Sharma
9  *
10  * Copyright (C) 2007 Authors
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 <gtkmm/stock.h>
20 #include <gtkmm/toolbar.h>
21 #include <vector>
23 #include "desktop.h"
24 #include "desktop-handles.h"
25 #include "document.h"
26 #include "gtkmm/widget.h"
27 #include "inkscape.h"
28 #include "live_effects/effect.h"
29 #include "live_effects/lpeobject.h"
30 #include "live_effects/lpeobject-reference.h"
31 #include "path-chemistry.h"
32 #include "selection.h"
33 #include "sp-item-group.h"
34 #include "sp-lpe-item.h"
35 #include "sp-path.h"
36 #include "sp-rect.h"
37 #include "sp-shape.h"
38 #include "ui/icon-names.h"
39 #include "ui/widget/imagetoggler.h"
40 #include "verbs.h"
41 #include "xml/node.h"
43 #include "livepatheffect-editor.h"
45 namespace Inkscape {
46 class Application;
48 namespace UI {
49 namespace Dialog {
52 /*####################
53  * Callback functions
54  */
55 void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
56 {
57     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
58     lpeeditor->lpe_list_locked = false;
59     lpeeditor->onSelectionChanged(selection);
60 }
62 static void lpeeditor_selection_modified (Inkscape::Selection * selection, guint /*flags*/, gpointer data)
63 {
64     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
65     lpeeditor->onSelectionChanged(selection);
66 }
69 /*#######################
70  * LivePathEffectEditor
71  */
73 LivePathEffectEditor::LivePathEffectEditor()
74     : UI::Widget::Panel("", "/dialogs/livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
75       lpe_list_locked(false),
76       combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
77       effectwidget(NULL),
78       explain_label("", Gtk::ALIGN_CENTER),
79       // TRANSLATORS: this dialog is accessible via menu Path - Path Effect Editor...
80       effectapplication_frame(_("Apply new effect")),
81       effectcontrol_frame(_("Current effect")),
82       effectlist_frame(_("Effect list")),
83       button_up(Gtk::Stock::GO_UP),
84       button_down(Gtk::Stock::GO_DOWN),
85       button_apply(Gtk::Stock::ADD),
86       button_remove(Gtk::Stock::REMOVE),
87       current_desktop(NULL),
88       current_lpeitem(NULL)
89 {
90     Gtk::Box *contents = _getContents();
91     contents->set_spacing(4);
93     //Add the TreeView, inside a ScrolledWindow, with the button underneath:
94     scrolled_window.add(effectlist_view);
95     //Only show the scrollbars when they are necessary:
96     scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
98     effectapplication_hbox.set_spacing(4);
99     effectcontrol_vbox.set_spacing(4);
100     effectlist_vbox.set_spacing(4);
102     effectapplication_hbox.pack_start(combo_effecttype, true, true);
103     effectapplication_hbox.pack_start(button_apply, true, true);
104     effectapplication_frame.add(effectapplication_hbox);
106     effectlist_vbox.pack_start(scrolled_window, Gtk::PACK_EXPAND_WIDGET);
107     effectlist_vbox.pack_end(toolbar, Gtk::PACK_SHRINK);
108    // effectlist_vbox.pack_end(button_hbox, Gtk::PACK_SHRINK);
109     effectlist_frame.add(effectlist_vbox);
111     effectcontrol_vbox.pack_start(explain_label, true, true);
112     effectcontrol_frame.add(effectcontrol_vbox);
114  //   button_hbox.pack_start(button_up, true, true);
115  //   button_hbox.pack_start(button_down, true, true);
116  //   button_hbox.pack_end(button_remove, true, true);
117     toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);
118  // Add toolbar items to toolbar
119     toolbar.append(button_up);
120     toolbar.append(button_down);
121     toolbar.append(button_remove);
124     // Add toolbar
125     //add_toolbar(toolbar);
126     toolbar.show_all(); //Show the toolbar and all its child widgets.
129     //Create the Tree model:
130     effectlist_store = Gtk::ListStore::create(columns);
131     effectlist_view.set_model(effectlist_store);
133     effectlist_view.set_headers_visible(false);
135     // Handle tree selections
136     effectlist_selection = effectlist_view.get_selection();
137     effectlist_selection->signal_changed().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_effect_selection_changed) );
139     //Add the visibility icon column:
140     Inkscape::UI::Widget::ImageToggler *eyeRenderer = manage( new Inkscape::UI::Widget::ImageToggler(
141         INKSCAPE_ICON_OBJECT_VISIBLE, INKSCAPE_ICON_OBJECT_HIDDEN) );
142     int visibleColNum = effectlist_view.append_column("is_visible", *eyeRenderer) - 1;
143     eyeRenderer->signal_toggled().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_visibility_toggled) );
144     eyeRenderer->property_activatable() = true;
145     Gtk::TreeViewColumn* col = effectlist_view.get_column(visibleColNum);
146     if ( col ) {
147         col->add_attribute( eyeRenderer->property_active(), columns.col_visible );
148     }
150     //Add the effect name column:
151     effectlist_view.append_column("Effect", columns.col_name);
153     contents->pack_start(effectapplication_frame, false, false);
154     contents->pack_start(effectlist_frame, true, true);
155     contents->pack_start(effectcontrol_frame, false, false);
157     // connect callback functions to buttons
158     button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
159     button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
161     button_up.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onUp));
162     button_down.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onDown));
164     show_all_children();
166     //button_remove.hide();
169 LivePathEffectEditor::~LivePathEffectEditor()
171     if (effectwidget) {
172         effectcontrol_vbox.remove(*effectwidget);
173         delete effectwidget;
174         effectwidget = NULL;
175     }
177     if (current_desktop) {
178         selection_changed_connection.disconnect();
179         selection_modified_connection.disconnect();
180     }
183 void
184 LivePathEffectEditor::showParams(LivePathEffect::Effect& effect)
186     if (effectwidget) {
187         effectcontrol_vbox.remove(*effectwidget);
188         delete effectwidget;
189         effectwidget = NULL;
190     }
192     explain_label.set_markup("<b>" + effect.getName() + "</b>");
193     effectwidget = effect.newWidget(&tooltips);
194     if (effectwidget) {
195         effectcontrol_vbox.pack_start(*effectwidget, true, true);
196     }
197     button_remove.show();
199     effectcontrol_vbox.show_all_children();
200     // fixme: add resizing of dialog
203 void
204 LivePathEffectEditor::selectInList(LivePathEffect::Effect* effect)
206     Gtk::TreeNodeChildren chi = effectlist_view.get_model()->children();
207     for (Gtk::TreeIter ci = chi.begin() ; ci != chi.end(); ci++) {
208         if (ci->get_value(columns.lperef)->lpeobject->get_lpe() == effect)
209             effectlist_view.get_selection()->select(ci);
210     }
214 void
215 LivePathEffectEditor::showText(Glib::ustring const &str)
217     if (effectwidget) {
218         effectcontrol_vbox.remove(*effectwidget);
219         delete effectwidget;
220         effectwidget = NULL;
221     }
223     explain_label.set_label(str);
224     //button_remove.hide();
226     // fixme: do resizing of dialog ?
229 void
230 LivePathEffectEditor::set_sensitize_all(bool sensitive)
232     combo_effecttype.set_sensitive(sensitive);
233     button_apply.set_sensitive(sensitive);
234     button_remove.set_sensitive(sensitive);
235     effectlist_view.set_sensitive(sensitive);
236     button_up.set_sensitive(sensitive);
237     button_down.set_sensitive(sensitive);
241 void
242 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
244     if (lpe_list_locked) {
245         // this was triggered by selecting a row in the list, so skip reloading
246         lpe_list_locked = false;
247         return;
248     } 
250     effectlist_store->clear();
251     current_lpeitem = NULL;
253     if ( sel && !sel->isEmpty() ) {
254         SPItem *item = sel->singleItem();
255         if ( item ) {
256             if ( SP_IS_LPE_ITEM(item) ) {
257                 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
259                 effect_list_reload(lpeitem);
261                 current_lpeitem = lpeitem;
263                 set_sensitize_all(true);
264                 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
265                     Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
266                     if (lpe) {
267                         showParams(*lpe);
268                         lpe_list_locked = true; 
269                         selectInList(lpe);
270                     } else {
271                         showText(_("Unknown effect is applied"));
272                     }
273                 } else {
274                     showText(_("No effect applied"));
275                     button_remove.set_sensitive(false);
276                 }
277             } else {
278                 showText(_("Item is not a path or shape"));
279                 set_sensitize_all(false);
280             }
281         } else {
282             showText(_("Only one item can be selected"));
283             set_sensitize_all(false);
284         }
285     } else {
286         showText(_("Empty selection"));
287         set_sensitize_all(false);
288     }
291 /*
292  * First clears the effectlist_store, then appends all effects from the effectlist.
293  */
294 void
295 LivePathEffectEditor::effect_list_reload(SPLPEItem *lpeitem)
297     effectlist_store->clear();
299     PathEffectList effectlist = sp_lpe_item_get_effect_list(lpeitem);
300     PathEffectList::iterator it;
301     for( it = effectlist.begin() ; it!=effectlist.end(); it++ )
302     {
303         if ( !(*it)->lpeobject ) {
304             continue;
305         }
307         if ((*it)->lpeobject->get_lpe()) {
308             Gtk::TreeModel::Row row = *(effectlist_store->append());
309             row[columns.col_name] = (*it)->lpeobject->get_lpe()->getName();
310             row[columns.lperef] = *it;
311             row[columns.col_visible] = (*it)->lpeobject->get_lpe()->isVisible();
312         } else {
313             Gtk::TreeModel::Row row = *(effectlist_store->append());
314             row[columns.col_name] = _("Unknown effect");
315             row[columns.lperef] = *it;
316             row[columns.col_visible] = false;
317         }
318     }
322 void
323 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
325     Panel::setDesktop(desktop);
327     if ( desktop == current_desktop ) {
328         return;
329     }
331     if (current_desktop) {
332         selection_changed_connection.disconnect();
333         selection_modified_connection.disconnect();
334     }
336     lpe_list_locked = false;
337     current_desktop = desktop;
338     if (desktop) {
339         Inkscape::Selection *selection = sp_desktop_selection(desktop);
340         selection_changed_connection = selection->connectChanged(
341             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
342         selection_modified_connection = selection->connectModified(
343             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
345         onSelectionChanged(selection);
346     } else {
347         onSelectionChanged(NULL);
348     }
354 /*########################################################################
355 # BUTTON CLICK HANDLERS    (callbacks)
356 ########################################################################*/
358 // TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
360 void
361 LivePathEffectEditor::onApply()
363     Inkscape::Selection *sel = _getSelection();
364     if ( sel && !sel->isEmpty() ) {
365         SPItem *item = sel->singleItem();
366         if ( item && SP_IS_LPE_ITEM(item) ) {
367             SPDocument *doc = current_desktop->doc();
369             const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
370             if (!data) return;
372             // If item is a SPRect, convert it to path first:
373             if ( SP_IS_RECT(item) ) {
374                 sp_selected_path_to_curves(current_desktop, false);
375                 item = sel->singleItem(); // get new item
376             }
378             LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
380             DocumentUndo::done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
381                                _("Create and apply path effect"));
383             lpe_list_locked = false;
384             onSelectionChanged(sel);
385         }
386     }
389 void
390 LivePathEffectEditor::onRemove()
392     Inkscape::Selection *sel = _getSelection();
393     if ( sel && !sel->isEmpty() ) {
394         SPItem *item = sel->singleItem();
395         if ( item && SP_IS_LPE_ITEM(item) ) {
396             sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
398             DocumentUndo::done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
399                                 _("Remove path effect") );
401             effect_list_reload(SP_LPE_ITEM(item));
402         }
403     }
406 void LivePathEffectEditor::onUp()
408     Inkscape::Selection *sel = _getSelection();
409     if ( sel && !sel->isEmpty() ) {
410         SPItem *item = sel->singleItem();
411         if ( item && SP_IS_LPE_ITEM(item) ) {
412             sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
414             DocumentUndo::done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
415                                 _("Move path effect up") );
417             effect_list_reload(SP_LPE_ITEM(item));
418         }
419     }
422 void LivePathEffectEditor::onDown()
424     Inkscape::Selection *sel = _getSelection();
425     if ( sel && !sel->isEmpty() ) {
426         SPItem *item = sel->singleItem();
427         if ( item && SP_IS_LPE_ITEM(item) ) {
428             sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
430             DocumentUndo::done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
431                                 _("Move path effect down") );
433             effect_list_reload(SP_LPE_ITEM(item));
434         }
435     }
438 void LivePathEffectEditor::on_effect_selection_changed()
440     Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
441     if (sel->count_selected_rows () == 0)
442         return;
444     Gtk::TreeModel::iterator it = sel->get_selected();
445     LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
447     if (lperef && current_lpeitem) {
448         if (lperef->lpeobject->get_lpe()) {
449             lpe_list_locked = true; // prevent reload of the list which would lose selection
450             sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
451             showParams(*lperef->lpeobject->get_lpe());
452         }
453     }
456 void LivePathEffectEditor::on_visibility_toggled( Glib::ustring const& str )
458     Gtk::TreeModel::Children::iterator iter = effectlist_view.get_model()->get_iter(str);
459     Gtk::TreeModel::Row row = *iter;
461     LivePathEffect::LPEObjectReference * lpeobjref = row[columns.lperef];
463     if ( lpeobjref && lpeobjref->lpeobject->get_lpe() ) {
464         bool newValue = !row[columns.col_visible];
465         row[columns.col_visible] = newValue;
466         /* FIXME: this explicit writing to SVG is wrong. The lpe_item should have a method to disable/enable an effect within its stack.
467          * So one can call:  lpe_item->setActive(lpeobjref->lpeobject); */
468         lpeobjref->lpeobject->get_lpe()->getRepr()->setAttribute("is_visible", newValue ? "true" : "false");
469         DocumentUndo::done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
470                             newValue ? _("Activate path effect") : _("Deactivate path effect"));
471     }
474 } // namespace Dialog
475 } // namespace UI
476 } // namespace Inkscape
478 /*
479   Local Variables:
480   mode:c++
481   c-file-style:"stroustrup"
482   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
483   indent-tabs-mode:nil
484   fill-column:99
485   End:
486 */
487 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :