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();
74 Loader::Loader(string const & file, DisplayType type)
81 Loader::Loader(string const & file, Params const & params)
88 Loader::Loader(Loader const & other)
91 Params const & params = other.pimpl_->params();
92 reset(params.filename, params);
100 Loader & Loader::operator=(Loader const & other)
102 if (this != &other) {
103 Params const & params = other.pimpl_->params();
104 reset(params.filename, params);
110 void Loader::reset(string const & file, DisplayType type) const
113 params.display = type;
114 pimpl_->resetParams(params);
116 pimpl_->resetFile(file);
117 pimpl_->createPixmap();
121 void Loader::reset(string const & file, Params const & params) const
123 pimpl_->resetParams(params);
124 pimpl_->resetFile(file);
125 pimpl_->createPixmap();
129 void Loader::reset(Params const & params) const
131 pimpl_->resetParams(params);
132 pimpl_->createPixmap();
136 void Loader::startLoading() const
138 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
140 pimpl_->startLoading();
144 void Loader::startMonitoring() const
146 if (!pimpl_->cached_item_.get())
149 pimpl_->cached_item_->startMonitoring();
153 bool Loader::monitoring() const
155 if (!pimpl_->cached_item_.get())
158 return pimpl_->cached_item_->monitoring();
162 unsigned long Loader::checksum() const
164 if (!pimpl_->cached_item_.get())
167 return pimpl_->cached_item_->checksum();
171 string const & Loader::filename() const
173 static string const empty;
174 return pimpl_->cached_item_.get() ?
175 pimpl_->cached_item_->filename() : empty;
179 ImageStatus Loader::status() const
181 return pimpl_->status_;
185 boost::signals::connection Loader::connect(slot_type const & slot) const
187 return pimpl_->signal_.connect(slot);
191 Image const * Loader::image() const
193 return pimpl_->image_.get();
198 : status_(WaitingToLoad)
203 Loader::Impl::~Impl()
209 void Loader::Impl::resetFile(string const & file)
211 string const old_file = cached_item_.get() ?
212 cached_item_->filename() : string();
214 if (file == old_file)
217 // If monitoring() the current file, should continue to monitor the
219 bool continue_monitoring = false;
221 if (!old_file.empty()) {
222 continue_monitoring = cached_item_->monitoring();
223 cached_item_.reset();
224 Cache::get().remove(old_file);
227 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
230 if (cached_item_.get() || file.empty())
233 Cache & gc = Cache::get();
234 if (!gc.inCache(file))
237 // We /must/ make a local copy of this.
238 cached_item_ = gc.item(file);
239 status_ = cached_item_->status();
241 if (continue_monitoring && !cached_item_->monitoring())
242 cached_item_->startMonitoring();
244 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
248 void Loader::Impl::resetParams(Params const & params)
250 if (params == params_)
254 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
259 void Loader::Impl::statusChanged()
261 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
267 void Loader::Impl::createPixmap()
269 if (!cached_item_.get() ||
270 params_.display == NoDisplay || status_ != Loaded)
273 image_.reset(cached_item_->image()->clone());
275 // These do nothing if there's nothing to do
276 image_->clip(params_);
277 image_->rotate(params_);
278 image_->scale(params_);
280 bool const success = image_->setPixmap(params_);
286 status_ = ErrorGeneratingPixmap;
290 void Loader::Impl::startLoading()
292 if (status_ != WaitingToLoad)
295 LoaderQueue::get().touch(cached_item_);