]> git.lyx.org Git - lyx.git/blob - src/graphics/GraphicsLoader.cpp
remove a few uses of boost::scoped_ptr
[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         delete pimpl_;
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         resetFile(FileName());
212 }
213
214
215 void Loader::Impl::resetFile(FileName const & file)
216 {
217         FileName const old_file = cached_item_.get() ?
218                 cached_item_->filename() : FileName();
219
220         if (file == old_file)
221                 return;
222
223         // If monitoring() the current file, should continue to monitor the
224         // new file.
225         bool continue_monitoring = false;
226
227         if (!old_file.empty()) {
228                 continue_monitoring = cached_item_->monitoring();
229                 // cached_item_ is going to be reset, so the connected
230                 // signal needs to be disconnected.
231                 sc_.disconnect();
232                 cached_item_.reset();
233                 Cache::get().remove(old_file);
234         }
235
236         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
237         image_.reset();
238
239         if (cached_item_.get() || file.empty())
240                 return;
241
242         Cache & gc = Cache::get();
243         if (!gc.inCache(file))
244                 gc.add(file);
245
246         // We /must/ make a local copy of this.
247         cached_item_ = gc.item(file);
248         status_ = cached_item_->status();
249
250         if (continue_monitoring && !cached_item_->monitoring())
251                 cached_item_->startMonitoring();
252
253         sc_ = cached_item_->connect(boost::bind(&Impl::statusChanged, this));
254 }
255
256
257 void Loader::Impl::resetParams(Params const & params)
258 {
259         if (params == params_)
260                 return;
261
262         params_ = params;
263         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
264         image_.reset();
265 }
266
267
268 void Loader::Impl::statusChanged()
269 {
270         status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
271         createPixmap();
272         signal_();
273 }
274
275
276 void Loader::Impl::createPixmap()
277 {
278         if (!cached_item_.get() ||
279             params_.display == NoDisplay || status_ != Loaded)
280                 return;
281
282         image_.reset(cached_item_->image()->clone());
283
284         // These do nothing if there's nothing to do
285         image_->clip(params_);
286         image_->rotate(params_);
287         image_->scale(params_);
288
289         bool const success = image_->setPixmap(params_);
290
291         if (success) {
292                 status_ = Ready;
293         } else {
294                 image_.reset();
295                 status_ = ErrorGeneratingPixmap;
296         }
297 }
298
299 void Loader::Impl::startLoading()
300 {
301         if (status_ != WaitingToLoad)
302                 return;
303
304         LoaderQueue::get().touch(cached_item_);
305 }
306
307
308 } // namespace graphics
309 } // namespace lyx