Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / modules / lyr_std / import.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file import.cpp
3 **      \brief Implementation of the "Import Image" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 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 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "import.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44 #include <synfig/canvas.h>
45
46 #endif
47
48 using namespace synfig;
49 using namespace std;
50 using namespace etl;
51
52 /* === M A C R O S ========================================================= */
53
54 /* === G L O B A L S ======================================================= */
55
56 SYNFIG_LAYER_INIT(Import);
57 SYNFIG_LAYER_SET_NAME(Import,"import");
58 SYNFIG_LAYER_SET_LOCAL_NAME(Import,N_("Import Image"));
59 SYNFIG_LAYER_SET_CATEGORY(Import,N_("Other"));
60 SYNFIG_LAYER_SET_VERSION(Import,"0.1");
61 SYNFIG_LAYER_SET_CVS_ID(Import,"$Id$");
62
63 /* === P R O C E D U R E S ================================================= */
64
65 /* === M E T H O D S ======================================================= */
66
67 Import::Import()
68 {
69         time_offset=0;
70         Layer::Vocab voc(get_param_vocab());
71         Layer::fill_static(voc);
72 }
73
74 Import::~Import()
75 {
76 }
77
78 void
79 Import::on_canvas_set()
80 {
81         if(get_canvas())set_param("filename",filename);
82 }
83
84 bool
85 Import::set_param(const String & param, const ValueBase &value)
86 {
87         try{
88         IMPORT(time_offset);
89         if(param=="filename" && value.same_type_as(filename))
90         {
91                 set_param_static(param, value.get_static());
92                 if(!get_canvas())
93                 {
94                         filename=value.get(filename);
95                         importer=0;
96                         surface.clear();
97                         return true;
98                 }
99
100                 String newfilename=value.get(string());
101                 String filename_with_path;
102
103                 // Get rid of any %20 crap
104                 {
105                         String::size_type n;
106                         while((n=newfilename.find("%20"))!=String::npos)
107                                 newfilename.replace(n,3," ");
108                 }
109
110                 //if(get_canvas()->get_file_path()==dirname(newfilename))
111                 //{
112                 //      synfig::info("Image seems to be in local directory. Adjusting path...");
113                 //      newfilename=basename(newfilename);
114                 //}
115
116 #ifndef WIN32
117                 if(is_absolute_path(newfilename))
118                 {
119                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
120                         while(basename(curpath)==".")curpath=dirname(curpath);
121
122                         newfilename=relative_path(curpath,newfilename);
123                         synfig::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
124                 }
125 #endif
126
127                 if(filename.empty())
128                         filename=newfilename;
129
130                 if(newfilename.empty())
131                 {
132                         filename=newfilename;
133                         importer=0;
134                         surface.clear();
135                         return true;
136                 }
137
138                 // If we are already loaded, don't reload
139                 if(filename==newfilename && importer)
140                 {
141                         synfig::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
142                         return true;
143                 }
144
145                 assert(get_canvas());
146
147                 if(is_absolute_path(newfilename))
148                         filename_with_path=newfilename;
149                 else
150                         filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+newfilename;
151
152                 handle<Importer> newimporter;
153
154                 newimporter=Importer::open(absolute_path(filename_with_path));
155
156                 if(!newimporter)
157                 {
158                         newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPARATOR+basename(newfilename));
159                         if(!newimporter)
160                         {
161                                 synfig::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
162                                 surface.clear();
163                                 return false;
164                         }
165                 }
166
167                 surface.clear();
168                 if(!newimporter->get_frame(surface,get_canvas()->rend_desc(),Time(0),trimmed,width,height,top,left))
169                 {
170                         synfig::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
171                 }
172
173                 importer=newimporter;
174                 filename=newfilename;
175                 abs_filename=absolute_path(filename_with_path);
176
177                 return true;
178         }
179         } catch(...) { set_amount(0); return false; }
180
181         return Layer_Bitmap::set_param(param,value);
182 }
183
184 ValueBase
185 Import::get_param(const String & param)const
186 {
187         EXPORT(time_offset);
188
189         if(get_canvas())
190         {
191                 if(param=="filename")
192                 {
193                         ValueBase ret(ValueBase::TYPE_STRING);
194                         ret.set_static(get_param_static(param));
195                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
196                         ret=relative_path(curpath,abs_filename);
197                         return ret;
198                 }
199         }
200         else
201                 EXPORT(filename);
202
203         EXPORT_NAME();
204         EXPORT_VERSION();
205
206         return Layer_Bitmap::get_param(param);
207 }
208
209 Layer::Vocab
210 Import::get_param_vocab()const
211 {
212         Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
213
214         ret.push_back(ParamDesc("filename")
215                 .set_local_name(_("Filename"))
216                 .set_description(_("File to import"))
217                 .set_hint("filename")
218         );
219         ret.push_back(ParamDesc("time_offset")
220                 .set_local_name(_("Time Offset"))
221         );
222
223         return ret;
224 }
225
226 void
227 Import::set_time(Context context, Time time)const
228 {
229         if(get_amount() && importer &&
230            importer->is_animated())
231                 importer->get_frame(surface,get_canvas()->rend_desc(),time+time_offset,trimmed,width,height,top,left);
232
233         context.set_time(time);
234 }
235
236 void
237 Import::set_time(Context context, Time time, const Point &pos)const
238 {
239         if(get_amount() && importer &&
240            importer->is_animated())
241                 importer->get_frame(surface,get_canvas()->rend_desc(),time+time_offset,trimmed,width,height,top,left);
242
243         context.set_time(time,pos);
244 }