]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsLoader.cpp
Check path of Qt tools if qtchooser is detected
[lyx.git] / src / graphics / GraphicsLoader.cpp
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.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GraphicsLoader.h"
14
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsParams.h"
18 #include "GraphicsCache.h"
19
20 #include "support/debug.h"
21 #include "support/Timeout.h"
22
23 #include "support/bind.h"
24
25 #include <queue>
26 #include <memory>
27 #include <set>
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33 namespace graphics {
34
35
36 /////////////////////////////////////////////////////////////////////
37 //
38 // LoaderQueue
39 //
40 /////////////////////////////////////////////////////////////////////
41
42 class LoaderQueue {
43 public:
44         /// Use this to request that the item is loaded.
45         void touch(Cache::ItemPtr const & item);
46         /// Query whether the clock is ticking.
47         bool running() const;
48         ///get the and only instance of the class
49         static LoaderQueue & get();
50 private:
51         /// This class is a singleton class... use LoaderQueue::get() instead
52         LoaderQueue();
53         /// The in-progress loading queue (elements are unique here).
54         list<Cache::ItemPtr> cache_queue_;
55         /// Used to make the insertion of new elements faster.
56         set<Cache::ItemPtr> cache_set_;
57         /// Newly touched elements go here. loadNext moves them to cache_queue_
58         queue<Cache::ItemPtr> bucket_;
59         ///
60         Timeout timer;
61         ///
62         bool running_;
63
64         /** This is the 'threaded' method, that does the loading in the
65          *  background.
66          */
67         void loadNext();
68         ///
69         void startLoader();
70         ///
71         void stopLoader();
72 };
73
74
75
76 //static int const s_numimages_ = 5;
77 static int const s_numimages_ = 10;
78 static int const s_millisecs_ = 500;
79
80
81 // FIXME THREAD
82 LoaderQueue & LoaderQueue::get()
83 {
84         static LoaderQueue singleton;
85         return singleton;
86 }
87
88
89 void LoaderQueue::loadNext()
90 {
91         LYXERR(Debug::GRAPHICS, "LoaderQueue: "
92                 << cache_queue_.size() << " items in the queue");
93         int counter = s_numimages_;
94         while (!cache_queue_.empty() && counter--) {
95                 Cache::ItemPtr ptr = cache_queue_.front();
96                 cache_set_.erase(ptr);
97                 cache_queue_.pop_front();
98                 if (ptr->status() == WaitingToLoad)
99                         ptr->startLoading();
100         }
101         if (!cache_queue_.empty())
102                 startLoader();
103         else
104                 stopLoader();
105 }
106
107
108 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
109                              running_(false)
110 {
111         timer.timeout.connect(bind(&LoaderQueue::loadNext, this));
112 }
113
114
115 void LoaderQueue::startLoader()
116 {
117         LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
118         running_ = true ;
119         timer.setTimeout(s_millisecs_);
120         timer.start();
121 }
122
123
124 void LoaderQueue::stopLoader()
125 {
126         timer.stop();
127         running_ = false ;
128         LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
129 }
130
131
132 bool LoaderQueue::running() const
133 {
134         return running_ ;
135 }
136
137
138 void LoaderQueue::touch(Cache::ItemPtr const & item)
139 {
140         if (! cache_set_.insert(item).second) {
141                 list<Cache::ItemPtr>::iterator
142                         it = cache_queue_.begin();
143                 list<Cache::ItemPtr>::iterator
144                         end = cache_queue_.end();
145
146                 it = find(it, end, item);
147                 if (it != end)
148                         cache_queue_.erase(it);
149         }
150         cache_queue_.push_front(item);
151         if (!running_)
152                 startLoader();
153 }
154
155
156
157 /////////////////////////////////////////////////////////////////////
158 //
159 // GraphicsLoader
160 //
161 /////////////////////////////////////////////////////////////////////
162
163 typedef std::shared_ptr<Image> ImagePtr;
164
165 class Loader::Impl : public boost::signals2::trackable {
166 public:
167         ///
168         Impl();
169         ///
170         ~Impl();
171         ///
172         void resetFile(FileName const &);
173         ///
174         void resetParams(Params const &);
175         ///
176         void createPixmap();
177         ///
178         void startLoading();
179         ///
180         Params const & params() const { return params_; }
181
182         /// The loading status of the image.
183         ImageStatus status_;
184         /** Must store a copy of the cached item to ensure that it is not
185          *  erased unexpectedly by the cache itself.
186          */
187         Cache::ItemPtr cached_item_;
188         /// We modify a local copy of the image once it is loaded.
189         ImagePtr image_;
190         /// This signal is emitted when the image loading status changes.
191         boost::signals2::signal<void()> signal_;
192         /// The connection of the signal StatusChanged  
193         boost::signals2::connection sc_;
194
195         double displayPixelRatio() const
196         {
197                 return params_.pixel_ratio;
198         }
199         void setDisplayPixelRatio(double scale)
200         {
201                 params_.pixel_ratio = scale;
202         }
203
204 private:
205         ///
206         void statusChanged();
207         ///
208         void checkedLoading();
209
210         ///
211         Params params_;
212 };
213
214
215 Loader::Loader()
216         : pimpl_(new Impl)
217 {}
218
219
220 Loader::Loader(FileName const & file, bool display)
221         : pimpl_(new Impl)
222 {
223         reset(file, display);
224 }
225
226
227 Loader::Loader(FileName const & file, Params const & params)
228         : pimpl_(new Impl)
229 {
230         reset(file, params);
231 }
232
233
234 Loader::Loader(Loader const & other)
235         : pimpl_(new Impl)
236 {
237         Params const & params = other.pimpl_->params();
238         reset(params.filename, params);
239 }
240
241
242 Loader::~Loader()
243 {
244         delete pimpl_;
245 }
246
247
248 Loader & Loader::operator=(Loader const & other)
249 {
250         if (this != &other) {
251                 Params const & params = other.pimpl_->params();
252                 reset(params.filename, params);
253         }
254         return *this;
255 }
256
257
258 void Loader::reset(FileName const & file, bool display) const
259 {
260         Params params;
261         params.display = display;
262         pimpl_->resetParams(params);
263
264         pimpl_->resetFile(file);
265         pimpl_->createPixmap();
266 }
267
268
269 void Loader::reset(FileName const & file, Params const & params) const
270 {
271         pimpl_->resetParams(params);
272         pimpl_->resetFile(file);
273         pimpl_->createPixmap();
274 }
275
276
277 void Loader::reset(Params const & params) const
278 {
279         pimpl_->resetParams(params);
280         pimpl_->createPixmap();
281 }
282
283
284 void Loader::startLoading() const
285 {
286         if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_)
287                 return;
288         pimpl_->startLoading();
289 }
290
291
292 void Loader::reload() const 
293 {
294         pimpl_->cached_item_->startLoading();
295 }
296
297
298 void Loader::startMonitoring() const
299 {
300         if (!pimpl_->cached_item_)
301                 return;
302
303         pimpl_->cached_item_->startMonitoring();
304 }
305
306
307 bool Loader::monitoring() const
308 {
309         if (!pimpl_->cached_item_)
310                 return false;
311
312         return pimpl_->cached_item_->monitoring();
313 }
314
315
316 unsigned long Loader::checksum() const
317 {
318         if (!pimpl_->cached_item_)
319                 return 0;
320
321         return pimpl_->cached_item_->checksum();
322 }
323
324
325 FileName const & Loader::filename() const
326 {
327         static FileName const empty;
328         return pimpl_->cached_item_ ?
329                 pimpl_->cached_item_->filename() : empty;
330 }
331
332
333 ImageStatus Loader::status() const
334 {
335         return pimpl_->status_;
336 }
337
338
339 double Loader::displayPixelRatio() const
340 {
341         return pimpl_->displayPixelRatio();
342 }
343
344
345 void Loader::setDisplayPixelRatio(double scale)
346 {
347         pimpl_->setDisplayPixelRatio(scale);
348 }
349
350
351 boost::signals2::connection Loader::connect(slot_type const & slot) const
352 {
353         return pimpl_->signal_.connect(slot);
354 }
355
356
357 Image const * Loader::image() const
358 {
359         return pimpl_->image_.get();
360 }
361
362
363 Loader::Impl::Impl()
364         : status_(WaitingToLoad)
365 {
366 }
367
368
369 Loader::Impl::~Impl()
370 {
371         resetFile(FileName());
372 }
373
374
375 void Loader::Impl::resetFile(FileName const & file)
376 {
377         FileName const old_file = cached_item_ ?
378                 cached_item_->filename() : FileName();
379
380         if (file == old_file)
381                 return;
382
383         // If monitoring() the current file, should continue to monitor the
384         // new file.
385         bool continue_monitoring = false;
386
387         if (!old_file.empty()) {
388                 continue_monitoring = cached_item_->monitoring();
389                 // cached_item_ is going to be reset, so the connected
390                 // signal needs to be disconnected.
391                 sc_.disconnect();
392                 cached_item_.reset();
393                 if (status_ != Converting) {
394                         Cache::get().remove(old_file);
395                 } else {
396                         //TODO remove cache item when it is not busy any more, see #7163
397                 }
398         }
399
400         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
401         image_.reset();
402
403         if (cached_item_ || file.empty())
404                 return;
405
406         Cache & gc = Cache::get();
407         if (!gc.inCache(file))
408                 gc.add(file);
409
410         // We /must/ make a local copy of this.
411         cached_item_ = gc.item(file);
412         status_ = cached_item_->status();
413
414         if (continue_monitoring && !cached_item_->monitoring())
415                 cached_item_->startMonitoring();
416
417         sc_ = cached_item_->connect(bind(&Impl::statusChanged, this));
418 }
419
420
421 void Loader::Impl::resetParams(Params const & params)
422 {
423         if (params == params_)
424                 return;
425
426         params_ = params;
427         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
428         image_.reset();
429 }
430
431
432 void Loader::Impl::statusChanged()
433 {
434         status_ = cached_item_ ? cached_item_->status() : WaitingToLoad;
435         createPixmap();
436         signal_();
437 }
438
439
440 void Loader::Impl::createPixmap()
441 {
442         if (!params_.display || status_ != Loaded)
443                 return;
444
445         if (!cached_item_) {
446                 LYXERR(Debug::GRAPHICS, "pixmap not cached yet");
447                 return;
448         }
449
450         if (!cached_item_->image()) {
451                 // There must have been a problem reading the file.
452                 LYXERR(Debug::GRAPHICS, "Graphics file not loaded.");
453                 return;
454         }
455
456         image_.reset(cached_item_->image()->clone());
457
458         if (params_.pixel_ratio == 1.0) {
459                 string filename = cached_item_->filename().absFileName();
460                 size_t idx = filename.find_last_of('.');
461                 if (idx != string::npos && idx > 3) {
462                         if (filename.substr(idx - 3, 3) == "@2x") {
463                                 params_.pixel_ratio = 2.0;
464                         }
465                 }
466         }
467
468         bool const success = image_->setPixmap(params_);
469
470         if (success) {
471                 status_ = Ready;
472         } else {
473                 image_.reset();
474                 status_ = ErrorGeneratingPixmap;
475         }
476 }
477
478 void Loader::Impl::startLoading()
479 {
480         if (status_ != WaitingToLoad)
481                 return;
482
483         if (cached_item_->tryDisplayFormat()) {
484                 status_ = Loaded;
485                 createPixmap();
486                 return;
487         }
488
489         LoaderQueue::get().touch(cached_item_);
490 }
491
492
493 } // namespace graphics
494 } // namespace lyx