1 /* === S Y N F I G ========================================================= */
2 /*!	\file layerencapsulatefilter.cpp
3 **	\brief Template File
4 **
5 **	$Id$
6 **
7 **	\legal
8 **	......... ... 2017 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 "layerencapsulatefilter.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::LayerEncapsulateFilter);
52 ACTION_SET_NAME(Action::LayerEncapsulateFilter,"LayerEncapsulateFilter");
53 ACTION_SET_LOCAL_NAME(Action::LayerEncapsulateFilter,N_("Group Layers into Filter"));
54 ACTION_SET_TASK(Action::LayerEncapsulateFilter,"encapsulate_filter");
55 ACTION_SET_CATEGORY(Action::LayerEncapsulateFilter,Action::CATEGORY_LAYER);
56 ACTION_SET_PRIORITY(Action::LayerEncapsulateFilter,0);
57 ACTION_SET_VERSION(Action::LayerEncapsulateFilter,"0.0");
58 ACTION_SET_CVS_ID(Action::LayerEncapsulateFilter,"$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 
LayerEncapsulateFilter()66 Action::LayerEncapsulateFilter::LayerEncapsulateFilter()
67 {
68 }
69 
70 synfig::String
get_local_name() const71 Action::LayerEncapsulateFilter::get_local_name()const
72 {
73 	return get_layer_descriptions(layers, _("Group Layer into Filter"), _("Group Layers into Filter"));
74 }
75 
76 Action::ParamVocab
get_param_vocab()77 Action::LayerEncapsulateFilter::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 filter"))
89 		.set_optional()
90 	);
91 
92 	return ret;
93 }
94 
95 bool
is_candidate(const ParamList & x)96 Action::LayerEncapsulateFilter::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::LayerEncapsulateFilter::set_param(const synfig::String& name, const Action::Param &param)
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::LayerEncapsulateFilter::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::LayerEncapsulateFilter::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::LayerEncapsulateFilter::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("filter_group"));
158 
159 	if (!description.empty()) new_layer->set_description(description);
160 	new_layer->set_param("canvas",child_canvas);
161 
162 	int target_depth(lowest_depth());
163 
164 	// Add the layer
165 	{
166 		Action::Handle action(LayerAdd::create());
167 
168 		action->set_param("canvas",get_canvas());
169 		action->set_param("canvas_interface",get_canvas_interface());
170 		action->set_param("new",new_layer);
171 
172 		add_action(action);
173 	}
174 
175 	// Move the layer
176 	{
177 		Action::Handle action(Action::create("LayerMove"));
178 
179 		assert(action);
180 
181 		action->set_param("canvas",get_canvas());
182 		action->set_param("canvas_interface",get_canvas_interface());
183 		action->set_param("layer",new_layer);
184 		action->set_param("new_index",target_depth);
185 
186 		add_action(action);
187 	}
188 
189 	std::list<synfig::Layer::Handle>::reverse_iterator iter;
190 
191 	for(iter=layers.rbegin();iter!=layers.rend();++iter)
192 	{
193 		Layer::Handle layer(*iter);
194 
195 		Canvas::Handle subcanvas(layer->get_canvas());
196 
197 		// Find the iterator for the layer
198 		Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
199 
200 		// If we couldn't find the layer in the canvas, then bail
201 		if(*iter!=layer)
202 			throw Error(_("This layer doesn't exist anymore."));
203 
204 		if(!subcanvas)
205 			throw Error(_("This layer doesn't have a parent canvas"));
206 
207 		// If the subcanvas isn't the same as the canvas,
208 		// then it had better be an inline canvas. If not,
209 		// bail
210 		if(get_canvas()!=subcanvas && !subcanvas->is_inline())
211 			throw Error(_("This layer doesn't belong to this canvas anymore"));
212 
213 		if(get_canvas()!=subcanvas)
214 			throw Error(_("get_canvas()!=subcanvas"));
215 
216 		// Remove the layer from the old canvas
217 		{
218 			Action::Handle action(LayerRemove::create());
219 
220 			action->set_param("canvas",subcanvas);
221 			action->set_param("canvas_interface",get_canvas_interface());
222 			action->set_param("layer",layer);
223 
224 			add_action(action);
225 		}
226 		// Add the layer to the new canvas
227 		{
228 			Action::Handle action(LayerAdd::create());
229 
230 			action->set_param("canvas",child_canvas);
231 			action->set_param("canvas_interface",get_canvas_interface());
232 			action->set_param("new",layer);
233 
234 			add_action(action);
235 		}
236 	}
237 }
238