my log
[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: layermove.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "layermove.h"
32 #include <synfigapp/canvasinterface.h>
33
34 #endif
35
36 using namespace std;
37 using namespace etl;
38 using namespace synfig;
39 using namespace synfigapp;
40 using namespace Action;
41
42 /* === M A C R O S ========================================================= */
43
44 ACTION_INIT(Action::LayerMove);
45 ACTION_SET_NAME(Action::LayerMove,"layer_move");
46 ACTION_SET_LOCAL_NAME(Action::LayerMove,_("Move Layer"));
47 ACTION_SET_TASK(Action::LayerMove,"move");
48 ACTION_SET_CATEGORY(Action::LayerMove,Action::CATEGORY_LAYER);
49 ACTION_SET_PRIORITY(Action::LayerMove,0);
50 ACTION_SET_VERSION(Action::LayerMove,"0.0");
51 ACTION_SET_CVS_ID(Action::LayerMove,"$Id: layermove.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
52
53 /* === G L O B A L S ======================================================= */
54
55 static const int nindex=-1;
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /* === M E T H O D S ======================================================= */
60
61 Action::LayerMove::LayerMove():
62         new_index(0xdeadbeef)
63 {
64 }
65
66 Action::ParamVocab
67 Action::LayerMove::get_param_vocab()
68 {
69         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
70         
71         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
72                 .set_local_name(_("Layer"))
73                 .set_desc(_("Layer to be moved"))
74         );
75
76         ret.push_back(ParamDesc("new_index",Param::TYPE_INTEGER)
77                 .set_local_name(_("New Index"))
78                 .set_desc(_("Where the layer is to be moved to"))
79         );
80
81         ret.push_back(ParamDesc("dest_canvas",Param::TYPE_CANVAS)
82                 .set_local_name(_("Destination Canvas"))
83                 .set_desc(_("The canvas the layer is to be moved to"))
84                 .set_optional()
85         );
86         
87         return ret;
88 }
89
90 bool
91 Action::LayerMove::is_canidate(const ParamList &x)
92 {
93         return canidate_check(get_param_vocab(),x);
94 }
95
96 bool
97 Action::LayerMove::set_param(const synfig::String& name, const Action::Param &param)
98 {
99         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
100         {
101
102                 layer=param.get_layer();
103                 
104                 return true;
105         }
106
107         if(name=="new_index" && param.get_type()==Param::TYPE_INTEGER)
108         {
109                 new_index=param.get_integer();
110                 
111                 return true;
112         }
113
114         if(name=="dest_canvas" && param.get_type()==Param::TYPE_CANVAS)
115         {
116                 dest_canvas=param.get_canvas();
117                 
118                 return true;
119         }
120
121         return Action::CanvasSpecific::set_param(name,param);
122 }
123
124 bool
125 Action::LayerMove::is_ready()const
126 {
127         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
128         if(!layer || (unsigned)new_index==0xdeadbeef)
129                 return false;
130         return Action::CanvasSpecific::is_ready();
131 }
132
133 void
134 Action::LayerMove::perform()
135 {               
136         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
137
138         Canvas::Handle subcanvas(layer->get_canvas());
139         src_canvas=subcanvas;
140         if(!dest_canvas)
141                 dest_canvas=subcanvas;
142                 
143         // Find the iterator for the layer
144         Canvas::iterator iter=find(src_canvas->begin(),src_canvas->end(),layer);
145         
146         // If we couldn't find the layer in the canvas, then bail
147         if(*iter!=layer)
148                 throw Error(_("This layer doesn't exist anymore."));
149
150         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
151
152         // If the subcanvas isn't the same as the canvas,
153         // then it had better be an inline canvas. If not,
154         // bail
155         //if(get_canvas()!=subcanvas && !subcanvas->is_inline())
156         if(get_canvas()->get_root()!=dest_canvas->get_root() || get_canvas()->get_root()!=src_canvas->get_root())
157                 throw Error(_("You cannot directly move layers across compositions"));
158         
159         old_index=iter-src_canvas->begin();
160         int depth;
161         
162         if(new_index<0)
163                 depth=dest_canvas->size()+new_index+1;
164         else
165                 depth=new_index;
166         
167         set_dirty(layer->active());
168
169         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
170
171         // If we were to move it to where it is
172         if(old_index==depth && src_canvas==dest_canvas)
173                 return;
174         
175         if(depth>dest_canvas->size())
176                 depth=dest_canvas->size();
177         if(depth<0)
178                 depth=0;
179                 
180         src_canvas->erase(iter);
181         
182         dest_canvas->insert(dest_canvas->begin()+depth,layer);          
183         layer->set_canvas(dest_canvas);
184         
185         layer->changed();
186         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
187         
188         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
189         
190         if(get_canvas_interface())
191         {
192                 if(src_canvas==dest_canvas)
193                 {
194                         if(new_index==old_index-1)      // Raise
195                                 get_canvas_interface()->signal_layer_raised()(layer);
196                         else if(new_index==old_index+1) // Lower
197                                 get_canvas_interface()->signal_layer_lowered()(layer);
198                         else            // Moved
199                         {
200                                 get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
201                         }
202                 }
203                 else
204                 {
205                         get_canvas_interface()->signal_layer_moved()(layer,depth,dest_canvas);
206                 }
207         }
208         else synfig::warning("CanvasInterface not set on action");
209
210         synfig::info(__FILE__":%d: layer->count()=%d",__LINE__,layer.count());
211 }
212
213 void
214 Action::LayerMove::undo()
215 {
216         // Find the iterator for the layer
217         Canvas::iterator iter=find(dest_canvas->begin(),dest_canvas->end(),layer);
218         
219         // If we couldn't find the layer in the canvas, then bail
220         if(*iter!=layer || (get_canvas()!=dest_canvas && !dest_canvas->is_inline()))
221                 throw Error(_("This layer doesn't exist anymore."));
222         
223         // If we were to move it to where it is
224         if(old_index==new_index && src_canvas==dest_canvas)
225                 return;
226
227         // Mark ourselves as dirty if necessary
228         set_dirty(layer->active());
229
230         dest_canvas->erase(iter);
231         
232         src_canvas->insert(src_canvas->begin()+old_index,layer);
233         layer->set_canvas(src_canvas);
234
235         layer->changed();
236         dest_canvas->changed(); if(dest_canvas!=src_canvas) src_canvas->changed();
237         
238         // Execute any signals
239         if(get_canvas_interface())
240         {
241                 if(src_canvas==dest_canvas)
242                 {
243                         if(new_index==old_index+1)      // Raise
244                                 get_canvas_interface()->signal_layer_raised()(layer);
245                         else if(new_index==old_index-1) // Lower
246                                 get_canvas_interface()->signal_layer_lowered()(layer);
247                         else            // Moved
248                         {
249                         get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
250                         }
251                 }
252                 else
253                 {
254                         get_canvas_interface()->signal_layer_moved()(layer,old_index,src_canvas);
255                         //get_canvas_interface()->signal_layer_removed()(layer);        
256                         //get_canvas_interface()->signal_layer_inserted()(layer,old_index);
257                 }
258         }
259         else synfig::warning("CanvasInterface not set on action");
260 }