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