Added a \todo note questioning a part of the code.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / layerencapsulate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerencapsulate.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) 2007 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 "layerencapsulate.h"
34 #include "layeradd.h"
35 #include "layerremove.h"
36 #include <synfigapp/canvasinterface.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(Action::LayerEncapsulate);
49 ACTION_SET_NAME(Action::LayerEncapsulate,"layer_encapsulate");
50 ACTION_SET_LOCAL_NAME(Action::LayerEncapsulate,"Encapsulate");
51 ACTION_SET_TASK(Action::LayerEncapsulate,"encapsulate");
52 ACTION_SET_CATEGORY(Action::LayerEncapsulate,Action::CATEGORY_LAYER);
53 ACTION_SET_PRIORITY(Action::LayerEncapsulate,0);
54 ACTION_SET_VERSION(Action::LayerEncapsulate,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerEncapsulate,"$Id$");
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Action::LayerEncapsulate::LayerEncapsulate()
64 {
65 }
66
67 Action::ParamVocab
68 Action::LayerEncapsulate::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 encapsulated"))
75                 .set_supports_multiple()
76         );
77
78         return ret;
79 }
80
81 bool
82 Action::LayerEncapsulate::is_candidate(const ParamList &x)
83 {
84         return candidate_check(get_param_vocab(),x);
85 }
86
87 bool
88 Action::LayerEncapsulate::set_param(const synfig::String& name, const Action::Param &param)
89 {
90         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
91         {
92                 layers.push_back(param.get_layer());
93
94                 return true;
95         }
96
97         return Action::CanvasSpecific::set_param(name,param);
98 }
99
100 bool
101 Action::LayerEncapsulate::is_ready()const
102 {
103         if(layers.empty())
104                 return false;
105         return Action::CanvasSpecific::is_ready();
106 }
107
108 int
109 Action::LayerEncapsulate::lowest_depth()const
110 {
111         std::list<synfig::Layer::Handle>::const_iterator iter;
112         int lowest_depth(0x7fffffff);
113
114         for(iter=layers.begin();iter!=layers.end();++iter)
115         {
116                 int depth((*iter)->get_depth());
117                 if(depth<lowest_depth)
118                         lowest_depth=depth;
119         }
120         if(lowest_depth==0x7fffffff)
121                 return 0;
122         return lowest_depth;
123 }
124
125 void
126 Action::LayerEncapsulate::prepare()
127 {
128
129         if(!first_time())
130                 return;
131
132         if(layers.empty())
133                 throw Error("No layers to encapsulate");
134
135         // First create the new canvas and layer
136         if(!child_canvas)
137                 child_canvas=Canvas::create_inline(get_canvas());
138
139         Layer::Handle new_layer(Layer::create("PasteCanvas"));
140
141         new_layer->set_param("canvas",child_canvas);
142
143         int target_depth(lowest_depth());
144
145         // Add the layer
146         {
147                 Action::Handle action(LayerAdd::create());
148
149                 action->set_param("canvas",get_canvas());
150                 action->set_param("canvas_interface",get_canvas_interface());
151                 action->set_param("new",new_layer);
152
153                 add_action(action);
154         }
155
156         // Move the layer
157         {
158                 Action::Handle action(Action::create("layer_move"));
159
160                 assert(action);
161
162                 action->set_param("canvas",get_canvas());
163                 action->set_param("canvas_interface",get_canvas_interface());
164                 action->set_param("layer",new_layer);
165                 action->set_param("new_index",target_depth);
166
167                 add_action(action);
168         }
169
170         std::list<synfig::Layer::Handle>::reverse_iterator iter;
171
172         for(iter=layers.rbegin();iter!=layers.rend();++iter)
173         {
174                 Layer::Handle layer(*iter);
175
176                 Canvas::Handle subcanvas(layer->get_canvas());
177
178                 // Find the iterator for the layer
179                 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
180
181                 // If we couldn't find the layer in the canvas, then bail
182                 if(*iter!=layer)
183                         throw Error(_("This layer doesn't exist anymore."));
184
185                 if(!subcanvas)
186                         throw Error(_("This layer doesn't have a parent canvas"));
187
188                 // If the subcanvas isn't the same as the canvas,
189                 // then it had better be an inline canvas. If not,
190                 // bail
191                 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
192                         throw Error(_("This layer doesn't belong to this canvas anymore"));
193
194                 if(get_canvas()!=subcanvas)
195                         throw Error(_("get_canvas()!=subcanvas"));
196
197                 // Remove the layer from the old canvas
198                 {
199                         Action::Handle action(LayerRemove::create());
200
201                         action->set_param("canvas",subcanvas);
202                         action->set_param("canvas_interface",get_canvas_interface());
203                         action->set_param("layer",layer);
204
205                         add_action(action);
206                 }
207                 // Add the layer to the new canvas
208                 {
209                         Action::Handle action(LayerAdd::create());
210
211                         action->set_param("canvas",child_canvas);
212                         action->set_param("canvas_interface",get_canvas_interface());
213                         action->set_param("new",layer);
214
215                         add_action(action);
216                 }
217         }
218 }