Blame synfig-core/src/modules/lyr_std/import.cpp

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file import.cpp
Carlos Lopez a09598
**	\brief Implementation of the "Import Image" layer
Carlos Lopez a09598
**
Carlos Lopez a09598
**	$Id$
Carlos Lopez a09598
**
Carlos Lopez a09598
**	\legal
Carlos Lopez a09598
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Carlos Lopez a09598
**	Copyright (c) 2007 Chris Moore
Carlos Lopez e83454
**	Copyright (c) 2011-2013 Carlos Lรณpez
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is free software; you can redistribute it and/or
Carlos Lopez a09598
**	modify it under the terms of the GNU General Public License as
Carlos Lopez a09598
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez a09598
**	the License, or (at your option) any later version.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is distributed in the hope that it will be useful,
Carlos Lopez a09598
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez a09598
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez a09598
**	General Public License for more details.
Carlos Lopez a09598
**	\endlegal
Carlos Lopez a09598
**
Carlos Lopez a09598
** === N O T E S ===========================================================
Carlos Lopez a09598
**
Carlos Lopez a09598
** ========================================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === H E A D E R S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
#ifdef USING_PCH
Carlos Lopez a09598
#	include "pch.h"
Carlos Lopez a09598
#else
Carlos Lopez a09598
#ifdef HAVE_CONFIG_H
Carlos Lopez a09598
#	include <config.h></config.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
#include "import.h"
bw 94d8a6
bw 94d8a6
#include <synfig localization.h=""></synfig>
bw 94d8a6
#include <synfig general.h=""></synfig>
bw 94d8a6
Carlos Lopez a09598
#include <synfig string.h=""></synfig>
Carlos Lopez a09598
#include <synfig time.h=""></synfig>
Carlos Lopez a09598
#include <synfig context.h=""></synfig>
Carlos Lopez a09598
#include <synfig paramdesc.h=""></synfig>
Carlos Lopez a09598
#include <synfig renddesc.h=""></synfig>
Carlos Lopez a09598
#include <synfig surface.h=""></synfig>
Carlos Lopez a09598
#include <synfig value.h=""></synfig>
Carlos Lopez a09598
#include <synfig valuenode.h=""></synfig>
Carlos Lopez a09598
#include <synfig canvas.h=""></synfig>
b3b6c7
#include <synfig canvasfilenaming.h=""></synfig>
b3b6c7
#include <synfig filesystem.h=""></synfig>
Carlos Lopez a09598
88c541
#include <synfig rendering="" software="" surfacesw.h=""></synfig>
88c541
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
using namespace std;
Carlos Lopez a09598
using namespace etl;
9f3c68
using namespace synfig;
144d3f
using namespace modules;
9f3c68
using namespace lyr_std;
Carlos Lopez a09598
Carlos Lopez a09598
/* === M A C R O S ========================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === G L O B A L S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
SYNFIG_LAYER_INIT(Import);
Carlos Lopez a09598
SYNFIG_LAYER_SET_NAME(Import,"import");
Carlos Lopez a09598
SYNFIG_LAYER_SET_LOCAL_NAME(Import,N_("Import Image"));
Carlos Lopez a09598
SYNFIG_LAYER_SET_CATEGORY(Import,N_("Other"));
Carlos Lopez a09598
SYNFIG_LAYER_SET_VERSION(Import,"0.1");
Carlos Lopez a09598
SYNFIG_LAYER_SET_CVS_ID(Import,"$Id$");
Carlos Lopez a09598
Carlos Lopez a09598
/* === P R O C E D U R E S ================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === M E T H O D S ======================================================= */
Carlos Lopez a09598
Carlos Lopez e36967
Import::Import():
Carlos Lopez e36967
	param_filename(ValueBase(String())),
Carlos Lopez e36967
	param_time_offset(ValueBase(Time(0)))
