Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_06 / src / synfigapp / actions / layerremove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerremove.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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 "layerremove.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
42
43 /* === M A C R O S ========================================================= */
44
45 ACTION_INIT(Action::LayerRemove);
46 ACTION_SET_NAME(Action::LayerRemove,"layer_remove");
47 ACTION_SET_LOCAL_NAME(Action::LayerRemove,"Remove Layer");
48 ACTION_SET_TASK(Action::LayerRemove,"remove");
49 ACTION_SET_CATEGORY(Action::LayerRemove,Action::CATEGORY_LAYER);
50 ACTION_SET_PRIORITY(Action::LayerRemove,0);
51 ACTION_SET_VERSION(Action::LayerRemove,"0.0");
52 ACTION_SET_CVS_ID(Action::LayerRemove,"$Id$");
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 Action::LayerRemove::LayerRemove()
61 {
62 }
63
64 Action::ParamVocab
65 Action::LayerRemove::get_param_vocab()
66 {
67         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
68
69         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
70                 .set_local_name(_("Layer"))
71                 .set_desc(_("Layer to be deleted"))
72                 .set_supports_multiple()
73         );
74
75         return ret;
76 }
77
78 bool
79 Action::LayerRemove::is_candidate(const ParamList &x)
80 {
81         return candidate_check(get_param_vocab(),x);
82 }
83
84 bool
85 Action::LayerRemove::set_param(const synfig::String& name, const Action::Param &param)
86 {
87         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
88         {
89                 std::pair<synfig::Layer::Handle,int> layer_pair;
90                 layer_pair.first=param.get_layer();
91                 layer_list.push_back(layer_pair);
92
93                 return true;
94         }
95
96         return Action::CanvasSpecific::set_param(name,param);
97 }
98
99 bool
100 Action::LayerRemove::is_ready()const
101 {
102         if(layer_list.empty())
103                 return false;
104         return Action::CanvasSpecific::is_ready();
105 }
106
107 void
108 Action::LayerRemove::perform()
109 {
110         std::list<std::pair<synfig::Layer::Handle,int> >::iterator iter;
111         for(iter=layer_list.begin();iter!=layer_list.end();++iter)
112         {
113                 Layer::Handle layer(iter->first);
114 //              int& depth(iter->second);
115                 Canvas::Handle subcanvas(layer->get_canvas());
116
117                 // Find the iterator for the layer
118                 Canvas::iterator iter2=find(subcanvas->begin(),subcanvas->end(),layer);
119
120                 // If we couldn't find the layer in the canvas, then bail
121                 if(*iter2!=layer)
122                 {
123                         /*!     \fixme We should really undo all prior removals
124                         **      before we go throwing shit around */
125                         throw Error(_("This layer doesn't exist anymore."));
126                 }
127
128                 // If the subcanvas isn't the same as the canvas,
129                 // then it had better be an inline canvas. If not,
130                 // bail
131                 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
132                 {
133                         /*!     \fixme We should really undo all prior removals
134                         **      before we go throwing shit around */
135                         throw Error(_("This layer doesn't belong to this canvas anymore"));
136                 }
137
138                 set_canvas(subcanvas);
139
140                 // Calculate the depth that the layer was at (For the undo)
141                 iter->second=layer->get_depth();
142
143                 // Mark ourselves as dirty if necessary
144                 set_dirty(layer->active());
145
146                 // Remove the layer from the canvas
147                 subcanvas->erase(iter2);
148
149                 // Signal that a layer has been removed
150                 if(get_canvas_interface())
151                         get_canvas_interface()->signal_layer_removed()(layer);
152         }
153 }
154
155 void
156 Action::LayerRemove::undo()
157 {
158         std::list<std::pair<synfig::Layer::Handle,int> >::reverse_iterator iter;
159         for(iter=layer_list.rbegin();iter!=layer_list.rend();++iter)
160         {
161                 Layer::Handle layer(iter->first);
162                 int& depth(iter->second);
163
164                 // Set the layer's canvas
165                 layer->set_canvas(get_canvas());
166
167                 // Make sure that the depth is valid
168                 if(get_canvas()->size()<depth)
169                         depth=get_canvas()->size();
170
171                 // Mark ourselves as dirty if necessary
172                 set_dirty(layer->active());
173
174                 // Insert the layer into the canvas at the desired depth
175                 get_canvas()->insert(get_canvas()->begin()+depth,layer);
176
177                 // Signal that a layer has been inserted
178                 if(get_canvas_interface())
179                         get_canvas_interface()->signal_layer_inserted()(layer,depth);
180         }
181 }