2 * \file GraphicsLoader.cpp
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"
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_;
61 /// The connection of the signal StatusChanged
62 boost::signals::connection sc_;
68 void checkedLoading();
80 Loader::Loader(FileName const & file, DisplayType type)
87 Loader::Loader(FileName const & file, Params const & params)
94 Loader::Loader(Loader const & other)
97 Params const & params = other.pimpl_->params();
98 reset(params.filename, params);
106 Loader & Loader::operator=(Loader const & other)
108 if (this != &other) {
109 Params const & params = other.pimpl_->params();
110 reset(params.filename, params);
116 void Loader::reset(FileName const & file, DisplayType type) const
119 params.display = type;
120 pimpl_->resetParams(params);
122 pimpl_->resetFile(file);
123 pimpl_->createPixmap();
127 void Loader::reset(FileName const & file, Params const & params) const
129 pimpl_->resetParams(params);
130 pimpl_->resetFile(file);
131 pimpl_->createPixmap();
135 void Loader::reset(Params const & params) const
137 pimpl_->resetParams(params);
138 pimpl_->createPixmap();
142 void Loader::startLoading() const
144 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
146 pimpl_->startLoading();
150 void Loader::startMonitoring() const
152 if (!pimpl_->cached_item_.get())
155 pimpl_->cached_item_->startMonitoring();
159 bool Loader::monitoring() const
161 if (!pimpl_->cached_item_.get())
164 return pimpl_->cached_item_->monitoring();
168 unsigned long Loader::checksum() const
170 if (!pimpl_->cached_item_.get())
173 return pimpl_->cached_item_->checksum();
177 FileName const & Loader::filename() const
179 static FileName const empty;
180 return pimpl_->cached_item_.get() ?
181 pimpl_->cached_item_->filename() : empty;
185 ImageStatus Loader::status() const
187 return pimpl_->status_;
191 boost::signals::connection Loader::connect(slot_type const & slot) const
193 return pimpl_->signal_.connect(slot);
197 Image const * Loader::image() const
199 return pimpl_->image_.get();
204 : status_(WaitingToLoad)
209 Loader::Impl::~Impl()
212 resetFile(FileName());
216 void Loader::Impl::resetFile(FileName const & file)
218 FileName const old_file = cached_item_.get() ?
219 cached_item_->filename() : FileName();
221 if (file == old_file)
224 // If monitoring() the current file, should continue to monitor the
226 bool continue_monitoring = false;
228 if (!old_file.empty()) {
229 continue_monitoring = cached_item_->monitoring();
230 // cached_item_ is going to be reset, so the connected
231 // signal needs to be disconnected.
233 cached_item_.reset();
234 Cache::get().remove(old_file);
237 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
240 if (cached_item_.get() || file.empty())
243 Cache & gc = Cache::get();
244 if (!gc.inCache(file))
247 // We /must/ make a local copy of this.
248 cached_item_ = gc.item(file);
249 status_ = cached_item_->status();
251 if (continue_monitoring && !cached_item_->monitoring())
252 cached_item_->startMonitoring();
254 sc_ = cached_item_->connect(boost::bind(&Impl::statusChanged, this));
258 void Loader::Impl::resetParams(Params const & params)
260 if (params == params_)
264 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
269 void Loader::Impl::statusChanged()
271 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
277 void Loader::Impl::createPixmap()
279 if (!cached_item_.get() ||
280 params_.display == NoDisplay || status_ != Loaded)
283 image_.reset(cached_item_->image()->clone());
285 // These do nothing if there's nothing to do
286 image_->clip(params_);
287 image_->rotate(params_);
288 image_->scale(params_);
290 bool const success = image_->setPixmap(params_);
296 status_ = ErrorGeneratingPixmap;
300 void Loader::Impl::startLoading()
302 if (status_ != WaitingToLoad)
305 LoaderQueue::get().touch(cached_item_);
309 } // namespace graphics