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")
134 LayerActionManager::~LayerActionManager()
139 LayerActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
143 #ifdef ONE_ACTION_GROUP
144 if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
146 if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
153 LayerActionManager::set_layer_tree(LayerTree* x)
155 selection_changed_connection.disconnect();
159 selection_changed_connection=layer_tree_->get_selection()->signal_changed().connect(
160 sigc::mem_fun(*this,&LayerActionManager::queue_refresh)
166 LayerActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
172 LayerActionManager::clear()
176 // Clear out old stuff
177 if(popup_id_!=no_prev_popup)
179 get_ui_manager()->remove_ui(popup_id_);
180 if(action_group_)get_ui_manager()->ensure_update();
181 popup_id_=no_prev_popup;
182 if(action_group_)while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
183 #ifdef ONE_ACTION_GROUP
185 if(action_group_)get_ui_manager()->remove_action_group(action_group_);
186 action_group_=Gtk::ActionGroup::create();
191 while(!update_connection_list.empty())
193 update_connection_list.front().disconnect();
194 update_connection_list.pop_front();
199 LayerActionManager::queue_refresh()
204 //queue_refresh_connection.disconnect();
205 queue_refresh_connection=Glib::signal_idle().connect(
207 sigc::mem_fun(*this,&LayerActionManager::refresh),
216 LayerActionManager::refresh()
221 //queue_refresh_connection.disconnect();
227 // Make sure we are ready
228 if(!ui_manager_ || !layer_tree_ || !canvas_interface_)
230 synfig::error("LayerActionManager::refresh(): Not ready!");
235 String ui_info, ui_toolbar_info;
237 action_paste_->set_sensitive(!clipboard_.empty());
238 action_group_->add(action_paste_);
240 if(layer_tree_->get_selection()->count_selected_rows()!=0)
242 bool multiple_selected(layer_tree_->get_selection()->count_selected_rows()>1);
243 Layer::Handle layer(layer_tree_->get_selected_layer());
246 bool canvas_set(false);
247 synfigapp::Action::ParamList param_list;
248 param_list.add("time",get_canvas_interface()->get_time());
249 param_list.add("canvas_interface",get_canvas_interface());
251 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
252 synfigapp::SelectionManager::LayerList::iterator iter;
253 action_copy_->set_sensitive(!layer_list.empty());
254 action_cut_->set_sensitive(!layer_list.empty());
255 action_group_->add(action_copy_);
256 action_group_->add(action_cut_);
258 action_amount_inc_->set_sensitive(!layer_list.empty());
259 action_amount_dec_->set_sensitive(!layer_list.empty());
260 action_amount_->set_sensitive(!layer_list.empty());
261 action_group_->add(action_amount_inc_);
262 action_group_->add(action_amount_dec_);
263 action_group_->add(action_amount_);
265 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
267 update_connection_list.push_back(
268 (*iter)->signal_changed().connect(
269 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
275 param_list.add("canvas",Canvas::Handle((*iter)->get_canvas()));
277 update_connection_list.push_back(
278 (*iter)->get_canvas()->signal_changed().connect(
279 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
283 param_list.add("layer",Layer::Handle(*iter));
287 if(!multiple_selected && layer->get_name()=="PasteCanvas")
289 action_group_->add(Gtk::Action::create(
290 "select-all-child-layers",
291 Gtk::StockID("synfig-select_all_child_layers"),
292 _("Select All Child Layers"),
293 _("Select All Child Layers")),
294 sigc::bind(sigc::mem_fun(*layer_tree_,
295 &studio::LayerTree::select_all_children_layers),
296 Layer::LooseHandle(layer)));
297 ui_info+="<menuitem action='select-all-child-layers'/>";
298 ui_toolbar_info+="<toolbar action='toolbar-layer'><toolitem action='select-all-child-layers'/></toolbar>";
300 handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
301 add_actions_to_group(action_group_, ui_info, param_list, synfigapp::Action::CATEGORY_LAYER);
306 "<popup action='menu-main'>"
307 "<menu action='menu-layer'>" +
310 "<menuitem action='cut' />"
311 "<menuitem action='copy' />"
312 "<menuitem action='paste' />"
318 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
319 #ifdef ONE_ACTION_GROUP
321 get_ui_manager()->insert_action_group(action_group_);
326 LayerActionManager::cut()
329 if(action_group_->get_action("action-layer_remove"))
330 action_group_->get_action("action-layer_remove")->activate();
334 LayerActionManager::copy()
336 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
340 while(!layer_list.empty())
342 clipboard_.push_back(layer_list.front()->clone(guid));
343 layer_list.pop_front();
346 action_paste_->set_sensitive(!clipboard_.empty());
352 LayerActionManager::paste()
356 // Create the action group
357 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
359 Canvas::Handle canvas(get_canvas_interface()->get_canvas());
362 // we are temporarily using the layer to hold something
363 Layer::Handle layer(layer_tree_->get_selected_layer());
366 depth=layer->get_depth();
367 canvas=layer->get_canvas();
370 synfigapp::SelectionManager::LayerList layer_selection;
372 for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
374 layer=(*iter)->clone(guid);
375 layer_selection.push_back(layer);
376 synfigapp::Action::Handle action(synfigapp::Action::create("layer_add"));
382 action->set_param("canvas",canvas);
383 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
384 action->set_param("new",layer);
386 if(!action->is_ready())
391 if(!get_instance()->perform_action(action))
396 // synfig::info("DEPTH=%d",depth);
398 // Action to move the layer (if necessary)
401 synfigapp::Action::Handle action(synfigapp::Action::create("layer_move"));
407 action->set_param("canvas",canvas);
408 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
409 action->set_param("layer",layer);
410 action->set_param("new_index",depth);
412 if(!action->is_ready())
414 //get_ui_interface()->error(_("Move Action Not Ready"));
419 if(!get_instance()->perform_action(action))
421 //get_ui_interface()->error(_("Move Action Not Ready"));
428 // automatically export the Index parameter of Duplicate layers when pasting
430 export_dup_nodes(layer, canvas, index);
432 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
433 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
437 LayerActionManager::export_dup_nodes(synfig::Layer::Handle layer, Canvas::Handle canvas, int &index)
439 // automatically export the Index parameter of Duplicate layers when pasting
440 if (layer->get_name() == "duplicate")
443 String name = strprintf(_("Index %d"), index++);
446 canvas->find_value_node(name);
448 catch (Exception::IDNotFound x)
450 get_canvas_interface()->add_value_node(layer->dynamic_param_list().find("index")->second, name);
456 Layer::ParamList param_list(layer->get_param_list());
457 for (Layer::ParamList::const_iterator iter(param_list.begin())
458 ; iter != param_list.end()
460 if (layer->dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
462 Canvas::Handle subcanvas(iter->second.get(Canvas::Handle()));
463 if (subcanvas && subcanvas->is_inline())
464 for (Context iter = subcanvas->get_context(); iter != subcanvas->end(); iter++)
465 export_dup_nodes(*iter, canvas, index);
468 for (Layer::DynamicParamList::const_iterator iter(layer->dynamic_param_list().begin())
469 ; iter != layer->dynamic_param_list().end()
471 if (iter->second->get_type()==ValueBase::TYPE_CANVAS)
473 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
474 if (canvas->is_inline())
475 //! \todo do we need to implement this? and if so, shouldn't we check all canvases, not just the one at t=0s?
476 warning("%s:%d not yet implemented - do we need to export duplicate valuenodes in dynamic canvas parameters?", __FILE__, __LINE__);
482 LayerActionManager::amount_inc()
486 // Create the action group
487 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
490 group.set_name(_("Increase Amount"));
492 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
494 while(!layer_list.empty())
496 ValueBase value(layer_list.front()->get_param("amount"));
497 if(value.same_type_as(Real()))
499 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
501 layer_list.pop_front();
506 LayerActionManager::amount_dec()
510 // Create the action group
511 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
514 group.set_name(_("Increase Amount"));
516 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
518 while(!layer_list.empty())
520 ValueBase value(layer_list.front()->get_param("amount"));
521 if(value.same_type_as(Real()))
523 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
525 layer_list.pop_front();