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