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 <boost/bind.hpp>
28 using support::FileName;
32 class Loader::Impl : public boost::signals::trackable {
39 void resetFile(FileName const &);
41 void resetParams(Params const &);
47 Params const & params() const { return params_; }
49 /// The loading status of the image.
51 /** Must store a copy of the cached item to ensure that it is not
52 * erased unexpectedly by the cache itself.
54 Cache::ItemPtr cached_item_;
55 /// We modify a local copy of the image once it is loaded.
56 Image::ImagePtr image_;
57 /// This signal is emitted when the image loading status changes.
58 boost::signal<void()> signal_;
64 void checkedLoading();
76 Loader::Loader(FileName const & file, DisplayType type)
83 Loader::Loader(FileName const & file, Params const & params)
90 Loader::Loader(Loader const & other)
93 Params const & params = other.pimpl_->params();
94 reset(params.filename, params);
102 Loader & Loader::operator=(Loader const & other)
104 if (this != &other) {
105 Params const & params = other.pimpl_->params();
106 reset(params.filename, params);
112 void Loader::reset(FileName const & file, DisplayType type) const
115 params.display = type;
116 pimpl_->resetParams(params);
118 pimpl_->resetFile(file);
119 pimpl_->createPixmap();
123 void Loader::reset(FileName const & file, Params const & params) const
125 pimpl_->resetParams(params);
126 pimpl_->resetFile(file);
127 pimpl_->createPixmap();
131 void Loader::reset(Params const & params) const
133 pimpl_->resetParams(params);
134 pimpl_->createPixmap();
138 void Loader::startLoading() const
140 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
142 pimpl_->startLoading();
146 void Loader::startMonitoring() const
148 if (!pimpl_->cached_item_.get())
151 pimpl_->cached_item_->startMonitoring();
155 bool Loader::monitoring() const
157 if (!pimpl_->cached_item_.get())
160 return pimpl_->cached_item_->monitoring();
164 unsigned long Loader::checksum() const
166 if (!pimpl_->cached_item_.get())
169 return pimpl_->cached_item_->checksum();
173 FileName const & Loader::filename() const
175 static FileName const empty;
176 return pimpl_->cached_item_.get() ?
177 pimpl_->cached_item_->filename() : empty;
181 ImageStatus Loader::status() const
183 return pimpl_->status_;
187 boost::signals::connection Loader::connect(slot_type const & slot) const
189 return pimpl_->signal_.connect(slot);
193 Image const * Loader::image() const
195 return pimpl_->image_.get();
200 : status_(WaitingToLoad)
205 Loader::Impl::~Impl()
207 resetFile(FileName());
211 void Loader::Impl::resetFile(FileName const & file)
213 FileName const old_file = cached_item_.get() ?
214 cached_item_->filename() : FileName();
216 if (file == old_file)
219 // If monitoring() the current file, should continue to monitor the
221 bool continue_monitoring = false;
223 if (!old_file.empty()) {
224 continue_monitoring = cached_item_->monitoring();
225 cached_item_.reset();
226 Cache::get().remove(old_file);
229 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
232 if (cached_item_.get() || file.empty())
235 Cache & gc = Cache::get();
236 if (!gc.inCache(file))
239 // We /must/ make a local copy of this.
240 cached_item_ = gc.item(file);
241 status_ = cached_item_->status();
243 if (continue_monitoring && !cached_item_->monitoring())
244 cached_item_->startMonitoring();
246 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
250 void Loader::Impl::resetParams(Params const & params)
252 if (params == params_)
256 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
261 void Loader::Impl::statusChanged()
263 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
269 void Loader::Impl::createPixmap()
271 if (!cached_item_.get() ||
272 params_.display == NoDisplay || status_ != Loaded)
275 image_.reset(cached_item_->image()->clone());
277 // These do nothing if there's nothing to do
278 image_->clip(params_);
279 image_->rotate(params_);
280 image_->scale(params_);
282 bool const success = image_->setPixmap(params_);
288 status_ = ErrorGeneratingPixmap;
292 void Loader::Impl::startLoading()
294 if (status_ != WaitingToLoad)
297 LoaderQueue::get().touch(cached_item_);
301 } // namespace graphics