Fix 1501383: "del key deletes object instead of character". Changed the LayerRemove...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / layerduplicate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layerduplicate.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layerduplicate.h"
34 #include "layeradd.h"
35 #include <synfig/context.h>
36 #include <synfigapp/canvasinterface.h>
37
38 #include <synfigapp/general.h>
39
40 #endif
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace synfigapp;
46 using namespace Action;
47
48 /* === M A C R O S ========================================================= */
49
50 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerDuplicate);
51 ACTION_SET_NAME(Action::LayerDuplicate,"LayerDuplicate");
52 ACTION_SET_LOCAL_NAME(Action::LayerDuplicate,N_("Duplicate Layer"));
53 ACTION_SET_TASK(Action::LayerDuplicate,"duplicate");
54 ACTION_SET_CATEGORY(Action::LayerDuplicate,Action::CATEGORY_LAYER);
55 ACTION_SET_PRIORITY(Action::LayerDuplicate,0);
56 ACTION_SET_VERSION(Action::LayerDuplicate,"0.0");
57 ACTION_SET_CVS_ID(Action::LayerDuplicate,"$Id$");
58
59 /* === G L O B A L S ======================================================= */
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 Action::LayerDuplicate::LayerDuplicate()
66 {
67 }
68
69 synfig::String
70 Action::LayerDuplicate::get_local_name()const
71 {
72         return get_layer_descriptions(layers, _("Duplicate Layer"), _("Duplicate Layers"));
73 }
74
75 Action::ParamVocab
76 Action::LayerDuplicate::get_param_vocab()
77 {
78         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
79
80         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
81                 .set_local_name(_("Layer"))
82                 .set_desc(_("Layer to be duplicated"))
83                 .set_supports_multiple()
84         );
85
86         return ret;
87 }
88
89 bool
90 Action::LayerDuplicate::is_candidate(const ParamList &x)
91 {
92         return candidate_check(get_param_vocab(),x);
93 }
94
95 bool
96 Action::LayerDuplicate::set_param(const synfig::String& name, const Action::Param &param)
97 {
98         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
99         {
100                 layers.push_back(param.get_layer());
101
102                 return true;
103         }
104
105         return Action::CanvasSpecific::set_param(name,param);
106 }
107
108 bool
109 Action::LayerDuplicate::is_ready()const
110 {
111         if(layers.empty())
112                 return false;
113         return Action::CanvasSpecific::is_ready();
114 }
115
116 void
117 Action::LayerDuplicate::prepare()
118 {
119         if(!first_time())
120                 return;
121
122         std::list<synfig::Layer::Handle>::const_iterator iter;
123
124         for(iter=layers.begin();iter!=layers.end();++iter)
125         {
126                 Layer::Handle layer(*iter);
127
128                 Canvas::Handle subcanvas(layer->get_canvas());
129
130                 // Find the iterator for the layer
131                 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
132
133                 // If we couldn't find the layer in the canvas, then bail
134                 if(*iter!=layer)
135                         throw Error(_("This layer doesn't exist anymore."));
136
137                 // If the subcanvas isn't the same as the canvas,
138                 // then it had better be an inline canvas. If not,
139                 // bail
140                 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
141                         throw Error(_("This layer doesn't belong to this canvas anymore"));
142
143                 Layer::Handle new_layer(layer->clone(guid));
144
145                 {
146                         Action::Handle action(Action::create("LayerMove"));
147
148                         action->set_param("canvas",subcanvas);
149                         action->set_param("canvas_interface",get_canvas_interface());
150                         action->set_param("layer",new_layer);
151                         action->set_param("new_index",layers.front()->get_depth());
152
153                         add_action_front(action);
154                 }
155                 {
156                         Action::Handle action(Action::create("LayerAdd"));
157
158                         action->set_param("canvas",subcanvas);
159                         action->set_param("canvas_interface",get_canvas_interface());
160                         action->set_param("new",new_layer);
161
162                         add_action_front(action);
163                 }
164
165                 // automatically export the Index parameter of Duplicate layers when duplicating
166                 int index = 1;
167                 export_dup_nodes(new_layer, subcanvas, index);
168         }
169 }
170
171 void
172 Action::LayerDuplicate::export_dup_nodes(synfig::Layer::Handle layer, Canvas::Handle canvas, int &index)
173 {
174         // automatically export the Index parameter of Duplicate layers when duplicating
175         if (layer->get_name() == "duplicate")
176                 while (true)
177                 {
178                         String name = strprintf(_("Index %d"), index++);
179                         try
180                         {
181                                 canvas->find_value_node(name);
182                         }
183                         catch (Exception::IDNotFound x)
184                         {
185                                 Action::Handle action(Action::create("ValueNodeAdd"));
186
187                                 action->set_param("canvas",canvas);
188                                 action->set_param("canvas_interface",get_canvas_interface());
189                                 action->set_param("new",layer->dynamic_param_list().find("index")->second);
190                                 action->set_param("name",name);
191
192                                 add_action_front(action);
193
194                                 break;
195                         }
196                 }
197         else
198         {
199                 Layer::ParamList param_list(layer->get_param_list());
200                 for (Layer::ParamList::const_iterator iter(param_list.begin())
201                                  ; iter != param_list.end()
202                                  ; iter++)
203                         if (layer->dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
204                         {
205                                 Canvas::Handle subcanvas(iter->second.get(Canvas::Handle()));
206                                 if (subcanvas && subcanvas->is_inline())
207                                         for (Context iter = subcanvas->get_context(); iter != subcanvas->end(); iter++)
208                                                 export_dup_nodes(*iter, canvas, index);
209                         }
210
211                 for (Layer::DynamicParamList::const_iterator iter(layer->dynamic_param_list().begin())
212                                  ; iter != layer->dynamic_param_list().end()
213                                  ; iter++)
214                         if (iter->second->get_type()==ValueBase::TYPE_CANVAS)
215                         {
216                                 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
217                                 if (canvas->is_inline())
218                                         //! \todo do we need to implement this?  and if so, shouldn't we check all canvases, not just the one at t=0s?
219                                         warning("%s:%d not yet implemented - do we need to export duplicate valuenodes in dynamic canvas parameters?", __FILE__, __LINE__);
220                         }
221         }
222 }