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>
30 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::signal0<void> signal_;
61 void checkedLoading();
70 : pimpl_(new Impl(Params()))
74 Loader::Loader(string const & file, DisplayType type)
75 : pimpl_(new Impl(Params()))
81 Loader::Loader(string const & file, Params const & params)
82 : pimpl_(new Impl(Params()))
88 Loader::Loader(Loader const & other)
89 : pimpl_(new Impl(Params()))
91 Params const & params = other.pimpl_->params();
92 reset(params.filename, params);
100 void Loader::reset(string const & file, DisplayType type) const
103 params.display = type;
104 pimpl_->resetParams(params);
106 pimpl_->resetFile(file);
107 pimpl_->createPixmap();
111 void Loader::reset(string const & file, Params const & params) const
113 pimpl_->resetParams(params);
114 pimpl_->resetFile(file);
115 pimpl_->createPixmap();
119 void Loader::reset(Params const & params) const
121 pimpl_->resetParams(params);
122 pimpl_->createPixmap();
126 void Loader::startLoading() const
128 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
130 pimpl_->startLoading();
134 void Loader::startMonitoring() const
136 if (!pimpl_->cached_item_.get())
139 pimpl_->cached_item_->startMonitoring();
143 bool Loader::monitoring() const
145 if (!pimpl_->cached_item_.get())
148 return pimpl_->cached_item_->monitoring();
152 unsigned long Loader::checksum() const
154 if (!pimpl_->cached_item_.get())
157 return pimpl_->cached_item_->checksum();
161 string const & Loader::filename() const
163 static string const empty;
164 return pimpl_->cached_item_.get() ?
165 pimpl_->cached_item_->filename() : empty;
169 ImageStatus Loader::status() const
171 return pimpl_->status_;
175 boost::signals::connection Loader::connect(slot_type const & slot) const
177 return pimpl_->signal_.connect(slot);
181 Image const * Loader::image() const
183 return pimpl_->image_.get();
187 Loader::Impl::Impl(Params const & params)
188 : status_(WaitingToLoad), params_(params)
193 Loader::Impl::~Impl()
199 void Loader::Impl::resetFile(string const & file)
201 string const old_file = cached_item_.get() ?
202 cached_item_->filename() : string();
204 if (file == old_file)
207 // If monitoring() the current file, should continue to monitor the
209 bool continue_monitoring = false;
211 if (!old_file.empty()) {
212 continue_monitoring = cached_item_->monitoring();
213 cached_item_.reset();
214 Cache::get().remove(old_file);
217 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
220 if (cached_item_.get() || file.empty())
223 Cache & gc = Cache::get();
224 if (!gc.inCache(file))
227 // We /must/ make a local copy of this.
228 cached_item_ = gc.item(file);
229 status_ = cached_item_->status();
231 if (continue_monitoring && !cached_item_->monitoring())
232 cached_item_->startMonitoring();
234 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
238 void Loader::Impl::resetParams(Params const & params)
240 if (params == params_)
244 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
249 void Loader::Impl::statusChanged()
251 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
257 void Loader::Impl::createPixmap()
259 if (!cached_item_.get() ||
260 params_.display == NoDisplay || status_ != Loaded)
263 image_.reset(cached_item_->image()->clone());
265 // These do nothing if there's nothing to do
266 image_->clip(params_);
267 image_->rotate(params_);
268 image_->scale(params_);
270 bool const success = image_->setPixmap(params_);
276 status_ = ErrorGeneratingPixmap;
280 void Loader::Impl::startLoading()
282 if (status_ != WaitingToLoad)
285 LoaderQueue::get().touch(cached_item_);