1 /* === S Y N F I G ========================================================= */
2 /*! \file layeractionmanager.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "layeractionmanager.h"
34 #include "layertree.h"
35 #include <synfig/context.h>
36 #include <synfigapp/action_param.h>
38 #include <synfigapp/selectionmanager.h>
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
49 using namespace studio;
51 static const guint no_prev_popup((guint)-1);
53 /* === M A C R O S ========================================================= */
55 //#define ONE_ACTION_GROUP 1
57 /* === G L O B A L S ======================================================= */
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 LayerActionManager::LayerActionManager():
64 action_group_(Gtk::ActionGroup::create()),
65 popup_id_(no_prev_popup),
66 action_group_copy_paste(Gtk::ActionGroup::create()),
69 action_cut_=Gtk::Action::create(
71 Gtk::StockID("gtk-cut")
73 action_cut_->signal_activate().connect(
76 &LayerActionManager::cut
79 action_copy_=Gtk::Action::create(
81 Gtk::StockID("gtk-copy")
83 action_copy_->signal_activate().connect(
86 &LayerActionManager::copy
89 action_paste_=Gtk::Action::create(
91 Gtk::StockID("gtk-paste")
93 action_paste_->signal_activate().connect(
96 &LayerActionManager::paste
101 action_amount_inc_=Gtk::Action::create(
103 Gtk::StockID("gtk-add"),
104 _("Increase Amount"),_("Increase Amount")
106 action_amount_inc_->signal_activate().connect(
109 &LayerActionManager::amount_inc
113 action_amount_dec_=Gtk::Action::create(
115 Gtk::StockID("gtk-remove"),
116 _("Decrease Amount"),_("Decrease Amount")
118 action_amount_dec_->signal_activate().connect(
121 &LayerActionManager::amount_dec
125 action_amount_=Gtk::Action::create(
127 Gtk::StockID("gtk-index"),
128 _("Amount"),_("Amount")
131 action_select_all_child_layers_=Gtk::Action::create(
132 "select-all-child-layers",
133 Gtk::StockID("synfig-select_all_child_layers"),
134 _("Select All Child Layers"),_("Select All Child Layers")
136 action_select_all_child_layers_->set_sensitive(false);
139 LayerActionManager::~LayerActionManager()
144 LayerActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
148 #ifdef ONE_ACTION_GROUP
149 if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
151 if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
158 LayerActionManager::set_layer_tree(LayerTree* x)
160 selection_changed_connection.disconnect();
164 selection_changed_connection=layer_tree_->get_selection()->signal_changed().connect(
165 sigc::mem_fun(*this,&LayerActionManager::queue_refresh)
171 LayerActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
177 LayerActionManager::clear()
181 // Clear out old stuff
182 if(popup_id_!=no_prev_popup)
184 get_ui_manager()->remove_ui(popup_id_);
185 if(action_group_)get_ui_manager()->ensure_update();
186 popup_id_=no_prev_popup;
187 if(action_group_)while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
188 #ifdef ONE_ACTION_GROUP
190 if(action_group_)get_ui_manager()->remove_action_group(action_group_);
191 action_group_=Gtk::ActionGroup::create();
196 while(!update_connection_list.empty())
198 update_connection_list.front().disconnect();
199 update_connection_list.pop_front();
204 LayerActionManager::queue_refresh()
209 //queue_refresh_connection.disconnect();
210 queue_refresh_connection=Glib::signal_idle().connect(
212 sigc::mem_fun(*this,&LayerActionManager::refresh),
221 LayerActionManager::refresh()
226 //queue_refresh_connection.disconnect();
232 // Make sure we are ready
233 if(!ui_manager_ || !layer_tree_ || !canvas_interface_)
235 synfig::error("LayerActionManager::refresh(): Not ready!");
241 action_paste_->set_sensitive(!clipboard_.empty());
242 action_group_->add(action_paste_);
244 if(layer_tree_->get_selection()->count_selected_rows()!=0)
246 bool multiple_selected(layer_tree_->get_selection()->count_selected_rows()>1);
247 Layer::Handle layer(layer_tree_->get_selected_layer());
250 bool canvas_set(false);
251 synfigapp::Action::ParamList param_list;
252 param_list.add("time",get_canvas_interface()->get_time());
253 param_list.add("canvas_interface",get_canvas_interface());
255 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
256 synfigapp::SelectionManager::LayerList::iterator iter;
257 action_copy_->set_sensitive(!layer_list.empty());
258 action_cut_->set_sensitive(!layer_list.empty());
259 action_group_->add(action_copy_);
260 action_group_->add(action_cut_);
262 action_amount_inc_->set_sensitive(!layer_list.empty());
263 action_amount_dec_->set_sensitive(!layer_list.empty());
264 action_amount_->set_sensitive(!layer_list.empty());
265 action_group_->add(action_amount_inc_);
266 action_group_->add(action_amount_dec_);
267 action_group_->add(action_amount_);
269 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
271 update_connection_list.push_back(
272 (*iter)->signal_changed().connect(
273 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
279 param_list.add("canvas",Canvas::Handle((*iter)->get_canvas()));
281 update_connection_list.push_back(
282 (*iter)->get_canvas()->signal_changed().connect(
283 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
287 param_list.add("layer",Layer::Handle(*iter));
291 if(!multiple_selected && layer->get_name()=="PasteCanvas")
293 if (select_all_child_layers_connection)
294 select_all_child_layers_connection.disconnect();
296 select_all_child_layers_connection = action_select_all_child_layers_->signal_activate().connect(
297 sigc::bind(sigc::mem_fun(*layer_tree_,
298 &studio::LayerTree::select_all_children_layers),
299 Layer::LooseHandle(layer)));
301 action_select_all_child_layers_->set_sensitive(true);
303 ui_info+="<menuitem action='select-all-child-layers'/>";
306 action_select_all_child_layers_->set_sensitive(false);
308 handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
309 add_actions_to_group(action_group_, ui_info, param_list, synfigapp::Action::CATEGORY_LAYER);
314 "<popup action='menu-main'>"
315 "<menu action='menu-layer'>" +
318 "<menuitem action='cut' />"
319 "<menuitem action='copy' />"
320 "<menuitem action='paste' />"
325 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
326 #ifdef ONE_ACTION_GROUP
328 get_ui_manager()->insert_action_group(action_group_);
333 LayerActionManager::cut()
336 if(action_group_->get_action("action-layer_remove"))
337 action_group_->get_action("action-layer_remove")->activate();
341 LayerActionManager::copy()
343 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
347 while(!layer_list.empty())
349 clipboard_.push_back(layer_list.front()->clone(guid));
350 layer_list.pop_front();
353 action_paste_->set_sensitive(!clipboard_.empty());
359 LayerActionManager::paste()
363 // Create the action group
364 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
366 Canvas::Handle canvas(get_canvas_interface()->get_canvas());
369 // we are temporarily using the layer to hold something
370 Layer::Handle layer(layer_tree_->get_selected_layer());
373 depth=layer->get_depth();
374 canvas=layer->get_canvas();
377 synfigapp::SelectionManager::LayerList layer_selection;
379 for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
381 layer=(*iter)->clone(guid);
382 layer_selection.push_back(layer);
383 synfigapp::Action::Handle action(synfigapp::Action::create("layer_add"));
389 action->set_param("canvas",canvas);
390 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
391 action->set_param("new",layer);
393 if(!action->is_ready())
398 if(!get_instance()->perform_action(action))
403 // synfig::info("DEPTH=%d",depth);
405 // Action to move the layer (if necessary)
408 synfigapp::Action::Handle action(synfigapp::Action::create("layer_move"));
414 action->set_param("canvas",canvas);
415 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
416 action->set_param("layer",layer);
417 action->set_param("new_index",depth);
419 if(!action->is_ready())
421 //get_ui_interface()->error(_("Move Action Not Ready"));
426 if(!get_instance()->perform_action(action))
428 //get_ui_interface()->error(_("Move Action Not Ready"));
435 // automatically export the Index parameter of Duplicate layers when pasting
437 export_dup_nodes(layer, canvas, index);
439 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
440 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
444 LayerActionManager::export_dup_nodes(synfig::Layer::Handle layer, Canvas::Handle canvas, int &index)
446 // automatically export the Index parameter of Duplicate layers when pasting
447 if (layer->get_name() == "duplicate")
450 String name = strprintf(_("Index %d"), index++);
453 canvas->find_value_node(name);
455 catch (Exception::IDNotFound x)
457 get_canvas_interface()->add_value_node(layer->dynamic_param_list().find("index")->second, name);
463 Layer::ParamList param_list(layer->get_param_list());
464 for (Layer::ParamList::const_iterator iter(param_list.begin())
465 ; iter != param_list.end()
467 if (layer->dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
469 Canvas::Handle subcanvas(iter->second.get(Canvas::Handle()));
470 if (subcanvas && subcanvas->is_inline())
471 for (Context iter = subcanvas->get_context(); iter != subcanvas->end(); iter++)
472 export_dup_nodes(*iter, canvas, index);
475 for (Layer::DynamicParamList::const_iterator iter(layer->dynamic_param_list().begin())
476 ; iter != layer->dynamic_param_list().end()
478 if (iter->second->get_type()==ValueBase::TYPE_CANVAS)
480 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
481 if (canvas->is_inline())
482 //! \todo do we need to implement this? and if so, shouldn't we check all canvases, not just the one at t=0s?
483 warning("%s:%d not yet implemented - do we need to export duplicate valuenodes in dynamic canvas parameters?", __FILE__, __LINE__);
489 LayerActionManager::amount_inc()
493 // Create the action group
494 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Increase Amount"));
495 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
497 for (; !layer_list.empty(); layer_list.pop_front())
499 ValueBase value(layer_list.front()->get_param("amount"));
500 if(value.same_type_as(Real()))
501 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
506 LayerActionManager::amount_dec()
510 // Create the action group
511 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
512 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
514 for (; !layer_list.empty(); layer_list.pop_front())
516 ValueBase value(layer_list.front()->get_param("amount"));
517 if(value.same_type_as(Real()))
518 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);