2002-06-26 14:15:08 +00:00
|
|
|
/*
|
|
|
|
* \file GraphicsLoader.C
|
|
|
|
* Copyright 2002 the LyX Team
|
|
|
|
* Read the file COPYING
|
|
|
|
*
|
|
|
|
* \author Angus Leeming <leeming@lyx.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "GraphicsLoader.h"
|
|
|
|
#include "GraphicsCache.h"
|
|
|
|
#include "GraphicsCacheItem.h"
|
|
|
|
#include "GraphicsImage.h"
|
|
|
|
#include "GraphicsParams.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
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-06-28 11:22:56 +00:00
|
|
|
Impl(Loader &, 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();
|
|
|
|
|
|
|
|
/// 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_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
///
|
|
|
|
void statusChanged();
|
|
|
|
|
|
|
|
///
|
|
|
|
Params params_;
|
|
|
|
///
|
|
|
|
Loader & parent_;
|
2002-06-26 14:15:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Loader::Impl::Impl(Loader & parent, Params const & params)
|
|
|
|
: status_(WaitingToLoad), params_(params), parent_(parent)
|
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-06-28 11:22:56 +00:00
|
|
|
if (!old_file.empty()) {
|
|
|
|
cached_item_.reset();
|
|
|
|
Cache::get().remove(old_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
|
|
|
|
image_.reset();
|
|
|
|
|
|
|
|
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-06-28 11:22:56 +00:00
|
|
|
cached_item_->statusChanged.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-06-26 14:15:08 +00:00
|
|
|
parent_.statusChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Loader::Impl::createPixmap()
|
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
if (!cached_item_.get() || image_.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Loader::Loader()
|
2002-06-28 11:22:56 +00:00
|
|
|
: pimpl_(new Impl(*this, Params()))
|
2002-06-26 14:15:08 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
Loader::Loader(string const & file, DisplayType type)
|
2002-06-28 11:22:56 +00:00
|
|
|
: pimpl_(new Impl(*this, Params()))
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
reset(file, type);
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Loader::Loader(string const & file, Params const & params)
|
2002-06-26 14:15:08 +00:00
|
|
|
: pimpl_(new Impl(*this, params))
|
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
reset(file, params);
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-26 16:57:59 +00:00
|
|
|
Loader::~Loader()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
void Loader::reset(string const & file, DisplayType type)
|
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
Params params;
|
|
|
|
params.display = type;
|
|
|
|
pimpl_->resetParams(params);
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->resetFile(file);
|
|
|
|
pimpl_->createPixmap();
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
void Loader::reset(string const & file, Params const & params)
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->resetParams(params);
|
|
|
|
pimpl_->resetFile(file);
|
|
|
|
pimpl_->createPixmap();
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
void Loader::reset(Params const & params)
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->resetParams(params);
|
|
|
|
pimpl_->createPixmap();
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Loader::startLoading()
|
|
|
|
{
|
2002-06-28 11:22:56 +00:00
|
|
|
if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
|
2002-06-26 14:15:08 +00:00
|
|
|
return;
|
2002-06-28 11:22:56 +00:00
|
|
|
pimpl_->cached_item_->startLoading();
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const & Loader::filename() const
|
|
|
|
{
|
|
|
|
static string const empty;
|
2002-06-28 11:22:56 +00:00
|
|
|
return pimpl_->cached_item_.get() ?
|
|
|
|
pimpl_->cached_item_->filename() : empty;
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ImageStatus Loader::status() const
|
|
|
|
{
|
|
|
|
return pimpl_->status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-28 11:22:56 +00:00
|
|
|
Image const * Loader::image() const
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
|
|
|
return pimpl_->image_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace grfx
|