be220a580fdb075f8d74c95f75919f8dd9d08ac6
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / layermove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layermove.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "layermove.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #include <synfigapp/general.h>
36
37 #endif
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
44
45 /* === M A C R O S ========================================================= */
46
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$");
55
56 /* === G L O B A L S ======================================================= */
57
58 static const int nindex=-1;
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 Action::LayerMove::LayerMove():
65         new_index(0xdeadbeef)
66 {
67 }
68
69 synfig::String
70 Action::LayerMove::get_local_name()const
71 {
72         if (layer)
73                 return strprintf("%s '%s'", _("Move Layer"), layer->get_non_empty_description().c_str());
74         else
75                 return _("Move Layer");
76 }
77
78 Action::ParamVocab
79 Action::LayerMove::get_param_vocab()
80 {
81         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
82
83         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
84                 .set_local_name(_("Layer"))
85                 .set_desc(_("Layer to be moved"))
86         );
87
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"))
91         );
92
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"))
96                 .set_optional()
97         );
98
99         return ret;
100 }
101
102 bool
103 Action::LayerMove::is_candidate(const ParamList &x)
104 {
105         return candidate_check(get_param_vocab(),x);
106 }
107
108 bool
109 Action::LayerMove::set_param(const synfig::String& name, const Action::Param &param)
110 {
111         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
112         {
113
114                 layer=param.get_layer();
115
116                 return true;
117         }
118
119         if(name=="new_index" && param.get_type()==Param::TYPE_INTEGER)
120         {
121                 new_index=param.get_integer();
122
123                 return true;
124         }
125
126         if(name=="dest_canvas" && param.get_type()==Param::TYPE_CANVAS)
127         {
128                 dest_canvas=param.get_canvas();
129
130                 return true;
131         }
132
133         return Action::CanvasSpecific::set_param(name,param);
134 }
135
136 bool
137 Action::LayerMove::is_ready()const
138 {
139         // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
140         if(!layer || (unsigned)new_index==0xdeadbeef)
141                 return false;
142         return Action::CanvasSpecific::is_ready();
143 }
144
145 void
146 Action::LayerMove::perform()
147 {
148         // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
149
150         Canvas::Handle subcanvas(layer->get_canvas());
151         src_canvas=subcanvas;
152         if(!dest_canvas)
153                 dest_canvas=subcanvas;
154
155         // Find the iterator for the layer
156         Canvas::iterator iter=find(src_canvas->begin(),src_canvas->end(),layer);
157
158         // If we couldn't find the layer in the canvas, then bail
159         if(*iter!=layer)
160                 throw Error(_("This layer doesn't exist anymore."));
161
162         // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
163
164         // If the subcanvas isn't the same as the canvas,
165         // then it had better be an inline canvas. If not,
166         // bail
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"));
170
171         old_index=iter-src_canvas->begin();
172         int depth;
173
174         if(new_index<0)
175                 depth=dest_canvas->size()+new_index+1;
176         else
177                 depth=new_index;
178
179         set_dirty(layer->active());
180
181         // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
182
183         // If we were to move it to where it is
184         if(old_index==depth && src_canvas==dest_canvas)
185                 return;
186
187         if(depth>dest_canvas->size())
188                 depth=dest_canvas->size();
189         if(depth<0)
190                 depth=0;
191
192         src_canvas->erase(iter);
193
194         dest_canvas->insert(dest_canvas->begin()+depth,layer);
195         layer->set_canvas(dest_canvas);
196
197         layer->changed();
198         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
199
200         // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
201
202         if(get_canvas_interface())
203         {
204                 if(src_canvas==dest_canvas)
205                 {
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);
210                         else            // Moved
211                         {
212                                 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
213                         }
214                 }
215                 else
216                 {
217                         get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
218                 }
219         }
220         else synfig::warning("CanvasInterface not set on action");
221
222         // synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
223 }
224
225 void
226 Action::LayerMove::undo()
227 {
228         // Find the iterator for the layer
229         Canvas::iterator iter=find(dest_canvas->begin(),dest_canvas->end(),layer);
230
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."));
234
235         // If we were to move it to where it is
236         if(old_index==new_index && src_canvas==dest_canvas)
237                 return;
238
239         // Mark ourselves as dirty if necessary
240         set_dirty(layer->active());
241
242         dest_canvas->erase(iter);
243
244         src_canvas->insert(src_canvas->begin()+old_index,layer);
245         layer->set_canvas(src_canvas);
246
247         layer->changed();
248         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
249
250         // Execute any signals
251         if(get_canvas_interface())
252         {
253                 if(src_canvas==dest_canvas)
254                 {
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);
259                         else            // Moved
260                         {
261                         get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
262                         }
263                 }
264                 else
265                 {
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);
269                 }
270         }
271         else synfig::warning("CanvasInterface not set on action");
272 }