1 /* === S Y N F I G ========================================================= */
2 /*! \file layerremove.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 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 "layerremove.h"
34 #include <synfigapp/canvasinterface.h>
36 #include <synfigapp/general.h>
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
46 /* === M A C R O S ========================================================= */
48 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerRemove);
49 ACTION_SET_NAME(Action::LayerRemove,"LayerRemove");
50 ACTION_SET_LOCAL_NAME(Action::LayerRemove,N_("Remove Layer"));
51 ACTION_SET_TASK(Action::LayerRemove,"remove");
52 ACTION_SET_CATEGORY(Action::LayerRemove,Action::CATEGORY_LAYER);
53 ACTION_SET_PRIORITY(Action::LayerRemove,0);
54 ACTION_SET_VERSION(Action::LayerRemove,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerRemove,"$Id$");
57 /* === G L O B A L S ======================================================= */
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 Action::LayerRemove::LayerRemove()
68 Action::LayerRemove::get_local_name()const
70 return get_layer_descriptions(layer_list, _("Remove Layer"), _("Remove Layers"));
74 Action::LayerRemove::get_param_vocab()
76 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
78 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
79 .set_local_name(_("Layer"))
80 .set_desc(_("Layer to be deleted"))
81 .set_supports_multiple()
88 Action::LayerRemove::is_candidate(const ParamList &x)
90 return candidate_check(get_param_vocab(),x);
94 Action::LayerRemove::set_param(const synfig::String& name, const Action::Param ¶m)
96 if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
98 std::pair<synfig::Layer::Handle,int> layer_pair;
99 layer_pair.first=param.get_layer();
100 layer_list.push_back(layer_pair);
105 return Action::CanvasSpecific::set_param(name,param);
109 Action::LayerRemove::is_ready()const
111 if(layer_list.empty())
113 return Action::CanvasSpecific::is_ready();
117 Action::LayerRemove::perform()
119 std::list<std::pair<synfig::Layer::Handle,int> >::iterator iter;
120 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
122 Layer::Handle layer(iter->first);
123 // int& depth(iter->second);
124 Canvas::Handle subcanvas(layer->get_canvas());
126 // Find the iterator for the layer
127 Canvas::iterator iter2=find(subcanvas->begin(),subcanvas->end(),layer);
129 // If we couldn't find the layer in the canvas, then bail
132 /*! \todo We should really undo all prior removals
133 ** before we go throwing shit around */
134 throw Error(_("This layer doesn't exist anymore."));
137 // If the subcanvas isn't the same as the canvas,
138 // then it had better be an inline canvas. If not,
140 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
142 /*! \todo We should really undo all prior removals
143 ** before we go throwing shit around */
144 throw Error(_("This layer doesn't belong to this canvas anymore"));
147 set_canvas(subcanvas);
149 // Calculate the depth that the layer was at (For the undo)
150 iter->second=layer->get_depth();
152 // Mark ourselves as dirty if necessary
153 set_dirty(layer->active());
155 // Remove the layer from the canvas
156 subcanvas->erase(iter2);
158 // Signal that a layer has been removed
159 if(get_canvas_interface())
160 get_canvas_interface()->signal_layer_removed()(layer);
165 Action::LayerRemove::undo()
167 std::list<std::pair<synfig::Layer::Handle,int> >::reverse_iterator iter;
168 for(iter=layer_list.rbegin();iter!=layer_list.rend();++iter)
170 Layer::Handle layer(iter->first);
171 int& depth(iter->second);
173 // Set the layer's canvas
174 layer->set_canvas(get_canvas());
176 // Make sure that the depth is valid
177 if(get_canvas()->size()<depth)
178 depth=get_canvas()->size();
180 // Mark ourselves as dirty if necessary
181 set_dirty(layer->active());
183 // Insert the layer into the canvas at the desired depth
184 get_canvas()->insert(get_canvas()->begin()+depth,layer);
186 // Signal that a layer has been inserted
187 if(get_canvas_interface())
188 get_canvas_interface()->signal_layer_inserted()(layer,depth);