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-09-05 11:31:30 +00:00
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
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>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "GraphicsLoader.h"
|
2002-08-02 16:30:58 +00:00
|
|
|
|
|
|
|
#include "BufferView.h"
|
|
|
|
|
2002-06-26 14:15:08 +00:00
|
|
|
#include "GraphicsCache.h"
|
|
|
|
#include "GraphicsCacheItem.h"
|
|
|
|
#include "GraphicsImage.h"
|
|
|
|
#include "GraphicsParams.h"
|
2002-07-15 11:08:46 +00:00
|
|
|
#include "GraphicsSupport.h"
|
|
|
|
|
2002-08-02 16:30:58 +00:00
|
|
|
#include "frontends/LyXView.h"
|
2002-07-15 11:08:46 +00:00
|
|
|
#include "frontends/Timeout.h"
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-08-02 16:30:58 +00:00
|
|
|
#include <boost/weak_ptr.hpp>
|
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
|
|
|
///
|
|
|
|
void startLoading(Inset const &, BufferView const &);
|
|
|
|
|
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
|
|
|
|
|
|
|
///
|
|
|
|
Timeout timer;
|
|
|
|
// Multiple Insets can share the same image
|
|
|
|
typedef std::list<Inset const *> InsetList;
|
|
|
|
///
|
|
|
|
InsetList insets;
|
|
|
|
///
|
2002-08-02 16:30:58 +00:00
|
|
|
boost::weak_ptr<BufferView const> view;
|
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)
|
2002-07-17 16:56:42 +00:00
|
|
|
: pimpl_(new Impl(params))
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
reset(file, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
pimpl_->cached_item_->startLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-17 16:56:42 +00:00
|
|
|
void Loader::startLoading(Inset const & inset, BufferView const & bv) const
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
|
|
|
|
return;
|
|
|
|
pimpl_->startLoading(inset, bv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
: status_(WaitingToLoad), params_(params),
|
2002-08-02 16:30:58 +00:00
|
|
|
timer(2000, Timeout::ONETIME)
|
2002-07-15 11:08:46 +00:00
|
|
|
{
|
|
|
|
timer.timeout.connect(boost::bind(&Impl::checkedLoading, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
void Loader::Impl::startLoading(Inset const & inset, BufferView const & bv)
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2002-07-15 11:08:46 +00:00
|
|
|
if (status_ != WaitingToLoad || timer.running())
|
|
|
|
return;
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
InsetList::const_iterator it = insets.begin();
|
|
|
|
InsetList::const_iterator end = insets.end();
|
|
|
|
it = std::find(it, end, &inset);
|
|
|
|
if (it == end)
|
|
|
|
insets.push_back(&inset);
|
2002-08-02 16:30:58 +00:00
|
|
|
view = bv.owner()->view();
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
timer.start();
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
namespace {
|
2002-06-26 16:57:59 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
struct FindVisibleInset {
|
2002-06-26 16:57:59 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
FindVisibleInset(std::list<VisibleParagraph> const & vps) : vps_(vps) {}
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
bool operator()(Inset const * inset_ptr)
|
|
|
|
{
|
|
|
|
if (!inset_ptr)
|
|
|
|
return false;
|
|
|
|
return isInsetVisible(*inset_ptr, vps_);
|
|
|
|
}
|
2002-07-17 16:56:42 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
private:
|
|
|
|
std::list<VisibleParagraph> const & vps_;
|
|
|
|
};
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
} // namespace anon
|
2002-06-26 14:15:08 +00:00
|
|
|
|
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
void Loader::Impl::checkedLoading()
|
2002-06-26 14:15:08 +00:00
|
|
|
{
|
2002-08-02 16:30:58 +00:00
|
|
|
if (insets.empty() || !view.get())
|
2002-06-26 14:15:08 +00:00
|
|
|
return;
|
|
|
|
|
2002-08-02 16:30:58 +00:00
|
|
|
std::list<VisibleParagraph> const vps =
|
|
|
|
getVisibleParagraphs(*view.get());
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
InsetList::const_iterator it = insets.begin();
|
|
|
|
InsetList::const_iterator end = insets.end();
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
it = std::find_if(it, end, FindVisibleInset(vps));
|
2002-06-26 14:15:08 +00:00
|
|
|
|
2002-07-15 11:08:46 +00:00
|
|
|
// One of the insets is visible, so start loading the image.
|
|
|
|
if (it != end)
|
|
|
|
cached_item_->startLoading();
|
2002-06-26 14:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace grfx
|