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