2 * \file GraphicsLoader.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming <leeming@lyx.org>
12 #pragma implementation
15 #include "GraphicsLoader.h"
16 #include "GraphicsCache.h"
17 #include "GraphicsCacheItem.h"
18 #include "GraphicsImage.h"
19 #include "GraphicsParams.h"
21 #include <boost/bind.hpp>
22 #include <boost/signals/trackable.hpp>
26 struct Loader::Impl : boost::signals::trackable {
28 Impl(Loader &, Params const &);
32 void resetFile(string const &);
34 void resetParams(Params const &);
38 /// The loading status of the image.
40 /** Must store a copy of the cached item to ensure that it is not
41 * erased unexpectedly by the cache itself.
43 Cache::ItemPtr cached_item_;
44 /// We modify a local copy of the image once it is loaded.
45 Image::ImagePtr image_;
58 Loader::Impl::Impl(Loader & parent, Params const & params)
59 : status_(WaitingToLoad), params_(params), parent_(parent)
69 void Loader::Impl::resetFile(string const & file)
71 string const old_file = cached_item_.get() ?
72 cached_item_->filename() : string();
77 if (!old_file.empty()) {
79 Cache::get().remove(old_file);
82 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
85 if (cached_item_.get() || file.empty())
88 Cache & gc = Cache::get();
89 if (!gc.inCache(file))
92 // We /must/ make a local copy of this.
93 cached_item_ = gc.item(file);
94 status_ = cached_item_->status();
96 cached_item_->statusChanged.connect(
97 boost::bind(&Impl::statusChanged, this));
101 void Loader::Impl::resetParams(Params const & params)
103 if (params == params_)
107 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
112 void Loader::Impl::statusChanged()
114 status_ = cached_item_.get() ? cached_item_->status() : WaitingToLoad;
116 parent_.statusChanged();
120 void Loader::Impl::createPixmap()
122 if (!cached_item_.get() || image_.get() ||
123 params_.display == NoDisplay || status_ != Loaded)
126 image_.reset(cached_item_->image()->clone());
128 // These do nothing if there's nothing to do
129 image_->clip(params_);
130 image_->rotate(params_);
131 image_->scale(params_);
133 bool const success = image_->setPixmap(params_);
139 status_ = ErrorGeneratingPixmap;
145 : pimpl_(new Impl(*this, Params()))
149 Loader::Loader(string const & file, DisplayType type)
150 : pimpl_(new Impl(*this, Params()))
156 Loader::Loader(string const & file, Params const & params)
157 : pimpl_(new Impl(*this, params))
167 void Loader::reset(string const & file, DisplayType type)
170 params.display = type;
171 pimpl_->resetParams(params);
173 pimpl_->resetFile(file);
174 pimpl_->createPixmap();
178 void Loader::reset(string const & file, Params const & params)
180 pimpl_->resetParams(params);
181 pimpl_->resetFile(file);
182 pimpl_->createPixmap();
186 void Loader::reset(Params const & params)
188 pimpl_->resetParams(params);
189 pimpl_->createPixmap();
193 void Loader::startLoading()
195 if (pimpl_->status_ != WaitingToLoad || !pimpl_->cached_item_.get())
197 pimpl_->cached_item_->startLoading();
201 string const & Loader::filename() const
203 static string const empty;
204 return pimpl_->cached_item_.get() ?
205 pimpl_->cached_item_->filename() : empty;
209 ImageStatus Loader::status() const
211 return pimpl_->status_;
215 Image const * Loader::image() const
217 return pimpl_->image_.get();