1 /* === S Y N F I G ========================================================= */
2 /*! \file layermove.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 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 "layermove.h"
34 #include <synfigapp/canvasinterface.h>
36 #include <synfigapp/general.h>
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
46 /* === M A C R O S ========================================================= */
48 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerMove);
49 ACTION_SET_NAME(Action::LayerMove,"layer_move");
50 ACTION_SET_LOCAL_NAME(Action::LayerMove,N_("Move Layer"));
51 ACTION_SET_TASK(Action::LayerMove,"move");
52 ACTION_SET_CATEGORY(Action::LayerMove,Action::CATEGORY_LAYER);
53 ACTION_SET_PRIORITY(Action::LayerMove,0);
54 ACTION_SET_VERSION(Action::LayerMove,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerMove,"$Id$");
57 /* === G L O B A L S ======================================================= */
59 static const int nindex=-1;
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 Action::LayerMove::LayerMove():
71 Action::LayerMove::get_local_name()const
74 return strprintf("%s '%s'", _("Move Layer"), layer->get_non_empty_description().c_str());
76 return _("Move Layer");
80 Action::LayerMove::get_param_vocab()
82 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
84 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
85 .set_local_name(_("Layer"))
86 .set_desc(_("Layer to be moved"))
89 ret.push_back(ParamDesc("new_index",Param::TYPE_INTEGER)
90 .set_local_name(_("New Index"))
91 .set_desc(_("Where the layer is to be moved to"))
94 ret.push_back(ParamDesc("dest_canvas",Param::TYPE_CANVAS)
95 .set_local_name(_("Destination Canvas"))
96 .set_desc(_("The canvas the layer is to be moved to"))
104 Action::LayerMove::is_candidate(const ParamList &x)
106 return candidate_check(get_param_vocab(),x);
110 Action::LayerMove::set_param(const synfig::String& name, const Action::Param ¶m)
112 if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
115 layer=param.get_layer();
120 if(name=="new_index" && param.get_type()==Param::TYPE_INTEGER)
122 new_index=param.get_integer();
127 if(name=="dest_canvas" && param.get_type()==Param::TYPE_CANVAS)
129 dest_canvas=param.get_canvas();
134 return Action::CanvasSpecific::set_param(name,param);
138 Action::LayerMove::is_ready()const
140 // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
141 if(!layer || (unsigned)new_index==0xdeadbeef)
143 return Action::CanvasSpecific::is_ready();
147 Action::LayerMove::perform()
149 // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
151 Canvas::Handle subcanvas(layer->get_canvas());
152 src_canvas=subcanvas;
154 dest_canvas=subcanvas;
156 // Find the iterator for the layer
157 Canvas::iterator iter=find(src_canvas->begin(),src_canvas->end(),layer);
159 // If we couldn't find the layer in the canvas, then bail
161 throw Error(_("This layer doesn't exist anymore."));
163 // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
165 // If the subcanvas isn't the same as the canvas,
166 // then it had better be an inline canvas. If not,
168 //if(get_canvas()!=subcanvas && !subcanvas->is_inline())
169 if(get_canvas()->get_root()!=dest_canvas->get_root() || get_canvas()->get_root()!=src_canvas->get_root())
170 throw Error(_("You cannot directly move layers across compositions"));
172 old_index=iter-src_canvas->begin();
176 depth=dest_canvas->size()+new_index+1;
180 set_dirty(layer->active());
182 // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
184 // If we were to move it to where it is
185 if(old_index==depth && src_canvas==dest_canvas)
188 if(depth>dest_canvas->size())
189 depth=dest_canvas->size();
193 src_canvas->erase(iter);
195 dest_canvas->insert(dest_canvas->begin()+depth,layer);
196 layer->set_canvas(dest_canvas);
199 dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
201 // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
203 if(get_canvas_interface())
205 if(src_canvas==dest_canvas)
207 if(new_index==old_index-1) // Raise
208 get_canvas_interface()->signal_layer_raised()(layer);
209 else if(new_index==old_index+1) // Lower
210 get_canvas_interface()->signal_layer_lowered()(layer);
213 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
218 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
221 else synfig::warning("CanvasInterface not set on action");
223 // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
227 Action::LayerMove::undo()
229 // Find the iterator for the layer
230 Canvas::iterator iter=find(dest_canvas->begin(),dest_canvas->end(),layer);
232 // If we couldn't find the layer in the canvas, then bail
233 if(*iter!=layer || (get_canvas()!=dest_canvas && !dest_canvas->is_inline()))
234 throw Error(_("This layer doesn't exist anymore."));
236 // If we were to move it to where it is
237 if(old_index==new_index && src_canvas==dest_canvas)
240 // Mark ourselves as dirty if necessary
241 set_dirty(layer->active());
243 dest_canvas->erase(iter);
245 src_canvas->insert(src_canvas->begin()+old_index,layer);
246 layer->set_canvas(src_canvas);
249 dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
251 // Execute any signals
252 if(get_canvas_interface())
254 if(src_canvas==dest_canvas)
256 if(new_index==old_index+1) // Raise
257 get_canvas_interface()->signal_layer_raised()(layer);
258 else if(new_index==old_index-1) // Lower
259 get_canvas_interface()->signal_layer_lowered()(layer);
262 get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
267 get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
268 //get_canvas_interface()->signal_layer_removed()(layer);
269 //get_canvas_interface()->signal_layer_inserted()(layer,old_index);
272 else synfig::warning("CanvasInterface not set on action");