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