Fixing warnings from doxygen:
[synfig.git] / synfig-core / trunk / src / modules / lyr_std / import.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file import.cpp
3 **      \brief Image Import Layer Implementation
4 **
5 **      \legal
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
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.
12 **
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.
17 **      \endlegal
18 **
19 ** === N O T E S ===========================================================
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 "import.h"
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>
42
43 #endif
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
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 $");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 Import::Import()
65 {
66         time_offset=0;
67 }
68
69 Import::~Import()
70 {
71 }
72
73 void
74 Import::on_canvas_set()
75 {
76         if(get_canvas())set_param("filename",filename);
77 }
78
79 bool
80 Import::set_param(const String & param, const ValueBase &value)
81 {
82         try{
83         IMPORT(time_offset);
84         if(param=="filename" && value.same_as(filename))
85         {
86                 if(!get_canvas())
87                 {
88                         filename=value.get(filename);
89                         importer=0;
90                         surface.clear();
91                         return true;
92                 }
93
94                 String newfilename=value.get(string());
95                 String filename_with_path;
96
97                 // Get rid of any %20 crap
98                 {
99                         unsigned int n;
100                         while((n=newfilename.find("%20"))!=String::npos)
101                                 newfilename.replace(n,3," ");
102                 }
103
104                 //if(get_canvas()->get_file_path()==dirname(newfilename))
105                 //{
106                 //      synfig::info("Image seems to be in local directory. Adjusting path...");
107                 //      newfilename=basename(newfilename);
108                 //}
109
110 #ifndef WIN32
111                 if(is_absolute_path(newfilename))
112                 {
113                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
114                         while(basename(curpath)==".")curpath=dirname(curpath);
115
116                         newfilename=relative_path(curpath,newfilename);
117                         synfig::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
118                 }
119 #endif
120
121                 if(filename.empty())
122                         filename=newfilename;
123
124                 if(newfilename.empty())
125                 {
126                         filename=newfilename;
127                         importer=0;
128                         surface.clear();
129                         return true;
130                 }
131
132                 // If we are already loaded, don't reload
133                 if(filename==newfilename && importer)
134                 {
135                         synfig::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
136                         return true;
137                 }
138
139                 assert(get_canvas());
140
141                 if(is_absolute_path(newfilename))
142                         filename_with_path=newfilename;
143                 else
144                         filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfilename;
145
146                 handle<Importer> newimporter;
147
148                 newimporter=Importer::open(absolute_path(filename_with_path));
149
150                 if(!newimporter)
151                 {
152                         newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+basename(newfilename));
153                         if(!newimporter)
154                         {
155                                 synfig::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
156                                 surface.clear();
157                                 return false;
158                         }
159                 }
160
161                 surface.clear();
162                 if(!newimporter->get_frame(surface,Time(0)))
163                 {
164                         synfig::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
165                 }
166
167                 importer=newimporter;
168                 filename=newfilename;
169                 abs_filename=absolute_path(filename_with_path);
170
171                 return true;
172         }
173         } catch(...) { set_amount(0); return false; }
174
175         return Layer_Bitmap::set_param(param,value);
176 }
177
178 ValueBase
179 Import::get_param(const String & param)const
180 {
181         EXPORT(time_offset);
182
183         if(get_canvas())
184         {
185                 if(param=="filename")
186                 {
187                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
188                         return relative_path(curpath,abs_filename);
189                 }
190         }
191         else
192                 EXPORT(filename);
193
194         EXPORT_NAME();
195         EXPORT_VERSION();
196
197         return Layer_Bitmap::get_param(param);
198 }
199
200 Layer::Vocab
201 Import::get_param_vocab()const
202 {
203         Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
204
205         ret.push_back(ParamDesc("filename")
206                 .set_local_name(_("Filename"))
207                 .set_description(_("File to import"))
208                 .set_hint("filename")
209         );
210         ret.push_back(ParamDesc("time_offset")
211                 .set_local_name(_("Time Offset"))
212         );
213
214         return ret;
215 }
216
217 void
218 Import::set_time(Context context, Time time)const
219 {
220         if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
221         //else surface.clear();
222         context.set_time(time);
223 }
224
225 void
226 Import::set_time(Context context, Time time, const Point &pos)const
227 {
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);
231 }