2 * \file GraphicsLoader.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "GraphicsLoader.h"
15 #include "GraphicsCacheItem.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsParams.h"
18 #include "LoaderQueue.h"
20 #include "support/filename.h"
22 #include <boost/bind.hpp>
30 using support::FileName;
34 class Loader::Impl : public boost::signals::trackable {
41 void resetFile(FileName const &);
43 void resetParams(Params const &);
49 Params const & params() const { return params_; }
51 /// The loading status of the image.
53 /** Must store a copy of the cached item to ensure that it is not
54 * erased unexpectedly by the cache itself.
56 Cache::ItemPtr cached_item_;
57 /// We modify a local copy of the image once it is loaded.
58 Image::ImagePtr image_;
59 /// This signal is emitted when the image loading status changes.
60 boost::signal<void()> signal_;
66 void checkedLoading();
78 Loader::Loader(FileName const & file, DisplayType type)
85 Loader::Loader(FileName const & file, Params const & params)
92 Loader::Loader(Loader const & other)
95 Params const & params = other.pimpl_->params();
96 reset(params.filename, params);
104 Loader & Loader::operator=(Loader const & other)
106 if (this != &other) {
107 Params const & params = other.pimpl_->params();
108 reset(params.filename, params);
114 void Loader::reset(FileName const & file, DisplayType type) const
117 params.display = type;
118 pimpl_->resetParams(params);
120 pimpl_->resetFile(file);
121 pimpl_->createPixmap();
125 void Loader::reset(FileName const & file, Params const & params) const
127 pimpl_->resetParams(params);
128 pimpl_->resetFile(file);
129 pimpl_->createPixmap();
133 void Loader::reset(Params const & params) const
135 pimpl_->resetParams(params);
136 pimpl_->createPixmap();
140 void Loader::startLoading() const
142 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
144 pimpl_->startLoading();
148 void Loader::startMonitoring() const
150 if (!pimpl_->cached_item_.get())
153 pimpl_->cached_item_->startMonitoring();
157 bool Loader::monitoring() const
159 if (!pimpl_->cached_item_.get())
162 return pimpl_->cached_item_->monitoring();
166 unsigned long Loader::checksum() const
168 if (!pimpl_->cached_item_.get())
171 return pimpl_->cached_item_->checksum();
175 FileName const & Loader::filename() const
177 static FileName const empty;
178 return pimpl_->cached_item_.get() ?
179 pimpl_->cached_item_->filename() : empty;
183 ImageStatus Loader::status() const
185 return pimpl_->status_;
189 boost::signals::connection Loader::connect(slot_type const & slot) const
191 return pimpl_->signal_.connect(slot);
195 Image const * Loader::image() const
197 return pimpl_->image_.get();
202 : status_(WaitingToLoad)
207 Loader::Impl::~Impl()
209 resetFile(FileName());
213 void Loader::Impl::resetFile(FileName const & file)
215 FileName const old_file = cached_item_.get() ?
216 cached_item_->filename() : FileName();
218 if (file == old_file)
221 // If monitoring() the current file, should continue to monitor the
223 bool continue_monitoring = false;
225 if (!old_file.empty()) {
226 continue_monitoring = cached_item_->monitoring();
227 cached_item_.reset();
228 Cache::get().remove(old_file);
231 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
234 if (cached_item_.get() || file.empty())
237 Cache & gc = Cache::get();
238 if (!gc.inCache(file))
241 // We /must/ make a local copy of this.
242 cached_item_ = gc.item(file);
243 status_ = cached_item_->status();
245 if (continue_monitoring && !cached_item_->monitoring())
246 cached_item_->startMonitoring();
248 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
252 void Loader::Impl::resetParams(Params const & params)
254 if (params == params_)
258 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
263 void Loader::Impl::statusChanged()
265 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
271 void Loader::Impl::createPixmap()
273 if (!cached_item_.get() ||
274 params_.display == NoDisplay || status_ != Loaded)
277 image_.reset(cached_item_->image()->clone());
279 // These do nothing if there's nothing to do
280 image_->clip(params_);
281 image_->rotate(params_);
282 image_->scale(params_);
284 bool const success = image_->setPixmap(params_);
290 status_ = ErrorGeneratingPixmap;
294 void Loader::Impl::startLoading()
296 if (status_ != WaitingToLoad)
299 LoaderQueue::get().touch(cached_item_);
303 } // namespace graphics