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>
26 struct Loader::Impl : boost::signals::trackable {
32 void resetFile(string const &);
34 void resetParams(Params const &);
40 Params const & params() const { return params_; }
42 /// The loading status of the image.
44 /** Must store a copy of the cached item to ensure that it is not
45 * erased unexpectedly by the cache itself.
47 Cache::ItemPtr cached_item_;
48 /// We modify a local copy of the image once it is loaded.
49 Image::ImagePtr image_;
50 /// This signal is emitted when the image loading status changes.
51 boost::signal0<void> signal_;
57 void checkedLoading();
69 Loader::Loader(string const & file, DisplayType type)
76 Loader::Loader(string const & file, Params const & params)
83 Loader::Loader(Loader const & other)
86 Params const & params = other.pimpl_->params();
87 reset(params.filename, params);
95 Loader & Loader::operator=(Loader const & other)
98 Params const & params = other.pimpl_->params();
99 reset(params.filename, params);
105 void Loader::reset(string const & file, DisplayType type) const
108 params.display = type;
109 pimpl_->resetParams(params);
111 pimpl_->resetFile(file);
112 pimpl_->createPixmap();
116 void Loader::reset(string const & file, Params const & params) const
118 pimpl_->resetParams(params);
119 pimpl_->resetFile(file);
120 pimpl_->createPixmap();
124 void Loader::reset(Params const & params) const
126 pimpl_->resetParams(params);
127 pimpl_->createPixmap();
131 void Loader::startLoading() const
133 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
135 pimpl_->startLoading();
139 void Loader::startMonitoring() const
141 if (!pimpl_->cached_item_.get())
144 pimpl_->cached_item_->startMonitoring();
148 bool Loader::monitoring() const
150 if (!pimpl_->cached_item_.get())
153 return pimpl_->cached_item_->monitoring();
157 unsigned long Loader::checksum() const
159 if (!pimpl_->cached_item_.get())
162 return pimpl_->cached_item_->checksum();
166 string const & Loader::filename() const
168 static string const empty;
169 return pimpl_->cached_item_.get() ?
170 pimpl_->cached_item_->filename() : empty;
174 ImageStatus Loader::status() const
176 return pimpl_->status_;
180 boost::signals::connection Loader::connect(slot_type const & slot) const
182 return pimpl_->signal_.connect(slot);
186 Image const * Loader::image() const
188 return pimpl_->image_.get();
193 : status_(WaitingToLoad)
198 Loader::Impl::~Impl()
204 void Loader::Impl::resetFile(string const & file)
206 string const old_file = cached_item_.get() ?
207 cached_item_->filename() : string();
209 if (file == old_file)
212 // If monitoring() the current file, should continue to monitor the
214 bool continue_monitoring = false;
216 if (!old_file.empty()) {
217 continue_monitoring = cached_item_->monitoring();
218 cached_item_.reset();
219 Cache::get().remove(old_file);
222 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
225 if (cached_item_.get() || file.empty())
228 Cache & gc = Cache::get();
229 if (!gc.inCache(file))
232 // We /must/ make a local copy of this.
233 cached_item_ = gc.item(file);
234 status_ = cached_item_->status();
236 if (continue_monitoring && !cached_item_->monitoring())
237 cached_item_->startMonitoring();
239 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
243 void Loader::Impl::resetParams(Params const & params)
245 if (params == params_)
249 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
254 void Loader::Impl::statusChanged()
256 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
262 void Loader::Impl::createPixmap()
264 if (!cached_item_.get() ||
265 params_.display == NoDisplay || status_ != Loaded)
268 image_.reset(cached_item_->image()->clone());
270 // These do nothing if there's nothing to do
271 image_->clip(params_);
272 image_->rotate(params_);
273 image_->scale(params_);
275 bool const success = image_->setPixmap(params_);
281 status_ = ErrorGeneratingPixmap;
285 void Loader::Impl::startLoading()
287 if (status_ != WaitingToLoad)
290 LoaderQueue::get().touch(cached_item_);
294 } // namespace graphics