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 "GraphicsCache.h"
16 #include "GraphicsCacheItem.h"
17 #include "GraphicsImage.h"
18 #include "GraphicsParams.h"
19 #include "LoaderQueue.h"
21 #include "frontends/LyXView.h"
23 #include <boost/bind.hpp>
24 #include <boost/signals/trackable.hpp>
31 struct Loader::Impl : boost::signals::trackable {
37 void resetFile(string const &);
39 void resetParams(Params const &);
45 Params const & params() const { return params_; }
47 /// The loading status of the image.
49 /** Must store a copy of the cached item to ensure that it is not
50 * erased unexpectedly by the cache itself.
52 Cache::ItemPtr cached_item_;
53 /// We modify a local copy of the image once it is loaded.
54 Image::ImagePtr image_;
55 /// This signal is emitted when the image loading status changes.
56 boost::signal0<void> signal_;
62 void checkedLoading();
75 Loader::Loader(string const & file, DisplayType type)
82 Loader::Loader(string const & file, Params const & params)
89 Loader::Loader(Loader const & other)
92 Params const & params = other.pimpl_->params();
93 reset(params.filename, params);
101 Loader & Loader::operator=(Loader const & other)
103 if (this != &other) {
104 Params const & params = other.pimpl_->params();
105 reset(params.filename, params);
111 void Loader::reset(string const & file, DisplayType type) const
114 params.display = type;
115 pimpl_->resetParams(params);
117 pimpl_->resetFile(file);
118 pimpl_->createPixmap();
122 void Loader::reset(string const & file, Params const & params) const
124 pimpl_->resetParams(params);
125 pimpl_->resetFile(file);
126 pimpl_->createPixmap();
130 void Loader::reset(Params const & params) const
132 pimpl_->resetParams(params);
133 pimpl_->createPixmap();
137 void Loader::startLoading() const
139 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
141 pimpl_->startLoading();
145 void Loader::startMonitoring() const
147 if (!pimpl_->cached_item_.get())
150 pimpl_->cached_item_->startMonitoring();
154 bool Loader::monitoring() const
156 if (!pimpl_->cached_item_.get())
159 return pimpl_->cached_item_->monitoring();
163 unsigned long Loader::checksum() const
165 if (!pimpl_->cached_item_.get())
168 return pimpl_->cached_item_->checksum();
172 string const & Loader::filename() const
174 static string const empty;
175 return pimpl_->cached_item_.get() ?
176 pimpl_->cached_item_->filename() : empty;
180 ImageStatus Loader::status() const
182 return pimpl_->status_;
186 boost::signals::connection Loader::connect(slot_type const & slot) const
188 return pimpl_->signal_.connect(slot);
192 Image const * Loader::image() const
194 return pimpl_->image_.get();
199 : status_(WaitingToLoad)
204 Loader::Impl::~Impl()
210 void Loader::Impl::resetFile(string const & file)
212 string const old_file = cached_item_.get() ?
213 cached_item_->filename() : string();
215 if (file == old_file)
218 // If monitoring() the current file, should continue to monitor the
220 bool continue_monitoring = false;
222 if (!old_file.empty()) {
223 continue_monitoring = cached_item_->monitoring();
224 cached_item_.reset();
225 Cache::get().remove(old_file);
228 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
231 if (cached_item_.get() || file.empty())
234 Cache & gc = Cache::get();
235 if (!gc.inCache(file))
238 // We /must/ make a local copy of this.
239 cached_item_ = gc.item(file);
240 status_ = cached_item_->status();
242 if (continue_monitoring && !cached_item_->monitoring())
243 cached_item_->startMonitoring();
245 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
249 void Loader::Impl::resetParams(Params const & params)
251 if (params == params_)
255 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
260 void Loader::Impl::statusChanged()
262 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
268 void Loader::Impl::createPixmap()
270 if (!cached_item_.get() ||
271 params_.display == NoDisplay || status_ != Loaded)
274 image_.reset(cached_item_->image()->clone());
276 // These do nothing if there's nothing to do
277 image_->clip(params_);
278 image_->rotate(params_);
279 image_->scale(params_);
281 bool const success = image_->setPixmap(params_);
287 status_ = ErrorGeneratingPixmap;
291 void Loader::Impl::startLoading()
293 if (status_ != WaitingToLoad)
296 LoaderQueue::get().touch(cached_item_);
300 } // namespace graphics