2 * \file GraphicsLoader.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "GraphicsLoader.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsParams.h"
18 #include "GraphicsCache.h"
20 #include "support/debug.h"
21 #include "support/Timeout.h"
23 #include "support/bind.h"
29 using namespace lyx::support;
35 /////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////
43 /// Use this to request that the item is loaded.
44 void touch(Cache::ItemPtr const & item);
45 /// Query whether the clock is ticking.
47 ///get the and only instance of the class
48 static LoaderQueue & get();
50 /// This class is a singleton class... use LoaderQueue::get() instead
52 /// The in-progress loading queue (elements are unique here).
53 list<Cache::ItemPtr> cache_queue_;
54 /// Used to make the insertion of new elements faster.
55 set<Cache::ItemPtr> cache_set_;
56 /// Newly touched elements go here. loadNext moves them to cache_queue_
57 queue<Cache::ItemPtr> bucket_;
63 /** This is the 'threaded' method, that does the loading in the
74 //static int s_numimages_ = 5;
75 //static int s_millisecs_ = 500;
77 static int s_numimages_ = 10;
78 static int s_millisecs_ = 500;
80 LoaderQueue & LoaderQueue::get()
82 static LoaderQueue singleton;
87 void LoaderQueue::loadNext()
89 LYXERR(Debug::GRAPHICS, "LoaderQueue: "
90 << cache_queue_.size() << " items in the queue");
91 int counter = s_numimages_;
92 while (!cache_queue_.empty() && counter--) {
93 Cache::ItemPtr ptr = cache_queue_.front();
94 cache_set_.erase(ptr);
95 cache_queue_.pop_front();
96 if (ptr->status() == WaitingToLoad)
99 if (!cache_queue_.empty())
106 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
109 timer.timeout.connect(bind(&LoaderQueue::loadNext, this));
113 void LoaderQueue::startLoader()
115 LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
117 timer.setTimeout(s_millisecs_);
122 void LoaderQueue::stopLoader()
126 LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
130 bool LoaderQueue::running() const
136 void LoaderQueue::touch(Cache::ItemPtr const & item)
138 if (! cache_set_.insert(item).second) {
139 list<Cache::ItemPtr>::iterator
140 it = cache_queue_.begin();
141 list<Cache::ItemPtr>::iterator
142 end = cache_queue_.end();
144 it = find(it, end, item);
146 cache_queue_.erase(it);
148 cache_queue_.push_front(item);
155 /////////////////////////////////////////////////////////////////////
159 /////////////////////////////////////////////////////////////////////
161 typedef shared_ptr<Image> ImagePtr;
163 class Loader::Impl : public boost::signals::trackable {
170 void resetFile(FileName const &);
172 void resetParams(Params const &);
178 Params const & params() const { return params_; }
180 /// The loading status of the image.
182 /** Must store a copy of the cached item to ensure that it is not
183 * erased unexpectedly by the cache itself.
185 Cache::ItemPtr cached_item_;
186 /// We modify a local copy of the image once it is loaded.
188 /// This signal is emitted when the image loading status changes.
189 boost::signal<void()> signal_;
190 /// The connection of the signal StatusChanged
191 boost::signals::connection sc_;
195 void statusChanged();
197 void checkedLoading();
209 Loader::Loader(FileName const & file, bool display)
212 reset(file, display);
216 Loader::Loader(FileName const & file, Params const & params)
223 Loader::Loader(Loader const & other)
226 Params const & params = other.pimpl_->params();
227 reset(params.filename, params);
237 Loader & Loader::operator=(Loader const & other)
239 if (this != &other) {
240 Params const & params = other.pimpl_->params();
241 reset(params.filename, params);
247 void Loader::reset(FileName const & file, bool display) const
250 params.display = display;
251 pimpl_->resetParams(params);
253 pimpl_->resetFile(file);
254 pimpl_->createPixmap();
258 void Loader::reset(FileName const & file, Params const & params) const
260 pimpl_->resetParams(params);
261 pimpl_->resetFile(file);
262 pimpl_->createPixmap();
266 void Loader::reset(Params const & params) const
268 pimpl_->resetParams(params);
269 pimpl_->createPixmap();
273 void Loader::startLoading() const
275 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_)
277 pimpl_->startLoading();
281 void Loader::reload() const
283 pimpl_->cached_item_->startLoading();
287 void Loader::startMonitoring() const
289 if (!pimpl_->cached_item_)
292 pimpl_->cached_item_->startMonitoring();
296 bool Loader::monitoring() const
298 if (!pimpl_->cached_item_)
301 return pimpl_->cached_item_->monitoring();
305 unsigned long Loader::checksum() const
307 if (!pimpl_->cached_item_)
310 return pimpl_->cached_item_->checksum();
314 FileName const & Loader::filename() const
316 static FileName const empty;
317 return pimpl_->cached_item_ ?
318 pimpl_->cached_item_->filename() : empty;
322 ImageStatus Loader::status() const
324 return pimpl_->status_;
328 boost::signals::connection Loader::connect(slot_type const & slot) const
330 return pimpl_->signal_.connect(slot);
334 Image const * Loader::image() const
336 return pimpl_->image_.get();
341 : status_(WaitingToLoad)
346 Loader::Impl::~Impl()
348 resetFile(FileName());
352 void Loader::Impl::resetFile(FileName const & file)
354 FileName const old_file = cached_item_ ?
355 cached_item_->filename() : FileName();
357 if (file == old_file)
360 // If monitoring() the current file, should continue to monitor the
362 bool continue_monitoring = false;
364 if (!old_file.empty()) {
365 continue_monitoring = cached_item_->monitoring();
366 // cached_item_ is going to be reset, so the connected
367 // signal needs to be disconnected.
369 cached_item_.reset();
370 if (status_ != Converting) {
371 Cache::get().remove(old_file);
373 //TODO remove cache item when it is not busy any more, see #7163
377 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
380 if (cached_item_ || file.empty())
383 Cache & gc = Cache::get();
384 if (!gc.inCache(file))
387 // We /must/ make a local copy of this.
388 cached_item_ = gc.item(file);
389 status_ = cached_item_->status();
391 if (continue_monitoring && !cached_item_->monitoring())
392 cached_item_->startMonitoring();
394 sc_ = cached_item_->connect(bind(&Impl::statusChanged, this));
398 void Loader::Impl::resetParams(Params const & params)
400 if (params == params_)
404 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
409 void Loader::Impl::statusChanged()
411 status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
417 void Loader::Impl::createPixmap()
419 if (!params_.display || status_ != Loaded)
423 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
427 if (!cached_item_->image()) {
428 // There must have been a problem reading the file.
429 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
433 image_.reset(cached_item_->image()->clone());
435 bool const success = image_->setPixmap(params_);
441 status_ = ErrorGeneratingPixmap;
445 void Loader::Impl::startLoading()
447 if (status_ != WaitingToLoad)
450 if (cached_item_->tryDisplayFormat()) {
456 LoaderQueue::get().touch(cached_item_);
460 } // namespace graphics