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 static void lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
55 {
56 LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
57 lpeeditor->onSelectionChanged(selection);
58 }
60 static void lpeeditor_selection_modified (Inkscape::Selection * selection, guint /*flags*/, gpointer data)
61 {
62 LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
63 lpeeditor->onSelectionChanged(selection);
64 }
67 /*#######################
68 * LivePathEffectEditor
69 */
71 LivePathEffectEditor::LivePathEffectEditor()
72 : UI::Widget::Panel("", "/dialogs/livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
73 lpe_list_locked(false),
74 combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
75 effectwidget(NULL),
76 explain_label("", Gtk::ALIGN_CENTER),
77 // TRANSLATORS: this dialog is accessible via menu Path - Path Effect Editor...
78 effectapplication_frame(_("Apply new effect")),
79 effectcontrol_frame(_("Current effect")),
80 effectlist_frame(_("Effect list")),
81 button_up(Gtk::Stock::GO_UP),
82 button_down(Gtk::Stock::GO_DOWN),
83 button_apply(Gtk::Stock::ADD),
84 button_remove(Gtk::Stock::REMOVE),
85 current_desktop(NULL),
86 current_lpeitem(NULL)
87 {
88 Gtk::Box *contents = _getContents();
89 contents->set_spacing(4);
91 //Add the TreeView, inside a ScrolledWindow, with the button underneath:
92 scrolled_window.add(effectlist_view);
93 //Only show the scrollbars when they are necessary:
94 scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
96 effectapplication_hbox.set_spacing(4);
97 effectcontrol_vbox.set_spacing(4);
98 effectlist_vbox.set_spacing(4);
100 effectapplication_hbox.pack_start(combo_effecttype, true, true);
101 effectapplication_hbox.pack_start(button_apply, true, true);
102 effectapplication_frame.add(effectapplication_hbox);
104 effectlist_vbox.pack_start(scrolled_window, Gtk::PACK_EXPAND_WIDGET);
105 effectlist_vbox.pack_end(toolbar, Gtk::PACK_SHRINK);
106 // effectlist_vbox.pack_end(button_hbox, Gtk::PACK_SHRINK);
107 effectlist_frame.add(effectlist_vbox);
109 effectcontrol_vbox.pack_start(explain_label, true, true);
110 effectcontrol_frame.add(effectcontrol_vbox);
112 // button_hbox.pack_start(button_up, true, true);
113 // button_hbox.pack_start(button_down, true, true);
114 // button_hbox.pack_end(button_remove, true, true);
115 toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);
116 // Add toolbar items to toolbar
117 toolbar.append(button_up);
118 toolbar.append(button_down);
119 toolbar.append(button_remove);
122 // Add toolbar
123 //add_toolbar(toolbar);
124 toolbar.show_all(); //Show the toolbar and all its child widgets.
127 //Create the Tree model:
128 effectlist_store = Gtk::ListStore::create(columns);
129 effectlist_view.set_model(effectlist_store);
131 effectlist_view.set_headers_visible(false);
133 // Handle tree selections
134 effectlist_selection = effectlist_view.get_selection();
135 effectlist_selection->signal_changed().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_effect_selection_changed) );
137 //Add the visibility icon column:
138 Inkscape::UI::Widget::ImageToggler *eyeRenderer = manage( new Inkscape::UI::Widget::ImageToggler(
139 INKSCAPE_ICON_OBJECT_VISIBLE, INKSCAPE_ICON_OBJECT_HIDDEN) );
140 int visibleColNum = effectlist_view.append_column("is_visible", *eyeRenderer) - 1;
141 eyeRenderer->signal_toggled().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_visibility_toggled) );
142 eyeRenderer->property_activatable() = true;
143 Gtk::TreeViewColumn* col = effectlist_view.get_column(visibleColNum);
144 if ( col ) {
145 col->add_attribute( eyeRenderer->property_active(), columns.col_visible );
146 }
148 //Add the effect name column:
149 effectlist_view.append_column("Effect", columns.col_name);
151 contents->pack_start(effectapplication_frame, false, false);
152 contents->pack_start(effectlist_frame, true, true);
153 contents->pack_start(effectcontrol_frame, false, false);
155 // connect callback functions to buttons
156 button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
157 button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
159 button_up.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onUp));
160 button_down.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onDown));
162 show_all_children();
164 //button_remove.hide();
165 }
167 LivePathEffectEditor::~LivePathEffectEditor()
168 {
169 if (effectwidget) {
170 effectcontrol_vbox.remove(*effectwidget);
171 delete effectwidget;
172 effectwidget = NULL;
173 }
175 if (current_desktop) {
176 selection_changed_connection.disconnect();
177 selection_modified_connection.disconnect();
178 }
179 }
181 void
182 LivePathEffectEditor::showParams(LivePathEffect::Effect& effect)
183 {
184 if (effectwidget) {
185 effectcontrol_vbox.remove(*effectwidget);
186 delete effectwidget;
187 effectwidget = NULL;
188 }
190 explain_label.set_markup("<b>" + effect.getName() + "</b>");
191 effectwidget = effect.newWidget(&tooltips);
192 if (effectwidget) {
193 effectcontrol_vbox.pack_start(*effectwidget, true, true);
194 }
195 button_remove.show();
197 effectcontrol_vbox.show_all_children();
198 // fixme: add resizing of dialog
199 }
201 void
202 LivePathEffectEditor::selectInList(LivePathEffect::Effect* effect)
203 {
204 Gtk::TreeNodeChildren chi = effectlist_view.get_model()->children();
205 for (Gtk::TreeIter ci = chi.begin() ; ci != chi.end(); ci++) {
206 if (ci->get_value(columns.lperef)->lpeobject->get_lpe() == effect)
207 effectlist_view.get_selection()->select(ci);
208 }
209 }
212 void
213 LivePathEffectEditor::showText(Glib::ustring const &str)
214 {
215 if (effectwidget) {
216 effectcontrol_vbox.remove(*effectwidget);
217 delete effectwidget;
218 effectwidget = NULL;
219 }
221 explain_label.set_label(str);
222 //button_remove.hide();
224 // fixme: do resizing of dialog ?
225 }
227 void
228 LivePathEffectEditor::set_sensitize_all(bool sensitive)
229 {
230 combo_effecttype.set_sensitive(sensitive);
231 button_apply.set_sensitive(sensitive);
232 button_remove.set_sensitive(sensitive);
233 effectlist_view.set_sensitive(sensitive);
234 button_up.set_sensitive(sensitive);
235 button_down.set_sensitive(sensitive);
236 }
239 void
240 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
241 {
242 if (lpe_list_locked) {
243 // this was triggered by selecting a row in the list, so skip reloading
244 lpe_list_locked = false;
245 return;
246 }
248 effectlist_store->clear();
249 current_lpeitem = NULL;
251 if ( sel && !sel->isEmpty() ) {
252 SPItem *item = sel->singleItem();
253 if ( item ) {
254 if ( SP_IS_LPE_ITEM(item) ) {
255 SPLPEItem *lpeitem = SP_LPE_ITEM(item);
257 effect_list_reload(lpeitem);
259 current_lpeitem = lpeitem;
261 set_sensitize_all(true);
262 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
263 Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
264 if (lpe) {
265 showParams(*lpe);
266 lpe_list_locked = true;
267 selectInList(lpe);
268 } else {
269 showText(_("Unknown effect is applied"));
270 }
271 } else {
272 showText(_("No effect applied"));
273 button_remove.set_sensitive(false);
274 }
275 } else {
276 showText(_("Item is not a path or shape"));
277 set_sensitize_all(false);
278 }
279 } else {
280 showText(_("Only one item can be selected"));
281 set_sensitize_all(false);
282 }
283 } else {
284 showText(_("Empty selection"));
285 set_sensitize_all(false);
286 }
287 }
289 /*
290 * First clears the effectlist_store, then appends all effects from the effectlist.
291 */
292 void
293 LivePathEffectEditor::effect_list_reload(SPLPEItem *lpeitem)
294 {
295 effectlist_store->clear();
297 PathEffectList effectlist = sp_lpe_item_get_effect_list(lpeitem);
298 PathEffectList::iterator it;
299 for( it = effectlist.begin() ; it!=effectlist.end(); it++ )
300 {
301 if ((*it)->lpeobject->get_lpe()) {
302 Gtk::TreeModel::Row row = *(effectlist_store->append());
303 row[columns.col_name] = (*it)->lpeobject->get_lpe()->getName();
304 row[columns.lperef] = *it;
305 row[columns.col_visible] = (*it)->lpeobject->get_lpe()->isVisible();
306 } else {
307 Gtk::TreeModel::Row row = *(effectlist_store->append());
308 row[columns.col_name] = _("Unknown effect");
309 row[columns.lperef] = *it;
310 row[columns.col_visible] = false;
311 }
312 }
313 }
316 void
317 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
318 {
319 Panel::setDesktop(desktop);
321 if ( desktop == current_desktop ) {
322 return;
323 }
325 if (current_desktop) {
326 selection_changed_connection.disconnect();
327 selection_modified_connection.disconnect();
328 }
330 current_desktop = desktop;
331 if (desktop) {
332 Inkscape::Selection *selection = sp_desktop_selection(desktop);
333 selection_changed_connection = selection->connectChanged(
334 sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
335 selection_modified_connection = selection->connectModified(
336 sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
338 onSelectionChanged(selection);
339 } else {
340 onSelectionChanged(NULL);
341 }
342 }
347 /*########################################################################
348 # BUTTON CLICK HANDLERS (callbacks)
349 ########################################################################*/
351 // TODO: factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
353 void
354 LivePathEffectEditor::onApply()
355 {
356 Inkscape::Selection *sel = _getSelection();
357 if ( sel && !sel->isEmpty() ) {
358 SPItem *item = sel->singleItem();
359 if ( item && SP_IS_LPE_ITEM(item) ) {
360 SPDocument *doc = current_desktop->doc();
362 const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
363 if (!data) return;
365 // If item is a SPRect, convert it to path first:
366 if ( SP_IS_RECT(item) ) {
367 sp_selected_path_to_curves(current_desktop, false);
368 item = sel->singleItem(); // get new item
369 }
371 LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
373 sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
374 _("Create and apply path effect"));
376 onSelectionChanged(sel);
377 }
378 }
379 }
381 void
382 LivePathEffectEditor::onRemove()
383 {
384 Inkscape::Selection *sel = _getSelection();
385 if ( sel && !sel->isEmpty() ) {
386 SPItem *item = sel->singleItem();
387 if ( item && SP_IS_LPE_ITEM(item) ) {
388 sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
390 sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
391 _("Remove path effect") );
393 effect_list_reload(SP_LPE_ITEM(item));
394 }
395 }
396 }
398 void LivePathEffectEditor::onUp()
399 {
400 Inkscape::Selection *sel = _getSelection();
401 if ( sel && !sel->isEmpty() ) {
402 SPItem *item = sel->singleItem();
403 if ( item && SP_IS_LPE_ITEM(item) ) {
404 sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
406 sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
407 _("Move path effect up") );
409 effect_list_reload(SP_LPE_ITEM(item));
410 }
411 }
412 }
414 void LivePathEffectEditor::onDown()
415 {
416 Inkscape::Selection *sel = _getSelection();
417 if ( sel && !sel->isEmpty() ) {
418 SPItem *item = sel->singleItem();
419 if ( item && SP_IS_LPE_ITEM(item) ) {
420 sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
422 sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
423 _("Move path effect down") );
425 effect_list_reload(SP_LPE_ITEM(item));
426 }
427 }
428 }
430 void LivePathEffectEditor::on_effect_selection_changed()
431 {
432 Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
433 if (sel->count_selected_rows () == 0)
434 return;
436 Gtk::TreeModel::iterator it = sel->get_selected();
437 LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
439 if (lperef && current_lpeitem) {
440 if (lperef->lpeobject->get_lpe()) {
441 lpe_list_locked = true; // prevent reload of the list which would lose selection
442 sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
443 showParams(*lperef->lpeobject->get_lpe());
444 }
445 }
446 }
448 void LivePathEffectEditor::on_visibility_toggled( Glib::ustring const& str )
449 {
450 Gtk::TreeModel::Children::iterator iter = effectlist_view.get_model()->get_iter(str);
451 Gtk::TreeModel::Row row = *iter;
453 LivePathEffect::LPEObjectReference * lpeobjref = row[columns.lperef];
455 if ( lpeobjref && lpeobjref->lpeobject->get_lpe() ) {
456 bool newValue = !row[columns.col_visible];
457 row[columns.col_visible] = newValue;
458 /* FIXME: this explicit writing to SVG is wrong. The lpe_item should have a method to disable/enable an effect within its stack.
459 * So one can call: lpe_item->setActive(lpeobjref->lpeobject); */
460 lpeobjref->lpeobject->get_lpe()->getRepr()->setAttribute("is_visible", newValue ? "true" : "false");
461 sp_document_done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
462 newValue ? _("Activate path effect") : _("Deactivate path effect"));
463 }
464 }
466 } // namespace Dialog
467 } // namespace UI
468 } // namespace Inkscape
470 /*
471 Local Variables:
472 mode:c++
473 c-file-style:"stroustrup"
474 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
475 indent-tabs-mode:nil
476 fill-column:99
477 End:
478 */
479 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :