1 /* === S Y N F I G ========================================================= */
2 /*! \file layercopy.cpp
3 ** \brief Template File
4 **
5 ** $Id$
6 **
7 ** \legal
8 ** ......... ... 2014 Ivan Mahonin
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 <synfig/general.h>
33
34 #include "layercopy.h"
35 #include "layeradd.h"
36 #include <synfig/context.h>
37 #include <synfigapp/canvasinterface.h>
38 #include <synfigapp/instance.h>
39 #include <synfigapp/localization.h>
40 #include <synfig/layers/layer_bitmap.h>
41
42 #endif
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48 using namespace Action;
49
50 /* === M A C R O S ========================================================= */
51
52 ACTION_INIT_NO_GET_LOCAL_NAME(Action::LayerCopy);
53 ACTION_SET_NAME(Action::LayerCopy,"LayerCopy");
54 ACTION_SET_LOCAL_NAME(Action::LayerCopy,N_("Simple Copy Layer"));
55 ACTION_SET_TASK(Action::LayerCopy,"copy");
56 ACTION_SET_CATEGORY(Action::LayerCopy,Action::CATEGORY_LAYER);
57 ACTION_SET_PRIORITY(Action::LayerCopy,0);
58 ACTION_SET_VERSION(Action::LayerCopy,"0.0");
59 ACTION_SET_CVS_ID(Action::LayerCopy,"$Id$");
60
61 /* === G L O B A L S ======================================================= */
62
63 /* === P R O C E D U R E S ================================================= */
64
65 /* === M E T H O D S ======================================================= */
66
LayerCopy()67 Action::LayerCopy::LayerCopy()
68 {
69 }
70
71 synfig::String
get_local_name() const72 Action::LayerCopy::get_local_name()const
73 {
74 return get_layer_descriptions(layers, _("Simple Copy Layer"), _("Simple Copy Layers"));
75 }
76
77 Action::ParamVocab
get_param_vocab()78 Action::LayerCopy::get_param_vocab()
79 {
80 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
81
82 ret.push_back(ParamDesc("layer",Param::TYPE_LAYER)
83 .set_local_name(_("Layer"))
84 .set_desc(_("Layer to be copied"))
85 );
86
87 return ret;
88 }
89
90 bool
is_candidate(const ParamList & x)91 Action::LayerCopy::is_candidate(const ParamList &x)
92 {
93 return candidate_check(get_param_vocab(),x);
94 }
95
96 bool
set_param(const synfig::String & name,const Action::Param & param)97 Action::LayerCopy::set_param(const synfig::String& name, const Action::Param ¶m)
98 {
99 if(name=="layer" && param.get_type()==Param::TYPE_LAYER && param.get_layer())
100 {
101 layers.push_back(param.get_layer());
102 return true;
103 }
104
105 return Action::CanvasSpecific::set_param(name,param);
106 }
107
108 bool
is_ready() const109 Action::LayerCopy::is_ready()const
110 {
111 if(layers.empty())
112 return false;
113 return Action::CanvasSpecific::is_ready();
114 }
115
116 void
prepare()117 Action::LayerCopy::prepare()
118 {
119 if(!first_time())
120 return;
121
122 for(std::list<Layer::Handle>::iterator i = layers.begin(); i != layers.end(); ++i)
123 {
124 Layer::Handle layer(*i);
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 // generate names
142 String description, filename, filename_param;
143 get_canvas_interface()
144 ->get_instance()
145 ->generate_new_name(
146 layer,
147 subcanvas,
148 get_canvas()->get_file_system(),
149 description,
150 filename,
151 filename_param );
152
153 // make copy
154 Layer::Handle new_layer = Layer::create(layer->get_name()).get();
155 new_layer->add_to_group(layer->get_group());
156 new_layer->set_active(layer->active());
157 new_layer->set_exclude_from_rendering(layer->get_exclude_from_rendering());
158 new_layer->set_param_list(layer->get_param_list());
159 new_layer->set_description(description);
160
161 // copy file
162 etl::handle<Layer_Bitmap> layer_bitmap = etl::handle<Layer_Bitmap>::cast_dynamic(layer);
163 if (layer_bitmap && !filename.empty())
164 {
165 get_canvas_interface()
166 ->get_instance()
167 ->save_surface(layer_bitmap->rendering_surface, filename);
168 filenames.push_back(filename);
169 new_layer->set_param("filename", filename_param);
170 }
171
172 Action::Handle action(Action::create("LayerAdd"));
173 action->set_param("canvas",subcanvas);
174 action->set_param("canvas_interface",get_canvas_interface());
175 action->set_param("new",new_layer);
176 add_action(action);
177 }
178 }
179
180 void
undo()181 Action::LayerCopy::undo() {
182 Action::Super::undo();
183 while(!filenames.empty())
184 {
185 get_canvas()->get_file_system()->file_remove(filenames.back());
186 filenames.pop_back();
187 }
188 }
189
190