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