1 /* === S Y N F I G ========================================================= */
3 ** \brief Image Import Layer Implementation
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === H E A D E R S ======================================================= */
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
41 #include <synfig/canvas.h>
45 using namespace synfig;
49 /* === M A C R O S ========================================================= */
51 /* === G L O B A L S ======================================================= */
53 SYNFIG_LAYER_INIT(Import);
54 SYNFIG_LAYER_SET_NAME(Import,"import");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Import,_("Import"));
56 SYNFIG_LAYER_SET_CATEGORY(Import,_("Other"));
57 SYNFIG_LAYER_SET_VERSION(Import,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(Import,"$Id: import.cpp,v 1.2 2005/03/19 04:26:42 darco Exp $");
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
74 Import::on_canvas_set()
76 if(get_canvas())set_param("filename",filename);
80 Import::set_param(const String & param, const ValueBase &value)
84 if(param=="filename" && value.same_as(filename))
88 filename=value.get(filename);
94 String newfilename=value.get(string());
95 String filename_with_path;
97 // Get rid of any %20 crap
100 while((n=newfilename.find("%20"))!=String::npos)
101 newfilename.replace(n,3," ");
104 //if(get_canvas()->get_file_path()==dirname(newfilename))
106 // synfig::info("Image seems to be in local directory. Adjusting path...");
107 // newfilename=basename(newfilename);
111 if(is_absolute_path(newfilename))
113 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
114 while(basename(curpath)==".")curpath=dirname(curpath);
116 newfilename=relative_path(curpath,newfilename);
117 synfig::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
122 filename=newfilename;
124 if(newfilename.empty())
126 filename=newfilename;
132 // If we are already loaded, don't reload
133 if(filename==newfilename && importer)
135 synfig::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
139 assert(get_canvas());
141 if(is_absolute_path(newfilename))
142 filename_with_path=newfilename;
144 filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfilename;
146 handle<Importer> newimporter;
148 newimporter=Importer::open(absolute_path(filename_with_path));
152 newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+basename(newfilename));
155 synfig::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
162 if(!newimporter->get_frame(surface,Time(0)))
164 synfig::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
167 importer=newimporter;
168 filename=newfilename;
169 abs_filename=absolute_path(filename_with_path);
173 } catch(...) { set_amount(0); return false; }
175 return Layer_Bitmap::set_param(param,value);
179 Import::get_param(const String & param)const
185 if(param=="filename")
187 string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
188 return relative_path(curpath,abs_filename);
197 return Layer_Bitmap::get_param(param);
201 Import::get_param_vocab()const
203 Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
205 ret.push_back(ParamDesc("filename")
206 .set_local_name(_("Filename"))
207 .set_description(_("File to import"))
208 .set_hint("filename")
210 ret.push_back(ParamDesc("time_offset")
211 .set_local_name(_("Time Offset"))
218 Import::set_time(Context context, Time time)const
220 if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
221 //else surface.clear();
222 context.set_time(time);
226 Import::set_time(Context context, Time time, const Point &pos)const
228 if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
229 //else surface.clear();
230 context.set_time(time,pos);