Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / lyr_std / import.cpp
1 /*! ========================================================================
2 ** Sinfg
3 ** Image Import Layer Implementation
4 ** $Id: import.cpp,v 1.2 2005/03/19 04:26:42 darco Exp $
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #ifdef USING_PCH
24 #       include "pch.h"
25 #else
26 #ifdef HAVE_CONFIG_H
27 #       include <config.h>
28 #endif
29
30 #include "import.h"
31 #include <sinfg/string.h>
32 #include <sinfg/time.h>
33 #include <sinfg/context.h>
34 #include <sinfg/paramdesc.h>
35 #include <sinfg/renddesc.h>
36 #include <sinfg/surface.h>
37 #include <sinfg/value.h>
38 #include <sinfg/valuenode.h>
39 #include <sinfg/canvas.h>
40
41 #endif
42
43 using namespace sinfg;
44 using namespace std;
45 using namespace etl;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 SINFG_LAYER_INIT(Import);
52 SINFG_LAYER_SET_NAME(Import,"import");
53 SINFG_LAYER_SET_LOCAL_NAME(Import,_("Import"));
54 SINFG_LAYER_SET_CATEGORY(Import,_("Other"));
55 SINFG_LAYER_SET_VERSION(Import,"0.1");
56 SINFG_LAYER_SET_CVS_ID(Import,"$Id: import.cpp,v 1.2 2005/03/19 04:26:42 darco Exp $");
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 Import::Import()
63 {
64         time_offset=0;
65 }
66
67 Import::~Import()
68 {
69 }
70
71 void
72 Import::on_canvas_set()
73 {
74         if(get_canvas())set_param("filename",filename);
75 }
76
77 bool
78 Import::set_param(const String & param, const ValueBase &value)
79 {
80         try{
81         IMPORT(time_offset);
82         if(param=="filename" && value.same_as(filename))
83         {
84                 if(!get_canvas())
85                 {
86                         filename=value.get(filename);
87                         importer=0;
88                         surface.clear();
89                         return true;
90                 }
91                 
92                 String newfilename=value.get(string());
93                 String filename_with_path;
94                 
95                 // Get rid of any %20 crap
96                 {
97                         unsigned int n;
98                         while((n=newfilename.find("%20"))!=String::npos)
99                                 newfilename.replace(n,3," ");
100                 }
101                 
102                 //if(get_canvas()->get_file_path()==dirname(newfilename))
103                 //{
104                 //      sinfg::info("Image seems to be in local directory. Adjusting path...");
105                 //      newfilename=basename(newfilename);
106                 //}
107                 
108 #ifndef WIN32
109                 if(is_absolute_path(newfilename))
110                 {
111                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
112                         while(basename(curpath)==".")curpath=dirname(curpath);
113                                 
114                         newfilename=relative_path(curpath,newfilename);
115                         sinfg::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
116                 }
117 #endif
118
119                 if(filename.empty())
120                         filename=newfilename;
121
122                 if(newfilename.empty())
123                 {
124                         filename=newfilename;
125                         importer=0;
126                         surface.clear();
127                         return true;
128                 }
129                 
130                 // If we are already loaded, don't reload
131                 if(filename==newfilename && importer)
132                 {
133                         sinfg::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
134                         return true;
135                 }
136                 
137                 assert(get_canvas());
138                 
139                 if(is_absolute_path(newfilename))
140                         filename_with_path=newfilename;
141                 else
142                         filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfilename;
143                         
144                 handle<Importer> newimporter;
145                 
146                 newimporter=Importer::open(absolute_path(filename_with_path));
147
148                 if(!newimporter)
149                 {
150                         newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+basename(newfilename));
151                         if(!newimporter)
152                         {
153                                 sinfg::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
154                                 surface.clear();
155                                 return false;
156                         }
157                 }
158
159                 surface.clear();
160                 if(!newimporter->get_frame(surface,Time(0)))
161                 {
162                         sinfg::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
163                 }
164
165                 importer=newimporter;
166                 filename=newfilename;
167                 abs_filename=absolute_path(filename_with_path);
168                 
169                 return true;
170         }
171         } catch(...) { set_amount(0); return false; }
172         
173         return Layer_Bitmap::set_param(param,value);
174 }
175
176 ValueBase
177 Import::get_param(const String & param)const
178 {
179         EXPORT(time_offset);
180
181         if(get_canvas())
182         {
183                 if(param=="filename")
184                 {
185                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
186                         return relative_path(curpath,abs_filename);
187                 }
188         }
189         else
190                 EXPORT(filename);
191
192         EXPORT_NAME();
193         EXPORT_VERSION();
194                 
195         return Layer_Bitmap::get_param(param);  
196 }
197
198 Layer::Vocab
199 Import::get_param_vocab()const
200 {
201         Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
202         
203         ret.push_back(ParamDesc("filename")
204                 .set_local_name(_("Filename"))
205                 .set_description(_("File to import"))
206                 .set_hint("filename")
207         );
208         ret.push_back(ParamDesc("time_offset")
209                 .set_local_name(_("Time Offset"))
210         );
211         
212         return ret;
213 }
214
215 void
216 Import::set_time(Context context, Time time)const
217 {
218         if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
219         //else surface.clear();
220         context.set_time(time);
221 }
222
223 void
224 Import::set_time(Context context, Time time, const Point &pos)const
225 {
226         if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
227         //else surface.clear();
228         context.set_time(time,pos);
229 }