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>
29 struct Loader::Impl : boost::signals::trackable {
35 void resetFile(string const &);
37 void resetParams(Params const &);
43 Params const & params() const { return params_; }
45 /// The loading status of the image.
47 /** Must store a copy of the cached item to ensure that it is not
48 * erased unexpectedly by the cache itself.
50 Cache::ItemPtr cached_item_;
51 /// We modify a local copy of the image once it is loaded.
52 Image::ImagePtr image_;
53 /// This signal is emitted when the image loading status changes.
54 boost::signal0<void> signal_;
60 void checkedLoading();
72 Loader::Loader(string const & file, DisplayType type)
79 Loader::Loader(string const & file, Params const & params)
86 Loader::Loader(Loader const & other)
89 Params const & params = other.pimpl_->params();
90 reset(params.filename, params);
98 Loader & Loader::operator=(Loader const & other)
100 if (this != &other) {
101 Params const & params = other.pimpl_->params();
102 reset(params.filename, params);
108 void Loader::reset(string const & file, DisplayType type) const
111 params.display = type;
112 pimpl_->resetParams(params);
114 pimpl_->resetFile(file);
115 pimpl_->createPixmap();
119 void Loader::reset(string const & file, Params const & params) const
121 pimpl_->resetParams(params);
122 pimpl_->resetFile(file);
123 pimpl_->createPixmap();
127 void Loader::reset(Params const & params) const
129 pimpl_->resetParams(params);
130 pimpl_->createPixmap();
134 void Loader::startLoading() const
136 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
138 pimpl_->startLoading();
142 void Loader::startMonitoring() const
144 if (!pimpl_->cached_item_.get())
147 pimpl_->cached_item_->startMonitoring();
151 bool Loader::monitoring() const
153 if (!pimpl_->cached_item_.get())
156 return pimpl_->cached_item_->monitoring();
160 unsigned long Loader::checksum() const
162 if (!pimpl_->cached_item_.get())
165 return pimpl_->cached_item_->checksum();
169 string const & Loader::filename() const
171 static string const empty;
172 return pimpl_->cached_item_.get() ?
173 pimpl_->cached_item_->filename() : empty;
177 ImageStatus Loader::status() const
179 return pimpl_->status_;
183 boost::signals::connection Loader::connect(slot_type const & slot) const
185 return pimpl_->signal_.connect(slot);
189 Image const * Loader::image() const
191 return pimpl_->image_.get();
196 : status_(WaitingToLoad)
201 Loader::Impl::~Impl()
207 void Loader::Impl::resetFile(string const & file)
209 string const old_file = cached_item_.get() ?
210 cached_item_->filename() : string();
212 if (file == old_file)
215 // If monitoring() the current file, should continue to monitor the
217 bool continue_monitoring = false;
219 if (!old_file.empty()) {
220 continue_monitoring = cached_item_->monitoring();
221 cached_item_.reset();
222 Cache::get().remove(old_file);
225 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
228 if (cached_item_.get() || file.empty())
231 Cache & gc = Cache::get();
232 if (!gc.inCache(file))
235 // We /must/ make a local copy of this.
236 cached_item_ = gc.item(file);
237 status_ = cached_item_->status();
239 if (continue_monitoring && !cached_item_->monitoring())
240 cached_item_->startMonitoring();
242 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
246 void Loader::Impl::resetParams(Params const & params)
248 if (params == params_)
252 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
257 void Loader::Impl::statusChanged()
259 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
265 void Loader::Impl::createPixmap()
267 if (!cached_item_.get() ||
268 params_.display == NoDisplay || status_ != Loaded)
271 image_.reset(cached_item_->image()->clone());
273 // These do nothing if there's nothing to do
274 image_->clip(params_);
275 image_->rotate(params_);
276 image_->scale(params_);
278 bool const success = image_->setPixmap(params_);
284 status_ = ErrorGeneratingPixmap;
288 void Loader::Impl::startLoading()
290 if (status_ != WaitingToLoad)
293 LoaderQueue::get().touch(cached_item_);
297 } // namespace graphics