2002-02-27 09:59:52 +00:00
|
|
|
/*
|
|
|
|
* \file GraphicsCacheItem.C
|
|
|
|
* Copyright 2002 the LyX Team
|
|
|
|
* Read the file COPYING
|
2000-07-31 12:30:10 +00:00
|
|
|
*
|
2002-02-27 09:59:52 +00:00
|
|
|
* \author Baruch Even <baruch.even@writeme.com>
|
2002-01-31 14:20:09 +00:00
|
|
|
* \author Herbert Voss <voss@lyx.org>
|
2002-02-27 09:59:52 +00:00
|
|
|
* \author Angus Leeming <a.leeming@ic.ac.uk>
|
|
|
|
*/
|
2000-07-31 12:30:10 +00:00
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-07-31 12:30:10 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
#include "graphics/GraphicsCache.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "graphics/GraphicsCacheItem.h"
|
|
|
|
#include "graphics/GraphicsImage.h"
|
|
|
|
#include "graphics/GraphicsParams.h"
|
|
|
|
#include "graphics/GraphicsConverter.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "insets/insetgraphics.h"
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "BufferView.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "debug.h"
|
2001-04-02 19:56:48 +00:00
|
|
|
#include "gettext.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "lyx_main.h" // for global dispatch method
|
2002-05-29 16:21:03 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "support/LAssert.h"
|
2002-02-16 15:59:55 +00:00
|
|
|
#include "support/filetools.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-05-29 16:21:03 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// Very, Very UGLY!
|
|
|
|
extern BufferView * current_view;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
using std::endl;
|
|
|
|
|
2002-03-11 22:58:05 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
namespace grfx {
|
|
|
|
|
|
|
|
GCacheItem::GCacheItem(InsetGraphics const & inset, GParams const & params)
|
|
|
|
: filename_(params.filename), zipped_(false),
|
|
|
|
remove_loaded_file_(false), status_(WaitingToLoad)
|
|
|
|
{
|
|
|
|
ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
|
|
|
|
modified_images.push_back(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
typedef GCacheItem::ModifiedItemPtr ModifiedItemPtr;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
class Compare_Params {
|
|
|
|
public:
|
|
|
|
Compare_Params(GParams const & p) : p_(p) {}
|
|
|
|
|
|
|
|
bool operator()(ModifiedItemPtr const & ptr)
|
|
|
|
{
|
|
|
|
if (!ptr.get())
|
|
|
|
return false;
|
|
|
|
return ptr->params() == p_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GParams const & p_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Find_Inset {
|
|
|
|
public:
|
|
|
|
Find_Inset(InsetGraphics const & i) : i_(i) {}
|
|
|
|
|
|
|
|
bool operator()(ModifiedItemPtr const & ptr)
|
|
|
|
{
|
|
|
|
if (!ptr.get())
|
|
|
|
return false;
|
|
|
|
return ptr->referencedBy(i_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
InsetGraphics const & i_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace anon
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2001-07-17 00:38:04 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
void GCacheItem::modify(InsetGraphics const & inset, GParams const & params)
|
2000-08-10 13:15:05 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
// Does this inset currently reference an existing ModifiedItem with
|
|
|
|
// different params?
|
|
|
|
// If so, remove the inset from the ModifiedItem's internal list
|
|
|
|
// of insets
|
|
|
|
ListType::iterator begin = modified_images.begin();
|
|
|
|
ListType::iterator end = modified_images.end();
|
|
|
|
ListType::iterator it = begin;
|
|
|
|
while (it != end) {
|
|
|
|
it = std::find_if(it, end, Find_Inset(inset));
|
|
|
|
if (it == end)
|
|
|
|
break;
|
|
|
|
if ((*it)->params() != params) {
|
|
|
|
(*it)->remove(inset);
|
|
|
|
if ((*it)->empty())
|
|
|
|
it = modified_images.erase(it);
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is there an existing ModifiedItem with these params?
|
|
|
|
// If so, add inset to the list of insets referencing this ModifiedItem
|
|
|
|
begin = modified_images.begin();
|
|
|
|
end = modified_images.end();
|
|
|
|
it = std::find_if(begin, end, Compare_Params(params));
|
|
|
|
if (it != end) {
|
|
|
|
(*it)->add(inset);
|
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// If no ModifiedItem exists with these params, then create one.
|
|
|
|
ModifiedItemPtr item(new ModifiedItem(inset, params, image_));
|
|
|
|
modified_images.push_back(item);
|
|
|
|
|
|
|
|
return;
|
2000-08-10 13:15:05 +00:00
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
void GCacheItem::remove(InsetGraphics const & inset)
|
|
|
|
{
|
|
|
|
// search the list of ModifiedItems for one referenced by this inset.
|
|
|
|
// If it is found, remove the reference.
|
|
|
|
// If the ModifiedItem is now referenced by no insets, remove it.
|
|
|
|
ListType::iterator begin = modified_images.begin();
|
|
|
|
ListType::iterator end = modified_images.end();
|
|
|
|
ListType::iterator it = std::find_if(begin, end, Find_Inset(inset));
|
|
|
|
|
|
|
|
if (it == end)
|
|
|
|
return;
|
|
|
|
|
|
|
|
(*it)->remove(inset);
|
|
|
|
if ((*it)->empty()) {
|
|
|
|
modified_images.clear();
|
|
|
|
}
|
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
void GCacheItem::startLoading(InsetGraphics const & inset)
|
2002-02-16 15:59:55 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
if (status() != WaitingToLoad)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check that the image is referenced by this inset
|
|
|
|
ListType::const_iterator begin = modified_images.begin();
|
|
|
|
ListType::const_iterator end = modified_images.end();
|
|
|
|
ListType::const_iterator it =
|
|
|
|
std::find_if(begin, end, Find_Inset(inset));
|
|
|
|
|
|
|
|
if (it == end)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((*it)->params().display == GParams::NONE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
convertToDisplayFormat();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GCacheItem::empty() const
|
|
|
|
{
|
|
|
|
return modified_images.empty();
|
2002-02-16 15:59:55 +00:00
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
bool GCacheItem::referencedBy(InsetGraphics const & inset) const
|
2001-07-17 00:38:04 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
// Is one of the list of ModifiedItems referenced by this inset?
|
|
|
|
ListType::const_iterator begin = modified_images.begin();
|
|
|
|
ListType::const_iterator end = modified_images.end();
|
|
|
|
return std::find_if(begin, end, Find_Inset(inset)) != end;
|
2001-07-17 00:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
string const & GCacheItem::filename() const
|
2002-02-16 15:59:55 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
return filename_;
|
2002-02-16 15:59:55 +00:00
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
ImagePtr const GCacheItem::image(InsetGraphics const & inset) const
|
2001-02-22 16:53:59 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
// find a ModifiedItem that is referenced by this inset.
|
|
|
|
ListType::const_iterator begin = modified_images.begin();
|
|
|
|
ListType::const_iterator end = modified_images.end();
|
2002-03-21 17:27:08 +00:00
|
|
|
ListType::const_iterator it =
|
2002-02-27 09:59:52 +00:00
|
|
|
std::find_if(begin, end, Find_Inset(inset));
|
|
|
|
|
|
|
|
// Someone's being daft.
|
|
|
|
if (it == end)
|
|
|
|
return ImagePtr();
|
|
|
|
|
|
|
|
// We are expressly requested to not render the image
|
|
|
|
if ((*it)->params().display == GParams::NONE)
|
|
|
|
return ImagePtr();
|
|
|
|
|
|
|
|
// If the original image has been loaded, return what's going on
|
|
|
|
// in the ModifiedItem
|
|
|
|
if (status() == Loaded)
|
|
|
|
return (*it)->image();
|
|
|
|
|
|
|
|
return ImagePtr();
|
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
ImageStatus GCacheItem::status(InsetGraphics const & inset) const
|
|
|
|
{
|
|
|
|
// find a ModifiedItem that is referenced by this inset.
|
|
|
|
ListType::const_iterator begin = modified_images.begin();
|
|
|
|
ListType::const_iterator end = modified_images.end();
|
2002-03-21 17:27:08 +00:00
|
|
|
ListType::const_iterator it =
|
2002-02-27 09:59:52 +00:00
|
|
|
std::find_if(begin, end, Find_Inset(inset));
|
|
|
|
|
|
|
|
// Someone's being daft.
|
|
|
|
if (it == end)
|
|
|
|
return ErrorUnknown;
|
|
|
|
|
|
|
|
if (status() == Loaded)
|
|
|
|
return (*it)->status();
|
|
|
|
|
|
|
|
return status();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Called internally only. Use to ascertain the status of the loading of the
|
|
|
|
// original image. No scaling etc.
|
|
|
|
ImageStatus GCacheItem::status() const
|
|
|
|
{
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCacheItem::setStatus(ImageStatus new_status)
|
|
|
|
{
|
|
|
|
status_ = new_status;
|
|
|
|
|
|
|
|
// Loop over all insets and tell the BufferView that it has changed.
|
|
|
|
typedef ModifiedItem::ListType::const_iterator inset_iterator;
|
|
|
|
|
|
|
|
ListType::const_iterator it = modified_images.begin();
|
|
|
|
ListType::const_iterator end = modified_images.end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
inset_iterator it2 = (*it)->insets.begin();
|
|
|
|
inset_iterator end2 = (*it)->insets.end();
|
|
|
|
|
|
|
|
for (; it2 != end2; ++it2) {
|
|
|
|
InsetGraphics * inset =
|
|
|
|
const_cast<InsetGraphics *>(*it2);
|
|
|
|
|
|
|
|
// Use of current_view is very, very Evil!!
|
|
|
|
current_view->updateInset(inset, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCacheItem::changeDisplay(bool changed_background)
|
|
|
|
{
|
|
|
|
ListType::iterator begin = modified_images.begin();
|
|
|
|
ListType::iterator end = modified_images.end();
|
|
|
|
|
|
|
|
// The background has changed. Change all modified images.
|
|
|
|
if (changed_background) {
|
|
|
|
for (ListType::iterator it = begin; it != end; ++it) {
|
|
|
|
(*it)->setPixmap();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
ListType temp_list;
|
|
|
|
|
|
|
|
for (ListType::iterator it = begin; it != end; ++it) {
|
|
|
|
// ModifiedItem::changeDisplay returns a full
|
|
|
|
// ModifiedItemPtr if any of the insets have display=DEFAULT
|
|
|
|
// and if that DEFAULT value has changed
|
|
|
|
ModifiedItemPtr new_item = (*it)->changeDisplay();
|
|
|
|
if (!new_item.get())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
temp_list.push_back(new_item);
|
|
|
|
|
|
|
|
// The original store may now be empty
|
|
|
|
if ((*it)->insets.empty()) {
|
|
|
|
it = modified_images.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_list.empty())
|
2001-02-22 16:53:59 +00:00
|
|
|
return;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
// Recombine new_list and modified_images.
|
|
|
|
begin = modified_images.begin();
|
|
|
|
end = modified_images.end();
|
|
|
|
|
|
|
|
ListType::const_iterator tbegin = temp_list.begin();
|
|
|
|
ListType::const_iterator tend = temp_list.end();
|
|
|
|
|
|
|
|
ListType append_list;
|
|
|
|
|
|
|
|
for (ListType::const_iterator tit = tbegin; tit != tend; ++tit) {
|
|
|
|
GParams const & params = (*tit)->params();
|
|
|
|
ListType::iterator it =
|
|
|
|
std::find_if(begin, end, Compare_Params(params));
|
|
|
|
if (it == end)
|
|
|
|
append_list.push_back(*tit);
|
|
|
|
else
|
|
|
|
(*it)->insets.merge((*tit)->insets);
|
2001-02-22 16:53:59 +00:00
|
|
|
}
|
2000-08-08 09:18:39 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
if (append_list.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
modified_images.splice(modified_images.end(), append_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCacheItem::imageConverted(string const & file_to_load)
|
|
|
|
{
|
|
|
|
bool const success =
|
|
|
|
(!file_to_load.empty() && IsFileReadable(file_to_load));
|
|
|
|
|
|
|
|
string const text = success ? "succeeded" : "failed";
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Image conversion " << text << "." << endl;
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
setStatus(ErrorConverting);
|
|
|
|
|
|
|
|
if (zipped_)
|
|
|
|
lyx::unlink(unzipped_filename_);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_.disconnect();
|
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
// Do the actual image loading from file to memory.
|
2002-02-27 09:59:52 +00:00
|
|
|
file_to_load_ = file_to_load;
|
|
|
|
|
|
|
|
loadImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function gets called from the callback after the image has been
|
|
|
|
// converted successfully.
|
|
|
|
void GCacheItem::loadImage()
|
|
|
|
{
|
|
|
|
setStatus(Loading);
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Loading image." << endl;
|
|
|
|
|
|
|
|
// Connect a signal to this->imageLoaded and pass this signal to
|
|
|
|
// GImage::loadImage.
|
|
|
|
SignalLoadTypePtr on_finish;
|
|
|
|
on_finish.reset(new SignalLoadType);
|
2002-05-29 16:21:03 +00:00
|
|
|
cl_ = on_finish->connect(boost::bind(&GCacheItem::imageLoaded, this, _1));
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
image_ = GImage::newImage();
|
|
|
|
image_->load(file_to_load_, on_finish);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCacheItem::imageLoaded(bool success)
|
|
|
|
{
|
|
|
|
string const text = success ? "succeeded" : "failed";
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Image loading " << text << "." << endl;
|
|
|
|
|
|
|
|
// Clean up after loading.
|
|
|
|
if (zipped_)
|
|
|
|
lyx::unlink(unzipped_filename_);
|
|
|
|
|
|
|
|
if (remove_loaded_file_ && unzipped_filename_ != file_to_load_)
|
|
|
|
lyx::unlink(file_to_load_);
|
|
|
|
|
|
|
|
cl_.disconnect();
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
setStatus(ErrorLoading);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus(Loaded);
|
|
|
|
|
|
|
|
// Loop over the list of modified images and create them.
|
|
|
|
ListType::iterator it = modified_images.begin();
|
|
|
|
ListType::iterator end = modified_images.end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
(*it)->modify(image_);
|
|
|
|
}
|
2001-04-02 19:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-11 17:40:44 +00:00
|
|
|
unsigned int GCacheItem::raw_width() const
|
|
|
|
{
|
|
|
|
if (!image_.get())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return image_->getWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int GCacheItem::raw_height() const
|
|
|
|
{
|
|
|
|
if (!image_.get())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return image_->getHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
namespace {
|
2001-04-24 15:25:26 +00:00
|
|
|
|
2001-04-02 19:56:48 +00:00
|
|
|
string const findTargetFormat(string const & from)
|
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
typedef GImage::FormatList FormatList;
|
2002-04-12 14:08:00 +00:00
|
|
|
FormatList const formats = GImage::loadableFormats();
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
// There must be a format to load from.
|
2002-02-27 09:59:52 +00:00
|
|
|
lyx::Assert(!formats.empty());
|
2001-07-23 16:07:29 +00:00
|
|
|
|
2002-02-28 19:55:26 +00:00
|
|
|
// First ascertain if we can load directly with no conversion
|
|
|
|
FormatList::const_iterator it1 = formats.begin();
|
2002-02-27 09:59:52 +00:00
|
|
|
FormatList::const_iterator end = formats.end();
|
2002-02-28 19:55:26 +00:00
|
|
|
for (; it1 != end; ++it1) {
|
|
|
|
if (from == *it1)
|
|
|
|
return *it1;
|
2001-04-02 19:56:48 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-02-28 19:55:26 +00:00
|
|
|
// So, we have to convert to a loadable format. Can we?
|
|
|
|
grfx::GConverter const & graphics_converter = grfx::GConverter::get();
|
|
|
|
|
|
|
|
FormatList::const_iterator it2 = formats.begin();
|
|
|
|
for (; it2 != end; ++it2) {
|
|
|
|
if (graphics_converter.isReachable(from, *it2))
|
|
|
|
return *it2;
|
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-02-28 19:55:26 +00:00
|
|
|
// Failed!
|
|
|
|
return string();
|
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
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
void GCacheItem::convertToDisplayFormat()
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
setStatus(Converting);
|
|
|
|
string filename = filename_; // Make a local copy in case we unzip it
|
|
|
|
string const displayed_filename = MakeDisplayPath(filename_);
|
|
|
|
|
|
|
|
// First, check that the file exists!
|
|
|
|
if (!IsFileReadable(filename)) {
|
|
|
|
setStatus(ErrorNoFile);
|
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-01-31 14:20:09 +00:00
|
|
|
// maybe that other zip extensions also be useful, especially the
|
|
|
|
// ones that may be declared in texmf/tex/latex/config/graphics.cfg.
|
|
|
|
// for example:
|
|
|
|
/* -----------snip-------------
|
2002-03-21 17:27:08 +00:00
|
|
|
{\DeclareGraphicsRule{.pz}{eps}{.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.eps.Z}{eps}{.eps.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.ps.Z}{eps}{.ps.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.ps.gz}{eps}{.ps.bb}{}%
|
|
|
|
\DeclareGraphicsRule{.eps.gz}{eps}{.eps.bb}{}}}%
|
2002-01-31 14:20:09 +00:00
|
|
|
-----------snip-------------*/
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "Attempting to convert image file: " << displayed_filename
|
|
|
|
<< "\nwith recognised extension: " << GetExtension(filename)
|
|
|
|
<< "." << endl;
|
|
|
|
|
|
|
|
zipped_ = zippedFile(filename);
|
|
|
|
if (zipped_) {
|
|
|
|
filename = unzipFile(filename);
|
|
|
|
unzipped_filename_ = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
string const from = getExtFromContents(filename);
|
|
|
|
string const to = grfx::findTargetFormat(from);
|
|
|
|
|
|
|
|
lyxerr[Debug::GRAPHICS]
|
|
|
|
<< "The file contains " << from << " format data." << endl;
|
|
|
|
|
|
|
|
if (to.empty()) {
|
|
|
|
setStatus(ErrorConverting);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-09-20 13:16:17 +00:00
|
|
|
if (from == to) {
|
|
|
|
// No conversion needed!
|
2002-02-27 09:59:52 +00:00
|
|
|
lyxerr[Debug::GRAPHICS] << "No conversion needed!" << endl;
|
|
|
|
file_to_load_ = filename;
|
|
|
|
loadImage();
|
|
|
|
return;
|
2001-09-20 13:16:17 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
lyxerr[Debug::GRAPHICS] << "Converting it to " << to << " format." << endl;
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// Take only the filename part of the file, without path or extension.
|
2002-02-27 09:59:52 +00:00
|
|
|
string const temp = ChangeExtension(OnlyFilename(filename), string());
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// Add some stuff to create a uniquely named temporary file.
|
|
|
|
// This file is deleted in loadImage after it is loaded into memory.
|
|
|
|
string const to_file_base = lyx::tempName(string(), temp);
|
|
|
|
remove_loaded_file_ = true;
|
|
|
|
|
2001-02-22 16:53:59 +00:00
|
|
|
// Remove the temp file, we only want the name...
|
2002-02-27 09:59:52 +00:00
|
|
|
lyx::unlink(to_file_base);
|
|
|
|
|
|
|
|
// Connect a signal to this->imageConverted and pass this signal to
|
|
|
|
// the graphics converter so that we can load the modified file
|
|
|
|
// on completion of the conversion process.
|
|
|
|
SignalConvertTypePtr on_finish;
|
|
|
|
on_finish.reset(new SignalConvertType);
|
2002-05-29 16:21:03 +00:00
|
|
|
cc_ = on_finish->connect(boost::bind(&GCacheItem::imageConverted, this, _1));
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
GConverter & graphics_converter = GConverter::get();
|
|
|
|
graphics_converter.convert(filename, to_file_base, from, to, on_finish);
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
ModifiedItem::ModifiedItem(InsetGraphics const & new_inset,
|
|
|
|
GParams const & new_params,
|
|
|
|
ImagePtr const & new_image)
|
|
|
|
: status_(ScalingEtc)
|
|
|
|
{
|
|
|
|
p_.reset(new GParams(new_params));
|
|
|
|
insets.push_back(&new_inset);
|
|
|
|
modify(new_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModifiedItem::add(InsetGraphics const & inset)
|
|
|
|
{
|
|
|
|
insets.push_back(&inset);
|
|
|
|
insets.sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModifiedItem::remove(InsetGraphics const & inset)
|
2000-08-08 09:18:39 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
ListType::iterator begin = insets.begin();
|
|
|
|
ListType::iterator end = insets.end();
|
|
|
|
ListType::iterator it = std::remove(begin, end, &inset);
|
|
|
|
insets.erase(it, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ModifiedItem::referencedBy(InsetGraphics const & inset) const
|
|
|
|
{
|
|
|
|
ListType::const_iterator begin = insets.begin();
|
|
|
|
ListType::const_iterator end = insets.end();
|
|
|
|
return std::find(begin, end, &inset) != end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImagePtr const ModifiedItem::image() const
|
|
|
|
{
|
|
|
|
if (modified_image_.get())
|
|
|
|
return modified_image_;
|
|
|
|
|
|
|
|
return original_image_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModifiedItem::modify(ImagePtr const & new_image)
|
|
|
|
{
|
2002-03-21 17:27:08 +00:00
|
|
|
if (!new_image.get())
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
original_image_ = new_image;
|
|
|
|
modified_image_.reset(original_image_->clone());
|
|
|
|
|
|
|
|
if (params().display == GParams::NONE) {
|
|
|
|
setStatus(Loaded);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus(ScalingEtc);
|
|
|
|
modified_image_->clip(params());
|
|
|
|
modified_image_->rotate(params());
|
|
|
|
modified_image_->scale(params());
|
|
|
|
setPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModifiedItem::setPixmap()
|
|
|
|
{
|
2002-03-21 17:27:08 +00:00
|
|
|
if (!modified_image_.get())
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (params().display == GParams::NONE) {
|
|
|
|
setStatus(Loaded);
|
|
|
|
return;
|
|
|
|
}
|
2001-02-22 16:53:59 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
bool const success = modified_image_->setPixmap(params());
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
setStatus(Loaded);
|
2001-02-22 16:53:59 +00:00
|
|
|
} else {
|
2002-02-27 09:59:52 +00:00
|
|
|
modified_image_.reset();
|
|
|
|
setStatus(ErrorScalingEtc);
|
2000-08-08 09:18:39 +00:00
|
|
|
}
|
2001-02-22 16:53:59 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
void ModifiedItem::setStatus(ImageStatus new_status)
|
|
|
|
{
|
|
|
|
status_ = new_status;
|
|
|
|
|
|
|
|
// Tell the BufferView that the inset has changed.
|
|
|
|
// Very, Very Ugly!!
|
|
|
|
ListType::const_iterator it = insets.begin();
|
|
|
|
ListType::const_iterator end = insets.end();
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
InsetGraphics * inset = const_cast<InsetGraphics *>(*it);
|
|
|
|
current_view->updateInset(inset, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Params_Changed {
|
|
|
|
|
|
|
|
Params_Changed(GParams const & p) : p_(p) {}
|
|
|
|
|
|
|
|
bool operator()(InsetGraphics const * inset)
|
|
|
|
{
|
2002-03-26 12:43:21 +00:00
|
|
|
string const path = OnlyPath(p_.filename);
|
2002-05-28 11:50:04 +00:00
|
|
|
return inset->params().asGParams(path) != p_;
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GParams p_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
// changeDisplay returns an initialised ModifiedItem if any of the insets
|
|
|
|
// have display == DEFAULT and if that DEFAULT value has changed.
|
|
|
|
// If this occurs, then (this) has these insets removed.
|
|
|
|
ModifiedItemPtr ModifiedItem::changeDisplay()
|
|
|
|
{
|
|
|
|
// Loop over the list of insets. Compare the updated params for each
|
|
|
|
// with params(). If different, move into a new list.
|
|
|
|
ListType::iterator begin = insets.begin();
|
2002-03-21 17:27:08 +00:00
|
|
|
ListType::iterator end = insets.end();
|
2002-02-27 09:59:52 +00:00
|
|
|
ListType::iterator it =
|
|
|
|
std::remove_if(begin, end, Params_Changed(params()));
|
|
|
|
|
|
|
|
if (it == end) {
|
|
|
|
// No insets have changed params
|
|
|
|
return ModifiedItemPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
// it -> end have params that are changed. Move to the new list.
|
|
|
|
ListType new_insets;
|
|
|
|
new_insets.insert(new_insets.begin(), it, end);
|
|
|
|
insets.erase(it, end);
|
|
|
|
|
|
|
|
// Create a new ModifiedItem with these new params. Note that
|
|
|
|
// the only params that have changed are the display ones,
|
|
|
|
// so we don't need to crop, rotate, scale.
|
2002-03-26 12:43:21 +00:00
|
|
|
string const path = OnlyPath(p_->filename);
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
ModifiedItemPtr new_item(new ModifiedItem(*this));
|
|
|
|
new_item->insets = new_insets;
|
2002-05-28 11:50:04 +00:00
|
|
|
*(new_item->p_) = (*new_insets.begin())->params().asGParams(path);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
new_item->setPixmap();
|
|
|
|
return new_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace grfx
|