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 "BufferView.h"
17 #include "GraphicsCache.h"
18 #include "GraphicsCacheItem.h"
19 #include "GraphicsImage.h"
20 #include "GraphicsParams.h"
21 #include "LoaderQueue.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Timeout.h"
26 #include <boost/weak_ptr.hpp>
27 #include <boost/bind.hpp>
28 #include <boost/signals/trackable.hpp>
34 struct Loader::Impl : boost::signals::trackable {
40 void resetFile(string const &);
42 void resetParams(Params const &);
47 void startLoading(Inset const &, BufferView const &);
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::signal0<void> signal_;
64 void checkedLoading();
69 // Multiple Insets can share the same image
70 typedef std::list<Inset const *> InsetList;
74 boost::weak_ptr<BufferView const> view;
79 : pimpl_(new Impl(Params()))
83 Loader::Loader(string const & file, DisplayType type)
84 : pimpl_(new Impl(Params()))
90 Loader::Loader(string const & file, Params const & params)
91 : pimpl_(new Impl(params))
101 void Loader::reset(string const & file, DisplayType type) const
104 params.display = type;
105 pimpl_->resetParams(params);
107 pimpl_->resetFile(file);
108 pimpl_->createPixmap();
112 void Loader::reset(string const & file, Params const & params) const
114 pimpl_->resetParams(params);
115 pimpl_->resetFile(file);
116 pimpl_->createPixmap();
120 void Loader::reset(Params const & params) const
122 pimpl_->resetParams(params);
123 pimpl_->createPixmap();
127 void Loader::startLoading() const
129 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
131 pimpl_->cached_item_->startLoading();
135 void Loader::startLoading(Inset const & inset, BufferView const & bv) const
137 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
139 pimpl_->startLoading(inset, bv);
143 void Loader::startMonitoring() const
145 if (!pimpl_->cached_item_.get())
148 pimpl_->cached_item_->startMonitoring();
152 bool Loader::monitoring() const
154 if (!pimpl_->cached_item_.get())
157 return pimpl_->cached_item_->monitoring();
161 unsigned long Loader::checksum() const
163 if (!pimpl_->cached_item_.get())
166 return pimpl_->cached_item_->checksum();
170 string const & Loader::filename() const
172 static string const empty;
173 return pimpl_->cached_item_.get() ?
174 pimpl_->cached_item_->filename() : empty;
178 ImageStatus Loader::status() const
180 return pimpl_->status_;
184 boost::signals::connection Loader::connect(slot_type const & slot) const
186 return pimpl_->signal_.connect(slot);
190 Image const * Loader::image() const
192 return pimpl_->image_.get();
196 Loader::Impl::Impl(Params const & params)
197 : status_(WaitingToLoad), params_(params)
202 Loader::Impl::~Impl()
208 void Loader::Impl::resetFile(string const & file)
210 string const old_file = cached_item_.get() ?
211 cached_item_->filename() : string();
213 if (file == old_file)
216 // If monitoring() the current file, should continue to monitor the
218 bool continue_monitoring = false;
220 if (!old_file.empty()) {
221 continue_monitoring = cached_item_->monitoring();
222 cached_item_.reset();
223 Cache::get().remove(old_file);
226 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
229 if (cached_item_.get() || file.empty())
232 Cache & gc = Cache::get();
233 if (!gc.inCache(file))
236 // We /must/ make a local copy of this.
237 cached_item_ = gc.item(file);
238 status_ = cached_item_->status();
240 if (continue_monitoring && !cached_item_->monitoring())
241 cached_item_->startMonitoring();
243 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
247 void Loader::Impl::resetParams(Params const & params)
249 if (params == params_)
253 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
258 void Loader::Impl::statusChanged()
260 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
266 void Loader::Impl::createPixmap()
268 if (!cached_item_.get() ||
269 params_.display == NoDisplay || status_ != Loaded)
272 image_.reset(cached_item_->image()->clone());
274 // These do nothing if there's nothing to do
275 image_->clip(params_);
276 image_->rotate(params_);
277 image_->scale(params_);
279 bool const success = image_->setPixmap(params_);
285 status_ = ErrorGeneratingPixmap;
290 void Loader::Impl::startLoading(Inset const & inset, BufferView const & bv)
292 if (status_ != WaitingToLoad)
295 InsetList::const_iterator it = insets.begin();
296 InsetList::const_iterator end = insets.end();
297 it = std::find(it, end, &inset);
299 insets.push_back(&inset);
300 view = bv.owner()->view();
302 LoaderQueue::get().touch(cached_item_);