Code

8a7e0adebdd571f9a31df11afbaee0ab0dcedf7c
[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  *
9  * Copyright (C) 2007 Authors
10  * Released under GNU GPL.  Read the file 'COPYING' for more information.
11  */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <glibmm/i18n.h>
18 #include "livepatheffect-editor.h"
19 #include "ui/widget/imagetoggler.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 }
58 static void lpeeditor_selection_modified (Inkscape::Selection * selection, guint /*flags*/, gpointer data)
59 {
60     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
61     lpeeditor->onSelectionChanged(selection);
62 }
65 /*#######################
66  * LivePathEffectEditor
67  */
69 LivePathEffectEditor::LivePathEffectEditor()
70     : UI::Widget::Panel("", "/dialogs/livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
71       lpe_list_locked(false),
72       combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
73       effectwidget(NULL),
74       explain_label("", Gtk::ALIGN_CENTER),
75       effectapplication_frame(_("Apply new effect")),
76       effectcontrol_frame(_("Current effect")),
77       effectlist_frame(_("Effect list")),
78       button_up(Gtk::Stock::GO_UP),
79       button_down(Gtk::Stock::GO_DOWN),
80       button_apply(Gtk::Stock::ADD),
81       button_remove(Gtk::Stock::REMOVE),
82       current_desktop(NULL),
83       current_lpeitem(NULL)
84 {
85     Gtk::Box *contents = _getContents();
86     contents->set_spacing(4);
88     //Add the TreeView, inside a ScrolledWindow, with the button underneath:
89     scrolled_window.add(effectlist_view);
90     //Only show the scrollbars when they are necessary:
91     scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
93     effectapplication_hbox.set_spacing(4);
94     effectcontrol_vbox.set_spacing(4);
95     effectlist_vbox.set_spacing(4);
97     effectapplication_hbox.pack_start(combo_effecttype, true, true);
98     effectapplication_hbox.pack_start(button_apply, true, true);
99     effectapplication_frame.add(effectapplication_hbox);
101     effectlist_vbox.pack_start(scrolled_window, Gtk::PACK_EXPAND_WIDGET);
102     effectlist_vbox.pack_end(toolbar, Gtk::PACK_SHRINK);
103    // effectlist_vbox.pack_end(button_hbox, Gtk::PACK_SHRINK);
104     effectlist_frame.add(effectlist_vbox);
106     effectcontrol_vbox.pack_start(explain_label, true, true);
107     effectcontrol_frame.add(effectcontrol_vbox);
109  //   button_hbox.pack_start(button_up, true, true);
110  //   button_hbox.pack_start(button_down, true, true);
111  //   button_hbox.pack_end(button_remove, true, true);
112     toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);
113  // Add toolbar items to toolbar
114     toolbar.append(button_up);
115     toolbar.append(button_down);
116     toolbar.append(button_remove);
119     // Add toolbar
120     //add_toolbar(toolbar);
121     toolbar.show_all(); //Show the toolbar and all its child widgets.
124     //Create the Tree model:
125     effectlist_store = Gtk::ListStore::create(columns);
126     effectlist_view.set_model(effectlist_store);
128     effectlist_view.set_headers_visible(false);
130     // Handle tree selections
131     effectlist_selection = effectlist_view.get_selection();
132     effectlist_selection->signal_changed().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_effect_selection_changed) );
134     //Add the visibility icon column:
135     Inkscape::UI::Widget::ImageToggler *eyeRenderer = manage( new Inkscape::UI::Widget::ImageToggler("visible", "hidden") );
136     int visibleColNum = effectlist_view.append_column("is_visible", *eyeRenderer) - 1;
137     eyeRenderer->signal_toggled().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_visibility_toggled) );
138     eyeRenderer->property_activatable() = true;
139     Gtk::TreeViewColumn* col = effectlist_view.get_column(visibleColNum);
140     if ( col ) {
141         col->add_attribute( eyeRenderer->property_active(), columns.col_visible );
142     }
144     //Add the effect name column:
145     effectlist_view.append_column("Effect", columns.col_name);
147     contents->pack_start(effectapplication_frame, false, false);
148     contents->pack_start(effectlist_frame, true, true);
149     contents->pack_start(effectcontrol_frame, false, false);
151     // connect callback functions to buttons
152     button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
153     button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
155     button_up.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onUp));
156     button_down.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onDown));
158     show_all_children();
160     //button_remove.hide();
163 LivePathEffectEditor::~LivePathEffectEditor()
165     if (effectwidget) {
166         effectcontrol_vbox.remove(*effectwidget);
167         delete effectwidget;
168         effectwidget = NULL;
169     }
171     if (current_desktop) {
172         selection_changed_connection.disconnect();
173         selection_modified_connection.disconnect();
174     }
177 void
178 LivePathEffectEditor::showParams(LivePathEffect::Effect& effect)
180     if (effectwidget) {
181         effectcontrol_vbox.remove(*effectwidget);
182         delete effectwidget;
183         effectwidget = NULL;
184     }
186     explain_label.set_markup("<b>" + effect.getName() + "</b>");
187     effectwidget = effect.newWidget(&tooltips);
188     if (effectwidget) {
189         effectcontrol_vbox.pack_start(*effectwidget, true, true);
190     }
191     button_remove.show();
193     effectcontrol_vbox.show_all_children();
194     // fixme: add resizing of dialog
197 void
198 LivePathEffectEditor::selectInList(LivePathEffect::Effect* effect)
200     Gtk::TreeNodeChildren chi = effectlist_view.get_model()->children();
201     for (Gtk::TreeIter ci = chi.begin() ; ci != chi.end(); ci++) {
202         if (ci->get_value(columns.lperef)->lpeobject->get_lpe() == effect)
203             effectlist_view.get_selection()->select(ci);
204     }
208 void
209 LivePathEffectEditor::showText(Glib::ustring const &str)
211     if (effectwidget) {
212         effectcontrol_vbox.remove(*effectwidget);
213         delete effectwidget;
214         effectwidget = NULL;
215     }
217     explain_label.set_label(str);
218     //button_remove.hide();
220     // fixme: do resizing of dialog ?
223 void
224 LivePathEffectEditor::set_sensitize_all(bool sensitive)
226     combo_effecttype.set_sensitive(sensitive);
227     button_apply.set_sensitive(sensitive);
228     button_remove.set_sensitive(sensitive);
229     effectlist_view.set_sensitive(sensitive);
230     button_up.set_sensitive(sensitive);
231     button_down.set_sensitive(sensitive);
235 void
236 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
238     if (lpe_list_locked) {
239         // this was triggered by selecting a row in the list, so skip reloading
240         lpe_list_locked = false;
241         return;
242     } 
244     effectlist_store->clear();
245     current_lpeitem = NULL;
247     if ( sel && !sel->isEmpty() ) {
248         SPItem *item = sel->singleItem();
249         if ( item ) {
250             if ( SP_IS_LPE_ITEM(item) ) {
251                 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
253                 effect_list_reload(lpeitem);
255                 current_lpeitem = lpeitem;
257                 set_sensitize_all(true);
258                 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
259                     Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
260                     if (lpe) {
261                         showParams(*lpe);
262                         lpe_list_locked = true; 
263                         selectInList(lpe);
264                     } else {
265                         showText(_("Unknown effect is applied"));
266                     }
267                 } else {
268                     showText(_("No effect applied"));
269                     button_remove.set_sensitive(false);
270                 }
271             } else {
272                 showText(_("Item is not a path or shape"));
273                 set_sensitize_all(false);
274             }
275         } else {
276             showText(_("Only one item can be selected"));
277             set_sensitize_all(false);
278         }
279     } else {
280         showText(_("Empty selection"));
281         set_sensitize_all(false);
282     }
285 /*
286  * First clears the effectlist_store, then appends all effects from the effectlist.
287  */
288 void
289 LivePathEffectEditor::effect_list_reload(SPLPEItem *lpeitem)
291     effectlist_store->clear();
293     PathEffectList effectlist = sp_lpe_item_get_effect_list(lpeitem);
294     PathEffectList::iterator it;
295     for( it = effectlist.begin() ; it!=effectlist.end(); it++ )
296     {
297         if ((*it)->lpeobject->get_lpe()) {
298             Gtk::TreeModel::Row row = *(effectlist_store->append());
299             row[columns.col_name] = (*it)->lpeobject->get_lpe()->getName();
300             row[columns.lperef] = *it;
301             row[columns.col_visible] = (*it)->lpeobject->get_lpe()->isVisible();
302         } else {
303             Gtk::TreeModel::Row row = *(effectlist_store->append());
304             row[columns.col_name] = "Unknown effect!";
305             row[columns.lperef] = *it;
306             row[columns.col_visible] = false;
307         }
308     }
312 void
313 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
315     Panel::setDesktop(desktop);
317     if ( desktop == current_desktop ) {
318         return;
319     }
321     if (current_desktop) {
322         selection_changed_connection.disconnect();
323         selection_modified_connection.disconnect();
324     }
326     current_desktop = desktop;
327     if (desktop) {
328         Inkscape::Selection *selection = sp_desktop_selection(desktop);
329         selection_changed_connection = selection->connectChanged(
330             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
331         selection_modified_connection = selection->connectModified(
332             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
334         onSelectionChanged(selection);
335     } else {
336         onSelectionChanged(NULL);
337     }
343 /*########################################################################
344 # BUTTON CLICK HANDLERS    (callbacks)
345 ########################################################################*/
347 // TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
349 void
350 LivePathEffectEditor::onApply()
352     Inkscape::Selection *sel = _getSelection();
353     if ( sel && !sel->isEmpty() ) {
354         SPItem *item = sel->singleItem();
355         if ( item && SP_IS_LPE_ITEM(item) ) {
356             SPDocument *doc = current_desktop->doc();
358             const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
359             if (!data) return;
361             // If item is a SPRect, convert it to path first:
362             if ( SP_IS_RECT(item) ) {
363                 sp_selected_path_to_curves(current_desktop, false);
364                 item = sel->singleItem(); // get new item
365             }
367             LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
369             sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
370                      _("Create and apply path effect"));
372             onSelectionChanged(sel);
373         }
374     }
377 void
378 LivePathEffectEditor::onRemove()
380     Inkscape::Selection *sel = _getSelection();
381     if ( sel && !sel->isEmpty() ) {
382         SPItem *item = sel->singleItem();
383         if ( item && SP_IS_LPE_ITEM(item) ) {
384             sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
386             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
387                                _("Remove path effect") );
389             effect_list_reload(SP_LPE_ITEM(item));
390         }
391     }
394 void LivePathEffectEditor::onUp()
396     Inkscape::Selection *sel = _getSelection();
397     if ( sel && !sel->isEmpty() ) {
398         SPItem *item = sel->singleItem();
399         if ( item && SP_IS_LPE_ITEM(item) ) {
400             sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
402             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
403                                _("Move path effect up") );
405             effect_list_reload(SP_LPE_ITEM(item));
406         }
407     }
410 void LivePathEffectEditor::onDown()
412     Inkscape::Selection *sel = _getSelection();
413     if ( sel && !sel->isEmpty() ) {
414         SPItem *item = sel->singleItem();
415         if ( item && SP_IS_LPE_ITEM(item) ) {
416             sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
418             sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
419                                _("Move path effect down") );
421             effect_list_reload(SP_LPE_ITEM(item));
422         }
423     }
426 void LivePathEffectEditor::on_effect_selection_changed()
428     Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
429     if (sel->count_selected_rows () == 0)
430         return;
432     Gtk::TreeModel::iterator it = sel->get_selected();
433     LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
435     if (lperef && current_lpeitem) {
436         if (lperef->lpeobject->get_lpe()) {
437             lpe_list_locked = true; // prevent reload of the list which would lose selection
438             sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
439             showParams(*lperef->lpeobject->get_lpe());
440         }
441     }
444 void LivePathEffectEditor::on_visibility_toggled( Glib::ustring const& str )
446     Gtk::TreeModel::Children::iterator iter = effectlist_view.get_model()->get_iter(str);
447     Gtk::TreeModel::Row row = *iter;
449     LivePathEffect::LPEObjectReference * lpeobjref = row[columns.lperef];
451     if ( lpeobjref && lpeobjref->lpeobject->get_lpe() ) {
452         bool newValue = !row[columns.col_visible];
453         row[columns.col_visible] = newValue;
454         /* FIXME: this explicit writing to SVG is wrong. The lpe_item should have a method to disable/enable an effect within its stack.
455          * So one can call:  lpe_item->setActive(lpeobjref->lpeobject); */
456         lpeobjref->lpeobject->get_lpe()->getRepr()->setAttribute("is_visible", newValue ? "true" : "false");
457         sp_document_done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
458                           newValue ? _("Activate path effect") : _("Deactivate path effect"));
459     }
462 } // namespace Dialog
463 } // namespace UI
464 } // namespace Inkscape
466 /*
467   Local Variables:
468   mode:c++
469   c-file-style:"stroustrup"
470   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
471   indent-tabs-mode:nil
472   fill-column:99
473   End:
474 */
475 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :