List the removed layers in the history dialog when removing layers.
[synfig.git] / synfig-studio / trunk / 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 #include <synfigapp/general.h>
36
37 #endif
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
44
45 /* === M A C R O S ========================================================= */
46
47 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerRemove);
48 ACTION_SET_NAME(Action::LayerRemove,"layer_remove");
49 ACTION_SET_LOCAL_NAME(Action::LayerRemove,N_("Remove Layer"));
50 ACTION_SET_TASK(Action::LayerRemove,"remove");
51 ACTION_SET_CATEGORY(Action::LayerRemove,Action::CATEGORY_LAYER);
52 ACTION_SET_PRIORITY(Action::LayerRemove,0);
53 ACTION_SET_VERSION(Action::LayerRemove,"0.0");
54 ACTION_SET_CVS_ID(Action::LayerRemove,"$Id$");
55
56 /* === G L O B A L S ======================================================= */
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 Action::LayerRemove::LayerRemove()
63 {
64 }
65
66 synfig::String
67 Action::LayerRemove::get_local_name()const
68 {
69         String ret;
70
71         if (layer_list.empty())
72                 return _("Remove Layer");
73
74         ret = strprintf("%s '%s'",
75                                         (layer_list.size() == 1
76                                          ? _("Remove Layer")
77                                          : _("Remove Layers")),
78                                         (layer_list.begin()->first->get_description().empty()
79                                          ? layer_list.begin()->first->get_local_name()
80                                          : layer_list.begin()->first->get_description()).c_str());
81
82         for(std::list<std::pair<synfig::Layer::Handle,int> >::const_iterator iter=++layer_list.begin(); iter!=layer_list.end(); ++iter)
83                 ret += strprintf(", '%s'", (iter->first->get_description().empty()
84                                                                         ? iter->first->get_local_name()
85                                                                         : iter->first->get_description()).c_str());
86         return ret;
87 }
88
89 Action::ParamVocab
90 Action::LayerRemove::get_param_vocab()
91 {
92         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
93
94         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
95                 .set_local_name(_("Layer"))
96                 .set_desc(_("Layer to be deleted"))
97                 .set_supports_multiple()
98         );
99
100         return ret;
101 }
102
103 bool
104 Action::LayerRemove::is_candidate(const ParamList &x)
105 {
106         return candidate_check(get_param_vocab(),x);
107 }
108
109 bool
110 Action::LayerRemove::set_param(const synfig::String& name, const Action::Param &param)
111 {
112         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
113         {
114                 std::pair<synfig::Layer::Handle,int> layer_pair;
115                 layer_pair.first=param.get_layer();
116                 layer_list.push_back(layer_pair);
117
118                 return true;
119         }
120
121         return Action::CanvasSpecific::set_param(name,param);
122 }
123
124 bool
125 Action::LayerRemove::is_ready()const
126 {
127         if(layer_list.empty())
128                 return false;
129         return Action::CanvasSpecific::is_ready();
130 }
131
132 void
133 Action::LayerRemove::perform()
134 {
135         std::list<std::pair<synfig::Layer::Handle,int> >::iterator iter;
136         for(iter=layer_list.begin();iter!=layer_list.end();++iter)
137         {
138                 Layer::Handle layer(iter->first);
139 //              int& depth(iter->second);
140                 Canvas::Handle subcanvas(layer->get_canvas());
141
142                 // Find the iterator for the layer
143                 Canvas::iterator iter2=find(subcanvas->begin(),subcanvas->end(),layer);
144
145                 // If we couldn't find the layer in the canvas, then bail
146                 if(*iter2!=layer)
147                 {
148                         /*!     \todo We should really undo all prior removals
149                         **      before we go throwing shit around */
150                         throw Error(_("This layer doesn't exist anymore."));
151                 }
152
153                 // If the subcanvas isn't the same as the canvas,
154                 // then it had better be an inline canvas. If not,
155                 // bail
156                 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
157                 {
158                         /*!     \todo We should really undo all prior removals
159                         **      before we go throwing shit around */
160                         throw Error(_("This layer doesn't belong to this canvas anymore"));
161                 }
162
163                 set_canvas(subcanvas);
164
165                 // Calculate the depth that the layer was at (For the undo)
166                 iter->second=layer->get_depth();
167
168                 // Mark ourselves as dirty if necessary
169                 set_dirty(layer->active());
170
171                 // Remove the layer from the canvas
172                 subcanvas->erase(iter2);
173
174                 // Signal that a layer has been removed
175                 if(get_canvas_interface())
176                         get_canvas_interface()->signal_layer_removed()(layer);
177         }
178 }
179
180 void
181 Action::LayerRemove::undo()
182 {
183         std::list<std::pair<synfig::Layer::Handle,int> >::reverse_iterator iter;
184         for(iter=layer_list.rbegin();iter!=layer_list.rend();++iter)
185         {
186                 Layer::Handle layer(iter->first);
187                 int& depth(iter->second);
188
189                 // Set the layer's canvas
190                 layer->set_canvas(get_canvas());
191
192                 // Make sure that the depth is valid
193                 if(get_canvas()->size()<depth)
194                         depth=get_canvas()->size();
195
196                 // Mark ourselves as dirty if necessary
197                 set_dirty(layer->active());
198
199                 // Insert the layer into the canvas at the desired depth
200                 get_canvas()->insert(get_canvas()->begin()+depth,layer);
201
202                 // Signal that a layer has been inserted
203                 if(get_canvas_interface())
204                         get_canvas_interface()->signal_layer_inserted()(layer,depth);
205         }
206 }