]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsLoader.C
Remove cached var from RenderPreview. Changes elsewhere to suit.
[lyx.git] / src / graphics / GraphicsLoader.C
1 /**
2  * \file GraphicsLoader.C
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 "LoaderQueue.h"
19
20 #include <boost/bind.hpp>
21
22
23 using std::string;
24
25
26 namespace lyx {
27 namespace graphics {
28
29 struct Loader::Impl : boost::signals::trackable {
30         ///
31         Impl();
32         ///
33         ~Impl();
34         ///
35         void resetFile(string const &);
36         ///
37         void resetParams(Params const &);
38         ///
39         void createPixmap();
40         ///
41         void startLoading();
42         ///
43         Params const & params() const { return params_; }
44
45         /// The loading status of the image.
46         ImageStatus status_;
47         /** Must store a copy of the cached item to ensure that it is not
48          *  erased unexpectedly by the cache itself.
49          */
50         Cache::ItemPtr cached_item_;
51         /// We modify a local copy of the image once it is loaded.
52         Image::ImagePtr image_;
53         /// This signal is emitted when the image loading status changes.
54         boost::signal0<void> signal_;
55
56 private:
57         ///
58         void statusChanged();
59         ///
60         void checkedLoading();
61
62         ///
63         Params params_;
64 };
65
66
67 Loader::Loader()
68         : pimpl_(new Impl)
69 {}
70
71
72 Loader::Loader(string const & file, DisplayType type)
73         : pimpl_(new Impl)
74 {
75         reset(file, type);
76 }
77
78
79 Loader::Loader(string const & file, Params const & params)
80         : pimpl_(new Impl)
81 {
82         reset(file, params);
83 }
84
85
86 Loader::Loader(Loader const & other)
87         : pimpl_(new Impl)
88 {
89         Params const & params = other.pimpl_->params();
90         reset(params.filename, params);
91 }
92
93
94 Loader::~Loader()
95 {}
96
97
98 Loader & Loader::operator=(Loader const & other)
99 {
100         if (this != &other) {
101                 Params const & params = other.pimpl_->params();
102                 reset(params.filename, params);
103         }
104         return *this;
105 }
106
107
108 void Loader::reset(string const & file, DisplayType type) const
109 {
110         Params params;
111         params.display = type;
112         pimpl_->resetParams(params);
113
114         pimpl_->resetFile(file);
115         pimpl_->createPixmap();
116 }
117
118
119 void Loader::reset(string const & file, Params const & params) const
120 {
121         pimpl_->resetParams(params);
122         pimpl_->resetFile(file);
123         pimpl_->createPixmap();
124 }
125
126
127 void Loader::reset(Params const & params) const
128 {
129         pimpl_->resetParams(params);
130         pimpl_->createPixmap();
131 }
132
133
134 void Loader::startLoading() const
135 {
136         if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
137                 return;
138         pimpl_->startLoading();
139 }
140
141
142 void Loader::startMonitoring() const
143 {
144         if (!pimpl_->cached_item_.get())
145                 return;
146
147         pimpl_->cached_item_->startMonitoring();
148 }
149
150
151 bool Loader::monitoring() const
152 {
153         if (!pimpl_->cached_item_.get())
154                 return false;
155
156         return pimpl_->cached_item_->monitoring();
157 }
158
159
160 unsigned long Loader::checksum() const
161 {
162         if (!pimpl_->cached_item_.get())
163                 return 0;
164
165         return pimpl_->cached_item_->checksum();
166 }
167
168
169 string const & Loader::filename() const
170 {
171         static string const empty;
172         return pimpl_->cached_item_.get() ?
173                 pimpl_->cached_item_->filename() : empty;
174 }
175
176
177 ImageStatus Loader::status() const
178 {
179         return pimpl_->status_;
180 }
181
182
183 boost::signals::connection Loader::connect(slot_type const & slot) const
184 {
185         return pimpl_->signal_.connect(slot);
186 }
187
188
189 Image const * Loader::image() const
190 {
191         return pimpl_->image_.get();
192 }
193
194
195 Loader::Impl::Impl()
196         : status_(WaitingToLoad)
197 {
198 }
199
200
201 Loader::Impl::~Impl()
202 {
203         resetFile(string());
204 }
205
206
207 void Loader::Impl::resetFile(string const & file)
208 {
209         string const old_file = cached_item_.get() ?
210                 cached_item_->filename() : string();
211
212         if (file == old_file)
213                 return;
214
215         // If monitoring() the current file, should continue to monitor the
216         // new file.
217         bool continue_monitoring = false;
218
219         if (!old_file.empty()) {
220                 continue_monitoring = cached_item_->monitoring();
221                 cached_item_.reset();
222                 Cache::get().remove(old_file);
223         }
224
225         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
226         image_.reset();
227
228         if (cached_item_.get() || file.empty())
229                 return;
230
231         Cache & gc = Cache::get();
232         if (!gc.inCache(file))
233                 gc.add(file);
234
235         // We /must/ make a local copy of this.
236         cached_item_ = gc.item(file);
237         status_ = cached_item_->status();
238
239         if (continue_monitoring && !cached_item_->monitoring())
240                 cached_item_->startMonitoring();
241
242         cached_item_->connect(boost::bind(&Impl::statusChanged, this));
243 }
244
245
246 void Loader::Impl::resetParams(Params const & params)
247 {
248         if (params == params_)
249                 return;
250
251         params_ = params;
252         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
253         image_.reset();
254 }
255
256
257 void Loader::Impl::statusChanged()
258 {
259         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
260         createPixmap();
261         signal_();
262 }
263
264
265 void Loader::Impl::createPixmap()
266 {
267         if (!cached_item_.get() ||
268             params_.display == NoDisplay || status_ != Loaded)
269                 return;
270
271         image_.reset(cached_item_->image()->clone());
272
273         // These do nothing if there's nothing to do
274         image_->clip(params_);
275         image_->rotate(params_);
276         image_->scale(params_);
277
278         bool const success = image_->setPixmap(params_);
279
280         if (success) {
281                 status_ = Ready;
282         } else {
283                 image_.reset();
284                 status_ = ErrorGeneratingPixmap;
285         }
286 }
287
288 void Loader::Impl::startLoading()
289 {
290         if (status_ != WaitingToLoad)
291                 return;
292
293         LoaderQueue::get().touch(cached_item_);
294 }
295
296
297 } // namespace graphics
298 } // namespace lyx