Code

Merging from trunk
[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 "ui/widget/imagetoggler.h"
21 #include "verbs.h"
22 #include "selection.h"
23 #include "sp-shape.h"
24 #include "sp-item-group.h"
25 #include "sp-path.h"
26 #include "sp-rect.h"
27 #include "sp-lpe-item.h"
28 #include "path-chemistry.h"
29 #include "live_effects/effect.h"
30 #include "live_effects/lpeobject.h"
31 #include "gtkmm/widget.h"
32 #include <vector>
33 #include "inkscape.h"
34 #include "desktop-handles.h"
35 #include "desktop.h"
36 #include "document.h"
37 #include "xml/node.h"
38 #include <gtkmm/stock.h>
39 #include <gtkmm/toolbar.h>
41 #include "live_effects/lpeobject-reference.h"
43 namespace Inkscape {
44 class Application;
46 namespace UI {
47 namespace Dialog {
50 /*####################
51  * Callback functions
52  */
53 static void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
54 {
55     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
56     lpeeditor->onSelectionChanged(selection);
57 }
59 static void lpeeditor_selection_modified (Inkscape::Selection * selection, guint /*flags*/, gpointer data)
60 {
61     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
62     lpeeditor->onSelectionChanged(selection);
63 }
66 /*#######################
67  * LivePathEffectEditor
68  */
70 LivePathEffectEditor::LivePathEffectEditor()
71     : UI::Widget::Panel("", "dialogs.livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
72       lpe_list_locked(false),
73       combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
74       effectwidget(NULL),
75       explain_label("", Gtk::ALIGN_CENTER),
76       effectapplication_frame(_("Apply new effect")),
77       effectcontrol_frame(_("Current effect")),
78       effectlist_frame(_("Effect list")),
79       button_up(Gtk::Stock::GO_UP),
80       button_down(Gtk::Stock::GO_DOWN),
81       button_apply(Gtk::Stock::ADD),
82       button_remove(Gtk::Stock::REMOVE),
83       current_desktop(NULL),
84       current_lpeitem(NULL)
85 {
86     Gtk::Box *contents = _getContents();
87     contents->set_spacing(4);
89     //Add the TreeView, inside a ScrolledWindow, with the button underneath:
90     scrolled_window.add(effectlist_view);
91     //Only show the scrollbars when they are necessary:
92     scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
94     effectapplication_hbox.set_spacing(4);
95     effectcontrol_vbox.set_spacing(4);
96     effectlist_vbox.set_spacing(4);
98     effectapplication_hbox.pack_start(combo_effecttype, true, true);
99     effectapplication_hbox.pack_start(button_apply, true, true);
100     effectapplication_frame.add(effectapplication_hbox);
102     effectlist_vbox.pack_start(scrolled_window, Gtk::PACK_EXPAND_WIDGET);
103     effectlist_vbox.pack_end(toolbar, Gtk::PACK_SHRINK);
104    // effectlist_vbox.pack_end(button_hbox, Gtk::PACK_SHRINK);
105     effectlist_frame.add(effectlist_vbox);
107     effectcontrol_vbox.pack_start(explain_label, true, true);
108     effectcontrol_frame.add(effectcontrol_vbox);
110  //   button_hbox.pack_start(button_up, true, true);
111  //   button_hbox.pack_start(button_down, true, true);
112  //   button_hbox.pack_end(button_remove, true, true);
113     toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);
114  // Add toolbar items to toolbar
115     toolbar.append(button_up);
116     toolbar.append(button_down);
117     toolbar.append(button_remove);
120     // Add toolbar
121     //add_toolbar(toolbar);
122     toolbar.show_all(); //Show the toolbar and all its child widgets.
125     //Create the Tree model:
126     effectlist_store = Gtk::ListStore::create(columns);
127     effectlist_view.set_model(effectlist_store);
129     effectlist_view.set_headers_visible(false);
131     // Handle tree selections
132     effectlist_selection = effectlist_view.get_selection();
133     effectlist_selection->signal_changed().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_effect_selection_changed) );
135     //Add the visibility icon column:
136     Inkscape::UI::Widget::ImageToggler *eyeRenderer = manage( new Inkscape::UI::Widget::ImageToggler("visible", "hidden") );
137     int visibleColNum = effectlist_view.append_column("is_visible", *eyeRenderer) - 1;
138     eyeRenderer->signal_toggled().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_visibility_toggled) );
139     eyeRenderer->property_activatable() = true;
140     Gtk::TreeViewColumn* col = effectlist_view.get_column(visibleColNum);
141     if ( col ) {
142         col->add_attribute( eyeRenderer->property_active(), columns.col_visible );
143     }
145     //Add the effect name column:
146     effectlist_view.append_column("Effect", columns.col_name);
148     contents->pack_start(effectapplication_frame, false, false);
149     contents->pack_start(effectlist_frame, true, true);
150     contents->pack_start(effectcontrol_frame, false, false);
152     // connect callback functions to buttons
153     button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
154     button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
156     button_up.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onUp));
157     button_down.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onDown));
159     show_all_children();
161     //button_remove.hide();
164 LivePathEffectEditor::~LivePathEffectEditor()
166     if (effectwidget) {
167         effectcontrol_vbox.remove(*effectwidget);
168         delete effectwidget;
169         effectwidget = NULL;
170     }
172     if (current_desktop) {
173         selection_changed_connection.disconnect();
174         selection_modified_connection.disconnect();
175     }
178 void
179 LivePathEffectEditor::showParams(LivePathEffect::Effect& effect)
181     if (effectwidget) {
182         effectcontrol_vbox.remove(*effectwidget);
183         delete effectwidget;
184         effectwidget = NULL;
185     }
187     explain_label.set_markup("<b>" + effect.getName() + "</b>");
188     effectwidget = effect.newWidget(&tooltips);
189     if (effectwidget) {
190         effectcontrol_vbox.pack_start(*effectwidget, true, true);
191     }
192     button_remove.show();
194     effectcontrol_vbox.show_all_children();
195     // fixme: add resizing of dialog
198 void
199 LivePathEffectEditor::selectInList(LivePathEffect::Effect* effect)
201     Gtk::TreeNodeChildren chi = effectlist_view.get_model()->children();
202     for (Gtk::TreeIter ci = chi.begin() ; ci != chi.end(); ci++) {
203         if (ci->get_value(columns.lperef)->lpeobject->get_lpe() == effect)
204             effectlist_view.get_selection()->select(ci);
205     }
209 void
210 LivePathEffectEditor::showText(Glib::ustring const &str)
212     if (effectwidget) {
213         effectcontrol_vbox.remove(*effectwidget);
214         delete effectwidget;
215         effectwidget = NULL;
216     }
218     explain_label.set_label(str);
219     //button_remove.hide();
221     // fixme: do resizing of dialog ?
224 void
225 LivePathEffectEditor::set_sensitize_all(bool sensitive)
227     combo_effecttype.set_sensitive(sensitive);
228     button_apply.set_sensitive(sensitive);
229     button_remove.set_sensitive(sensitive);
230     effectlist_view.set_sensitive(sensitive);
231     button_up.set_sensitive(sensitive);
232     button_down.set_sensitive(sensitive);
236 void
237 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
239     if (lpe_list_locked) {
240         // this was triggered by selecting a row in the list, so skip reloading
241         lpe_list_locked = false;
242         return;
243     } 
245     effectlist_store->clear();
246     current_lpeitem = NULL;
248     if ( sel && !sel->isEmpty() ) {
249         SPItem *item = sel->singleItem();
250         if ( item ) {
251             if ( SP_IS_LPE_ITEM(item) ) {
252                 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
254                 effect_list_reload(lpeitem);
256                 current_lpeitem = lpeitem;
258                 set_sensitize_all(true);
259                 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
260                     Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
261                     if (lpe) {
262                         showParams(*lpe);
263                         lpe_list_locked = true; 
264                         selectInList(lpe);
265                     } else {
266                         showText(_("Unknown effect is applied"));
267                     }
268                 } else {
269                     showText(_("No effect applied"));
270                     button_remove.set_sensitive(false);
271                 }
272             } else {
273                 showText(_("Item is not a path or shape"));
274                 set_sensitize_all(false);
275             }
276         } else {
277             showText(_("Only one item can be selected"));
278             set_sensitize_all(false);
279         }
280     } else {
281         showText(_("Empty selection"));
282         set_sensitize_all(false);
283     }
286 /*
287  * First clears the effectlist_store, then appends all effects from the effectlist.
288  */
289 void
290 LivePathEffectEditor::effect_list_reload(SPLPEItem *lpeitem)
292     effectlist_store->clear();
294     PathEffectList effectlist = sp_lpe_item_get_effect_list(lpeitem);
295     PathEffectList::iterator it;
296     for( it = effectlist.begin() ; it!=effectlist.end(); it++ )
297     {
298         if ((*it)->lpeobject->get_lpe()) {
299             Gtk::TreeModel::Row row = *(effectlist_store->append());
300             row[columns.col_name] = (*it)->lpeobject->get_lpe()->getName();
301             row[columns.lperef] = *it;
302             row[columns.col_visible] = (*it)->lpeobject->get_lpe()->isVisible();
303         } else {
304             Gtk::TreeModel::Row row = *(effectlist_store->append());
305             row[columns.col_name] = "Unknown effect!";
306             row[columns.lperef] = *it;
307             row[columns.col_visible] = false;
308         }
309     }
313 void
314 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
316     Panel::setDesktop(desktop);
318     if ( desktop == current_desktop ) {
319         return;
320     }
322     if (current_desktop) {
323         selection_changed_connection.disconnect();
324         selection_modified_connection.disconnect();
325     }
327     current_desktop = desktop;
328     if (desktop) {
329         Inkscape::Selection *selection = sp_desktop_selection(desktop);
330         selection_changed_connection = selection->connectChanged(
331             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
332         selection_modified_connection = selection->connectModified(
333             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
335         onSelectionChanged(selection);
336     } else {
337         onSelectionChanged(NULL);
338     }
344 /*########################################################################
345 # BUTTON CLICK HANDLERS    (callbacks)
346 ########################################################################*/
348 // TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
350 void
351 LivePathEffectEditor::onApply()
353     Inkscape::Selection *sel = _getSelection();
354     if ( sel && !sel->isEmpty() ) {
355         SPItem *item = sel->singleItem();
356         if ( item && SP_IS_LPE_ITEM(item) ) {
357             SPDocument *doc = current_desktop->doc();
359             const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
360             if (!data) return;
362             // If item is a SPRect, convert it to path first:
363             if ( SP_IS_RECT(item) ) {
364                 sp_selected_path_to_curves(current_desktop, false);
365                 item = sel->singleItem(); // get new item
366             }
368             LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
370             sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
371                      _("Create and apply path effect"));
373             onSelectionChanged(sel);
374         }
375     }
378 void
379 LivePathEffectEditor::onRemove()
381     Inkscape::Selection *sel = _getSelection();
382     if ( sel && !sel->isEmpty() ) {
383         SPItem *item = sel->singleItem();
384         if ( item && SP_IS_LPE_ITEM(item) ) {
385             sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
387             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
388                                _("Remove path effect") );
390             effect_list_reload(SP_LPE_ITEM(item));
391         }
392     }
395 void LivePathEffectEditor::onUp()
397     Inkscape::Selection *sel = _getSelection();
398     if ( sel && !sel->isEmpty() ) {
399         SPItem *item = sel->singleItem();
400         if ( item && SP_IS_LPE_ITEM(item) ) {
401             sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
403             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
404                                _("Move path effect up") );
406             effect_list_reload(SP_LPE_ITEM(item));
407         }
408     }
411 void LivePathEffectEditor::onDown()
413     Inkscape::Selection *sel = _getSelection();
414     if ( sel && !sel->isEmpty() ) {
415         SPItem *item = sel->singleItem();
416         if ( item && SP_IS_LPE_ITEM(item) ) {
417             sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
419             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
420                                _("Move path effect down") );
422             effect_list_reload(SP_LPE_ITEM(item));
423         }
424     }
427 void LivePathEffectEditor::on_effect_selection_changed()
429     Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
430     if (sel->count_selected_rows () == 0)
431         return;
433     Gtk::TreeModel::iterator it = sel->get_selected();
434     LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
436     if (lperef && current_lpeitem) {
437         if (lperef->lpeobject->get_lpe()) {
438             lpe_list_locked = true; // prevent reload of the list which would lose selection
439             sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
440             showParams(*lperef->lpeobject->get_lpe());
441         }
442     }
445 void LivePathEffectEditor::on_visibility_toggled( Glib::ustring const& str )
447     Gtk::TreeModel::Children::iterator iter = effectlist_view.get_model()->get_iter(str);
448     Gtk::TreeModel::Row row = *iter;
450     LivePathEffect::LPEObjectReference * lpeobjref = row[columns.lperef];
452     if ( lpeobjref && lpeobjref->lpeobject->get_lpe() ) {
453         bool newValue = !row[columns.col_visible];
454         row[columns.col_visible] = newValue;
455         /* FIXME: this explicit writing to SVG is wrong. The lpe_item should have a method to disable/enable an effect within its stack.
456          * So one can call:  lpe_item->setActive(lpeobjref->lpeobject); */
457         lpeobjref->lpeobject->get_lpe()->getRepr()->setAttribute("is_visible", newValue ? "true" : "false");
458         sp_document_done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
459                           newValue ? _("Activate path effect") : _("Deactivate path effect"));
460     }
463 } // namespace Dialog
464 } // namespace UI
465 } // namespace Inkscape
467 /*
468   Local Variables:
469   mode:c++
470   c-file-style:"stroustrup"
471   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
472   indent-tabs-mode:nil
473   fill-column:99
474   End:
475 */
476 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :