2000-07-31 12:30:10 +00:00
|
|
|
/* This file is part of
|
|
|
|
* =================================================
|
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
* Copyright 1995 Matthias Ettrich.
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2001-07-17 00:38:04 +00:00
|
|
|
/*
|
|
|
|
* The order of conversion:
|
|
|
|
*
|
|
|
|
* The c-tor calls convertImage()
|
|
|
|
*
|
|
|
|
* convertImage() verifies that we need to do conversion, if not it will just
|
|
|
|
* call the loadImage()
|
|
|
|
* if conversion is needed, it will initiate the conversion.
|
|
|
|
*
|
|
|
|
* When the conversion is completed imageConverted() is called, which in turn
|
|
|
|
* calls loadImage().
|
|
|
|
*
|
|
|
|
* Since we currently do everything synchronously, convertImage() calls
|
|
|
|
* imageConverted() right after it does the call to the conversion process.
|
|
|
|
*/
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
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-07-17 00:38:04 +00:00
|
|
|
if (! success) // Conversion failed miserably (couldn't even start).
|
|
|
|
setStatus(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-07-17 00:38:04 +00:00
|
|
|
void GraphicsCacheItem::setStatus(ImageStatus new_status)
|
|
|
|
{
|
|
|
|
imageStatus_ = new_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2001-07-17 00:38:04 +00:00
|
|
|
GraphicsCacheItem::imageConverted(bool success)
|
2001-02-22 16:53:59 +00:00
|
|
|
{
|
2001-07-17 00:38:04 +00:00
|
|
|
lyxerr << "imageConverted, status=" << success << endl;
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2001-07-17 00:38:04 +00:00
|
|
|
if (! success) {
|
2001-02-22 16:53:59 +00:00
|
|
|
lyxerr << "(GraphicsCacheItem::imageConverter) "
|
|
|
|
"Error converting image." << endl;
|
2001-07-17 00:38:04 +00:00
|
|
|
setStatus(GraphicsCacheItem::ErrorConverting);
|
2001-02-22 16:53:59 +00:00
|
|
|
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-07-23 16:07:29 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2001-07-23 16:07:29 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
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;
|
|
|
|
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-07-23 16:07:29 +00:00
|
|
|
bool result = converters.Convert(0, filename, tempfile, from, to);
|
|
|
|
tempfile.append(".xpm");
|
2001-07-29 06:01:24 +00:00
|
|
|
|
2001-07-17 00:38:04 +00:00
|
|
|
// For now we are synchronous
|
2001-07-23 16:07:29 +00:00
|
|
|
imageConverted(result);
|
2001-07-17 00:38:04 +00:00
|
|
|
|
|
|
|
// Cleanup after the conversion.
|
|
|
|
lyx::unlink(tempfile);
|
|
|
|
tempfile = string();
|
|
|
|
|
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());
|
2001-07-17 00:38:04 +00:00
|
|
|
setStatus(GraphicsCacheItem::Loaded);
|
2001-02-22 16:53:59 +00:00
|
|
|
} else {
|
2001-03-16 12:08:14 +00:00
|
|
|
lyxerr << "Loading " << tempfile << "Failed" << endl;
|
2001-07-17 00:38:04 +00:00
|
|
|
setStatus(GraphicsCacheItem::ErrorReading);
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
2001-02-22 16:53:59 +00:00
|
|
|
}
|