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 &);
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();
69 : pimpl_(new Impl(Params()))
73 Loader::Loader(string const & file, DisplayType type)
74 : pimpl_(new Impl(Params()))
80 Loader::Loader(string const & file, Params const & params)
81 : pimpl_(new Impl(params))
91 void Loader::reset(string const & file, DisplayType type) const
94 params.display = type;
95 pimpl_->resetParams(params);
97 pimpl_->resetFile(file);
98 pimpl_->createPixmap();
102 void Loader::reset(string const & file, Params const & params) const
104 pimpl_->resetParams(params);
105 pimpl_->resetFile(file);
106 pimpl_->createPixmap();
110 void Loader::reset(Params const & params) const
112 pimpl_->resetParams(params);
113 pimpl_->createPixmap();
117 void Loader::startLoading() const
119 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
121 pimpl_->startLoading();
125 void Loader::startMonitoring() const
127 if (!pimpl_->cached_item_.get())
130 pimpl_->cached_item_->startMonitoring();
134 bool Loader::monitoring() const
136 if (!pimpl_->cached_item_.get())
139 return pimpl_->cached_item_->monitoring();
143 unsigned long Loader::checksum() const
145 if (!pimpl_->cached_item_.get())
148 return pimpl_->cached_item_->checksum();
152 string const & Loader::filename() const
154 static string const empty;
155 return pimpl_->cached_item_.get() ?
156 pimpl_->cached_item_->filename() : empty;
160 ImageStatus Loader::status() const
162 return pimpl_->status_;
166 boost::signals::connection Loader::connect(slot_type const & slot) const
168 return pimpl_->signal_.connect(slot);
172 Image const * Loader::image() const
174 return pimpl_->image_.get();
178 Loader::Impl::Impl(Params const & params)
179 : status_(WaitingToLoad), params_(params)
184 Loader::Impl::~Impl()
190 void Loader::Impl::resetFile(string const & file)
192 string const old_file = cached_item_.get() ?
193 cached_item_->filename() : string();
195 if (file == old_file)
198 // If monitoring() the current file, should continue to monitor the
200 bool continue_monitoring = false;
202 if (!old_file.empty()) {
203 continue_monitoring = cached_item_->monitoring();
204 cached_item_.reset();
205 Cache::get().remove(old_file);
208 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
211 if (cached_item_.get() || file.empty())
214 Cache & gc = Cache::get();
215 if (!gc.inCache(file))
218 // We /must/ make a local copy of this.
219 cached_item_ = gc.item(file);
220 status_ = cached_item_->status();
222 if (continue_monitoring && !cached_item_->monitoring())
223 cached_item_->startMonitoring();
225 cached_item_->connect(boost::bind(&Impl::statusChanged, this));
229 void Loader::Impl::resetParams(Params const & params)
231 if (params == params_)
235 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
240 void Loader::Impl::statusChanged()
242 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
248 void Loader::Impl::createPixmap()
250 if (!cached_item_.get() ||
251 params_.display == NoDisplay || status_ != Loaded)
254 image_.reset(cached_item_->image()->clone());
256 // These do nothing if there's nothing to do
257 image_->clip(params_);
258 image_->rotate(params_);
259 image_->scale(params_);
261 bool const success = image_->setPixmap(params_);
267 status_ = ErrorGeneratingPixmap;
272 void Loader::Impl::startLoading()
274 if (status_ != WaitingToLoad)
277 LoaderQueue::get().touch(cached_item_);