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