1 /* === S Y N F I G ========================================================= */
2 /*! \file layerencapsulateswitch.cpp
3 ** \brief Template File
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** ......... ... 2014 Ivan Mahonin
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 <synfig/general.h>
33
34 #include "layerencapsulateswitch.h"
35 #include "layeradd.h"
36 #include "layerremove.h"
37 #include <synfigapp/canvasinterface.h>
38
39 #include <synfigapp/localization.h>
40
41 #endif
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace synfigapp;
47 using namespace Action;
48
49 /* === M A C R O S ========================================================= */
50
51 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerEncapsulateSwitch);
52 ACTION_SET_NAME(Action::LayerEncapsulateSwitch,"LayerEncapsulateSwitch");
53 ACTION_SET_LOCAL_NAME(Action::LayerEncapsulateSwitch,N_("Group Layer into Switch"));
54 ACTION_SET_TASK(Action::LayerEncapsulateSwitch,"encapsulate_switch");
55 ACTION_SET_CATEGORY(Action::LayerEncapsulateSwitch,Action::CATEGORY_LAYER);
56 ACTION_SET_PRIORITY(Action::LayerEncapsulateSwitch,0);
57 ACTION_SET_VERSION(Action::LayerEncapsulateSwitch,"0.0");
58 ACTION_SET_CVS_ID(Action::LayerEncapsulateSwitch,"$Id$");
59
60 /* === G L O B A L S ======================================================= */
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
LayerEncapsulateSwitch()66 Action::LayerEncapsulateSwitch::LayerEncapsulateSwitch()
67 {
68 }
69
70 synfig::String
get_local_name() const71 Action::LayerEncapsulateSwitch::get_local_name()const
72 {
73 return get_layer_descriptions(layers, _("Group Layer into Switch"), _("Group Layers into Switch"));
74 }
75
76 Action::ParamVocab
get_param_vocab()77 Action::LayerEncapsulateSwitch::get_param_vocab()
78 {
79 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
80
81 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
82 .set_local_name(_("Layer"))
83 .set_desc(_("Layer to be grouped"))
84 .set_supports_multiple()
85 );
86 ret.push_back(ParamDesc("description",Param::TYPE_STRING)
87 .set_local_name(_("Description"))
88 .set_desc(_("Description of new switch"))
89 .set_optional()
90 );
91
92 return ret;
93 }
94
95 bool
is_candidate(const ParamList & x)96 Action::LayerEncapsulateSwitch::is_candidate(const ParamList &x)
97 {
98 return candidate_check(get_param_vocab(),x);
99 }
100
101 bool
set_param(const synfig::String & name,const Action::Param & param)102 Action::LayerEncapsulateSwitch::set_param(const synfig::String& name, const Action::Param ¶m)
103 {
104 if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
105 {
106 layers.push_back(param.get_layer());
107 return true;
108 }
109 if(name=="description" && param.get_type()==Param::TYPE_STRING)
110 {
111 description = param.get_string();
112 return true;
113 }
114
115 return Action::CanvasSpecific::set_param(name,param);
116 }
117
118 bool
is_ready() const119 Action::LayerEncapsulateSwitch::is_ready()const
120 {
121 if(layers.empty())
122 return false;
123 return Action::CanvasSpecific::is_ready();
124 }
125
126 int
lowest_depth() const127 Action::LayerEncapsulateSwitch::lowest_depth()const
128 {
129 std::list<synfig::Layer::Handle>::const_iterator iter;
130 int lowest_depth(0x7fffffff);
131
132 for(iter=layers.begin();iter!=layers.end();++iter)
133 {
134 int depth((*iter)->get_depth());
135 if(depth<lowest_depth)
136 lowest_depth=depth;
137 }
138 if(lowest_depth==0x7fffffff)
139 return 0;
140 return lowest_depth;
141 }
142
143 void
prepare()144 Action::LayerEncapsulateSwitch::prepare()
145 {
146
147 if(!first_time())
148 return;
149
150 if(layers.empty())
151 throw Error(_("No layers to group"));
152
153 // First create the new canvas and layer
154 if(!child_canvas)
155 child_canvas=Canvas::create_inline(get_canvas());
156
157 Layer::Handle new_layer(Layer::create("switch"));
158
159 if (!description.empty()) new_layer->set_description(description);
160 new_layer->set_param("canvas",child_canvas);
161 new_layer->set_param("layer_name",layers.front()->get_description());
162
163 int target_depth(lowest_depth());
164
165 // Add the layer
166 {
167 Action::Handle action(LayerAdd::create());
168
169 action->set_param("canvas",get_canvas());
170 action->set_param("canvas_interface",get_canvas_interface());
171 action->set_param("new",new_layer);
172
173 add_action(action);
174 }
175
176 // Move the layer
177 {
178 Action::Handle action(Action::create("LayerMove"));
179
180 assert(action);
181
182 action->set_param("canvas",get_canvas());
183 action->set_param("canvas_interface",get_canvas_interface());
184 action->set_param("layer",new_layer);
185 action->set_param("new_index",target_depth);
186
187 add_action(action);
188 }
189
190 std::list<synfig::Layer::Handle>::reverse_iterator iter;
191
192 for(iter=layers.rbegin();iter!=layers.rend();++iter)
193 {
194 Layer::Handle layer(*iter);
195
196 Canvas::Handle subcanvas(layer->get_canvas());
197
198 // Find the iterator for the layer
199 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
200
201 // If we couldn't find the layer in the canvas, then bail
202 if(*iter!=layer)
203 throw Error(_("This layer doesn't exist anymore."));
204
205 if(!subcanvas)
206 throw Error(_("This layer doesn't have a parent canvas"));
207
208 // If the subcanvas isn't the same as the canvas,
209 // then it had better be an inline canvas. If not,
210 // bail
211 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
212 throw Error(_("This layer doesn't belong to this canvas anymore"));
213
214 if(get_canvas()!=subcanvas)
215 throw Error(_("get_canvas()!=subcanvas"));
216
217 // Remove the layer from the old canvas
218 {
219 Action::Handle action(LayerRemove::create());
220
221 action->set_param("canvas",subcanvas);
222 action->set_param("canvas_interface",get_canvas_interface());
223 action->set_param("layer",layer);
224
225 add_action(action);
226 }
227 // Add the layer to the new canvas
228 {
229 Action::Handle action(LayerAdd::create());
230
231 action->set_param("canvas",child_canvas);
232 action->set_param("canvas_interface",get_canvas_interface());
233 action->set_param("new",layer);
234
235 add_action(action);
236 }
237 }
238 }
239