2002-09-05 11:31:30 +00:00
|
|
|
/**
|
2002-06-26 14:15:08 +00:00
|
|
|
* \file GraphicsLoader.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-06-26 14:15:08 +00:00
|
|
|
*
|
2002-12-01 22:59:25 +00:00
|
|
|
* \author Angus Leeming
|
2002-09-05 11:31:30 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-06-26 14:15:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "GraphicsLoader.h"
|
2002-08-02 16:30:58 +00:00
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
#include "GraphicsCacheItem.h"
|
|
|
|
#include "GraphicsImage.h"
|
|
|
|
#include "GraphicsParams.h"
|
2003-02-21 10:04:08 +00:00
|
|
|
#include "LoaderQueue.h"
|
2002-07-15 11:08:46 +00:00
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace lyx {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
|
|
|
using support::FileName;
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
namespace graphics {
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2005-02-15 10:17:56 +00:00
|
|
|
class Loader::Impl : public boost::signals::trackable {
|
2005-01-19 15:03:31 +00:00
|
|
|
public:
|
2002-06-26 14:15:08 +00:00
|
|
|
///
|
2003-06-05 22:45:51 +00:00
|
|
|
Impl();
|
2002-06-26 14:15:08 +00:00
|
|
|
///
|
|
|
|
~Impl();
|
|
|
|
///
|
2006-11-26 21:30:39 +00:00
|
|
|
void resetFile(FileName const &);
|
2002-06-26 14:15:08 +00:00
|
|
|
///
|
2002-06-28 11:22:56 +00:00
|
|
|
void resetParams(Params const &);
|
2002-06-26 14:15:08 +00:00
|
|
|
///
|
|
|
|
void createPixmap();
|
2002-07-15 11:08:46 +00:00
|
|
|
///
|
2003-02-26 11:41:23 +00:00
|
|
|
void startLoading();
|
2003-06-03 10:13:26 +00:00
|
|
|
///
|
|
|
|
Params const & params() const { return params_; }
|
2002-07-15 11:08:46 +00:00
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
/// The loading status of the image.
|
|
|
|
ImageStatus status_;
|
|
|
|
/** Must store a copy of the cached item to ensure that it is not
|
|
|
|
* erased unexpectedly by the cache itself.
|
|
|
|
*/
|
2002-06-28 11:22:56 +00:00
|
|
|
Cache::ItemPtr cached_item_;
|
2002-06-26 14:15:08 +00:00
|
|
|
/// We modify a local copy of the image once it is loaded.
|
2002-06-28 11:22:56 +00:00
|
|
|
Image::ImagePtr image_;
|
2002-07-17 16:56:42 +00:00
|
|
|
/// This signal is emitted when the image loading status changes.
|
2004-09-26 14:19:47 +00:00
|
|
|
boost::signal<void()> signal_;
|
2002-06-28 11:22:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
///
|
|
|
|
void statusChanged();
|
2002-07-15 11:08:46 +00:00
|
|
|
///
|
|
|
|
void checkedLoading();
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
///
|
|
|
|
Params params_;
|
2002-06-26 14:15:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
Loader::Loader()
|
2003-06-05 22:45:51 +00:00
|
|
|
: pimpl_(new Impl)
|
2002-06-26 14:15:08 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
Loader::Loader(FileName const & file, DisplayType type)
|
2003-06-05 22:45:51 +00:00
|
|
|
: pimpl_(new Impl)
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
reset(file, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
Loader::Loader(FileName const & file, Params const & params)
|
2003-06-05 22:45:51 +00:00
|
|
|
: pimpl_(new Impl)
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
reset(file, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-03 10:13:26 +00:00
|
|
|
Loader::Loader(Loader const & other)
|
2003-06-05 22:45:51 +00:00
|
|
|
: pimpl_(new Impl)
|
2003-06-04 15:14:42 +00:00
|
|
|
{
|
|
|
|
Params const & params = other.pimpl_->params();
|
|
|
|
reset(params.filename, params);
|
|
|
|
}
|
2003-06-03 10:13:26 +00:00
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
Loader::~Loader()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2003-06-05 22:45:51 +00:00
|
|
|
Loader & Loader::operator=(Loader const & other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
Params const & params = other.pimpl_->params();
|
|
|
|
reset(params.filename, params);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void Loader::reset(FileName const & file, DisplayType type) const
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
Params params;
|
|
|
|
params.display = type;
|
|
|
|
pimpl_->resetParams(params);
|
|
|
|
|
|
|
|
pimpl_->resetFile(file);
|
|
|
|
pimpl_->createPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void Loader::reset(FileName const & file, Params const & params) const
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
pimpl_->resetParams(params);
|
|
|
|
pimpl_->resetFile(file);
|
|
|
|
pimpl_->createPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void Loader::reset(Params const & params) const
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
pimpl_->resetParams(params);
|
|
|
|
pimpl_->createPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void Loader::startLoading() const
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
|
|
|
|
return;
|
2003-02-26 11:41:23 +00:00
|
|
|
pimpl_->startLoading();
|
2002-07-15 11:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
void Loader::startMonitoring() const
|
|
|
|
{
|
|
|
|
if (!pimpl_->cached_item_.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
pimpl_->cached_item_->startMonitoring();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Loader::monitoring() const
|
|
|
|
{
|
|
|
|
if (!pimpl_->cached_item_.get())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return pimpl_->cached_item_->monitoring();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long Loader::checksum() const
|
|
|
|
{
|
|
|
|
if (!pimpl_->cached_item_.get())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return pimpl_->cached_item_->checksum();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const & Loader::filename() const
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
static FileName const empty;
|
2002-07-15 11:08:46 +00:00
|
|
|
return pimpl_->cached_item_.get() ?
|
|
|
|
pimpl_->cached_item_->filename() : empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImageStatus Loader::status() const
|
|
|
|
{
|
|
|
|
return pimpl_->status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
boost::signals::connection Loader::connect(slot_type const & slot) const
|
|
|
|
{
|
|
|
|
return pimpl_->signal_.connect(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
Image const * Loader::image() const
|
|
|
|
{
|
|
|
|
return pimpl_->image_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-05 22:45:51 +00:00
|
|
|
Loader::Impl::Impl()
|
|
|
|
: status_(WaitingToLoad)
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
Loader::Impl::~Impl()
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
resetFile(FileName());
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void Loader::Impl::resetFile(FileName const & file)
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const old_file = cached_item_.get() ?
|
|
|
|
cached_item_->filename() : FileName();
|
2002-06-28 11:22:56 +00:00
|
|
|
|
|
|
|
if (file == old_file)
|
2002-06-26 14:15:08 +00:00
|
|
|
return;
|
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
// If monitoring() the current file, should continue to monitor the
|
|
|
|
// new file.
|
|
|
|
bool continue_monitoring = false;
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
if (!old_file.empty()) {
|
2002-07-18 14:01:42 +00:00
|
|
|
continue_monitoring = cached_item_->monitoring();
|
2002-06-28 11:22:56 +00:00
|
|
|
cached_item_.reset();
|
|
|
|
Cache::get().remove(old_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
|
|
|
|
image_.reset();
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
if (cached_item_.get() || file.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Cache & gc = Cache::get();
|
2002-06-26 14:15:08 +00:00
|
|
|
if (!gc.inCache(file))
|
|
|
|
gc.add(file);
|
|
|
|
|
|
|
|
// We /must/ make a local copy of this.
|
2002-06-28 11:22:56 +00:00
|
|
|
cached_item_ = gc.item(file);
|
|
|
|
status_ = cached_item_->status();
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
if (continue_monitoring && !cached_item_->monitoring())
|
|
|
|
cached_item_->startMonitoring();
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
cached_item_->connect(boost::bind(&Impl::statusChanged, this));
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
void Loader::Impl::resetParams(Params const & params)
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
if (params == params_)
|
2002-06-26 14:15:08 +00:00
|
|
|
return;
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
params_ = params;
|
|
|
|
status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
|
2002-06-26 14:15:08 +00:00
|
|
|
image_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Loader::Impl::statusChanged()
|
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
|
|
|
|
createPixmap();
|
2002-07-17 16:56:42 +00:00
|
|
|
signal_();
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Loader::Impl::createPixmap()
|
|
|
|
{
|
2002-07-18 14:01:42 +00:00
|
|
|
if (!cached_item_.get() ||
|
2002-06-26 14:15:08 +00:00
|
|
|
params_.display == NoDisplay || status_ != Loaded)
|
|
|
|
return;
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
image_.reset(cached_item_->image()->clone());
|
2002-06-26 14:15:08 +00:00
|
|
|
|
|
|
|
// These do nothing if there's nothing to do
|
|
|
|
image_->clip(params_);
|
|
|
|
image_->rotate(params_);
|
|
|
|
image_->scale(params_);
|
|
|
|
|
|
|
|
bool const success = image_->setPixmap(params_);
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
status_ = Ready;
|
|
|
|
} else {
|
|
|
|
image_.reset();
|
|
|
|
status_ = ErrorGeneratingPixmap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-26 11:41:23 +00:00
|
|
|
void Loader::Impl::startLoading()
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2003-02-21 10:04:08 +00:00
|
|
|
if (status_ != WaitingToLoad)
|
2002-07-15 11:08:46 +00:00
|
|
|
return;
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2003-02-21 10:04:08 +00:00
|
|
|
LoaderQueue::get().touch(cached_item_);
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-04 08:23:23 +00:00
|
|
|
} // namespace graphics
|
|
|
|
} // namespace lyx
|