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();
166 }
168 LivePathEffectEditor::~LivePathEffectEditor()
169 {
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 }
180 }
182 void
183 LivePathEffectEditor::showParams(LivePathEffect::Effect& effect)
184 {
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
200 }
202 void
203 LivePathEffectEditor::selectInList(LivePathEffect::Effect* effect)
204 {
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 }
210 }
213 void
214 LivePathEffectEditor::showText(Glib::ustring const &str)
215 {
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 ?
226 }
228 void
229 LivePathEffectEditor::set_sensitize_all(bool sensitive)
230 {
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);
237 }
240 void
241 LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
242 {
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 }
288 }
290 /*
291 * First clears the effectlist_store, then appends all effects from the effectlist.
292 */
293 void
294 LivePathEffectEditor::effect_list_reload(SPLPEItem *lpeitem)
295 {
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 ) {
303 continue;
304 }
306 if ((*it)->lpeobject->get_lpe()) {
307 Gtk::TreeModel::Row row = *(effectlist_store->append());
308 row[columns.col_name] = (*it)->lpeobject->get_lpe()->getName();
309 row[columns.lperef] = *it;
310 row[columns.col_visible] = (*it)->lpeobject->get_lpe()->isVisible();
311 } else {
312 Gtk::TreeModel::Row row = *(effectlist_store->append());
313 row[columns.col_name] = _("Unknown effect");
314 row[columns.lperef] = *it;
315 row[columns.col_visible] = false;
316 }
317 }
318 }
321 void
322 LivePathEffectEditor::setDesktop(SPDesktop *desktop)
323 {
324 Panel::setDesktop(desktop);
326 if ( desktop == current_desktop ) {
327 return;
328 }
330 if (current_desktop) {
331 selection_changed_connection.disconnect();
332 selection_modified_connection.disconnect();
333 }
335 lpe_list_locked = false;
336 current_desktop = desktop;
337 if (desktop) {
338 Inkscape::Selection *selection = sp_desktop_selection(desktop);
339 selection_changed_connection = selection->connectChanged(
340 sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
341 selection_modified_connection = selection->connectModified(
342 sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
344 onSelectionChanged(selection);
345 } else {
346 onSelectionChanged(NULL);
347 }
348 }
353 /*########################################################################
354 # BUTTON CLICK HANDLERS (callbacks)
355 ########################################################################*/
357 // TODO: factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
359 void
360 LivePathEffectEditor::onApply()
361 {
362 Inkscape::Selection *sel = _getSelection();
363 if ( sel && !sel->isEmpty() ) {
364 SPItem *item = sel->singleItem();
365 if ( item && SP_IS_LPE_ITEM(item) ) {
366 SPDocument *doc = current_desktop->doc();
368 const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
369 if (!data) return;
371 // If item is a SPRect, convert it to path first:
372 if ( SP_IS_RECT(item) ) {
373 sp_selected_path_to_curves(current_desktop, false);
374 item = sel->singleItem(); // get new item
375 }
377 LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
379 sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
380 _("Create and apply path effect"));
382 lpe_list_locked = false;
383 onSelectionChanged(sel);
384 }
385 }
386 }
388 void
389 LivePathEffectEditor::onRemove()
390 {
391 Inkscape::Selection *sel = _getSelection();
392 if ( sel && !sel->isEmpty() ) {
393 SPItem *item = sel->singleItem();
394 if ( item && SP_IS_LPE_ITEM(item) ) {
395 sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
397 sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
398 _("Remove path effect") );
400 effect_list_reload(SP_LPE_ITEM(item));
401 }
402 }
403 }
405 void LivePathEffectEditor::onUp()
406 {
407 Inkscape::Selection *sel = _getSelection();
408 if ( sel && !sel->isEmpty() ) {
409 SPItem *item = sel->singleItem();
410 if ( item && SP_IS_LPE_ITEM(item) ) {
411 sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
413 sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
414 _("Move path effect up") );
416 effect_list_reload(SP_LPE_ITEM(item));
417 }
418 }
419 }
421 void LivePathEffectEditor::onDown()
422 {
423 Inkscape::Selection *sel = _getSelection();
424 if ( sel && !sel->isEmpty() ) {
425 SPItem *item = sel->singleItem();
426 if ( item && SP_IS_LPE_ITEM(item) ) {
427 sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
429 sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
430 _("Move path effect down") );
432 effect_list_reload(SP_LPE_ITEM(item));
433 }
434 }
435 }
437 void LivePathEffectEditor::on_effect_selection_changed()
438 {
439 Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
440 if (sel->count_selected_rows () == 0)
441 return;
443 Gtk::TreeModel::iterator it = sel->get_selected();
444 LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
446 if (lperef && current_lpeitem) {
447 if (lperef->lpeobject->get_lpe()) {
448 lpe_list_locked = true; // prevent reload of the list which would lose selection
449 sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
450 showParams(*lperef->lpeobject->get_lpe());
451 }
452 }
453 }
455 void LivePathEffectEditor::on_visibility_toggled( Glib::ustring const& str )
456 {
457 Gtk::TreeModel::Children::iterator iter = effectlist_view.get_model()->get_iter(str);
458 Gtk::TreeModel::Row row = *iter;
460 LivePathEffect::LPEObjectReference * lpeobjref = row[columns.lperef];
462 if ( lpeobjref && lpeobjref->lpeobject->get_lpe() ) {
463 bool newValue = !row[columns.col_visible];
464 row[columns.col_visible] = newValue;
465 /* FIXME: this explicit writing to SVG is wrong. The lpe_item should have a method to disable/enable an effect within its stack.
466 * So one can call: lpe_item->setActive(lpeobjref->lpeobject); */
467 lpeobjref->lpeobject->get_lpe()->getRepr()->setAttribute("is_visible", newValue ? "true" : "false");
468 sp_document_done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
469 newValue ? _("Activate path effect") : _("Deactivate path effect"));
470 }
471 }
473 } // namespace Dialog
474 } // namespace UI
475 } // namespace Inkscape
477 /*
478 Local Variables:
479 mode:c++
480 c-file-style:"stroustrup"
481 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
482 indent-tabs-mode:nil
483 fill-column:99
484 End:
485 */
486 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :