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 &);
43 void startLoading(Inset const &);
45 /// The loading status of the image.
47 /** Must store a copy of the cached item to ensure that it is not
48 * erased unexpectedly by the cache itself.
50 Cache::ItemPtr cached_item_;
51 /// We modify a local copy of the image once it is loaded.
52 Image::ImagePtr image_;
53 /// This signal is emitted when the image loading status changes.
54 boost::signal0<void> signal_;
60 void checkedLoading();
65 // Multiple Insets can share the same image
66 typedef std::list<Inset const *> InsetList;
73 : pimpl_(new Impl(Params()))
77 Loader::Loader(string const & file, DisplayType type)
78 : pimpl_(new Impl(Params()))
84 Loader::Loader(string const & file, Params const & params)
85 : pimpl_(new Impl(params))
95 void Loader::reset(string const & file, DisplayType type) const
98 params.display = type;
99 pimpl_->resetParams(params);
101 pimpl_->resetFile(file);
102 pimpl_->createPixmap();
106 void Loader::reset(string const & file, Params const & params) const
108 pimpl_->resetParams(params);
109 pimpl_->resetFile(file);
110 pimpl_->createPixmap();
114 void Loader::reset(Params const & params) const
116 pimpl_->resetParams(params);
117 pimpl_->createPixmap();
121 void Loader::startLoading() const
123 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
125 pimpl_->cached_item_->startLoading();
129 void Loader::startLoading(Inset const & inset) const
131 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
133 pimpl_->startLoading(inset);
137 void Loader::startMonitoring() const
139 if (!pimpl_->cached_item_.get())
142 pimpl_->cached_item_->startMonitoring();
146 bool Loader::monitoring() const
148 if (!pimpl_->cached_item_.get())
151 return pimpl_->cached_item_->monitoring();
155 unsigned long Loader::checksum() const
157 if (!pimpl_->cached_item_.get())
160 return pimpl_->cached_item_->checksum();
164 string const & Loader::filename() const
166 static string const empty;
167 return pimpl_->cached_item_.get() ?
168 pimpl_->cached_item_->filename() : empty;
172 ImageStatus Loader::status() const
174 return pimpl_->status_;
178 boost::signals::connection Loader::connect(slot_type const & slot) const
180 return pimpl_->signal_.connect(slot);
184 Image const * Loader::image() const
186 return pimpl_->image_.get();
190 Loader::Impl::Impl(Params const & params)
191 : status_(WaitingToLoad), params_(params)
196 Loader::Impl::~Impl()
202 void Loader::Impl::resetFile(string const & file)
204 string const old_file = cached_item_.get() ?
205 cached_item_->filename() : string();
207 if (file == old_file)
210 // If monitoring() the current file, should continue to monitor the
212 bool continue_monitoring = false;
214 if (!old_file.empty()) {
215 continue_monitoring = cached_item_->monitoring();
216 cached_item_.reset();
217 Cache::get().remove(old_file);
220 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
223 if (cached_item_.get() || file.empty())
226 Cache & gc = Cache::get();
227 if (!gc.inCache(file))
230 // We /must/ make a local copy of this.
231 cached_item_ = gc.item(file);
232 status_ = cached_item_->status();
234 if (continue_monitoring && !cached_item_->monitoring())
235 cached_item_->startMonitoring();
237 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
241 void Loader::Impl::resetParams(Params const & params)
243 if (params == params_)
247 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
252 void Loader::Impl::statusChanged()
254 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
260 void Loader::Impl::createPixmap()
262 if (!cached_item_.get() ||
263 params_.display == NoDisplay || status_ != Loaded)
266 image_.reset(cached_item_->image()->clone());
268 // These do nothing if there's nothing to do
269 image_->clip(params_);
270 image_->rotate(params_);
271 image_->scale(params_);
273 bool const success = image_->setPixmap(params_);
279 status_ = ErrorGeneratingPixmap;
284 void Loader::Impl::startLoading(Inset const & inset)
286 if (status_ != WaitingToLoad)
289 InsetList::const_iterator it = insets.begin();
290 InsetList::const_iterator end = insets.end();
291 it = std::find(it, end, &inset);
293 insets.push_back(&inset);
295 LoaderQueue::get().touch(cached_item_);