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();
70 : pimpl_(new Impl(Params()))
74 Loader::Loader(string const & file, DisplayType type)
75 : pimpl_(new Impl(Params()))
81 Loader::Loader(string const & file, Params const & params)
82 : pimpl_(new Impl(params))
88 Loader::Loader(Loader const & other)
89 : pimpl_(new Impl(other.pimpl_->params()))
97 void Loader::reset(string const & file, DisplayType type) const
100 params.display = type;
101 pimpl_->resetParams(params);
103 pimpl_->resetFile(file);
104 pimpl_->createPixmap();
108 void Loader::reset(string const & file, Params const & params) const
110 pimpl_->resetParams(params);
111 pimpl_->resetFile(file);
112 pimpl_->createPixmap();
116 void Loader::reset(Params const & params) const
118 pimpl_->resetParams(params);
119 pimpl_->createPixmap();
123 void Loader::startLoading() const
125 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
127 pimpl_->startLoading();
131 void Loader::startMonitoring() const
133 if (!pimpl_->cached_item_.get())
136 pimpl_->cached_item_->startMonitoring();
140 bool Loader::monitoring() const
142 if (!pimpl_->cached_item_.get())
145 return pimpl_->cached_item_->monitoring();
149 unsigned long Loader::checksum() const
151 if (!pimpl_->cached_item_.get())
154 return pimpl_->cached_item_->checksum();
158 string const & Loader::filename() const
160 static string const empty;
161 return pimpl_->cached_item_.get() ?
162 pimpl_->cached_item_->filename() : empty;
166 ImageStatus Loader::status() const
168 return pimpl_->status_;
172 boost::signals::connection Loader::connect(slot_type const & slot) const
174 return pimpl_->signal_.connect(slot);
178 Image const * Loader::image() const
180 return pimpl_->image_.get();
184 Loader::Impl::Impl(Params const & params)
185 : status_(WaitingToLoad), params_(params)
190 Loader::Impl::~Impl()
196 void Loader::Impl::resetFile(string const & file)
198 string const old_file = cached_item_.get() ?
199 cached_item_->filename() : string();
201 if (file == old_file)
204 // If monitoring() the current file, should continue to monitor the
206 bool continue_monitoring = false;
208 if (!old_file.empty()) {
209 continue_monitoring = cached_item_->monitoring();
210 cached_item_.reset();
211 Cache::get().remove(old_file);
214 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
217 if (cached_item_.get() || file.empty())
220 Cache & gc = Cache::get();
221 if (!gc.inCache(file))
224 // We /must/ make a local copy of this.
225 cached_item_ = gc.item(file);
226 status_ = cached_item_->status();
228 if (continue_monitoring && !cached_item_->monitoring())
229 cached_item_->startMonitoring();
231 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
235 void Loader::Impl::resetParams(Params const & params)
237 if (params == params_)
241 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
246 void Loader::Impl::statusChanged()
248 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
254 void Loader::Impl::createPixmap()
256 if (!cached_item_.get() ||
257 params_.display == NoDisplay || status_ != Loaded)
260 image_.reset(cached_item_->image()->clone());
262 // These do nothing if there's nothing to do
263 image_->clip(params_);
264 image_->rotate(params_);
265 image_->scale(params_);
267 bool const success = image_->setPixmap(params_);
273 status_ = ErrorGeneratingPixmap;
278 void Loader::Impl::startLoading()
280 if (status_ != WaitingToLoad)
283 LoaderQueue::get().touch(cached_item_);