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