2 * \file GraphicsLoader.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming <leeming@lyx.org>
12 #pragma implementation
15 #include "GraphicsLoader.h"
16 #include "GraphicsCache.h"
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19 #include "GraphicsParams.h"
20 #include "GraphicsSupport.h"
22 #include "frontends/Timeout.h"
24 #include <boost/bind.hpp>
25 #include <boost/signals/trackable.hpp>
31 struct Loader::Impl : boost::signals::trackable {
37 void resetFile(string const &);
39 void resetParams(Params const &);
44 void startLoading(Inset const &, BufferView const &);
46 /// The loading status of the image.
48 /** Must store a copy of the cached item to ensure that it is not
49 * erased unexpectedly by the cache itself.
51 Cache::ItemPtr cached_item_;
52 /// We modify a local copy of the image once it is loaded.
53 Image::ImagePtr image_;
54 /// This signal is emitted when the image loading status changes.
55 boost::signal0<void> signal_;
61 void checkedLoading();
68 // Multiple Insets can share the same image
69 typedef std::list<Inset const *> InsetList;
73 BufferView const * view;
78 : pimpl_(new Impl(Params()))
82 Loader::Loader(string const & file, DisplayType type)
83 : pimpl_(new Impl(Params()))
89 Loader::Loader(string const & file, Params const & params)
90 : pimpl_(new Impl(params))
100 void Loader::reset(string const & file, DisplayType type) const
103 params.display = type;
104 pimpl_->resetParams(params);
106 pimpl_->resetFile(file);
107 pimpl_->createPixmap();
111 void Loader::reset(string const & file, Params const & params) const
113 pimpl_->resetParams(params);
114 pimpl_->resetFile(file);
115 pimpl_->createPixmap();
119 void Loader::reset(Params const & params) const
121 pimpl_->resetParams(params);
122 pimpl_->createPixmap();
126 void Loader::startLoading() const
128 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
130 pimpl_->cached_item_->startLoading();
134 void Loader::startLoading(Inset const & inset, BufferView const & bv) const
136 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
138 pimpl_->startLoading(inset, bv);
142 void Loader::startMonitoring() const
144 if (!pimpl_->cached_item_.get())
147 pimpl_->cached_item_->startMonitoring();
151 bool Loader::monitoring() const
153 if (!pimpl_->cached_item_.get())
156 return pimpl_->cached_item_->monitoring();
160 unsigned long Loader::checksum() const
162 if (!pimpl_->cached_item_.get())
165 return pimpl_->cached_item_->checksum();
169 string const & Loader::filename() const
171 static string const empty;
172 return pimpl_->cached_item_.get() ?
173 pimpl_->cached_item_->filename() : empty;
177 ImageStatus Loader::status() const
179 return pimpl_->status_;
183 boost::signals::connection Loader::connect(slot_type const & slot) const
185 return pimpl_->signal_.connect(slot);
189 Image const * Loader::image() const
191 return pimpl_->image_.get();
195 Loader::Impl::Impl(Params const & params)
196 : status_(WaitingToLoad), params_(params),
197 timer(2000, Timeout::ONETIME), view(0)
199 timer.timeout.connect(boost::bind(&Impl::checkedLoading, this));
203 Loader::Impl::~Impl()
209 void Loader::Impl::resetFile(string const & file)
211 string const old_file = cached_item_.get() ?
212 cached_item_->filename() : string();
214 if (file == old_file)
217 // If monitoring() the current file, should continue to monitor the
219 bool continue_monitoring = false;
221 if (!old_file.empty()) {
222 continue_monitoring = cached_item_->monitoring();
223 cached_item_.reset();
224 Cache::get().remove(old_file);
227 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
230 if (cached_item_.get() || file.empty())
233 Cache & gc = Cache::get();
234 if (!gc.inCache(file))
237 // We /must/ make a local copy of this.
238 cached_item_ = gc.item(file);
239 status_ = cached_item_->status();
241 if (continue_monitoring && !cached_item_->monitoring())
242 cached_item_->startMonitoring();
244 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
248 void Loader::Impl::resetParams(Params const & params)
250 if (params == params_)
254 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
259 void Loader::Impl::statusChanged()
261 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
267 void Loader::Impl::createPixmap()
269 if (!cached_item_.get() ||
270 params_.display == NoDisplay || status_ != Loaded)
273 image_.reset(cached_item_->image()->clone());
275 // These do nothing if there's nothing to do
276 image_->clip(params_);
277 image_->rotate(params_);
278 image_->scale(params_);
280 bool const success = image_->setPixmap(params_);
286 status_ = ErrorGeneratingPixmap;
291 void Loader::Impl::startLoading(Inset const & inset, BufferView const & bv)
293 if (status_ != WaitingToLoad || timer.running())
296 InsetList::const_iterator it = insets.begin();
297 InsetList::const_iterator end = insets.end();
298 it = std::find(it, end, &inset);
300 insets.push_back(&inset);
309 struct FindVisibleInset {
311 FindVisibleInset(std::list<VisibleParagraph> const & vps) : vps_(vps) {}
313 bool operator()(Inset const * inset_ptr)
317 return isInsetVisible(*inset_ptr, vps_);
321 std::list<VisibleParagraph> const & vps_;
327 void Loader::Impl::checkedLoading()
329 if (insets.empty() || !view)
332 std::list<VisibleParagraph> const vps = getVisibleParagraphs(*view);
334 InsetList::const_iterator it = insets.begin();
335 InsetList::const_iterator end = insets.end();
337 it = std::find_if(it, end, FindVisibleInset(vps));
339 // One of the insets is visible, so start loading the image.
341 cached_item_->startLoading();