]> git.lyx.org Git - lyx.git/blob - src/graphics/LoaderQueue.cpp
Fix dialog handling of Insert Plain Text
[lyx.git] / src / graphics / LoaderQueue.cpp
1 /**
2  * \file LoaderQueue.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "LoaderQueue.h"
14 #include "GraphicsCacheItem.h"
15
16 #include "debug.h"
17
18 #include <boost/bind.hpp>
19
20 using std::endl;
21 using std::list;
22
23 namespace lyx {
24 namespace graphics {
25
26 static int s_numimages_ = 5;
27 static int s_millisecs_ = 500;
28
29
30 LoaderQueue & LoaderQueue::get()
31 {
32         static LoaderQueue singleton;
33         return singleton;
34 }
35
36
37 void LoaderQueue::loadNext()
38 {
39         LYXERR(Debug::GRAPHICS, "LoaderQueue: "
40                 << cache_queue_.size() << " items in the queue");
41         int counter = s_numimages_;
42         while (cache_queue_.size() && counter--) {
43                 Cache::ItemPtr ptr = cache_queue_.front();
44                 cache_set_.erase(ptr);
45                 cache_queue_.pop_front();
46                 if (ptr->status() == WaitingToLoad)
47                         ptr->startLoading();
48         }
49         if (cache_queue_.size()) {
50                 startLoader();
51         } else {
52                 stopLoader();
53         }
54 }
55
56
57 void LoaderQueue::setPriority(int numimages , int millisecs)
58 {
59         s_numimages_ = numimages;
60         s_millisecs_ = millisecs;
61         LYXERR(Debug::GRAPHICS, "LoaderQueue:  priority set to "
62                                 << s_numimages_ << " images at a time, "
63                                 << s_millisecs_ << " milliseconds between calls");
64 }
65
66
67 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
68                              running_(false)
69 {
70         timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
71 }
72
73
74 void LoaderQueue::startLoader()
75 {
76         LYXERR(Debug::GRAPHICS, "LoaderQueue: waking up");
77         running_ = true ;
78         timer.setTimeout(s_millisecs_);
79         timer.start();
80 }
81
82
83 void LoaderQueue::stopLoader()
84 {
85         timer.stop();
86         running_ = false ;
87         LYXERR(Debug::GRAPHICS, "LoaderQueue: I'm going to sleep");
88 }
89
90
91 bool LoaderQueue::running() const
92 {
93         return running_ ;
94 }
95
96
97 void LoaderQueue::touch(Cache::ItemPtr const & item)
98 {
99         if (! cache_set_.insert(item).second) {
100                 list<Cache::ItemPtr>::iterator
101                         it = cache_queue_.begin();
102                 list<Cache::ItemPtr>::iterator
103                         end = cache_queue_.end();
104
105                 it = std::find(it, end, item);
106                 if (it != end)
107                         cache_queue_.erase(it);
108         }
109         cache_queue_.push_front(item);
110         if (!running_)
111                 startLoader();
112 }
113
114
115 } // namespace graphics
116 } // namespace lyx