1 /* === S Y N F I G ========================================================= */
2 /*! \file layerencapsulate.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "layerencapsulate.h"
35 #include "layerremove.h"
36 #include <synfigapp/canvasinterface.h>
38 #include <synfigapp/general.h>
44 using namespace synfig;
45 using namespace synfigapp;
46 using namespace Action;
48 /* === M A C R O S ========================================================= */
50 ACTION_INIT(Action::LayerEncapsulate);
51 ACTION_SET_NAME(Action::LayerEncapsulate,"layer_encapsulate");
52 ACTION_SET_LOCAL_NAME(Action::LayerEncapsulate,N_("Encapsulate"));
53 ACTION_SET_TASK(Action::LayerEncapsulate,"encapsulate");
54 ACTION_SET_CATEGORY(Action::LayerEncapsulate,Action::CATEGORY_LAYER);
55 ACTION_SET_PRIORITY(Action::LayerEncapsulate,0);
56 ACTION_SET_VERSION(Action::LayerEncapsulate,"0.0");
57 ACTION_SET_CVS_ID(Action::LayerEncapsulate,"$Id$");
59 /* === G L O B A L S ======================================================= */
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 Action::LayerEncapsulate::LayerEncapsulate()
70 Action::LayerEncapsulate::get_param_vocab()
72 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
74 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
75 .set_local_name(_("Layer"))
76 .set_desc(_("Layer to be encapsulated"))
77 .set_supports_multiple()
84 Action::LayerEncapsulate::is_candidate(const ParamList &x)
86 return candidate_check(get_param_vocab(),x);
90 Action::LayerEncapsulate::set_param(const synfig::String& name, const Action::Param ¶m)
92 if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
94 layers.push_back(param.get_layer());
99 return Action::CanvasSpecific::set_param(name,param);
103 Action::LayerEncapsulate::is_ready()const
107 return Action::CanvasSpecific::is_ready();
111 Action::LayerEncapsulate::lowest_depth()const
113 std::list<synfig::Layer::Handle>::const_iterator iter;
114 int lowest_depth(0x7fffffff);
116 for(iter=layers.begin();iter!=layers.end();++iter)
118 int depth((*iter)->get_depth());
119 if(depth<lowest_depth)
122 if(lowest_depth==0x7fffffff)
128 Action::LayerEncapsulate::prepare()
135 throw Error(_("No layers to encapsulate"));
137 // First create the new canvas and layer
139 child_canvas=Canvas::create_inline(get_canvas());
141 Layer::Handle new_layer(Layer::create("PasteCanvas"));
143 new_layer->set_param("canvas",child_canvas);
145 int target_depth(lowest_depth());
149 Action::Handle action(LayerAdd::create());
151 action->set_param("canvas",get_canvas());
152 action->set_param("canvas_interface",get_canvas_interface());
153 action->set_param("new",new_layer);
160 Action::Handle action(Action::create("layer_move"));
164 action->set_param("canvas",get_canvas());
165 action->set_param("canvas_interface",get_canvas_interface());
166 action->set_param("layer",new_layer);
167 action->set_param("new_index",target_depth);
172 std::list<synfig::Layer::Handle>::reverse_iterator iter;
174 for(iter=layers.rbegin();iter!=layers.rend();++iter)
176 Layer::Handle layer(*iter);
178 Canvas::Handle subcanvas(layer->get_canvas());
180 // Find the iterator for the layer
181 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
183 // If we couldn't find the layer in the canvas, then bail
185 throw Error(_("This layer doesn't exist anymore."));
188 throw Error(_("This layer doesn't have a parent canvas"));
190 // If the subcanvas isn't the same as the canvas,
191 // then it had better be an inline canvas. If not,
193 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
194 throw Error(_("This layer doesn't belong to this canvas anymore"));
196 if(get_canvas()!=subcanvas)
197 throw Error(_("get_canvas()!=subcanvas"));
199 // Remove the layer from the old canvas
201 Action::Handle action(LayerRemove::create());
203 action->set_param("canvas",subcanvas);
204 action->set_param("canvas_interface",get_canvas_interface());
205 action->set_param("layer",layer);
209 // Add the layer to the new canvas
211 Action::Handle action(LayerAdd::create());
213 action->set_param("canvas",child_canvas);
214 action->set_param("canvas_interface",get_canvas_interface());
215 action->set_param("new",layer);