Code

A simple layout document as to what, why and how is cppification.
[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 <gtkmm/stock.h>
19 #include <gtkmm/toolbar.h>
20 #include <vector>
22 #include "desktop.h"
23 #include "desktop-handles.h"
24 #include "document.h"
25 #include "gtkmm/widget.h"
26 #include "inkscape.h"
27 #include "live_effects/effect.h"
28 #include "live_effects/lpeobject.h"
29 #include "live_effects/lpeobject-reference.h"
30 #include "path-chemistry.h"
31 #include "selection.h"
32 #include "sp-item-group.h"
33 #include "sp-lpe-item.h"
34 #include "sp-path.h"
35 #include "sp-rect.h"
36 #include "sp-shape.h"
37 #include "ui/icon-names.h"
38 #include "ui/widget/imagetoggler.h"
39 #include "verbs.h"
40 #include "xml/node.h"
42 #include "livepatheffect-editor.h"
44 namespace Inkscape {
45 class Application;
47 namespace UI {
48 namespace Dialog {
51 /*####################
52  * Callback functions
53  */
54 void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
55 {
56     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
57     lpeeditor->lpe_list_locked = false;
58     lpeeditor->onSelectionChanged(selection);
59 }
61 static void lpeeditor_selection_modified (Inkscape::Selection * selection, guint /*flags*/, gpointer data)
62 {
63     LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
64     lpeeditor->onSelectionChanged(selection);
65 }
68 /*#######################
69  * LivePathEffectEditor
70  */
72 LivePathEffectEditor::LivePathEffectEditor()
73     : UI::Widget::Panel("", "/dialogs/livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
74       lpe_list_locked(false),
75       combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
76       effectwidget(NULL),
77       explain_label("", Gtk::ALIGN_CENTER),
78       // TRANSLATORS: this dialog is accessible via menu Path - Path Effect Editor...
79       effectapplication_frame(_("Apply new effect")),
80       effectcontrol_frame(_("Current effect")),
81       effectlist_frame(_("Effect list")),
82       button_up(Gtk::Stock::GO_UP),
83       button_down(Gtk::Stock::GO_DOWN),
84       button_apply(Gtk::Stock::ADD),
85       button_remove(Gtk::Stock::REMOVE),
86       current_desktop(NULL),
87       current_lpeitem(NULL)
88 {
89     Gtk::Box *contents = _getContents();
90     contents->set_spacing(4);
92     //Add the TreeView, inside a ScrolledWindow, with the button underneath:
93     scrolled_window.add(effectlist_view);
94     //Only show the scrollbars when they are necessary:
95     scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
97     effectapplication_hbox.set_spacing(4);
98     effectcontrol_vbox.set_spacing(4);
99     effectlist_vbox.set_spacing(4);
101     effectapplication_hbox.pack_start(combo_effecttype, true, true);
102     effectapplication_hbox.pack_start(button_apply, true, true);
103     effectapplication_frame.add(effectapplication_hbox);
105     effectlist_vbox.pack_start(scrolled_window, Gtk::PACK_EXPAND_WIDGET);
106     effectlist_vbox.pack_end(toolbar, Gtk::PACK_SHRINK);
107    // effectlist_vbox.pack_end(button_hbox, Gtk::PACK_SHRINK);
108     effectlist_frame.add(effectlist_vbox);
110     effectcontrol_vbox.pack_start(explain_label, true, true);
111     effectcontrol_frame.add(effectcontrol_vbox);
113  //   button_hbox.pack_start(button_up, true, true);
114  //   button_hbox.pack_start(button_down, true, true);
115  //   button_hbox.pack_end(button_remove, true, true);
116     toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);
117  // Add toolbar items to toolbar
118     toolbar.append(button_up);
119     toolbar.append(button_down);
120     toolbar.append(button_remove);
123     // Add toolbar
124     //add_toolbar(toolbar);
125     toolbar.show_all(); //Show the toolbar and all its child widgets.
128     //Create the Tree model:
129     effectlist_store = Gtk::ListStore::create(columns);
130     effectlist_view.set_model(effectlist_store);
132     effectlist_view.set_headers_visible(false);
134     // Handle tree selections
135     effectlist_selection = effectlist_view.get_selection();
136     effectlist_selection->signal_changed().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_effect_selection_changed) );
138     //Add the visibility icon column:
139     Inkscape::UI::Widget::ImageToggler *eyeRenderer = manage( new Inkscape::UI::Widget::ImageToggler(
140         INKSCAPE_ICON_OBJECT_VISIBLE, INKSCAPE_ICON_OBJECT_HIDDEN) );
141     int visibleColNum = effectlist_view.append_column("is_visible", *eyeRenderer) - 1;
142     eyeRenderer->signal_toggled().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_visibility_toggled) );
143     eyeRenderer->property_activatable() = true;
144     Gtk::TreeViewColumn* col = effectlist_view.get_column(visibleColNum);
145     if ( col ) {
146         col->add_attribute( eyeRenderer->property_active(), columns.col_visible );
147     }
149     //Add the effect name column:
150     effectlist_view.append_column("Effect", columns.col_name);
152     contents->pack_start(effectapplication_frame, false, false);
153     contents->pack_start(effectlist_frame, true, true);
154     contents->pack_start(effectcontrol_frame, false, false);
156     // connect callback functions to buttons
157     button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
158     button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
160     button_up.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onUp));
161     button_down.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onDown));
163     show_all_children();
165     //button_remove.hide();
168 LivePathEffectEditor::~LivePathEffectEditor()
170     if (effectwidget) {
171         effectcontrol_vbox.remove(*effectwidget);
172         delete effectwidget;
173         effectwidget = NULL;
174     }
176     if (current_desktop) {
177         selection_changed_connection.disconnect();
178         selection_modified_connection.disconnect();
179     }
182 void
183 LivePathEffectEditor::showParams(LivePathEffect::Effect& effect)
185     if (effectwidget) {
186         effectcontrol_vbox.remove(*effectwidget);
187         delete effectwidget;
188         effectwidget = NULL;
189     }
191     explain_label.set_markup("<b>" + effect.getName() + "</b>");
192     effectwidget = effect.newWidget(&tooltips);
193     if (effectwidget) {
194         effectcontrol_vbox.pack_start(*effectwidget, true, true);
195     }
196     button_remove.show();
198     effectcontrol_vbox.show_all_children();
199     // fixme: add resizing of dialog
202 void
203 LivePathEffectEditor::selectInList(LivePathEffect::Effect* effect)
205     Gtk::TreeNodeChildren chi = effectlist_view.get_model()->children();
206     for (Gtk::TreeIter ci = chi.begin() ; ci != chi.end(); ci++) {
207         if (ci->get_value(columns.lperef)->lpeobject->get_lpe() == effect)
208             effectlist_view.get_selection()->select(ci);
209     }
213 void
214 LivePathEffectEditor::showText(Glib::ustring const &str)
216     if (effectwidget) {
217         effectcontrol_vbox.remove(*effectwidget);
218         delete effectwidget;
219         effectwidget = NULL;
220     }
222     explain_label.set_label(str);
223     //button_remove.hide();
225     // fixme: do resizing of dialog ?
228 void
229 LivePathEffectEditor::set_sensitize_all(bool sensitive)
231     combo_effecttype.set_sensitive(sensitive);
232     button_apply.set_sensitive(sensitive);
233     button_remove.set_sensitive(sensitive);
234     effectlist_view.set_sensitive(sensitive);
235     button_up.set_sensitive(sensitive);
236     button_down.set_sensitive(sensitive);
240 void
241 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
243     if (lpe_list_locked) {
244         // this was triggered by selecting a row in the list, so skip reloading
245         lpe_list_locked = false;
246         return;
247     } 
249     effectlist_store->clear();
250     current_lpeitem = NULL;
252     if ( sel && !sel->isEmpty() ) {
253         SPItem *item = sel->singleItem();
254         if ( item ) {
255             if ( SP_IS_LPE_ITEM(item) ) {
256                 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
258                 effect_list_reload(lpeitem);
260                 current_lpeitem = lpeitem;
262                 set_sensitize_all(true);
263                 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
264                     Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
265                     if (lpe) {
266                         showParams(*lpe);
267                         lpe_list_locked = true; 
268                         selectInList(lpe);
269                     } else {
270                         showText(_("Unknown effect is applied"));
271                     }
272                 } else {
273                     showText(_("No effect applied"));
274                     button_remove.set_sensitive(false);
275                 }
276             } else {
277                 showText(_("Item is not a path or shape"));
278                 set_sensitize_all(false);
279             }
280         } else {
281             showText(_("Only one item can be selected"));
282             set_sensitize_all(false);
283         }
284     } else {
285         showText(_("Empty selection"));
286         set_sensitize_all(false);
287     }
290 /*
291  * First clears the effectlist_store, then appends all effects from the effectlist.
292  */
293 void
294 LivePathEffectEditor::effect_list_reload(SPLPEItem *lpeitem)
296     effectlist_store->clear();
298     PathEffectList effectlist = sp_lpe_item_get_effect_list(lpeitem);
299     PathEffectList::iterator it;
300     for( it = effectlist.begin() ; it!=effectlist.end(); it++ )
301     {
302         if ((*it)->lpeobject->get_lpe()) {
303             Gtk::TreeModel::Row row = *(effectlist_store->append());
304             row[columns.col_name] = (*it)->lpeobject->get_lpe()->getName();
305             row[columns.lperef] = *it;
306             row[columns.col_visible] = (*it)->lpeobject->get_lpe()->isVisible();
307         } else {
308             Gtk::TreeModel::Row row = *(effectlist_store->append());
309             row[columns.col_name] = _("Unknown effect");
310             row[columns.lperef] = *it;
311             row[columns.col_visible] = false;
312         }
313     }
317 void
318 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
320     Panel::setDesktop(desktop);
322     if ( desktop == current_desktop ) {
323         return;
324     }
326     if (current_desktop) {
327         selection_changed_connection.disconnect();
328         selection_modified_connection.disconnect();
329     }
331     lpe_list_locked = false;
332     current_desktop = desktop;
333     if (desktop) {
334         Inkscape::Selection *selection = sp_desktop_selection(desktop);
335         selection_changed_connection = selection->connectChanged(
336             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
337         selection_modified_connection = selection->connectModified(
338             sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
340         onSelectionChanged(selection);
341     } else {
342         onSelectionChanged(NULL);
343     }
349 /*########################################################################
350 # BUTTON CLICK HANDLERS    (callbacks)
351 ########################################################################*/
353 // TODO:  factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
355 void
356 LivePathEffectEditor::onApply()
358     Inkscape::Selection *sel = _getSelection();
359     if ( sel && !sel->isEmpty() ) {
360         SPItem *item = sel->singleItem();
361         if ( item && SP_IS_LPE_ITEM(item) ) {
362             SPDocument *doc = current_desktop->doc();
364             const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
365             if (!data) return;
367             // If item is a SPRect, convert it to path first:
368             if ( SP_IS_RECT(item) ) {
369                 sp_selected_path_to_curves(current_desktop, false);
370                 item = sel->singleItem(); // get new item
371             }
373             LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
375             SPDocumentUndo::done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
376                      _("Create and apply path effect"));
378             lpe_list_locked = false;
379             onSelectionChanged(sel);
380         }
381     }
384 void
385 LivePathEffectEditor::onRemove()
387     Inkscape::Selection *sel = _getSelection();
388     if ( sel && !sel->isEmpty() ) {
389         SPItem *item = sel->singleItem();
390         if ( item && SP_IS_LPE_ITEM(item) ) {
391             sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
393             SPDocumentUndo::done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
394                                _("Remove path effect") );
396             effect_list_reload(SP_LPE_ITEM(item));
397         }
398     }
401 void LivePathEffectEditor::onUp()
403     Inkscape::Selection *sel = _getSelection();
404     if ( sel && !sel->isEmpty() ) {
405         SPItem *item = sel->singleItem();
406         if ( item && SP_IS_LPE_ITEM(item) ) {
407             sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
409             SPDocumentUndo::done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
410                                _("Move path effect up") );
412             effect_list_reload(SP_LPE_ITEM(item));
413         }
414     }
417 void LivePathEffectEditor::onDown()
419     Inkscape::Selection *sel = _getSelection();
420     if ( sel && !sel->isEmpty() ) {
421         SPItem *item = sel->singleItem();
422         if ( item && SP_IS_LPE_ITEM(item) ) {
423             sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
425             SPDocumentUndo::done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
426                                _("Move path effect down") );
428             effect_list_reload(SP_LPE_ITEM(item));
429         }
430     }
433 void LivePathEffectEditor::on_effect_selection_changed()
435     Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
436     if (sel->count_selected_rows () == 0)
437         return;
439     Gtk::TreeModel::iterator it = sel->get_selected();
440     LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
442     if (lperef && current_lpeitem) {
443         if (lperef->lpeobject->get_lpe()) {
444             lpe_list_locked = true; // prevent reload of the list which would lose selection
445             sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
446             showParams(*lperef->lpeobject->get_lpe());
447         }
448     }
451 void LivePathEffectEditor::on_visibility_toggled( Glib::ustring const& str )
453     Gtk::TreeModel::Children::iterator iter = effectlist_view.get_model()->get_iter(str);
454     Gtk::TreeModel::Row row = *iter;
456     LivePathEffect::LPEObjectReference * lpeobjref = row[columns.lperef];
458     if ( lpeobjref && lpeobjref->lpeobject->get_lpe() ) {
459         bool newValue = !row[columns.col_visible];
460         row[columns.col_visible] = newValue;
461         /* FIXME: this explicit writing to SVG is wrong. The lpe_item should have a method to disable/enable an effect within its stack.
462          * So one can call:  lpe_item->setActive(lpeobjref->lpeobject); */
463         lpeobjref->lpeobject->get_lpe()->getRepr()->setAttribute("is_visible", newValue ? "true" : "false");
464         SPDocumentUndo::done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
465                           newValue ? _("Activate path effect") : _("Deactivate path effect"));
466     }
469 } // namespace Dialog
470 } // namespace UI
471 } // namespace Inkscape
473 /*
474   Local Variables:
475   mode:c++
476   c-file-style:"stroustrup"
477   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
478   indent-tabs-mode:nil
479   fill-column:99
480   End:
481 */
482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :