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
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 "layermove.h"
33 #include <synfigapp/canvasinterface.h>
35 #include <synfigapp/general.h>
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
45 /* === M A C R O S ========================================================= */
47 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerMove);
48 ACTION_SET_NAME(Action::LayerMove,"layer_move");
49 ACTION_SET_LOCAL_NAME(Action::LayerMove,N_("Move Layer"));
50 ACTION_SET_TASK(Action::LayerMove,"move");
51 ACTION_SET_CATEGORY(Action::LayerMove,Action::CATEGORY_LAYER);
52 ACTION_SET_PRIORITY(Action::LayerMove,0);
53 ACTION_SET_VERSION(Action::LayerMove,"0.0");
54 ACTION_SET_CVS_ID(Action::LayerMove,"$Id$");
56 /* === G L O B A L S ======================================================= */
58 static const int nindex=-1;
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 Action::LayerMove::LayerMove():
70 Action::LayerMove::get_local_name()const
73 return strprintf("%s '%s'", _("Move Layer"), layer->get_non_empty_description().c_str());
75 return _("Move Layer");
79 Action::LayerMove::get_param_vocab()
81 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
83 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
84 .set_local_name(_("Layer"))
85 .set_desc(_("Layer to be moved"))
88 ret.push_back(ParamDesc("new_index",Param::TYPE_INTEGER)
89 .set_local_name(_("New Index"))
90 .set_desc(_("Where the layer is to be moved to"))
93 ret.push_back(ParamDesc("dest_canvas",Param::TYPE_CANVAS)
94 .set_local_name(_("Destination Canvas"))
95 .set_desc(_("The canvas the layer is to be moved to"))
103 Action::LayerMove::is_candidate(const ParamList &x)
105 return candidate_check(get_param_vocab(),x);
109 Action::LayerMove::set_param(const synfig::String& name, const Action::Param ¶m)
111 if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
114 layer=param.get_layer();
119 if(name=="new_index" && param.get_type()==Param::TYPE_INTEGER)
121 new_index=param.get_integer();
126 if(name=="dest_canvas" && param.get_type()==Param::TYPE_CANVAS)
128 dest_canvas=param.get_canvas();
133 return Action::CanvasSpecific::set_param(name,param);
137 Action::LayerMove::is_ready()const
139 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
140 if(!layer || (unsigned)new_index==0xdeadbeef)
142 return Action::CanvasSpecific::is_ready();
146 Action::LayerMove::perform()
148 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
150 Canvas::Handle subcanvas(layer->get_canvas());
151 src_canvas=subcanvas;
153 dest_canvas=subcanvas;
155 // Find the iterator for the layer
156 Canvas::iterator iter=find(src_canvas->begin(),src_canvas->end(),layer);
158 // If we couldn't find the layer in the canvas, then bail
160 throw Error(_("This layer doesn't exist anymore."));
162 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
164 // If the subcanvas isn't the same as the canvas,
165 // then it had better be an inline canvas. If not,
167 //if(get_canvas()!=subcanvas && !subcanvas->is_inline())
168 if(get_canvas()->get_root()!=dest_canvas->get_root() || get_canvas()->get_root()!=src_canvas->get_root())
169 throw Error(_("You cannot directly move layers across compositions"));
171 old_index=iter-src_canvas->begin();
175 depth=dest_canvas->size()+new_index+1;
179 set_dirty(layer->active());
181 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
183 // If we were to move it to where it is
184 if(old_index==depth && src_canvas==dest_canvas)
187 if(depth>dest_canvas->size())
188 depth=dest_canvas->size();
192 src_canvas->erase(iter);
194 dest_canvas->insert(dest_canvas->begin()+depth,layer);
195 layer->set_canvas(dest_canvas);
198 dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
200 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
202 if(get_canvas_interface())
204 if(src_canvas==dest_canvas)
206 if(new_index==old_index-1) // Raise
207 get_canvas_interface()->signal_layer_raised()(layer);
208 else if(new_index==old_index+1) // Lower
209 get_canvas_interface()->signal_layer_lowered()(layer);
212 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
217 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
220 else synfig::warning("CanvasInterface not set on action");
222 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
226 Action::LayerMove::undo()
228 // Find the iterator for the layer
229 Canvas::iterator iter=find(dest_canvas->begin(),dest_canvas->end(),layer);
231 // If we couldn't find the layer in the canvas, then bail
232 if(*iter!=layer || (get_canvas()!=dest_canvas && !dest_canvas->is_inline()))
233 throw Error(_("This layer doesn't exist anymore."));
235 // If we were to move it to where it is
236 if(old_index==new_index && src_canvas==dest_canvas)
239 // Mark ourselves as dirty if necessary
240 set_dirty(layer->active());
242 dest_canvas->erase(iter);
244 src_canvas->insert(src_canvas->begin()+old_index,layer);
245 layer->set_canvas(src_canvas);
248 dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
250 // Execute any signals
251 if(get_canvas_interface())
253 if(src_canvas==dest_canvas)
255 if(new_index==old_index+1) // Raise
256 get_canvas_interface()->signal_layer_raised()(layer);
257 else if(new_index==old_index-1) // Lower
258 get_canvas_interface()->signal_layer_lowered()(layer);
261 get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
266 get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
267 //get_canvas_interface()->signal_layer_removed()(layer);
268 //get_canvas_interface()->signal_layer_inserted()(layer,old_index);
271 else synfig::warning("CanvasInterface not set on action");