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 <synfig/layer_pastecanvas.h>
37 #include <synfigapp/action_param.h>
39 #include <synfigapp/selectionmanager.h>
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
50 using namespace studio;
52 static const guint no_prev_popup((guint)-1);
54 /* === M A C R O S ========================================================= */
56 //#define ONE_ACTION_GROUP 1
58 /* === G L O B A L S ======================================================= */
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 LayerActionManager::LayerActionManager():
65 action_group_(Gtk::ActionGroup::create("action_group_layer_action_manager")),
66 popup_id_(no_prev_popup),
67 action_group_copy_paste(Gtk::ActionGroup::create("action_group_copy_paste")),
70 action_cut_=Gtk::Action::create(
72 Gtk::StockID("gtk-cut")
74 action_cut_->signal_activate().connect(
77 &LayerActionManager::cut
80 action_copy_=Gtk::Action::create(
82 Gtk::StockID("gtk-copy")
84 action_copy_->signal_activate().connect(
87 &LayerActionManager::copy
90 action_paste_=Gtk::Action::create(
92 Gtk::StockID("gtk-paste")
94 action_paste_->signal_activate().connect(
97 &LayerActionManager::paste
102 action_amount_inc_=Gtk::Action::create(
104 Gtk::StockID("gtk-add"),
105 _("Increase Amount"),_("Increase Amount")
107 action_amount_inc_->signal_activate().connect(
110 &LayerActionManager::amount_inc
114 action_amount_dec_=Gtk::Action::create(
116 Gtk::StockID("gtk-remove"),
117 _("Decrease Amount"),_("Decrease Amount")
119 action_amount_dec_->signal_activate().connect(
122 &LayerActionManager::amount_dec
126 action_amount_=Gtk::Action::create(
128 Gtk::StockID("gtk-index"),
129 _("Amount"),_("Amount")
132 action_select_all_child_layers_=Gtk::Action::create(
133 "select-all-child-layers",
134 Gtk::StockID("synfig-select_all_child_layers"),
135 _("Select All Child Layers"),_("Select All Child Layers")
137 action_select_all_child_layers_->set_sensitive(false);
140 LayerActionManager::~LayerActionManager()
145 LayerActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
149 #ifdef ONE_ACTION_GROUP
150 if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
152 if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
159 LayerActionManager::set_layer_tree(LayerTree* x)
161 selection_changed_connection.disconnect();
165 selection_changed_connection=layer_tree_->get_selection()->signal_changed().connect(
166 sigc::mem_fun(*this,&LayerActionManager::queue_refresh)
172 LayerActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
178 LayerActionManager::clear()
182 // Clear out old stuff
183 if(popup_id_!=no_prev_popup)
185 get_ui_manager()->remove_ui(popup_id_);
186 if(action_group_)get_ui_manager()->ensure_update();
187 popup_id_=no_prev_popup;
188 if(action_group_)while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
189 #ifdef ONE_ACTION_GROUP
191 if(action_group_)get_ui_manager()->remove_action_group(action_group_);
192 action_group_=Gtk::ActionGroup::create("action_group_layer_action_manager");
197 while(!update_connection_list.empty())
199 update_connection_list.front().disconnect();
200 update_connection_list.pop_front();
205 LayerActionManager::queue_refresh()
210 //queue_refresh_connection.disconnect();
211 queue_refresh_connection=Glib::signal_idle().connect(
213 sigc::mem_fun(*this,&LayerActionManager::refresh),
222 LayerActionManager::refresh()
227 //queue_refresh_connection.disconnect();
233 // Make sure we are ready
234 if(!ui_manager_ || !layer_tree_ || !canvas_interface_)
236 synfig::error("LayerActionManager::refresh(): Not ready!");
242 action_paste_->set_sensitive(!clipboard_.empty());
243 action_group_->add(action_paste_);
245 if(layer_tree_->get_selection()->count_selected_rows()!=0)
247 bool multiple_selected(layer_tree_->get_selection()->count_selected_rows()>1);
248 Layer::Handle layer(layer_tree_->get_selected_layer());
251 bool canvas_set(false);
252 synfigapp::Action::ParamList param_list;
253 param_list.add("time",get_canvas_interface()->get_time());
254 param_list.add("canvas_interface",get_canvas_interface());
256 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
257 synfigapp::SelectionManager::LayerList::iterator iter;
258 action_copy_->set_sensitive(!layer_list.empty());
259 action_cut_->set_sensitive(!layer_list.empty());
260 action_group_->add(action_copy_);
261 action_group_->add(action_cut_);
263 action_amount_inc_->set_sensitive(!layer_list.empty());
264 action_amount_dec_->set_sensitive(!layer_list.empty());
265 action_amount_->set_sensitive(!layer_list.empty());
266 action_group_->add(action_amount_inc_);
267 action_group_->add(action_amount_dec_);
268 action_group_->add(action_amount_);
270 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
272 update_connection_list.push_back(
273 (*iter)->signal_changed().connect(
274 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
280 param_list.add("canvas",Canvas::Handle((*iter)->get_canvas()));
282 update_connection_list.push_back(
283 (*iter)->get_canvas()->signal_changed().connect(
284 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
288 param_list.add("layer",Layer::Handle(*iter));
292 if(!multiple_selected && layer->get_name()=="PasteCanvas")
294 if (select_all_child_layers_connection)
295 select_all_child_layers_connection.disconnect();
297 select_all_child_layers_connection = action_select_all_child_layers_->signal_activate().connect(
298 sigc::bind(sigc::mem_fun(*layer_tree_,
299 &studio::LayerTree::select_all_children_layers),
300 Layer::LooseHandle(layer)));
302 action_select_all_child_layers_->set_sensitive(true);
304 ui_info+="<menuitem action='select-all-child-layers'/>";
307 action_select_all_child_layers_->set_sensitive(false);
309 handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
310 add_actions_to_group(action_group_, ui_info, param_list, synfigapp::Action::CATEGORY_LAYER);
315 "<popup action='menu-main'>"
316 "<menu action='menu-layer'>" +
319 "<menuitem action='cut' />"
320 "<menuitem action='copy' />"
321 "<menuitem action='paste' />"
326 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
327 #ifdef ONE_ACTION_GROUP
329 get_ui_manager()->insert_action_group(action_group_);
334 LayerActionManager::cut()
337 if(action_group_->get_action("action-LayerRemove"))
338 action_group_->get_action("action-LayerRemove")->activate();
342 LayerActionManager::copy()
344 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
348 while(!layer_list.empty())
350 clipboard_.push_back(layer_list.front()->clone(guid));
351 layer_list.pop_front();
354 action_paste_->set_sensitive(!clipboard_.empty());
360 LayerActionManager::paste()
364 // Create the action group
365 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
367 Canvas::Handle canvas(get_canvas_interface()->get_canvas());
370 // we are temporarily using the layer to hold something
371 Layer::Handle layer(layer_tree_->get_selected_layer());
374 depth=layer->get_depth();
375 canvas=layer->get_canvas();
378 synfigapp::SelectionManager::LayerList layer_selection;
380 for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
382 layer=(*iter)->clone(guid);
383 layer_selection.push_back(layer);
384 synfigapp::Action::Handle action(synfigapp::Action::create("LayerAdd"));
390 action->set_param("canvas",canvas);
391 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
392 action->set_param("new",layer);
394 if(!action->is_ready())
399 if(!get_instance()->perform_action(action))
404 etl::handle<Layer_PasteCanvas> paste = etl::handle<Layer_PasteCanvas>::cast_dynamic(layer);
405 if (paste) paste->update_renddesc();
407 // synfig::info("DEPTH=%d",depth);
409 // Action to move the layer (if necessary)
412 synfigapp::Action::Handle action(synfigapp::Action::create("LayerMove"));
418 action->set_param("canvas",canvas);
419 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
420 action->set_param("layer",layer);
421 action->set_param("new_index",depth);
423 if(!action->is_ready())
425 //get_ui_interface()->error(_("Move Action Not Ready"));
430 if(!get_instance()->perform_action(action))
432 //get_ui_interface()->error(_("Move Action Not Ready"));
439 // automatically export the Index parameter of Duplicate layers when pasting
441 export_dup_nodes(layer, canvas, index);
443 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
444 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
448 LayerActionManager::export_dup_nodes(synfig::Layer::Handle layer, Canvas::Handle canvas, int &index)
450 // automatically export the Index parameter of Duplicate layers when pasting
451 if (layer->get_name() == "duplicate")
454 String name = strprintf(_("Index %d"), index++);
457 canvas->find_value_node(name);
459 catch (Exception::IDNotFound x)
461 get_canvas_interface()->add_value_node(layer->dynamic_param_list().find("index")->second, name);
467 Layer::ParamList param_list(layer->get_param_list());
468 for (Layer::ParamList::const_iterator iter(param_list.begin())
469 ; iter != param_list.end()
471 if (layer->dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
473 Canvas::Handle subcanvas(iter->second.get(Canvas::Handle()));
474 if (subcanvas && subcanvas->is_inline())
475 for (Context iter = subcanvas->get_context(); iter != subcanvas->end(); iter++)
476 export_dup_nodes(*iter, canvas, index);
479 for (Layer::DynamicParamList::const_iterator iter(layer->dynamic_param_list().begin())
480 ; iter != layer->dynamic_param_list().end()
482 if (iter->second->get_type()==ValueBase::TYPE_CANVAS)
484 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
485 if (canvas->is_inline())
486 //! \todo do we need to implement this? and if so, shouldn't we check all canvases, not just the one at t=0s?
487 warning("%s:%d not yet implemented - do we need to export duplicate valuenodes in dynamic canvas parameters?", __FILE__, __LINE__);
493 LayerActionManager::amount_inc()
497 // Create the action group
498 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Increase Amount"));
499 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
501 for (; !layer_list.empty(); layer_list.pop_front())
503 ValueBase value(layer_list.front()->get_param("amount"));
504 if(value.same_type_as(Real()))
505 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
510 LayerActionManager::amount_dec()
514 // Create the action group
515 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
516 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
518 for (; !layer_list.empty(); layer_list.pop_front())
520 ValueBase value(layer_list.front()->get_param("amount"));
521 if(value.same_type_as(Real()))
522 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);