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>
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
43 /* === M A C R O S ========================================================= */
45 ACTION_INIT(Action::LayerMove);
46 ACTION_SET_NAME(Action::LayerMove,"layer_move");
47 ACTION_SET_LOCAL_NAME(Action::LayerMove,_("Move Layer"));
48 ACTION_SET_TASK(Action::LayerMove,"move");
49 ACTION_SET_CATEGORY(Action::LayerMove,Action::CATEGORY_LAYER);
50 ACTION_SET_PRIORITY(Action::LayerMove,0);
51 ACTION_SET_VERSION(Action::LayerMove,"0.0");
52 ACTION_SET_CVS_ID(Action::LayerMove,"$Id$");
54 /* === G L O B A L S ======================================================= */
56 static const int nindex=-1;
58 /* === P R O C E D U R E S ================================================= */
60 /* === M E T H O D S ======================================================= */
62 Action::LayerMove::LayerMove():
68 Action::LayerMove::get_param_vocab()
70 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
72 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
73 .set_local_name(_("Layer"))
74 .set_desc(_("Layer to be moved"))
77 ret.push_back(ParamDesc("new_index",Param::TYPE_INTEGER)
78 .set_local_name(_("New Index"))
79 .set_desc(_("Where the layer is to be moved to"))
82 ret.push_back(ParamDesc("dest_canvas",Param::TYPE_CANVAS)
83 .set_local_name(_("Destination Canvas"))
84 .set_desc(_("The canvas the layer is to be moved to"))
92 Action::LayerMove::is_candidate(const ParamList &x)
94 return candidate_check(get_param_vocab(),x);
98 Action::LayerMove::set_param(const synfig::String& name, const Action::Param ¶m)
100 if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
103 layer=param.get_layer();
108 if(name=="new_index" && param.get_type()==Param::TYPE_INTEGER)
110 new_index=param.get_integer();
115 if(name=="dest_canvas" && param.get_type()==Param::TYPE_CANVAS)
117 dest_canvas=param.get_canvas();
122 return Action::CanvasSpecific::set_param(name,param);
126 Action::LayerMove::is_ready()const
128 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
129 if(!layer || (unsigned)new_index==0xdeadbeef)
131 return Action::CanvasSpecific::is_ready();
135 Action::LayerMove::perform()
137 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
139 Canvas::Handle subcanvas(layer->get_canvas());
140 src_canvas=subcanvas;
142 dest_canvas=subcanvas;
144 // Find the iterator for the layer
145 Canvas::iterator iter=find(src_canvas->begin(),src_canvas->end(),layer);
147 // If we couldn't find the layer in the canvas, then bail
149 throw Error(_("This layer doesn't exist anymore."));
151 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
153 // If the subcanvas isn't the same as the canvas,
154 // then it had better be an inline canvas. If not,
156 //if(get_canvas()!=subcanvas && !subcanvas->is_inline())
157 if(get_canvas()->get_root()!=dest_canvas->get_root() || get_canvas()->get_root()!=src_canvas->get_root())
158 throw Error(_("You cannot directly move layers across compositions"));
160 old_index=iter-src_canvas->begin();
164 depth=dest_canvas->size()+new_index+1;
168 set_dirty(layer->active());
170 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
172 // If we were to move it to where it is
173 if(old_index==depth && src_canvas==dest_canvas)
176 if(depth>dest_canvas->size())
177 depth=dest_canvas->size();
181 src_canvas->erase(iter);
183 dest_canvas->insert(dest_canvas->begin()+depth,layer);
184 layer->set_canvas(dest_canvas);
187 dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
189 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
191 if(get_canvas_interface())
193 if(src_canvas==dest_canvas)
195 if(new_index==old_index-1) // Raise
196 get_canvas_interface()->signal_layer_raised()(layer);
197 else if(new_index==old_index+1) // Lower
198 get_canvas_interface()->signal_layer_lowered()(layer);
201 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
206 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
209 else synfig::warning("CanvasInterface not set on action");
211 synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
215 Action::LayerMove::undo()
217 // Find the iterator for the layer
218 Canvas::iterator iter=find(dest_canvas->begin(),dest_canvas->end(),layer);
220 // If we couldn't find the layer in the canvas, then bail
221 if(*iter!=layer || (get_canvas()!=dest_canvas && !dest_canvas->is_inline()))
222 throw Error(_("This layer doesn't exist anymore."));
224 // If we were to move it to where it is
225 if(old_index==new_index && src_canvas==dest_canvas)
228 // Mark ourselves as dirty if necessary
229 set_dirty(layer->active());
231 dest_canvas->erase(iter);
233 src_canvas->insert(src_canvas->begin()+old_index,layer);
234 layer->set_canvas(src_canvas);
237 dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
239 // Execute any signals
240 if(get_canvas_interface())
242 if(src_canvas==dest_canvas)
244 if(new_index==old_index+1) // Raise
245 get_canvas_interface()->signal_layer_raised()(layer);
246 else if(new_index==old_index-1) // Lower
247 get_canvas_interface()->signal_layer_lowered()(layer);
250 get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
255 get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
256 //get_canvas_interface()->signal_layer_removed()(layer);
257 //get_canvas_interface()->signal_layer_inserted()(layer,old_index);
260 else synfig::warning("CanvasInterface not set on action");