2000-07-31 12:30:10 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/* This file is part of
|
|
|
|
* =================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
* Copyright 1995 Matthias Ettrich.
|
|
|
|
* Copyright 1995-2000 The LyX Team.
|
|
|
|
*
|
|
|
|
* This file Copyright 2000 Baruch Even
|
|
|
|
* ================================================= */
|
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2000-08-10 13:15:05 +00:00
|
|
|
#include "graphics/GraphicsCache.h"
|
|
|
|
#include "graphics/GraphicsCacheItem.h"
|
2000-10-12 10:46:06 +00:00
|
|
|
#include "frontends/support/LyXImage.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "graphics/ImageLoaderXPM.h"
|
2001-04-02 19:56:48 +00:00
|
|
|
#include "converter.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lyxlib.h"
|
2001-04-02 19:56:48 +00:00
|
|
|
#include "lyx_gui_misc.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "debug.h"
|
2001-04-02 19:56:48 +00:00
|
|
|
#include "support/LAssert.h"
|
|
|
|
#include "gettext.h"
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
GraphicsCacheItem::GraphicsCacheItem(string const & filename)
|
|
|
|
: imageStatus_(GraphicsCacheItem::Loading)
|
2000-08-10 13:15:05 +00:00
|
|
|
{
|
2001-02-22 16:53:59 +00:00
|
|
|
filename_ = filename;
|
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
bool success = convertImage(filename);
|
2001-02-22 16:53:59 +00:00
|
|
|
// For now we do it synchronously
|
2001-04-02 19:56:48 +00:00
|
|
|
if (success)
|
|
|
|
imageConverted(0);
|
|
|
|
else
|
|
|
|
imageStatus_ = ErrorConverting;
|
2000-08-10 13:15:05 +00:00
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
GraphicsCacheItem::~GraphicsCacheItem()
|
2001-02-22 16:53:59 +00:00
|
|
|
{}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
GraphicsCacheItem::ImageStatus
|
|
|
|
GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
LyXImage *
|
|
|
|
GraphicsCacheItem::getImage() const { return image_.get(); }
|
2000-08-08 09:18:39 +00:00
|
|
|
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
void
|
|
|
|
GraphicsCacheItem::imageConverted(int retval)
|
|
|
|
{
|
|
|
|
lyxerr << "imageConverted, retval=" << retval << endl;
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
if (retval) {
|
|
|
|
lyxerr << "(GraphicsCacheItem::imageConverter) "
|
|
|
|
"Error converting image." << endl;
|
|
|
|
imageStatus_ = GraphicsCacheItem::ErrorConverting;
|
|
|
|
return;
|
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
// Do the actual image loading from file to memory.
|
|
|
|
loadImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2001-04-24 15:25:26 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
string const findTargetFormat(string const & from)
|
|
|
|
{
|
|
|
|
typedef ImageLoader::FormatList FormatList;
|
|
|
|
FormatList formats = ImageLoaderXPM().loadableFormats();
|
2001-04-24 15:25:26 +00:00
|
|
|
lyx::Assert(formats.size() > 0); // There must be a format to load from.
|
2001-04-02 19:56:48 +00:00
|
|
|
|
|
|
|
FormatList::const_iterator iter = formats.begin();
|
|
|
|
FormatList::const_iterator end = formats.end();
|
|
|
|
|
|
|
|
for (; iter != end; ++iter) {
|
|
|
|
if (converters.IsReachable(from, *iter))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (iter == end) {
|
|
|
|
// We do not know how to convert the image to something loadable.
|
|
|
|
lyxerr << "ERROR: Do not know how to convert image." << std::endl;
|
|
|
|
|
|
|
|
string const first(_("Cannot convert image to display format"));
|
|
|
|
string const second1(_("Need converter from "));
|
|
|
|
string const second2(_(" to "));
|
|
|
|
string const second(second1 + from + second2 + formats[0]);
|
|
|
|
|
|
|
|
WriteAlert(first, second);
|
|
|
|
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*iter);
|
2000-10-12 10:46:06 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
} // anon namespace
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
|
|
|
|
bool
|
2001-04-02 19:56:48 +00:00
|
|
|
GraphicsCacheItem::convertImage(string const & filename)
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2001-04-02 19:56:48 +00:00
|
|
|
string const from = GetExtension(filename);
|
|
|
|
string const to = findTargetFormat(from);
|
|
|
|
if (to.empty())
|
|
|
|
return false;
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// Take only the filename part of the file, without path or extension.
|
|
|
|
string temp = OnlyFilename(filename);
|
|
|
|
temp = ChangeExtension(filename, string());
|
|
|
|
|
|
|
|
// Add some stuff to have it a unique temp file.
|
2001-04-02 19:56:48 +00:00
|
|
|
// This tempfile is deleted in loadImage after it is loaded to memory.
|
2001-02-22 16:53:59 +00:00
|
|
|
tempfile = lyx::tempName(string(), temp);
|
|
|
|
// Remove the temp file, we only want the name...
|
|
|
|
lyx::unlink(tempfile);
|
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
converters.Convert(0, filename, tempfile, from, to);
|
2001-02-22 16:53:59 +00:00
|
|
|
|
|
|
|
return true;
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// This function gets called from the callback after the image has been
|
|
|
|
// converted successfully.
|
2000-08-08 09:18:39 +00:00
|
|
|
void
|
2001-04-02 19:56:48 +00:00
|
|
|
GraphicsCacheItem::loadImage()
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2001-02-22 16:53:59 +00:00
|
|
|
lyxerr << "Loading XPM Image... ";
|
|
|
|
|
|
|
|
ImageLoaderXPM imageLoader;
|
|
|
|
if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
|
|
|
|
lyxerr << "Success." << endl;
|
|
|
|
image_.reset(imageLoader.getImage());
|
|
|
|
imageStatus_ = GraphicsCacheItem::Loaded;
|
|
|
|
} else {
|
2001-03-16 12:08:14 +00:00
|
|
|
lyxerr << "Loading " << tempfile << "Failed" << endl;
|
2001-02-22 16:53:59 +00:00
|
|
|
imageStatus_ = GraphicsCacheItem::ErrorReading;
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// remove the xpm file now.
|
|
|
|
lyx::unlink(tempfile);
|
|
|
|
// and remove the reference to the filename.
|
|
|
|
tempfile = string();
|
|
|
|
}
|