fa7e68340298863acb06af4ebde0f69b36c90831
[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 **
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 "layerduplicate.h"
33 #include "layeradd.h"
34 #include <synfigapp/canvasinterface.h>
35
36 #include <synfigapp/general.h>
37
38 #endif
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
45
46 /* === M A C R O S ========================================================= */
47
48 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerDuplicate);
49 ACTION_SET_NAME(Action::LayerDuplicate,"layer_duplicate");
50 ACTION_SET_LOCAL_NAME(Action::LayerDuplicate,N_("Duplicate Layer"));
51 ACTION_SET_TASK(Action::LayerDuplicate,"duplicate");
52 ACTION_SET_CATEGORY(Action::LayerDuplicate,Action::CATEGORY_LAYER);
53 ACTION_SET_PRIORITY(Action::LayerDuplicate,0);
54 ACTION_SET_VERSION(Action::LayerDuplicate,"0.0");
55 ACTION_SET_CVS_ID(Action::LayerDuplicate,"$Id$");
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Action::LayerDuplicate::LayerDuplicate()
64 {
65 }
66
67 synfig::String
68 Action::LayerDuplicate::get_local_name()const
69 {
70         return get_layer_descriptions(layers, _("Duplicate Layer"), _("Duplicate Layers"));
71 }
72
73 Action::ParamVocab
74 Action::LayerDuplicate::get_param_vocab()
75 {
76         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
77
78         ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
79                 .set_local_name(_("Layer"))
80                 .set_desc(_("Layer to be duplicated"))
81                 .set_supports_multiple()
82         );
83
84         return ret;
85 }
86
87 bool
88 Action::LayerDuplicate::is_candidate(const ParamList &x)
89 {
90         return candidate_check(get_param_vocab(),x);
91 }
92
93 bool
94 Action::LayerDuplicate::set_param(const synfig::String& name, const Action::Param &param)
95 {
96         if(name=="layer" && param.get_type()==Param::TYPE_LAYER)
97         {
98                 layers.push_back(param.get_layer());
99
100                 return true;
101         }
102
103         return Action::CanvasSpecific::set_param(name,param);
104 }
105
106 bool
107 Action::LayerDuplicate::is_ready()const
108 {
109         if(layers.empty())
110                 return false;
111         return Action::CanvasSpecific::is_ready();
112 }
113
114 void
115 Action::LayerDuplicate::prepare()
116 {
117         if(!first_time())
118                 return;
119
120         std::list<synfig::Layer::Handle>::const_iterator iter;
121
122         for(iter=layers.begin();iter!=layers.end();++iter)
123         {
124                 Layer::Handle layer(*iter);
125
126                 Canvas::Handle subcanvas(layer->get_canvas());
127
128                 // Find the iterator for the layer
129                 Canvas::iterator iter=find(subcanvas->begin(),subcanvas->end(),layer);
130
131                 // If we couldn't find the layer in the canvas, then bail
132                 if(*iter!=layer)
133                         throw Error(_("This layer doesn't exist anymore."));
134
135                 // If the subcanvas isn't the same as the canvas,
136                 // then it had better be an inline canvas. If not,
137                 // bail
138                 if(get_canvas()!=subcanvas && !subcanvas->is_inline())
139                         throw Error(_("This layer doesn't belong to this canvas anymore"));
140
141                 Layer::Handle new_layer(layer->clone(guid));
142
143                 {
144                         Action::Handle action(Action::create("layer_move"));
145
146                         action->set_param("canvas",subcanvas);
147                         action->set_param("canvas_interface",get_canvas_interface());
148                         action->set_param("layer",new_layer);
149                         action->set_param("new_index",layers.front()->get_depth());
150
151                         add_action_front(action);
152                 }
153                 {
154                         Action::Handle action(Action::create("layer_add"));
155
156                         action->set_param("canvas",subcanvas);
157                         action->set_param("canvas_interface",get_canvas_interface());
158                         action->set_param("new",new_layer);
159
160                         add_action_front(action);
161                 }
162
163                 // automatically export the Index parameter of Duplicate layers when duplicating
164                 if (new_layer->get_name() == "duplicate")
165                         for (int i = 1; ; i++)
166                         {
167                                 String name = strprintf(_("Index %d"), i);
168                                 try
169                                 {
170                                         subcanvas->find_value_node(name);
171                                 }
172                                 catch (Exception::IDNotFound x)
173                                 {
174                                         Action::Handle action(Action::create("value_node_add"));
175
176                                         action->set_param("canvas",subcanvas);
177                                         action->set_param("canvas_interface",get_canvas_interface());
178                                         action->set_param("new",new_layer->dynamic_param_list().find("index")->second);
179                                         action->set_param("name",name);
180
181                                         add_action_front(action);
182
183                                         break;
184                                 }
185                         }
186         }
187 }