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