livepatheffect-editor.cpp revision a4030d5ca449e7e384bc699cd249ee704faaeab0
e6bdf746e2d9e775704a475a29cc1bb167ec271cjohanengelen/** @file
e6bdf746e2d9e775704a475a29cc1bb167ec271cjohanengelen * @brief Live Path Effect editing dialog - implementation
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/* Authors:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Johan Engelen <j.b.c.engelen@utwente.nl>
feb44ad276e996ca244e071f936c61b009bd73f4johanengelen * Steren Giannini <steren.giannini@gmail.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Bastien Bouclet <bgkweb@gmail.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright (C) 2007 Authors
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Released under GNU GPL. Read the file 'COPYING' for more information.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#ifdef HAVE_CONFIG_H
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen# include <config.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#endif
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <glibmm/i18n.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <gtkmm/stock.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <gtkmm/toolbar.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <vector>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "desktop.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "desktop-handles.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "document.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "gtkmm/widget.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "inkscape.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "live_effects/effect.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "live_effects/lpeobject.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "live_effects/lpeobject-reference.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "path-chemistry.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "selection.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "sp-item-group.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "sp-lpe-item.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "sp-path.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "sp-rect.h"
c3bc76d351bed6e320f6dbf2242012f026659287johanengelen#include "sp-shape.h"
c3bc76d351bed6e320f6dbf2242012f026659287johanengelen#include "ui/icon-names.h"
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen#include "ui/widget/imagetoggler.h"
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen#include "verbs.h"
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen#include "xml/node.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include "livepatheffect-editor.h"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Inkscape {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenclass Application;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace UI {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Dialog {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*####################
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Callback functions
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenvoid lpeeditor_selection_changed (Inkscape::Selection * selection, gpointer data)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen lpeeditor->lpe_list_locked = false;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen lpeeditor->onSelectionChanged(selection);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstatic void lpeeditor_selection_modified (Inkscape::Selection * selection, guint /*flags*/, gpointer data)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen{
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen LivePathEffectEditor *lpeeditor = static_cast<LivePathEffectEditor *>(data);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen lpeeditor->onSelectionChanged(selection);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*#######################
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * LivePathEffectEditor
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenLivePathEffectEditor::LivePathEffectEditor()
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen : UI::Widget::Panel("", "/dialogs/livepatheffect", SP_VERB_DIALOG_LIVE_PATH_EFFECT),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen lpe_list_locked(false),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen combo_effecttype(Inkscape::LivePathEffect::LPETypeConverter),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen effectwidget(NULL),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen explain_label("", Gtk::ALIGN_CENTER),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // TRANSLATORS: this dialog is accessible via menu Path - Path Effect Editor...
c3bc76d351bed6e320f6dbf2242012f026659287johanengelen effectapplication_frame(_("Apply new effect")),
c3bc76d351bed6e320f6dbf2242012f026659287johanengelen effectcontrol_frame(_("Current effect")),
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen effectlist_frame(_("Effect list")),
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen button_up(Gtk::Stock::GO_UP),
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen button_down(Gtk::Stock::GO_DOWN),
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen button_apply(Gtk::Stock::ADD),
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen button_remove(Gtk::Stock::REMOVE),
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen current_desktop(NULL),
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen current_lpeitem(NULL)
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen{
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen Gtk::Box *contents = _getContents();
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen contents->set_spacing(4);
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen //Add the TreeView, inside a ScrolledWindow, with the button underneath:
scrolled_window.add(effectlist_view);
//Only show the scrollbars when they are necessary:
scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
effectapplication_hbox.set_spacing(4);
effectcontrol_vbox.set_spacing(4);
effectlist_vbox.set_spacing(4);
effectapplication_hbox.pack_start(combo_effecttype, true, true);
effectapplication_hbox.pack_start(button_apply, true, true);
effectapplication_frame.add(effectapplication_hbox);
effectlist_vbox.pack_start(scrolled_window, Gtk::PACK_EXPAND_WIDGET);
effectlist_vbox.pack_end(toolbar, Gtk::PACK_SHRINK);
// effectlist_vbox.pack_end(button_hbox, Gtk::PACK_SHRINK);
effectlist_frame.add(effectlist_vbox);
effectcontrol_vbox.pack_start(explain_label, true, true);
effectcontrol_frame.add(effectcontrol_vbox);
// button_hbox.pack_start(button_up, true, true);
// button_hbox.pack_start(button_down, true, true);
// button_hbox.pack_end(button_remove, true, true);
toolbar.set_toolbar_style(Gtk::TOOLBAR_ICONS);
// Add toolbar items to toolbar
toolbar.append(button_up);
toolbar.append(button_down);
toolbar.append(button_remove);
// Add toolbar
//add_toolbar(toolbar);
toolbar.show_all(); //Show the toolbar and all its child widgets.
//Create the Tree model:
effectlist_store = Gtk::ListStore::create(columns);
effectlist_view.set_model(effectlist_store);
effectlist_view.set_headers_visible(false);
// Handle tree selections
effectlist_selection = effectlist_view.get_selection();
effectlist_selection->signal_changed().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_effect_selection_changed) );
//Add the visibility icon column:
Inkscape::UI::Widget::ImageToggler *eyeRenderer = manage( new Inkscape::UI::Widget::ImageToggler(
INKSCAPE_ICON_OBJECT_VISIBLE, INKSCAPE_ICON_OBJECT_HIDDEN) );
int visibleColNum = effectlist_view.append_column("is_visible", *eyeRenderer) - 1;
eyeRenderer->signal_toggled().connect( sigc::mem_fun(*this, &LivePathEffectEditor::on_visibility_toggled) );
eyeRenderer->property_activatable() = true;
Gtk::TreeViewColumn* col = effectlist_view.get_column(visibleColNum);
if ( col ) {
col->add_attribute( eyeRenderer->property_active(), columns.col_visible );
}
//Add the effect name column:
effectlist_view.append_column("Effect", columns.col_name);
contents->pack_start(effectapplication_frame, false, false);
contents->pack_start(effectlist_frame, true, true);
contents->pack_start(effectcontrol_frame, false, false);
// connect callback functions to buttons
button_apply.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onApply));
button_remove.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onRemove));
button_up.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onUp));
button_down.signal_clicked().connect(sigc::mem_fun(*this, &LivePathEffectEditor::onDown));
show_all_children();
//button_remove.hide();
}
LivePathEffectEditor::~LivePathEffectEditor()
{
if (effectwidget) {
effectcontrol_vbox.remove(*effectwidget);
delete effectwidget;
effectwidget = NULL;
}
if (current_desktop) {
selection_changed_connection.disconnect();
selection_modified_connection.disconnect();
}
}
void
LivePathEffectEditor::showParams(LivePathEffect::Effect& effect)
{
if (effectwidget) {
effectcontrol_vbox.remove(*effectwidget);
delete effectwidget;
effectwidget = NULL;
}
explain_label.set_markup("<b>" + effect.getName() + "</b>");
effectwidget = effect.newWidget(&tooltips);
if (effectwidget) {
effectcontrol_vbox.pack_start(*effectwidget, true, true);
}
button_remove.show();
effectcontrol_vbox.show_all_children();
// fixme: add resizing of dialog
}
void
LivePathEffectEditor::selectInList(LivePathEffect::Effect* effect)
{
Gtk::TreeNodeChildren chi = effectlist_view.get_model()->children();
for (Gtk::TreeIter ci = chi.begin() ; ci != chi.end(); ci++) {
if (ci->get_value(columns.lperef)->lpeobject->get_lpe() == effect)
effectlist_view.get_selection()->select(ci);
}
}
void
LivePathEffectEditor::showText(Glib::ustring const &str)
{
if (effectwidget) {
effectcontrol_vbox.remove(*effectwidget);
delete effectwidget;
effectwidget = NULL;
}
explain_label.set_label(str);
//button_remove.hide();
// fixme: do resizing of dialog ?
}
void
LivePathEffectEditor::set_sensitize_all(bool sensitive)
{
combo_effecttype.set_sensitive(sensitive);
button_apply.set_sensitive(sensitive);
button_remove.set_sensitive(sensitive);
effectlist_view.set_sensitive(sensitive);
button_up.set_sensitive(sensitive);
button_down.set_sensitive(sensitive);
}
void
LivePathEffectEditor::onSelectionChanged(Inkscape::Selection *sel)
{
if (lpe_list_locked) {
// this was triggered by selecting a row in the list, so skip reloading
lpe_list_locked = false;
return;
}
effectlist_store->clear();
current_lpeitem = NULL;
if ( sel && !sel->isEmpty() ) {
SPItem *item = sel->singleItem();
if ( item ) {
if ( SP_IS_LPE_ITEM(item) ) {
SPLPEItem *lpeitem = SP_LPE_ITEM(item);
effect_list_reload(lpeitem);
current_lpeitem = lpeitem;
set_sensitize_all(true);
if ( sp_lpe_item_has_path_effect(lpeitem) ) {
Inkscape::LivePathEffect::Effect *lpe = sp_lpe_item_get_current_lpe(lpeitem);
if (lpe) {
showParams(*lpe);
lpe_list_locked = true;
selectInList(lpe);
} else {
showText(_("Unknown effect is applied"));
}
} else {
showText(_("No effect applied"));
button_remove.set_sensitive(false);
}
} else {
showText(_("Item is not a path or shape"));
set_sensitize_all(false);
}
} else {
showText(_("Only one item can be selected"));
set_sensitize_all(false);
}
} else {
showText(_("Empty selection"));
set_sensitize_all(false);
}
}
/*
* First clears the effectlist_store, then appends all effects from the effectlist.
*/
void
LivePathEffectEditor::effect_list_reload(SPLPEItem *lpeitem)
{
effectlist_store->clear();
PathEffectList effectlist = sp_lpe_item_get_effect_list(lpeitem);
PathEffectList::iterator it;
for( it = effectlist.begin() ; it!=effectlist.end(); it++ )
{
if ( !(*it)->lpeobject ) {
continue;
}
if ((*it)->lpeobject->get_lpe()) {
Gtk::TreeModel::Row row = *(effectlist_store->append());
row[columns.col_name] = (*it)->lpeobject->get_lpe()->getName();
row[columns.lperef] = *it;
row[columns.col_visible] = (*it)->lpeobject->get_lpe()->isVisible();
} else {
Gtk::TreeModel::Row row = *(effectlist_store->append());
row[columns.col_name] = _("Unknown effect");
row[columns.lperef] = *it;
row[columns.col_visible] = false;
}
}
}
void
LivePathEffectEditor::setDesktop(SPDesktop *desktop)
{
Panel::setDesktop(desktop);
if ( desktop == current_desktop ) {
return;
}
if (current_desktop) {
selection_changed_connection.disconnect();
selection_modified_connection.disconnect();
}
lpe_list_locked = false;
current_desktop = desktop;
if (desktop) {
Inkscape::Selection *selection = sp_desktop_selection(desktop);
selection_changed_connection = selection->connectChanged(
sigc::bind (sigc::ptr_fun(&lpeeditor_selection_changed), this ) );
selection_modified_connection = selection->connectModified(
sigc::bind (sigc::ptr_fun(&lpeeditor_selection_modified), this ) );
onSelectionChanged(selection);
} else {
onSelectionChanged(NULL);
}
}
/*########################################################################
# BUTTON CLICK HANDLERS (callbacks)
########################################################################*/
// TODO: factor out the effect applying code which can be called from anywhere. (selection-chemistry.cpp also needs it)
void
LivePathEffectEditor::onApply()
{
Inkscape::Selection *sel = _getSelection();
if ( sel && !sel->isEmpty() ) {
SPItem *item = sel->singleItem();
if ( item && SP_IS_LPE_ITEM(item) ) {
SPDocument *doc = current_desktop->doc();
const Util::EnumData<LivePathEffect::EffectType>* data = combo_effecttype.get_active_data();
if (!data) return;
// If item is a SPRect, convert it to path first:
if ( SP_IS_RECT(item) ) {
sp_selected_path_to_curves(current_desktop, false);
item = sel->singleItem(); // get new item
}
LivePathEffect::Effect::createAndApply(data->key.c_str(), doc, item);
sp_document_done(doc, SP_VERB_DIALOG_LIVE_PATH_EFFECT,
_("Create and apply path effect"));
lpe_list_locked = false;
onSelectionChanged(sel);
}
}
}
void
LivePathEffectEditor::onRemove()
{
Inkscape::Selection *sel = _getSelection();
if ( sel && !sel->isEmpty() ) {
SPItem *item = sel->singleItem();
if ( item && SP_IS_LPE_ITEM(item) ) {
sp_lpe_item_remove_current_path_effect(SP_LPE_ITEM(item), false);
sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
_("Remove path effect") );
effect_list_reload(SP_LPE_ITEM(item));
}
}
}
void LivePathEffectEditor::onUp()
{
Inkscape::Selection *sel = _getSelection();
if ( sel && !sel->isEmpty() ) {
SPItem *item = sel->singleItem();
if ( item && SP_IS_LPE_ITEM(item) ) {
sp_lpe_item_up_current_path_effect(SP_LPE_ITEM(item));
sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
_("Move path effect up") );
effect_list_reload(SP_LPE_ITEM(item));
}
}
}
void LivePathEffectEditor::onDown()
{
Inkscape::Selection *sel = _getSelection();
if ( sel && !sel->isEmpty() ) {
SPItem *item = sel->singleItem();
if ( item && SP_IS_LPE_ITEM(item) ) {
sp_lpe_item_down_current_path_effect(SP_LPE_ITEM(item));
sp_document_done ( sp_desktop_document (current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
_("Move path effect down") );
effect_list_reload(SP_LPE_ITEM(item));
}
}
}
void LivePathEffectEditor::on_effect_selection_changed()
{
Glib::RefPtr<Gtk::TreeSelection> sel = effectlist_view.get_selection();
if (sel->count_selected_rows () == 0)
return;
Gtk::TreeModel::iterator it = sel->get_selected();
LivePathEffect::LPEObjectReference * lperef = (*it)[columns.lperef];
if (lperef && current_lpeitem) {
if (lperef->lpeobject->get_lpe()) {
lpe_list_locked = true; // prevent reload of the list which would lose selection
sp_lpe_item_set_current_path_effect(current_lpeitem, lperef);
showParams(*lperef->lpeobject->get_lpe());
}
}
}
void LivePathEffectEditor::on_visibility_toggled( Glib::ustring const& str )
{
Gtk::TreeModel::Children::iterator iter = effectlist_view.get_model()->get_iter(str);
Gtk::TreeModel::Row row = *iter;
LivePathEffect::LPEObjectReference * lpeobjref = row[columns.lperef];
if ( lpeobjref && lpeobjref->lpeobject->get_lpe() ) {
bool newValue = !row[columns.col_visible];
row[columns.col_visible] = newValue;
/* FIXME: this explicit writing to SVG is wrong. The lpe_item should have a method to disable/enable an effect within its stack.
* So one can call: lpe_item->setActive(lpeobjref->lpeobject); */
lpeobjref->lpeobject->get_lpe()->getRepr()->setAttribute("is_visible", newValue ? "true" : "false");
sp_document_done( sp_desktop_document(current_desktop), SP_VERB_DIALOG_LIVE_PATH_EFFECT,
newValue ? _("Activate path effect") : _("Deactivate path effect"));
}
}
} // namespace Dialog
} // namespace UI
} // namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :