Typo: 'totaly' -> 'totally'.
[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 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
42
43 /* === M A C R O S ========================================================= */
44
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$");
53
54 /* === G L O B A L S ======================================================= */
55
56 static const int nindex=-1;
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 Action::LayerMove::LayerMove():
63         new_index(0xdeadbeef)
64 {
65 }
66
67 Action::ParamVocab
68 Action::LayerMove::get_param_vocab()
69 {
70         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
71
72         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
73                 .set_local_name(_("Layer"))
74                 .set_desc(_("Layer to be moved"))
75         );
76
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"))
80         );
81
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"))
85                 .set_optional()
86         );
87
88         return ret;
89 }
90
91 bool
92 Action::LayerMove::is_candidate(const ParamList &x)
93 {
94         return candidate_check(get_param_vocab(),x);
95 }
96
97 bool
98 Action::LayerMove::set_param(const synfig::String& name, const Action::Param &param)
99 {
100         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
101         {
102
103                 layer=param.get_layer();
104
105                 return true;
106         }
107
108         if(name=="new_index" && param.get_type()==Param::TYPE_INTEGER)
109         {
110                 new_index=param.get_integer();
111
112                 return true;
113         }
114
115         if(name=="dest_canvas" && param.get_type()==Param::TYPE_CANVAS)
116         {
117                 dest_canvas=param.get_canvas();
118
119                 return true;
120         }
121
122         return Action::CanvasSpecific::set_param(name,param);
123 }
124
125 bool
126 Action::LayerMove::is_ready()const
127 {
128         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
129         if(!layer || (unsigned)new_index==0xdeadbeef)
130                 return false;
131         return Action::CanvasSpecific::is_ready();
132 }
133
134 void
135 Action::LayerMove::perform()
136 {
137         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
138
139         Canvas::Handle subcanvas(layer->get_canvas());
140         src_canvas=subcanvas;
141         if(!dest_canvas)
142                 dest_canvas=subcanvas;
143
144         // Find the iterator for the layer
145         Canvas::iterator iter=find(src_canvas->begin(),src_canvas->end(),layer);
146
147         // If we couldn't find the layer in the canvas, then bail
148         if(*iter!=layer)
149                 throw Error(_("This layer doesn't exist anymore."));
150
151         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
152
153         // If the subcanvas isn't the same as the canvas,
154         // then it had better be an inline canvas. If not,
155         // bail
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"));
159
160         old_index=iter-src_canvas->begin();
161         int depth;
162
163         if(new_index<0)
164                 depth=dest_canvas->size()+new_index+1;
165         else
166                 depth=new_index;
167
168         set_dirty(layer->active());
169
170         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
171
172         // If we were to move it to where it is
173         if(old_index==depth && src_canvas==dest_canvas)
174                 return;
175
176         if(depth>dest_canvas->size())
177                 depth=dest_canvas->size();
178         if(depth<0)
179                 depth=0;
180
181         src_canvas->erase(iter);
182
183         dest_canvas->insert(dest_canvas->begin()+depth,layer);
184         layer->set_canvas(dest_canvas);
185
186         layer->changed();
187         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
188
189         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
190
191         if(get_canvas_interface())
192         {
193                 if(src_canvas==dest_canvas)
194                 {
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);
199                         else            // Moved
200                         {
201                                 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
202                         }
203                 }
204                 else
205                 {
206                         get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
207                 }
208         }
209         else synfig::warning("CanvasInterface not set on action");
210
211         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
212 }
213
214 void
215 Action::LayerMove::undo()
216 {
217         // Find the iterator for the layer
218         Canvas::iterator iter=find(dest_canvas->begin(),dest_canvas->end(),layer);
219
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."));
223
224         // If we were to move it to where it is
225         if(old_index==new_index && src_canvas==dest_canvas)
226                 return;
227
228         // Mark ourselves as dirty if necessary
229         set_dirty(layer->active());
230
231         dest_canvas->erase(iter);
232
233         src_canvas->insert(src_canvas->begin()+old_index,layer);
234         layer->set_canvas(src_canvas);
235
236         layer->changed();
237         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
238
239         // Execute any signals
240         if(get_canvas_interface())
241         {
242                 if(src_canvas==dest_canvas)
243                 {
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);
248                         else            // Moved
249                         {
250                         get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
251                         }
252                 }
253                 else
254                 {
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);
258                 }
259         }
260         else synfig::warning("CanvasInterface not set on action");
261 }