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
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "layeractionmanager.h"
33 #include "layertree.h"
34 #include <synfigapp/action_param.h>
36 #include <synfigapp/selectionmanager.h>
40 /* === U S I N G =========================================================== */
44 using namespace synfig;
45 using namespace studio;
47 static const guint no_prev_popup((guint)-1);
49 /* === M A C R O S ========================================================= */
51 //#define ONE_ACTION_GROUP 1
53 /* === G L O B A L S ======================================================= */
55 /* === P R O C E D U R E S ================================================= */
57 /* === M E T H O D S ======================================================= */
59 LayerActionManager::LayerActionManager():
60 action_group_(Gtk::ActionGroup::create()),
61 action_group_copy_paste(Gtk::ActionGroup::create()),
62 popup_id_(no_prev_popup),
65 action_cut_=Gtk::Action::create(
67 Gtk::StockID("gtk-cut")
69 action_cut_->signal_activate().connect(
72 &LayerActionManager::cut
75 action_copy_=Gtk::Action::create(
77 Gtk::StockID("gtk-copy")
79 action_copy_->signal_activate().connect(
82 &LayerActionManager::copy
85 action_paste_=Gtk::Action::create(
87 Gtk::StockID("gtk-paste")
89 action_paste_->signal_activate().connect(
92 &LayerActionManager::paste
97 action_amount_inc_=Gtk::Action::create(
99 Gtk::StockID("gtk-add"),
100 _("Increase Amount"),_("Increase Amount")
102 action_amount_inc_->signal_activate().connect(
105 &LayerActionManager::amount_inc
109 action_amount_dec_=Gtk::Action::create(
111 Gtk::StockID("gtk-remove"),
112 _("Decrease Amount"),_("Decrease Amount")
114 action_amount_dec_->signal_activate().connect(
117 &LayerActionManager::amount_dec
121 action_amount_=Gtk::Action::create(
123 Gtk::StockID("gtk-index"),
124 _("Amount"),_("Amount")
130 LayerActionManager::~LayerActionManager()
135 LayerActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
139 #ifdef ONE_ACTION_GROUP
140 if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
142 if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
149 LayerActionManager::set_layer_tree(LayerTree* x)
151 selection_changed_connection.disconnect();
155 selection_changed_connection=layer_tree_->get_selection()->signal_changed().connect(
156 sigc::mem_fun(*this,&LayerActionManager::queue_refresh)
162 LayerActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
168 LayerActionManager::clear()
172 // Clear out old stuff
173 if(popup_id_!=no_prev_popup)
175 get_ui_manager()->remove_ui(popup_id_);
176 if(action_group_)get_ui_manager()->ensure_update();
177 popup_id_=no_prev_popup;
178 if(action_group_)while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
179 #ifdef ONE_ACTION_GROUP
181 if(action_group_)get_ui_manager()->remove_action_group(action_group_);
182 action_group_=Gtk::ActionGroup::create();
187 while(!update_connection_list.empty())
189 update_connection_list.front().disconnect();
190 update_connection_list.pop_front();
195 LayerActionManager::queue_refresh()
200 //queue_refresh_connection.disconnect();
201 queue_refresh_connection=Glib::signal_idle().connect(
203 sigc::mem_fun(*this,&LayerActionManager::refresh),
212 LayerActionManager::refresh()
217 //queue_refresh_connection.disconnect();
223 // Make sure we are ready
224 if(!ui_manager_ || !layer_tree_ || !canvas_interface_)
226 synfig::error("LayerActionManager::refresh(): Not ready!");
233 action_paste_->set_sensitive(!clipboard_.empty());
234 action_group_->add(action_paste_);
236 if(layer_tree_->get_selection()->count_selected_rows()!=0)
238 bool multiple_selected(layer_tree_->get_selection()->count_selected_rows()>1);
239 Layer::Handle layer(layer_tree_->get_selected_layer());
242 bool canvas_set(false);
243 synfigapp::Action::ParamList param_list;
244 param_list.add("time",get_canvas_interface()->get_time());
245 param_list.add("canvas_interface",get_canvas_interface());
247 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
248 synfigapp::SelectionManager::LayerList::iterator iter;
249 action_copy_->set_sensitive(!layer_list.empty());
250 action_cut_->set_sensitive(!layer_list.empty());
251 action_group_->add(action_copy_);
252 action_group_->add(action_cut_);
254 action_amount_inc_->set_sensitive(!layer_list.empty());
255 action_amount_dec_->set_sensitive(!layer_list.empty());
256 action_amount_->set_sensitive(!layer_list.empty());
257 action_group_->add(action_amount_inc_);
258 action_group_->add(action_amount_dec_);
259 action_group_->add(action_amount_);
261 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
263 update_connection_list.push_back(
264 (*iter)->signal_changed().connect(
265 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
271 param_list.add("canvas",Canvas::Handle((*iter)->get_canvas()));
273 update_connection_list.push_back(
274 (*iter)->get_canvas()->signal_changed().connect(
275 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
279 param_list.add("layer",Layer::Handle(*iter));
283 if(!multiple_selected && layer->get_name()=="PasteCanvas")
285 action_group_->add(Gtk::Action::create(
286 "select-all-child-layers",
287 _("Select All Child Layers")
292 &studio::LayerTree::select_all_children_layers
294 Layer::LooseHandle(layer)
297 ui_info+="<menuitem action='select-all-child-layers'/>";
299 handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
300 add_actions_to_group(action_group_, ui_info, param_list, synfigapp::Action::CATEGORY_LAYER);
304 ui_info="<ui><menubar action='menu-main'><menu action='menu-layer'>"+ui_info+"<separator/><menuitem action='cut' /><menuitem action='copy' /><menuitem action='paste' /><separator/></menu></menubar></ui>";
305 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
306 #ifdef ONE_ACTION_GROUP
308 get_ui_manager()->insert_action_group(action_group_);
314 LayerActionManager::cut()
317 if(action_group_->get_action("action-layer_remove"))
318 action_group_->get_action("action-layer_remove")->activate();
322 LayerActionManager::copy()
324 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
328 while(!layer_list.empty())
330 clipboard_.push_back(layer_list.front()->clone(guid));
331 layer_list.pop_front();
334 action_paste_->set_sensitive(!clipboard_.empty());
340 LayerActionManager::paste()
344 // Create the action group
345 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
347 Canvas::Handle canvas(get_canvas_interface()->get_canvas());
350 // we are temporarily using the layer to hold something
351 Layer::Handle layer(layer_tree_->get_selected_layer());
354 depth=layer->get_depth();
355 canvas=layer->get_canvas();
358 synfigapp::SelectionManager::LayerList layer_selection;
360 for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
362 layer=(*iter)->clone(guid);
363 layer_selection.push_back(layer);
364 synfigapp::Action::Handle action(synfigapp::Action::create("layer_add"));
370 action->set_param("canvas",canvas);
371 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
372 action->set_param("new",layer);
374 if(!action->is_ready())
379 if(!get_instance()->perform_action(action))
384 synfig::info("DEPTH=%d",depth);
385 // Action to move the layer (if necessary)
388 synfigapp::Action::Handle action(synfigapp::Action::create("layer_move"));
394 action->set_param("canvas",canvas);
395 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
396 action->set_param("layer",layer);
397 action->set_param("new_index",depth);
399 if(!action->is_ready())
401 //get_ui_interface()->error(_("Move Action Not Ready"));
406 if(!get_instance()->perform_action(action))
408 //get_ui_interface()->error(_("Move Action Not Ready"));
415 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
416 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
420 LayerActionManager::amount_inc()
424 // Create the action group
425 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
428 group.set_name(_("Increase Amount"));
430 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
432 while(!layer_list.empty())
434 ValueBase value(layer_list.front()->get_param("amount"));
435 if(value.same_as(Real()))
437 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
439 layer_list.pop_front();
444 LayerActionManager::amount_dec()
448 // Create the action group
449 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
452 group.set_name(_("Increase Amount"));
454 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
456 while(!layer_list.empty())
458 ValueBase value(layer_list.front()->get_param("amount"));
459 if(value.same_as(Real()))
461 get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
463 layer_list.pop_front();