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