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"
|
2002-06-25 15:59:10 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#include "support/filetools.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-06-25 15:59:10 +00:00
|
|
|
std::vector<string> GCache::loadableFormats() const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
return GImage::loadableFormats();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
void GCache::add(string const & file)
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
if (!AbsolutePath(file)) {
|
|
|
|
lyxerr << "GCacheItem::add(" << file << "):\n"
|
|
|
|
<< "The file must be have an absolute path."
|
|
|
|
<< std::endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-06-25 15:59:10 +00:00
|
|
|
|
|
|
|
// Is the file in the cache already?
|
|
|
|
if (inCache(file)) {
|
|
|
|
lyxerr[Debug::GRAPHICS] << "GCache::add(" << file << "):\n"
|
|
|
|
<< "The file is already in the cache."
|
|
|
|
<< std::endl;
|
2002-02-27 09:59:52 +00:00
|
|
|
return;
|
2002-06-25 15:59:10 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
|
|
|
|
(*cache)[file] = GraphicPtr(new GCacheItem(file));
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
void GCache::remove(string const & file)
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
CacheType::iterator it = cache->find(file);
|
2002-02-27 09:59:52 +00:00
|
|
|
if (it == cache->end())
|
2002-06-25 15:59:10 +00:00
|
|
|
return;
|
2002-02-27 09:59:52 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
GraphicPtr item = it->second;
|
|
|
|
|
|
|
|
if (item.use_count() == 1) {
|
|
|
|
// The graphics file is in the cache, but nothing else
|
|
|
|
// references it.
|
|
|
|
cache->erase(it);
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
2000-07-31 12:30:10 +00:00
|
|
|
}
|
2002-02-27 09:59:52 +00:00
|
|
|
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
bool GCache::inCache(string const & file) const
|
2002-02-27 09:59:52 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
return cache->find(file) != cache->end();
|
2002-02-27 09:59:52 +00:00
|
|
|
}
|
|
|
|
|
2002-04-11 17:40:44 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
GraphicPtr const GCache::graphic(string const & file) const
|
2002-04-11 17:40:44 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
CacheType::const_iterator it = cache->find(file);
|
2002-04-11 17:40:44 +00:00
|
|
|
if (it == cache->end())
|
2002-06-25 15:59:10 +00:00
|
|
|
return GraphicPtr();
|
2002-04-11 17:40:44 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
return it->second;
|
2002-04-11 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
ImagePtr const GCache::image(string const & file) const
|
2002-04-11 17:40:44 +00:00
|
|
|
{
|
2002-06-25 15:59:10 +00:00
|
|
|
CacheType::const_iterator it = cache->find(file);
|
2002-04-11 17:40:44 +00:00
|
|
|
if (it == cache->end())
|
2002-06-25 15:59:10 +00:00
|
|
|
return ImagePtr();
|
2002-04-11 17:40:44 +00:00
|
|
|
|
2002-06-25 15:59:10 +00:00
|
|
|
return it->second->image();
|
2002-04-11 17:40:44 +00:00
|
|
|
}
|
|
|
|
|
2002-04-19 09:17:05 +00:00
|
|
|
|
2002-02-27 09:59:52 +00:00
|
|
|
} // namespace grfx
|