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"
20 #include <boost/bind.hpp>
28 using support::FileName;
32 typedef boost::shared_ptr<Image> ImagePtr;
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.
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);
108 Loader & Loader::operator=(Loader const & other)
110 if (this != &other) {
111 Params const & params = other.pimpl_->params();
112 reset(params.filename, params);
118 void Loader::reset(FileName const & file, DisplayType type) const
121 params.display = type;
122 pimpl_->resetParams(params);
124 pimpl_->resetFile(file);
125 pimpl_->createPixmap();
129 void Loader::reset(FileName const & file, Params const & params) const
131 pimpl_->resetParams(params);
132 pimpl_->resetFile(file);
133 pimpl_->createPixmap();
137 void Loader::reset(Params const & params) const
139 pimpl_->resetParams(params);
140 pimpl_->createPixmap();
144 void Loader::startLoading() const
146 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
148 pimpl_->startLoading();
152 void Loader::startMonitoring() const
154 if (!pimpl_->cached_item_.get())
157 pimpl_->cached_item_->startMonitoring();
161 bool Loader::monitoring() const
163 if (!pimpl_->cached_item_.get())
166 return pimpl_->cached_item_->monitoring();
170 unsigned long Loader::checksum() const
172 if (!pimpl_->cached_item_.get())
175 return pimpl_->cached_item_->checksum();
179 FileName const & Loader::filename() const
181 static FileName const empty;
182 return pimpl_->cached_item_.get() ?
183 pimpl_->cached_item_->filename() : empty;
187 ImageStatus Loader::status() const
189 return pimpl_->status_;
193 boost::signals::connection Loader::connect(slot_type const & slot) const
195 return pimpl_->signal_.connect(slot);
199 Image const * Loader::image() const
201 return pimpl_->image_.get();
206 : status_(WaitingToLoad)
211 Loader::Impl::~Impl()
213 resetFile(FileName());
217 void Loader::Impl::resetFile(FileName const & file)
219 FileName const old_file = cached_item_.get() ?
220 cached_item_->filename() : FileName();
222 if (file == old_file)
225 // If monitoring() the current file, should continue to monitor the
227 bool continue_monitoring = false;
229 if (!old_file.empty()) {
230 continue_monitoring = cached_item_->monitoring();
231 // cached_item_ is going to be reset, so the connected
232 // signal needs to be disconnected.
234 cached_item_.reset();
235 Cache::get().remove(old_file);
238 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
241 if (cached_item_.get() || file.empty())
244 Cache & gc = Cache::get();
245 if (!gc.inCache(file))
248 // We /must/ make a local copy of this.
249 cached_item_ = gc.item(file);
250 status_ = cached_item_->status();
252 if (continue_monitoring && !cached_item_->monitoring())
253 cached_item_->startMonitoring();
255 sc_ = cached_item_->connect(boost::bind(&Impl::statusChanged, this));
259 void Loader::Impl::resetParams(Params const & params)
261 if (params == params_)
265 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
270 void Loader::Impl::statusChanged()
272 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
278 void Loader::Impl::createPixmap()
280 if (!cached_item_.get() ||
281 params_.display == NoDisplay || status_ != Loaded)
284 image_.reset(cached_item_->image()->clone());
286 // These do nothing if there's nothing to do
287 image_->clip(params_);
288 image_->rotate(params_);
289 image_->scale(params_);
291 bool const success = image_->setPixmap(params_);
297 status_ = ErrorGeneratingPixmap;
301 void Loader::Impl::startLoading()
303 if (status_ != WaitingToLoad)
306 LoaderQueue::get().touch(cached_item_);
310 } // namespace graphics