Carlos Lopez a09598
{
Carlos Lopez b3fcc2
	SET_INTERPOLATION_DEFAULTS();
Carlos Lopez b3fcc2
	SET_STATIC_DEFAULTS();
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
Import::~Import()
Carlos Lopez a09598
{
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
void
Carlos Lopez a09598
Import::on_canvas_set()
Carlos Lopez a09598
{
0ea095
	Layer_Bitmap::on_canvas_set();
Carlos Lopez e36967
	if(get_canvas())set_param("filename",param_filename);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
bool
Carlos Lopez a09598
Import::set_param(const String & param, const ValueBase &value)
Carlos Lopez a09598
{
Carlos Lopez a09598
	try{
Carlos Lopez e36967
	IMPORT_VALUE(param_time_offset);
84440b
84440b
	IMPORT_VALUE_PLUS_BEGIN(param_filename)
Carlos Lopez a09598
	{
b3b6c7
		if(!get_canvas() || !get_canvas()->get_file_system())
Carlos Lopez a09598
		{
3bd54c
			importer.reset();
3bd54c
			cimporter.reset();
3bd54c
			rendering_surface.reset();
b3b6c7
			param_filename.set(value.get(String()));
Carlos Lopez a09598
			return true;
Carlos Lopez a09598
		}
Carlos Lopez a09598
0dde72
		if (is_surface_modified())
0dde72
		{
0dde72
			error("Unable to load new file, already opened file is not saved");
0dde72
			return false;
0dde72
		}
0dde72
b3b6c7
		String filename = value.get(String());
b3b6c7
		String fixed_filename = filename;
Carlos Lopez a09598
b3b6c7
		// TODO: find source of this sreening of unicode characters
Carlos Lopez a09598
		// Get rid of any %20 crap
2e482a
		for(String::size_type n; (n = fixed_filename.find("%20")) != String::npos;)
2e482a
			fixed_filename.replace(n,3," ");
Carlos Lopez a09598
b3b6c7
		String full_filename = CanvasFileNaming::make_full_filename(get_canvas()->get_file_name(), fixed_filename);
b3b6c7
		if (full_filename.empty())
Carlos Lopez a09598
		{
3bd54c
			importer.reset();
3bd54c
			cimporter.reset();
3bd54c
			rendering_surface.reset();
Carlos Lopez e36967
			param_filename.set(filename);
Carlos Lopez a09598
			return true;
Carlos Lopez a09598
		}
Carlos Lopez a09598
b3b6c7
		String independent_filename = CanvasFileNaming::make_canvas_independent_filename(get_canvas()->get_file_name(), full_filename);
b3b6c7
b3b6c7
		// If we are already loaded, don't reload
386914
		// here we need something to force reload if file is changed
b3b6c7
		if(this->independent_filename==independent_filename && importer)
Carlos Lopez a09598
		{
b3b6c7
			param_filename.set(filename);
b3b6c7
			return true;
b3b6c7
		}
0fd272
b3b6c7
		this->independent_filename = independent_filename;
88c541
b3b6c7
		handle<importer> newimporter;</importer>
b3b6c7
		newimporter = Importer::open(get_canvas()->get_file_system()->get_identifier(full_filename));
Carlos Lopez 707a31
b3b6c7
		if (!newimporter)
b3b6c7
		{
b3b6c7
			String local_filename = CanvasFileNaming::make_local_filename(get_canvas()->get_file_name(), full_filename);
b3b6c7
			newimporter = Importer::open(get_canvas()->get_file_system()->get_identifier(local_filename));
b3b6c7
			if(!newimporter)
Carlos Lopez 707a31
			{
b3b6c7
				error(strprintf("Unable to create an importer object with file \"%s\"", independent_filename.c_str()));
3bd54c
				importer.reset();
3bd54c
				cimporter.reset();
d4b6c4
				param_filename.set(filename);
b3b6c7
				rendering_surface.reset();
2e482a
				return true;
Carlos Lopez a09598
			}
Carlos Lopez a09598
		}
b3b6c7
b3b6c7
		Time time_offset = param_time_offset.get(Time());
2825dd
		Time time = (get_time_mark() == Time::end()) ? time_offset : get_time_mark() + time_offset;
b3b6c7
		if (!newimporter->is_animated())
b3b6c7
			time = Time(0);
b3b6c7
5e2754
		rendering_surface = new rendering::SurfaceResource(
5e2754
			newimporter->get_frame(get_canvas()->rend_desc(), time) );
b3b6c7
		importer=newimporter;
b3b6c7
		param_filename.set(filename);
b3b6c7
b3b6c7
		return true;
Carlos Lopez a09598
	}
84440b
	IMPORT_VALUE_PLUS_END
88928b
	} catch(...) { return false; }
Carlos Lopez a09598
Carlos Lopez a09598
	return Layer_Bitmap::set_param(param,value);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
ValueBase
Carlos Lopez a09598
Import::get_param(const String & param)const
Carlos Lopez a09598
{
Carlos Lopez e36967
	EXPORT_VALUE(param_time_offset);
b3b6c7
	EXPORT_VALUE(param_filename);
Carlos Lopez a09598
Carlos Lopez a09598
	EXPORT_NAME();
Carlos Lopez a09598
	EXPORT_VERSION();
Carlos Lopez a09598
Carlos Lopez a09598
	return Layer_Bitmap::get_param(param);
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
Layer::Vocab
Carlos Lopez a09598
Import::get_param_vocab()const
Carlos Lopez a09598
{
Carlos Lopez a09598
	Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
Carlos Lopez a09598
Carlos Lopez a09598
	ret.push_back(ParamDesc("filename")
Carlos Lopez a09598
		.set_local_name(_("Filename"))
Carlos Lopez a09598
		.set_description(_("File to import"))
Carlos Lopez a09598
		.set_hint("filename")
Carlos Lopez a09598
	);
Carlos Lopez a09598
	ret.push_back(ParamDesc("time_offset")
Carlos Lopez a09598
		.set_local_name(_("Time Offset"))
Carlos Lopez d243d7
		.set_description(_("Time Offset to apply to the imported file"))
Carlos Lopez a09598
	);
Carlos Lopez a09598
Carlos Lopez a09598
	return ret;
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
void
dd97a8
Import::set_time_vfunc(IndependentContext context, Time time)const
Carlos Lopez a09598
{
7476c3
	//Time time_offset = param_time_offset.get(Time());
c006c9
	//if(get_amount() && importer && importer->is_animated())
c006c9
	//	rendering_surface = new rendering::SurfaceResource(
c006c9
	//		importer->get_frame(get_canvas()->rend_desc(), time+time_offset) );
c006c9
	context.set_time(time);
c006c9
}
c006c9
c006c9
void
c006c9
Import::load_resources_vfunc(IndependentContext context, Time time)const
c006c9
{
c006c9
	Time time_offset=param_time_offset.get(Time());
b3b6c7
	if(get_amount() && importer && importer->is_animated())
5e2754
		rendering_surface = new rendering::SurfaceResource(
5e2754
			importer->get_frame(get_canvas()->rend_desc(), time+time_offset) );
c006c9
	context.load_resources(time);
Carlos Lopez a09598
}