]> git.lyx.org Git - lyx.git/blob - src/graphics/LoaderQueue.cpp
Fixed some lines that were too long. It compiled afterwards.
[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 int LoaderQueue::s_numimages_ = 5;
27 int LoaderQueue::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()
41                                 << " items in the queue" << endl;
42         int counter = s_numimages_;
43         while (cache_queue_.size() && counter--) {
44                 Cache::ItemPtr ptr = cache_queue_.front();
45                 cache_set_.erase(ptr);
46                 cache_queue_.pop_front();
47                 if (ptr->status() == WaitingToLoad)
48                         ptr->startLoading();
49         }
50         if (cache_queue_.size()) {
51                 startLoader();
52         } else {
53                 stopLoader();
54         }
55 }
56
57
58 void LoaderQueue::setPriority(int numimages , int millisecs)
59 {
60         s_numimages_ = numimages;
61         s_millisecs_ = millisecs;
62         LYXERR(Debug::GRAPHICS) << "LoaderQueue:  priority set to "
63                                 << s_numimages_ << " images at a time, "
64                                 << s_millisecs_ << " milliseconds between calls"
65                                 << endl;
66 }
67
68
69 LoaderQueue::LoaderQueue() : timer(s_millisecs_, Timeout::ONETIME),
70                              running_(false)
71 {
72         timer.timeout.connect(boost::bind(&LoaderQueue::loadNext, this));
73 }
74
75
76 void LoaderQueue::startLoader()
77 {
78         LYXERR(Debug::GRAPHICS) << "LoaderQueue: waking up" << endl;
79         running_ = true ;
80         timer.setTimeout(s_millisecs_);
81         timer.start();
82 }
83
84
85 void LoaderQueue::stopLoader()
86 {
87         timer.stop();
88         running_ = false ;
89         LYXERR(Debug::GRAPHICS) << "LoaderQueue: I'm going to sleep" << endl;
90 }
91
92
93 bool LoaderQueue::running() const
94 {
95         return running_ ;
96 }
97
98
99 void LoaderQueue::touch(Cache::ItemPtr const & item)
100 {
101         if (! cache_set_.insert(item).second) {
102                 list<Cache::ItemPtr>::iterator
103                         it = cache_queue_.begin();
104                 list<Cache::ItemPtr>::iterator
105                         end = cache_queue_.end();
106
107                 it = std::find(it, end, item);
108                 if (it != end)
109                         cache_queue_.erase(it);
110         }
111         cache_queue_.push_front(item);
112         if (!running_)
113                 startLoader();
114 }
115
116
117 } // namespace graphics
118 } // namespace lyx