]> git.lyx.org Git - lyx.git/blob - src/graphics/LoaderQueue.C
If the graphics loader has a copy c-tor it should have copy assignment that does...
[lyx.git] / src / graphics / LoaderQueue.C
1 /**
2  * \file LoaderQueue.C
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
15 #include "debug.h"
16
17 #include <boost/bind.hpp>
18
19 using std::endl;
20 using std::list;
21
22
23 namespace grfx {
24
25 int LoaderQueue::s_numimages_ = 5;
26 int LoaderQueue::s_millisecs_ = 500;
27
28
29 LoaderQueue & LoaderQueue::get()
30 {
31         static LoaderQueue singleton;
32         return singleton;
33 }
34
35
36 void LoaderQueue::loadNext()
37 {
38         lyxerr[Debug::GRAPHICS] << "LoaderQueue: "
39                                 << cache_queue_.size()
40                                 << " items in the queue" << endl;
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                                 << endl;
65 }
66
67
68 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
69                              running_(false)
70 {
71         timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
72 }
73
74
75 void LoaderQueue::startLoader()
76 {
77         lyxerr[Debug::GRAPHICS] << "LoaderQueue: waking up" << endl;
78         running_ = true ;
79         timer.setTimeout(s_millisecs_);
80         timer.start();
81 }
82
83
84 void LoaderQueue::stopLoader()
85 {
86         timer.stop();
87         running_ = false ;
88         lyxerr[Debug::GRAPHICS] << "LoaderQueue: I'm going to sleep" << endl;
89 }
90
91
92 bool LoaderQueue::running() const
93 {
94         return running_ ;
95 }
96
97
98 void LoaderQueue::touch(Cache::ItemPtr const & item)
99 {
100         if (! cache_set_.insert(item).second) {
101                 list<Cache::ItemPtr>::iterator
102                         it = cache_queue_.begin();
103                 list<Cache::ItemPtr>::iterator
104                         end = cache_queue_.end();
105
106                 it = std::find(it, end, item);
107                 if (it != end)
108                         cache_queue_.erase(it);
109         }
110         cache_queue_.push_front(item);
111         if (!running_)
112                 startLoader();
113 }
114
115
116 } // namespace grfx