2 * \file GraphicsLoader.C
3 * Read the file COPYING
5 * \author Angus Leeming
7 * Full author contact details are available in file CREDITS
13 #pragma implementation
16 #include "GraphicsLoader.h"
18 #include "BufferView.h"
20 #include "GraphicsCache.h"
21 #include "GraphicsCacheItem.h"
22 #include "GraphicsImage.h"
23 #include "GraphicsParams.h"
24 #include "GraphicsSupport.h"
26 #include "frontends/LyXView.h"
27 #include "frontends/Timeout.h"
29 #include <boost/weak_ptr.hpp>
30 #include <boost/bind.hpp>
31 #include <boost/signals/trackable.hpp>
37 struct Loader::Impl : boost::signals::trackable {
43 void resetFile(string const &);
45 void resetParams(Params const &);
50 void startLoading(Inset const &, BufferView const &);
52 /// The loading status of the image.
54 /** Must store a copy of the cached item to ensure that it is not
55 * erased unexpectedly by the cache itself.
57 Cache::ItemPtr cached_item_;
58 /// We modify a local copy of the image once it is loaded.
59 Image::ImagePtr image_;
60 /// This signal is emitted when the image loading status changes.
61 boost::signal0<void> signal_;
67 void checkedLoading();
74 // Multiple Insets can share the same image
75 typedef std::list<Inset const *> InsetList;
79 boost::weak_ptr<BufferView const> view;
84 : pimpl_(new Impl(Params()))
88 Loader::Loader(string const & file, DisplayType type)
89 : pimpl_(new Impl(Params()))
95 Loader::Loader(string const & file, Params const & params)
96 : pimpl_(new Impl(params))
106 void Loader::reset(string const & file, DisplayType type) const
109 params.display = type;
110 pimpl_->resetParams(params);
112 pimpl_->resetFile(file);
113 pimpl_->createPixmap();
117 void Loader::reset(string const & file, Params const & params) const
119 pimpl_->resetParams(params);
120 pimpl_->resetFile(file);
121 pimpl_->createPixmap();
125 void Loader::reset(Params const & params) const
127 pimpl_->resetParams(params);
128 pimpl_->createPixmap();
132 void Loader::startLoading() const
134 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
136 pimpl_->cached_item_->startLoading();
140 void Loader::startLoading(Inset const & inset, BufferView const & bv) const
142 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
144 pimpl_->startLoading(inset, bv);
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 string const & Loader::filename() const
177 static string 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();
201 Loader::Impl::Impl(Params const & params)
202 : status_(WaitingToLoad), params_(params),
203 timer(2000, Timeout::ONETIME)
205 timer.timeout.connect(boost::bind(&Impl::checkedLoading, this));
209 Loader::Impl::~Impl()
215 void Loader::Impl::resetFile(string const & file)
217 string const old_file = cached_item_.get() ?
218 cached_item_->filename() : string();
220 if (file == old_file)
223 // If monitoring() the current file, should continue to monitor the
225 bool continue_monitoring = false;
227 if (!old_file.empty()) {
228 continue_monitoring = cached_item_->monitoring();
229 cached_item_.reset();
230 Cache::get().remove(old_file);
233 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
236 if (cached_item_.get() || file.empty())
239 Cache & gc = Cache::get();
240 if (!gc.inCache(file))
243 // We /must/ make a local copy of this.
244 cached_item_ = gc.item(file);
245 status_ = cached_item_->status();
247 if (continue_monitoring && !cached_item_->monitoring())
248 cached_item_->startMonitoring();
250 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
254 void Loader::Impl::resetParams(Params const & params)
256 if (params == params_)
260 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
265 void Loader::Impl::statusChanged()
267 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
273 void Loader::Impl::createPixmap()
275 if (!cached_item_.get() ||
276 params_.display == NoDisplay || status_ != Loaded)
279 image_.reset(cached_item_->image()->clone());
281 // These do nothing if there's nothing to do
282 image_->clip(params_);
283 image_->rotate(params_);
284 image_->scale(params_);
286 bool const success = image_->setPixmap(params_);
292 status_ = ErrorGeneratingPixmap;
297 void Loader::Impl::startLoading(Inset const & inset, BufferView const & bv)
299 if (status_ != WaitingToLoad || timer.running())
302 InsetList::const_iterator it = insets.begin();
303 InsetList::const_iterator end = insets.end();
304 it = std::find(it, end, &inset);
306 insets.push_back(&inset);
307 view = bv.owner()->view();
315 struct FindVisibleInset {
317 FindVisibleInset(std::list<VisibleParagraph> const & vps) : vps_(vps) {}
319 bool operator()(Inset const * inset_ptr)
323 return isInsetVisible(*inset_ptr, vps_);
327 std::list<VisibleParagraph> const & vps_;
333 void Loader::Impl::checkedLoading()
335 if (insets.empty() || !view.get())
338 std::list<VisibleParagraph> const vps =
339 getVisibleParagraphs(*view.get());
341 InsetList::const_iterator it = insets.begin();
342 InsetList::const_iterator end = insets.end();
344 it = std::find_if(it, end, FindVisibleInset(vps));
346 // One of the insets is visible, so start loading the image.
348 cached_item_->startLoading();