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
|
|
|
*
|
2002-09-05 14:10:50 +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 "GraphicsCache.h"
|
|
|
|
#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-08-02 16:30:58 +00:00
|
|
|
#include "frontends/LyXView.h"
|
2002-06-26 14:15:08 +00:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
2002-06-28 11:22:56 +00:00
|
|
|
#include <boost/signals/trackable.hpp>
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
#include <list>
|
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
namespace grfx {
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
struct Loader::Impl : boost::signals::trackable {
|
2002-06-26 14:15:08 +00:00
|
|
|
///
|
2002-07-17 16:56:42 +00:00
|
|
|
Impl(Params const &);
|
2002-06-26 14:15:08 +00:00
|
|
|
///
|
|
|
|
~Impl();
|
|
|
|
///
|
2002-06-28 11:22:56 +00:00
|
|
|
void resetFile(string 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.
|
|
|
|
boost::signal0<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-07-15 11:08:46 +00:00
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
Loader::Loader()
|
2002-07-17 16:56:42 +00:00
|
|
|
: pimpl_(new Impl(Params()))
|
2002-06-26 14:15:08 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
Loader::Loader(string const & file, DisplayType type)
|
2002-07-17 16:56:42 +00:00
|
|
|
: pimpl_(new Impl(Params()))
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
reset(file, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Loader::Loader(string const & file, Params const & params)
|
2003-06-04 15:14:42 +00:00
|
|
|
: pimpl_(new Impl(Params()))
|
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-04 15:14:42 +00:00
|
|
|
: pimpl_(new Impl(Params()))
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void Loader::reset(string 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void Loader::reset(string 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
string const & Loader::filename() const
|
|
|
|
{
|
|
|
|
static string const empty;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
Loader::Impl::Impl(Params const & params)
|
2003-02-21 10:04:08 +00:00
|
|
|
: status_(WaitingToLoad), params_(params)
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
Loader::Impl::~Impl()
|
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
resetFile(string());
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
void Loader::Impl::resetFile(string const & file)
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
string const old_file = cached_item_.get() ?
|
|
|
|
cached_item_->filename() : string();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace grfx
|