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 {
36 void resetFile(string const &);
38 void resetParams(Params const &);
44 Params const & params() const { return params_; }
46 /// The loading status of the image.
48 /** Must store a copy of the cached item to ensure that it is not
49 * erased unexpectedly by the cache itself.
51 Cache::ItemPtr cached_item_;
52 /// We modify a local copy of the image once it is loaded.
53 Image::ImagePtr image_;
54 /// This signal is emitted when the image loading status changes.
55 boost::signal<void()> signal_;
61 void checkedLoading();
73 Loader::Loader(string const & file, DisplayType type)
80 Loader::Loader(string const & file, Params const & params)
87 Loader::Loader(Loader const & other)
90 Params const & params = other.pimpl_->params();
91 reset(params.filename, params);
99 Loader & Loader::operator=(Loader const & other)
101 if (this != &other) {
102 Params const & params = other.pimpl_->params();
103 reset(params.filename, params);
109 void Loader::reset(string const & file, DisplayType type) const
112 params.display = type;
113 pimpl_->resetParams(params);
115 pimpl_->resetFile(file);
116 pimpl_->createPixmap();
120 void Loader::reset(string const & file, Params const & params) const
122 pimpl_->resetParams(params);
123 pimpl_->resetFile(file);
124 pimpl_->createPixmap();
128 void Loader::reset(Params const & params) const
130 pimpl_->resetParams(params);
131 pimpl_->createPixmap();
135 void Loader::startLoading() const
137 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
139 pimpl_->startLoading();
143 void Loader::startMonitoring() const
145 if (!pimpl_->cached_item_.get())
148 pimpl_->cached_item_->startMonitoring();
152 bool Loader::monitoring() const
154 if (!pimpl_->cached_item_.get())
157 return pimpl_->cached_item_->monitoring();
161 unsigned long Loader::checksum() const
163 if (!pimpl_->cached_item_.get())
166 return pimpl_->cached_item_->checksum();
170 string const & Loader::filename() const
172 static string const empty;
173 return pimpl_->cached_item_.get() ?
174 pimpl_->cached_item_->filename() : empty;
178 ImageStatus Loader::status() const
180 return pimpl_->status_;
184 boost::signals::connection Loader::connect(slot_type const & slot) const
186 return pimpl_->signal_.connect(slot);
190 Image const * Loader::image() const
192 return pimpl_->image_.get();
197 : status_(WaitingToLoad)
202 Loader::Impl::~Impl()
208 void Loader::Impl::resetFile(string const & file)
210 string const old_file = cached_item_.get() ?
211 cached_item_->filename() : string();
213 if (file == old_file)
216 // If monitoring() the current file, should continue to monitor the
218 bool continue_monitoring = false;
220 if (!old_file.empty()) {
221 continue_monitoring = cached_item_->monitoring();
222 cached_item_.reset();
223 Cache::get().remove(old_file);
226 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
229 if (cached_item_.get() || file.empty())
232 Cache & gc = Cache::get();
233 if (!gc.inCache(file))
236 // We /must/ make a local copy of this.
237 cached_item_ = gc.item(file);
238 status_ = cached_item_->status();
240 if (continue_monitoring && !cached_item_->monitoring())
241 cached_item_->startMonitoring();
243 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
247 void Loader::Impl::resetParams(Params const & params)
249 if (params == params_)
253 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
258 void Loader::Impl::statusChanged()
260 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
266 void Loader::Impl::createPixmap()
268 if (!cached_item_.get() ||
269 params_.display == NoDisplay || status_ != Loaded)
272 image_.reset(cached_item_->image()->clone());
274 // These do nothing if there's nothing to do
275 image_->clip(params_);
276 image_->rotate(params_);
277 image_->scale(params_);
279 bool const success = image_->setPixmap(params_);
285 status_ = ErrorGeneratingPixmap;
289 void Loader::Impl::startLoading()
291 if (status_ != WaitingToLoad)
294 LoaderQueue::get().touch(cached_item_);
298 } // namespace graphics