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>
41 /* === U S I N G =========================================================== */
45 using namespace synfig;
46 using namespace studio;
48 static const guint no_prev_popup((guint)-1);
50 /* === M A C R O S ========================================================= */
52 //#define ONE_ACTION_GROUP 1
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
60 LayerActionManager::LayerActionManager():
61 action_group_(Gtk::ActionGroup::create()),
62 popup_id_(no_prev_popup),
63 action_group_copy_paste(Gtk::ActionGroup::create()),
66 action_cut_=Gtk::Action::create(
68 Gtk::StockID("gtk-cut")
70 action_cut_->signal_activate().connect(
73 &LayerActionManager::cut
76 action_copy_=Gtk::Action::create(
78 Gtk::StockID("gtk-copy")
80 action_copy_->signal_activate().connect(
83 &LayerActionManager::copy
86 action_paste_=Gtk::Action::create(
88 Gtk::StockID("gtk-paste")
90 action_paste_->signal_activate().connect(
93 &LayerActionManager::paste
98 action_amount_inc_=Gtk::Action::create(
100 Gtk::StockID("gtk-add"),
101 _("Increase Amount"),_("Increase Amount")
103 action_amount_inc_->signal_activate().connect(
106 &LayerActionManager::amount_inc
110 action_amount_dec_=Gtk::Action::create(
112 Gtk::StockID("gtk-remove"),
113 _("Decrease Amount"),_("Decrease Amount")
115 action_amount_dec_->signal_activate().connect(
118 &LayerActionManager::amount_dec
122 action_amount_=Gtk::Action::create(
124 Gtk::StockID("gtk-index"),
125 _("Amount"),_("Amount")
131 LayerActionManager::~LayerActionManager()
136 LayerActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
140 #ifdef ONE_ACTION_GROUP
141 if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
143 if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
150 LayerActionManager::set_layer_tree(LayerTree* x)
152 selection_changed_connection.disconnect();
156 selection_changed_connection=layer_tree_->get_selection()->signal_changed().connect(
157 sigc::mem_fun(*this,&LayerActionManager::queue_refresh)
163 LayerActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
169 LayerActionManager::clear()
173 // Clear out old stuff
174 if(popup_id_!=no_prev_popup)
176 get_ui_manager()->remove_ui(popup_id_);
177 if(action_group_)get_ui_manager()->ensure_update();
178 popup_id_=no_prev_popup;
179 if(action_group_)while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
180 #ifdef ONE_ACTION_GROUP
182 if(action_group_)get_ui_manager()->remove_action_group(action_group_);
183 action_group_=Gtk::ActionGroup::create();
188 while(!update_connection_list.empty())
190 update_connection_list.front().disconnect();
191 update_connection_list.pop_front();
196 LayerActionManager::queue_refresh()
201 //queue_refresh_connection.disconnect();
202 queue_refresh_connection=Glib::signal_idle().connect(
204 sigc::mem_fun(*this,&LayerActionManager::refresh),
213 LayerActionManager::refresh()
218 //queue_refresh_connection.disconnect();
224 // Make sure we are ready
225 if(!ui_manager_ || !layer_tree_ || !canvas_interface_)
227 synfig::error("LayerActionManager::refresh(): Not ready!");
234 action_paste_->set_sensitive(!clipboard_.empty());
235 action_group_->add(action_paste_);
237 if(layer_tree_->get_selection()->count_selected_rows()!=0)
239 bool multiple_selected(layer_tree_->get_selection()->count_selected_rows()>1);
240 Layer::Handle layer(layer_tree_->get_selected_layer());
243 bool canvas_set(false);
244 synfigapp::Action::ParamList param_list;
245 param_list.add("time",get_canvas_interface()->get_time());
246 param_list.add("canvas_interface",get_canvas_interface());
248 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
249 synfigapp::SelectionManager::LayerList::iterator iter;
250 action_copy_->set_sensitive(!layer_list.empty());
251 action_cut_->set_sensitive(!layer_list.empty());
252 action_group_->add(action_copy_);
253 action_group_->add(action_cut_);
255 action_amount_inc_->set_sensitive(!layer_list.empty());
256 action_amount_dec_->set_sensitive(!layer_list.empty());
257 action_amount_->set_sensitive(!layer_list.empty());
258 action_group_->add(action_amount_inc_);
259 action_group_->add(action_amount_dec_);
260 action_group_->add(action_amount_);
262 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
264 update_connection_list.push_back(
265 (*iter)->signal_changed().connect(
266 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
272 param_list.add("canvas",Canvas::Handle((*iter)->get_canvas()));
274 update_connection_list.push_back(
275 (*iter)->get_canvas()->signal_changed().connect(
276 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
280 param_list.add("layer",Layer::Handle(*iter));
284 if(!multiple_selected && layer->get_name()=="PasteCanvas")
286 action_group_->add(Gtk::Action::create(
287 "select-all-child-layers",
288 _("Select All Child Layers")
293 &studio::LayerTree::select_all_children_layers
295 Layer::LooseHandle(layer)
298 ui_info+="<menuitem action='select-all-child-layers'/>";
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);
305 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>";
306 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
307 #ifdef ONE_ACTION_GROUP
309 get_ui_manager()->insert_action_group(action_group_);
315 LayerActionManager::cut()
318 if(action_group_->get_action("action-layer_remove"))
319 action_group_->get_action("action-layer_remove")->activate();
323 LayerActionManager::copy()
325 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
329 while(!layer_list.empty())
331 clipboard_.push_back(layer_list.front()->clone(guid));
332 layer_list.pop_front();
335 action_paste_->set_sensitive(!clipboard_.empty());
341 LayerActionManager::paste()
345 // Create the action group
346 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
348 Canvas::Handle canvas(get_canvas_interface()->get_canvas());
351 // we are temporarily using the layer to hold something
352 Layer::Handle layer(layer_tree_->get_selected_layer());
355 depth=layer->get_depth();
356 canvas=layer->get_canvas();
359 synfigapp::SelectionManager::LayerList layer_selection;
361 for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
363 layer=(*iter)->clone(guid);
364 layer_selection.push_back(layer);
365 synfigapp::Action::Handle action(synfigapp::Action::create("layer_add"));
371 action->set_param("canvas",canvas);
372 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
373 action->set_param("new",layer);
375 if(!action->is_ready())
380 if(!get_instance()->perform_action(action))
385 synfig::info("DEPTH=%d",depth);
386 // Action to move the layer (if necessary)
389 synfigapp::Action::Handle action(synfigapp::Action::create("layer_move"));
395 action->set_param("canvas",canvas);
396 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
397 action->set_param("layer",layer);
398 action->set_param("new_index",depth);
400 if(!action->is_ready())
402 //get_ui_interface()->error(_("Move Action Not Ready"));
407 if(!get_instance()->perform_action(action))
409 //get_ui_interface()->error(_("Move Action Not Ready"));
416 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
417 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
421 LayerActionManager::amount_inc()
425 // Create the action group
426 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
429 group.set_name(_("Increase Amount"));
431 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
433 while(!layer_list.empty())
435 ValueBase value(layer_list.front()->get_param("amount"));
436 if(value.same_type_as(Real()))
438 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
440 layer_list.pop_front();
445 LayerActionManager::amount_dec()
449 // Create the action group
450 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
453 group.set_name(_("Increase Amount"));
455 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
457 while(!layer_list.empty())
459 ValueBase value(layer_list.front()->get_param("amount"));
460 if(value.same_type_as(Real()))
462 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
464 layer_list.pop_front();