Not so soon, I guess, since that FIXME was from r6305.
[lyx.git] / src / graphics / GraphicsLoader.cpp
blobac5d614c6e5a0d370a08d2db22b56f77421597a1
1 /**
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.
9 */
11 #include <config.h>
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 <boost/bind.hpp>
25 #include <set>
26 #include <queue>
28 using namespace std;
29 using namespace lyx::support;
31 namespace lyx {
32 namespace graphics {
35 /////////////////////////////////////////////////////////////////////
37 // LoaderQueue
39 /////////////////////////////////////////////////////////////////////
41 class LoaderQueue {
42 public:
43 /// Use this to request that the item is loaded.
44 void touch(Cache::ItemPtr const & item);
45 /// Query whether the clock is ticking.
46 bool running() const;
47 ///get the and only instance of the class
48 static LoaderQueue & get();
49 private:
50 /// This class is a singleton class... use LoaderQueue::get() instead
51 LoaderQueue();
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_;
58 ///
59 Timeout timer;
60 ///
61 bool running_;
63 /** This is the 'threaded' method, that does the loading in the
64 * background.
66 void loadNext();
67 ///
68 void startLoader();
69 ///
70 void stopLoader();
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;
83 return 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_.size() && counter--) {
93 Cache::ItemPtr ptr = cache_queue_.front();
94 cache_set_.erase(ptr);
95 cache_queue_.pop_front();
96 if (ptr->status() == WaitingToLoad)
97 ptr->startLoading();
99 if (cache_queue_.size()) {
100 startLoader();
101 } else {
102 stopLoader();
107 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
108 running_(false)
110 timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
114 void LoaderQueue::startLoader()
116 LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
117 running_ = true ;
118 timer.setTimeout(s_millisecs_);
119 timer.start();
123 void LoaderQueue::stopLoader()
125 timer.stop();
126 running_ = false ;
127 LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
131 bool LoaderQueue::running() const
133 return running_ ;
137 void LoaderQueue::touch(Cache::ItemPtr const & item)
139 if (! cache_set_.insert(item).second) {
140 list<Cache::ItemPtr>::iterator
141 it = cache_queue_.begin();
142 list<Cache::ItemPtr>::iterator
143 end = cache_queue_.end();
145 it = find(it, end, item);
146 if (it != end)
147 cache_queue_.erase(it);
149 cache_queue_.push_front(item);
150 if (!running_)
151 startLoader();
156 /////////////////////////////////////////////////////////////////////
158 // GraphicsLoader
160 /////////////////////////////////////////////////////////////////////
162 typedef boost::shared_ptr<Image> ImagePtr;
164 class Loader::Impl : public boost::signals::trackable {
165 public:
167 Impl();
169 ~Impl();
171 void resetFile(FileName const &);
173 void resetParams(Params const &);
175 void createPixmap();
177 void startLoading();
179 Params const & params() const { return params_; }
181 /// The loading status of the image.
182 ImageStatus status_;
183 /** Must store a copy of the cached item to ensure that it is not
184 * erased unexpectedly by the cache itself.
186 Cache::ItemPtr cached_item_;
187 /// We modify a local copy of the image once it is loaded.
188 ImagePtr image_;
189 /// This signal is emitted when the image loading status changes.
190 boost::signal<void()> signal_;
191 /// The connection of the signal StatusChanged
192 boost::signals::connection sc_;
194 private:
196 void statusChanged();
198 void checkedLoading();
201 Params params_;
205 Loader::Loader()
206 : pimpl_(new Impl)
210 Loader::Loader(FileName const & file, bool display)
211 : pimpl_(new Impl)
213 reset(file, display);
217 Loader::Loader(FileName const & file, Params const & params)
218 : pimpl_(new Impl)
220 reset(file, params);
224 Loader::Loader(Loader const & other)
225 : pimpl_(new Impl)
227 Params const & params = other.pimpl_->params();
228 reset(params.filename, params);
232 Loader::~Loader()
234 delete pimpl_;
238 Loader & Loader::operator=(Loader const & other)
240 if (this != &other) {
241 Params const & params = other.pimpl_->params();
242 reset(params.filename, params);
244 return *this;
248 void Loader::reset(FileName const & file, bool display) const
250 Params params;
251 params.display = display;
252 pimpl_->resetParams(params);
254 pimpl_->resetFile(file);
255 pimpl_->createPixmap();
259 void Loader::reset(FileName const & file, Params const & params) const
261 pimpl_->resetParams(params);
262 pimpl_->resetFile(file);
263 pimpl_->createPixmap();
267 void Loader::reset(Params const & params) const
269 pimpl_->resetParams(params);
270 pimpl_->createPixmap();
274 void Loader::startLoading() const
276 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
277 return;
278 pimpl_->startLoading();
282 void Loader::reload() const
284 pimpl_->cached_item_->startLoading();
288 void Loader::startMonitoring() const
290 if (!pimpl_->cached_item_.get())
291 return;
293 pimpl_->cached_item_->startMonitoring();
297 bool Loader::monitoring() const
299 if (!pimpl_->cached_item_.get())
300 return false;
302 return pimpl_->cached_item_->monitoring();
306 unsigned long Loader::checksum() const
308 if (!pimpl_->cached_item_.get())
309 return 0;
311 return pimpl_->cached_item_->checksum();
315 FileName const & Loader::filename() const
317 static FileName const empty;
318 return pimpl_->cached_item_.get() ?
319 pimpl_->cached_item_->filename() : empty;
323 ImageStatus Loader::status() const
325 return pimpl_->status_;
329 boost::signals::connection Loader::connect(slot_type const & slot) const
331 return pimpl_->signal_.connect(slot);
335 Image const * Loader::image() const
337 return pimpl_->image_.get();
341 Loader::Impl::Impl()
342 : status_(WaitingToLoad)
347 Loader::Impl::~Impl()
349 resetFile(FileName());
353 void Loader::Impl::resetFile(FileName const & file)
355 FileName const old_file = cached_item_.get() ?
356 cached_item_->filename() : FileName();
358 if (file == old_file)
359 return;
361 // If monitoring() the current file, should continue to monitor the
362 // new file.
363 bool continue_monitoring = false;
365 if (!old_file.empty()) {
366 continue_monitoring = cached_item_->monitoring();
367 // cached_item_ is going to be reset, so the connected
368 // signal needs to be disconnected.
369 sc_.disconnect();
370 cached_item_.reset();
371 Cache::get().remove(old_file);
374 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
375 image_.reset();
377 if (cached_item_.get() || file.empty())
378 return;
380 Cache & gc = Cache::get();
381 if (!gc.inCache(file))
382 gc.add(file);
384 // We /must/ make a local copy of this.
385 cached_item_ = gc.item(file);
386 status_ = cached_item_->status();
388 if (continue_monitoring && !cached_item_->monitoring())
389 cached_item_->startMonitoring();
391 sc_ = cached_item_->connect(boost::bind(&Impl::statusChanged, this));
395 void Loader::Impl::resetParams(Params const & params)
397 if (params == params_)
398 return;
400 params_ = params;
401 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
402 image_.reset();
406 void Loader::Impl::statusChanged()
408 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
409 createPixmap();
410 signal_();
414 void Loader::Impl::createPixmap()
416 if (!params_.display || status_ != Loaded)
417 return;
419 if (!cached_item_.get()) {
420 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
421 return;
424 if (!cached_item_->image()) {
425 // There must have been a problem reading the file.
426 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
427 return;
430 image_.reset(cached_item_->image()->clone());
432 bool const success = image_->setPixmap(params_);
434 if (success) {
435 status_ = Ready;
436 } else {
437 image_.reset();
438 status_ = ErrorGeneratingPixmap;
442 void Loader::Impl::startLoading()
444 if (status_ != WaitingToLoad)
445 return;
447 if (cached_item_->tryDisplayFormat()) {
448 status_ = Loaded;
449 createPixmap();
450 return;
453 LoaderQueue::get().touch(cached_item_);
457 } // namespace graphics
458 } // namespace lyx