1 /* === S Y N F I G ========================================================= */
3 ** \brief Image Import Layer Implementation
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43 #include <synfig/canvas.h>
47 using namespace synfig;
51 /* === M A C R O S ========================================================= */
53 /* === G L O B A L S ======================================================= */
55 SYNFIG_LAYER_INIT(Import);
56 SYNFIG_LAYER_SET_NAME(Import,"import");
57 SYNFIG_LAYER_SET_LOCAL_NAME(Import,_("Import"));
58 SYNFIG_LAYER_SET_CATEGORY(Import,_("Other"));
59 SYNFIG_LAYER_SET_VERSION(Import,"0.1");
60 SYNFIG_LAYER_SET_CVS_ID(Import,"$Id$");
62 /* === P R O C E D U R E S ================================================= */
64 /* === M E T H O D S ======================================================= */
76 Import::on_canvas_set()
78 if(get_canvas())set_param("filename",filename);
82 Import::set_param(const String & param, const ValueBase &value)
86 if(param=="filename" && value.same_as(filename))
90 filename=value.get(filename);
96 String newfilename=value.get(string());
97 String filename_with_path;
99 // Get rid of any %20 crap
102 while((n=newfilename.find("%20"))!=String::npos)
103 newfilename.replace(n,3," ");
106 //if(get_canvas()->get_file_path()==dirname(newfilename))
108 // synfig::info("Image seems to be in local directory. Adjusting path...");
109 // newfilename=basename(newfilename);
113 if(is_absolute_path(newfilename))
115 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
116 while(basename(curpath)==".")curpath=dirname(curpath);
118 newfilename=relative_path(curpath,newfilename);
119 synfig::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
124 filename=newfilename;
126 if(newfilename.empty())
128 filename=newfilename;
134 // If we are already loaded, don't reload
135 if(filename==newfilename && importer)
137 synfig::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
141 assert(get_canvas());
143 if(is_absolute_path(newfilename))
144 filename_with_path=newfilename;
146 filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfilename;
148 handle<Importer> newimporter;
150 newimporter=Importer::open(absolute_path(filename_with_path));
154 newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+basename(newfilename));
157 synfig::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
164 if(!newimporter->get_frame(surface,Time(0)))
166 synfig::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
169 importer=newimporter;
170 filename=newfilename;
171 abs_filename=absolute_path(filename_with_path);
175 } catch(...) { set_amount(0); return false; }
177 return Layer_Bitmap::set_param(param,value);
181 Import::get_param(const String & param)const
187 if(param=="filename")
189 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
190 return relative_path(curpath,abs_filename);
199 return Layer_Bitmap::get_param(param);
203 Import::get_param_vocab()const
205 Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
207 ret.push_back(ParamDesc("filename")
208 .set_local_name(_("Filename"))
209 .set_description(_("File to import"))
210 .set_hint("filename")
212 ret.push_back(ParamDesc("time_offset")
213 .set_local_name(_("Time Offset"))
220 Import::set_time(Context context, Time time)const
222 if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
223 //else surface.clear();
224 context.set_time(time);
228 Import::set_time(Context context, Time time, const Point &pos)const
230 if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
231 //else surface.clear();
232 context.set_time(time,pos);