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 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 <synfigapp/action_param.h>
37 #include <synfigapp/selectionmanager.h>
43 /* === U S I N G =========================================================== */
47 using namespace synfig;
48 using namespace studio;
50 static const guint no_prev_popup((guint)-1);
52 /* === M A C R O S ========================================================= */
54 //#define ONE_ACTION_GROUP 1
56 /* === G L O B A L S ======================================================= */
58 /* === P R O C E D U R E S ================================================= */
60 /* === M E T H O D S ======================================================= */
62 LayerActionManager::LayerActionManager():
63 action_group_(Gtk::ActionGroup::create()),
64 popup_id_(no_prev_popup),
65 action_group_copy_paste(Gtk::ActionGroup::create()),
68 action_cut_=Gtk::Action::create(
70 Gtk::StockID("gtk-cut")
72 action_cut_->signal_activate().connect(
75 &LayerActionManager::cut
78 action_copy_=Gtk::Action::create(
80 Gtk::StockID("gtk-copy")
82 action_copy_->signal_activate().connect(
85 &LayerActionManager::copy
88 action_paste_=Gtk::Action::create(
90 Gtk::StockID("gtk-paste")
92 action_paste_->signal_activate().connect(
95 &LayerActionManager::paste
100 action_amount_inc_=Gtk::Action::create(
102 Gtk::StockID("gtk-add"),
103 _("Increase Amount"),_("Increase Amount")
105 action_amount_inc_->signal_activate().connect(
108 &LayerActionManager::amount_inc
112 action_amount_dec_=Gtk::Action::create(
114 Gtk::StockID("gtk-remove"),
115 _("Decrease Amount"),_("Decrease Amount")
117 action_amount_dec_->signal_activate().connect(
120 &LayerActionManager::amount_dec
124 action_amount_=Gtk::Action::create(
126 Gtk::StockID("gtk-index"),
127 _("Amount"),_("Amount")
133 LayerActionManager::~LayerActionManager()
138 LayerActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
142 #ifdef ONE_ACTION_GROUP
143 if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
145 if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
152 LayerActionManager::set_layer_tree(LayerTree* x)
154 selection_changed_connection.disconnect();
158 selection_changed_connection=layer_tree_->get_selection()->signal_changed().connect(
159 sigc::mem_fun(*this,&LayerActionManager::queue_refresh)
165 LayerActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
171 LayerActionManager::clear()
175 // Clear out old stuff
176 if(popup_id_!=no_prev_popup)
178 get_ui_manager()->remove_ui(popup_id_);
179 if(action_group_)get_ui_manager()->ensure_update();
180 popup_id_=no_prev_popup;
181 if(action_group_)while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
182 #ifdef ONE_ACTION_GROUP
184 if(action_group_)get_ui_manager()->remove_action_group(action_group_);
185 action_group_=Gtk::ActionGroup::create();
190 while(!update_connection_list.empty())
192 update_connection_list.front().disconnect();
193 update_connection_list.pop_front();
198 LayerActionManager::queue_refresh()
203 //queue_refresh_connection.disconnect();
204 queue_refresh_connection=Glib::signal_idle().connect(
206 sigc::mem_fun(*this,&LayerActionManager::refresh),
215 LayerActionManager::refresh()
220 //queue_refresh_connection.disconnect();
226 // Make sure we are ready
227 if(!ui_manager_ || !layer_tree_ || !canvas_interface_)
229 synfig::error("LayerActionManager::refresh(): Not ready!");
236 action_paste_->set_sensitive(!clipboard_.empty());
237 action_group_->add(action_paste_);
239 if(layer_tree_->get_selection()->count_selected_rows()!=0)
241 bool multiple_selected(layer_tree_->get_selection()->count_selected_rows()>1);
242 Layer::Handle layer(layer_tree_->get_selected_layer());
245 bool canvas_set(false);
246 synfigapp::Action::ParamList param_list;
247 param_list.add("time",get_canvas_interface()->get_time());
248 param_list.add("canvas_interface",get_canvas_interface());
250 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
251 synfigapp::SelectionManager::LayerList::iterator iter;
252 action_copy_->set_sensitive(!layer_list.empty());
253 action_cut_->set_sensitive(!layer_list.empty());
254 action_group_->add(action_copy_);
255 action_group_->add(action_cut_);
257 action_amount_inc_->set_sensitive(!layer_list.empty());
258 action_amount_dec_->set_sensitive(!layer_list.empty());
259 action_amount_->set_sensitive(!layer_list.empty());
260 action_group_->add(action_amount_inc_);
261 action_group_->add(action_amount_dec_);
262 action_group_->add(action_amount_);
264 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
266 update_connection_list.push_back(
267 (*iter)->signal_changed().connect(
268 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
274 param_list.add("canvas",Canvas::Handle((*iter)->get_canvas()));
276 update_connection_list.push_back(
277 (*iter)->get_canvas()->signal_changed().connect(
278 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
282 param_list.add("layer",Layer::Handle(*iter));
286 if(!multiple_selected && layer->get_name()=="PasteCanvas")
288 action_group_->add(Gtk::Action::create(
289 "select-all-child-layers",
290 _("Select All Child Layers")
295 &studio::LayerTree::select_all_children_layers
297 Layer::LooseHandle(layer)
300 ui_info+="<menuitem action='select-all-child-layers'/>";
302 handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
303 add_actions_to_group(action_group_, ui_info, param_list, synfigapp::Action::CATEGORY_LAYER);
307 ui_info="<ui><popup action='menu-main'><menu action='menu-layer'>"+ui_info+"<separator/><menuitem action='cut' /><menuitem action='copy' /><menuitem action='paste' /><separator/></menu></popup></ui>";
308 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
309 #ifdef ONE_ACTION_GROUP
311 get_ui_manager()->insert_action_group(action_group_);
316 LayerActionManager::cut()
319 if(action_group_->get_action("action-layer_remove"))
320 action_group_->get_action("action-layer_remove")->activate();
324 LayerActionManager::copy()
326 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
330 while(!layer_list.empty())
332 clipboard_.push_back(layer_list.front()->clone(guid));
333 layer_list.pop_front();
336 action_paste_->set_sensitive(!clipboard_.empty());
342 LayerActionManager::paste()
346 // Create the action group
347 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
349 Canvas::Handle canvas(get_canvas_interface()->get_canvas());
352 // we are temporarily using the layer to hold something
353 Layer::Handle layer(layer_tree_->get_selected_layer());
356 depth=layer->get_depth();
357 canvas=layer->get_canvas();
360 synfigapp::SelectionManager::LayerList layer_selection;
362 for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
364 layer=(*iter)->clone(guid);
365 layer_selection.push_back(layer);
366 synfigapp::Action::Handle action(synfigapp::Action::create("layer_add"));
372 action->set_param("canvas",canvas);
373 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
374 action->set_param("new",layer);
376 if(!action->is_ready())
381 if(!get_instance()->perform_action(action))
386 synfig::info("DEPTH=%d",depth);
387 // Action to move the layer (if necessary)
390 synfigapp::Action::Handle action(synfigapp::Action::create("layer_move"));
396 action->set_param("canvas",canvas);
397 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
398 action->set_param("layer",layer);
399 action->set_param("new_index",depth);
401 if(!action->is_ready())
403 //get_ui_interface()->error(_("Move Action Not Ready"));
408 if(!get_instance()->perform_action(action))
410 //get_ui_interface()->error(_("Move Action Not Ready"));
417 // automatically export the Index parameter of Duplicate layers when pasting
418 if (layer->get_name() == "duplicate")
419 for (int i = 1; ; i++)
421 String name = strprintf(_("Index %d"), i);
424 canvas->find_value_node(name);
426 catch (Exception::IDNotFound x)
428 get_canvas_interface()->add_value_node(layer->dynamic_param_list().find("index")->second, name);
433 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
434 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
438 LayerActionManager::amount_inc()
442 // Create the action group
443 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
446 group.set_name(_("Increase Amount"));
448 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
450 while(!layer_list.empty())
452 ValueBase value(layer_list.front()->get_param("amount"));
453 if(value.same_type_as(Real()))
455 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
457 layer_list.pop_front();
462 LayerActionManager::amount_dec()
466 // Create the action group
467 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
470 group.set_name(_("Increase Amount"));
472 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
474 while(!layer_list.empty())
476 ValueBase value(layer_list.front()->get_param("amount"));
477 if(value.same_type_as(Real()))
479 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
481 layer_list.pop_front();