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
14 #pragma implementation
17 #include "GraphicsLoader.h"
19 #include "BufferView.h"
21 #include "GraphicsCache.h"
22 #include "GraphicsCacheItem.h"
23 #include "GraphicsImage.h"
24 #include "GraphicsParams.h"
25 #include "GraphicsSupport.h"
27 #include "frontends/LyXView.h"
28 #include "frontends/Timeout.h"
30 #include <boost/weak_ptr.hpp>
31 #include <boost/bind.hpp>
32 #include <boost/signals/trackable.hpp>
38 struct Loader::Impl : boost::signals::trackable {
44 void resetFile(string const &);
46 void resetParams(Params const &);
51 void startLoading(Inset const &, BufferView const &);
53 /// The loading status of the image.
55 /** Must store a copy of the cached item to ensure that it is not
56 * erased unexpectedly by the cache itself.
58 Cache::ItemPtr cached_item_;
59 /// We modify a local copy of the image once it is loaded.
60 Image::ImagePtr image_;
61 /// This signal is emitted when the image loading status changes.
62 boost::signal0<void> signal_;
68 void checkedLoading();
75 // Multiple Insets can share the same image
76 typedef std::list<Inset const *> InsetList;
80 boost::weak_ptr<BufferView const> view;
85 : pimpl_(new Impl(Params()))
89 Loader::Loader(string const & file, DisplayType type)
90 : pimpl_(new Impl(Params()))
96 Loader::Loader(string const & file, Params const & params)
97 : pimpl_(new Impl(params))
107 void Loader::reset(string const & file, DisplayType type) const
110 params.display = type;
111 pimpl_->resetParams(params);
113 pimpl_->resetFile(file);
114 pimpl_->createPixmap();
118 void Loader::reset(string const & file, Params const & params) const
120 pimpl_->resetParams(params);
121 pimpl_->resetFile(file);
122 pimpl_->createPixmap();
126 void Loader::reset(Params const & params) const
128 pimpl_->resetParams(params);
129 pimpl_->createPixmap();
133 void Loader::startLoading() const
135 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
137 pimpl_->cached_item_->startLoading();
141 void Loader::startLoading(Inset const & inset, BufferView const & bv) const
143 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
145 pimpl_->startLoading(inset, bv);
149 void Loader::startMonitoring() const
151 if (!pimpl_->cached_item_.get())
154 pimpl_->cached_item_->startMonitoring();
158 bool Loader::monitoring() const
160 if (!pimpl_->cached_item_.get())
163 return pimpl_->cached_item_->monitoring();
167 unsigned long Loader::checksum() const
169 if (!pimpl_->cached_item_.get())
172 return pimpl_->cached_item_->checksum();
176 string const & Loader::filename() const
178 static string const empty;
179 return pimpl_->cached_item_.get() ?
180 pimpl_->cached_item_->filename() : empty;
184 ImageStatus Loader::status() const
186 return pimpl_->status_;
190 boost::signals::connection Loader::connect(slot_type const & slot) const
192 return pimpl_->signal_.connect(slot);
196 Image const * Loader::image() const
198 return pimpl_->image_.get();
202 Loader::Impl::Impl(Params const & params)
203 : status_(WaitingToLoad), params_(params),
204 timer(2000, Timeout::ONETIME)
206 timer.timeout.connect(boost::bind(&Impl::checkedLoading, this));
210 Loader::Impl::~Impl()
216 void Loader::Impl::resetFile(string const & file)
218 string const old_file = cached_item_.get() ?
219 cached_item_->filename() : string();
221 if (file == old_file)
224 // If monitoring() the current file, should continue to monitor the
226 bool continue_monitoring = false;
228 if (!old_file.empty()) {
229 continue_monitoring = cached_item_->monitoring();
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 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;
298 void Loader::Impl::startLoading(Inset const & inset, BufferView const & bv)
300 if (status_ != WaitingToLoad || timer.running())
303 InsetList::const_iterator it = insets.begin();
304 InsetList::const_iterator end = insets.end();
305 it = std::find(it, end, &inset);
307 insets.push_back(&inset);
308 view = bv.owner()->view();
316 struct FindVisibleInset {
318 FindVisibleInset(std::list<VisibleParagraph> const & vps) : vps_(vps) {}
320 bool operator()(Inset const * inset_ptr)
324 return isInsetVisible(*inset_ptr, vps_);
328 std::list<VisibleParagraph> const & vps_;
334 void Loader::Impl::checkedLoading()
336 if (insets.empty() || !view.get())
339 std::list<VisibleParagraph> const vps =
340 getVisibleParagraphs(*view.get());
342 InsetList::const_iterator it = insets.begin();
343 InsetList::const_iterator end = insets.end();
345 it = std::find_if(it, end, FindVisibleInset(vps));
347 // One of the insets is visible, so start loading the image.
349 cached_item_->startLoading();