2002-02-27 09:59:52 +00:00
|
|
|
/*
|
|
|
|
* \file GraphicsCache.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>
|
|
|
|
* \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
|
|
|
|
|
|
|
|
#include "GraphicsCache.h"
|
2001-02-22 16:53:59 +00:00
|
|
|
#include "GraphicsCacheItem.h"
|
2002-02-27 09:59:52 +00:00
|
|
|
#include "GraphicsImage.h"
|
|
|
|
#include "GraphicsParams.h"
|
|
|
|
#include "insets/insetgraphics.h"
|
2002-06-12 09:47:10 +00:00
|
|
|
#include "frontends/lyx_gui.h"
|
2002-02-27 17:27:59 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
namespace grfx {
|
|
|
|
|
|
|
|
GCache & GCache::get()
|
|
|
|
{
|
|
|
|
static bool start = true;
|
|
|
|
if (start) {
|
|
|
|
start = false;
|
2002-06-12 09:47:10 +00:00
|
|
|
lyx_gui::init_graphics();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now return the cache
|
|
|
|
static GCache singleton;
|
2000-09-14 17:53:12 +00:00
|
|
|
return singleton;
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
GCache::GCache()
|
2000-07-31 16:39:50 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
cache = new CacheType;
|
2000-07-31 16:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// all elements are destroyed by the shared_ptr's in the map.
|
|
|
|
GCache::~GCache()
|
2000-07-31 12:30:10 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
delete cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-22 16:37:52 +00:00
|
|
|
void GCache::update(InsetGraphics const & inset, string const & filepath)
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
|
|
|
// A subset only of InsetGraphicsParams is needed for display purposes.
|
|
|
|
// The GraphicsParams c-tor also interrogates lyxrc to ascertain whether
|
|
|
|
// to display or not.
|
2002-05-28 11:50:04 +00:00
|
|
|
GParams params = inset.params().asGParams(filepath);
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
// Each inset can reference only one file, so check the cache for any
|
|
|
|
// graphics files referenced by inset. If the name of this file is
|
|
|
|
// different from that in params, then remove the reference.
|
|
|
|
CacheType::iterator it = find(inset);
|
|
|
|
|
|
|
|
if (it != cache->end()) {
|
|
|
|
CacheItemType item = it->second;
|
|
|
|
if (item->filename() != params.filename) {
|
|
|
|
item->remove(inset);
|
|
|
|
if (item->empty())
|
|
|
|
cache->erase(it);
|
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
}
|
2001-02-22 16:53:59 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
// Are we adding a new file or modifying the display of an existing one?
|
|
|
|
it = cache->find(params.filename);
|
|
|
|
|
|
|
|
if (it != cache->end()) {
|
|
|
|
it->second->modify(inset, params);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheItemType item(new GCacheItem(inset, params));
|
|
|
|
if (item.get() != 0)
|
|
|
|
(*cache)[params.filename] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCache::remove(InsetGraphics const & inset)
|
|
|
|
{
|
|
|
|
CacheType::iterator it = find(inset);
|
|
|
|
if (it == cache->end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CacheItemType item = it->second;
|
|
|
|
item->remove(inset);
|
|
|
|
if (item->empty()) {
|
|
|
|
cache->erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GCache::startLoading(InsetGraphics const & inset)
|
|
|
|
{
|
|
|
|
CacheType::iterator it = find(inset);
|
|
|
|
if (it == cache->end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
it->second->startLoading(inset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImagePtr const GCache::image(InsetGraphics const & inset) const
|
|
|
|
{
|
|
|
|
CacheType::const_iterator it = find(inset);
|
|
|
|
if (it == cache->end())
|
|
|
|
return ImagePtr();
|
|
|
|
|
|
|
|
return it->second->image(inset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImageStatus GCache::status(InsetGraphics const & inset) const
|
|
|
|
{
|
|
|
|
CacheType::const_iterator it = find(inset);
|
|
|
|
if (it == cache->end())
|
|
|
|
return ErrorUnknown;
|
|
|
|
|
|
|
|
return it->second->status(inset);
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
|
|
|
|
2000-07-31 16:39:50 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
void GCache::changeDisplay(bool changed_background)
|
2000-07-31 12:30:10 +00:00
|
|
|
{
|
2002-02-27 09:59:52 +00:00
|
|
|
CacheType::iterator it = cache->begin();
|
|
|
|
CacheType::iterator end = cache->end();
|
|
|
|
for(; it != end; ++it)
|
|
|
|
it->second->changeDisplay(changed_background);
|
|
|
|
}
|
2000-08-10 13:15:05 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
GCache::CacheType::iterator
|
|
|
|
GCache::find(InsetGraphics const & inset)
|
|
|
|
{
|
2002-03-14 18:01:51 +00:00
|
|
|
CacheType::iterator it = cache->begin();
|
|
|
|
CacheType::iterator end = cache->end();
|
|
|
|
for (; it != end; ++it) {
|
2002-02-27 09:59:52 +00:00
|
|
|
if (it->second->referencedBy(inset))
|
|
|
|
return it;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
return cache->end();
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
GCache::CacheType::const_iterator
|
|
|
|
GCache::find(InsetGraphics const & inset) const
|
|
|
|
{
|
2002-03-14 18:01:51 +00:00
|
|
|
CacheType::const_iterator it = cache->begin();
|
|
|
|
CacheType::const_iterator end = cache->end();
|
|
|
|
for (; it != end; ++it) {
|
2002-02-27 09:59:52 +00:00
|
|
|
if (it->second->referencedBy(inset))
|
|
|
|
return it;
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
return cache->end();
|
|
|
|
}
|
|
|
|
|
2002-04-11 17:40:44 +00:00
|
|
|
|
|
|
|
unsigned int GCache::raw_width(string const & filename) const
|
|
|
|
{
|
|
|
|
CacheType::const_iterator it = cache->find(filename);
|
|
|
|
if (it == cache->end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return it->second->raw_width();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int GCache::raw_height(string const & filename) const
|
|
|
|
{
|
|
|
|
CacheType::const_iterator it = cache->find(filename);
|
|
|
|
if (it == cache->end())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return it->second->raw_height();
|
|
|
|
}
|
|
|
|
|
2002-04-19 09:17:05 +00:00
|
|
|
|
|
|
|
std::vector<string> GCache::loadableFormats() const {
|
|
|
|
return GImage::loadableFormats();
|
|
|
|
}
|
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
} // namespace grfx
|
2002-04-11 17:40:44 +00:00
|
|
|
|