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 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_;
59 /// The connection of the signal StatusChanged
60 boost::signals::connection sc_;
66 void checkedLoading();
78 Loader::Loader(FileName const & file, DisplayType type)
85 Loader::Loader(FileName const & file, Params const & params)
92 Loader::Loader(Loader const & other)
95 Params const & params = other.pimpl_->params();
96 reset(params.filename, params);
104 Loader & Loader::operator=(Loader const & other)
106 if (this != &other) {
107 Params const & params = other.pimpl_->params();
108 reset(params.filename, params);
114 void Loader::reset(FileName const & file, DisplayType type) const
117 params.display = type;
118 pimpl_->resetParams(params);
120 pimpl_->resetFile(file);
121 pimpl_->createPixmap();
125 void Loader::reset(FileName const & file, Params const & params) const
127 pimpl_->resetParams(params);
128 pimpl_->resetFile(file);
129 pimpl_->createPixmap();
133 void Loader::reset(Params const & params) const
135 pimpl_->resetParams(params);
136 pimpl_->createPixmap();
140 void Loader::startLoading() const
142 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
144 pimpl_->startLoading();
148 void Loader::startMonitoring() const
150 if (!pimpl_->cached_item_.get())
153 pimpl_->cached_item_->startMonitoring();
157 bool Loader::monitoring() const
159 if (!pimpl_->cached_item_.get())
162 return pimpl_->cached_item_->monitoring();
166 unsigned long Loader::checksum() const
168 if (!pimpl_->cached_item_.get())
171 return pimpl_->cached_item_->checksum();
175 FileName const & Loader::filename() const
177 static FileName const empty;
178 return pimpl_->cached_item_.get() ?
179 pimpl_->cached_item_->filename() : empty;
183 ImageStatus Loader::status() const
185 return pimpl_->status_;
189 boost::signals::connection Loader::connect(slot_type const & slot) const
191 return pimpl_->signal_.connect(slot);
195 Image const * Loader::image() const
197 return pimpl_->image_.get();
202 : status_(WaitingToLoad)
207 Loader::Impl::~Impl()
209 resetFile(FileName());
213 void Loader::Impl::resetFile(FileName const & file)
215 FileName const old_file = cached_item_.get() ?
216 cached_item_->filename() : FileName();
218 if (file == old_file)
221 // If monitoring() the current file, should continue to monitor the
223 bool continue_monitoring = false;
225 if (!old_file.empty()) {
226 continue_monitoring = cached_item_->monitoring();
227 // cached_item_ is going to be reset, so the connected
228 // signal needs to be disconnected.
230 cached_item_.reset();
231 Cache::get().remove(old_file);
234 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
237 if (cached_item_.get() || file.empty())
240 Cache & gc = Cache::get();
241 if (!gc.inCache(file))
244 // We /must/ make a local copy of this.
245 cached_item_ = gc.item(file);
246 status_ = cached_item_->status();
248 if (continue_monitoring && !cached_item_->monitoring())
249 cached_item_->startMonitoring();
251 sc_ = cached_item_->connect(boost::bind(&Impl::statusChanged, this));
255 void Loader::Impl::resetParams(Params const & params)
257 if (params == params_)
261 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
266 void Loader::Impl::statusChanged()
268 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
274 void Loader::Impl::createPixmap()
276 if (!cached_item_.get() ||
277 params_.display == NoDisplay || status_ != Loaded)
280 image_.reset(cached_item_->image()->clone());
282 // These do nothing if there's nothing to do
283 image_->clip(params_);
284 image_->rotate(params_);
285 image_->scale(params_);
287 bool const success = image_->setPixmap(params_);
293 status_ = ErrorGeneratingPixmap;
297 void Loader::Impl::startLoading()
299 if (status_ != WaitingToLoad)
302 LoaderQueue::get().touch(cached_item_);
306 } // namespace graphics