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>
27 Impl(Loader &, GParams const &);
31 void setFile(string const & file);
41 /// The loading status of the image.
43 /** Must store a copy of the cached item to ensure that it is not
44 * erased unexpectedly by the cache itself.
49 /// We modify a local copy of the image once it is loaded.
54 Loader::Impl::Impl(Loader & parent, GParams const & params)
55 : parent_(parent), status_(WaitingToLoad), params_(params)
65 void Loader::Impl::setFile(string const & file)
70 GCache & gc = GCache::get();
71 if (!gc.inCache(file))
74 // We /must/ make a local copy of this.
75 graphic_ = gc.graphic(file);
76 status_ = graphic_->status();
78 if (status_ == Loaded) {
82 // It's easiest to do this without checking
83 parent_.statusChanged();
87 void Loader::Impl::unsetOldFile()
92 string const old_file = graphic_->filename();
94 GCache::get().remove(old_file);
96 status_ = WaitingToLoad;
102 void Loader::Impl::statusChanged()
104 status_ = graphic_->status();
105 if (status_ == Loaded)
108 parent_.statusChanged();
112 void Loader::Impl::createPixmap()
114 if (!graphic_.get() || image_.get() ||
115 params_.display == NoDisplay || status_ != Loaded)
118 image_.reset(graphic_->image()->clone());
120 // These do nothing if there's nothing to do
121 image_->clip(params_);
122 image_->rotate(params_);
123 image_->scale(params_);
125 bool const success = image_->setPixmap(params_);
131 status_ = ErrorGeneratingPixmap;
137 : pimpl_(new Impl(*this, GParams()))
141 Loader::Loader(string const & file, DisplayType type)
142 : pimpl_(new Impl(*this, GParams()))
144 pimpl_->params_.display = type;
145 pimpl_->setFile(file);
149 Loader::Loader(string const & file, GParams const & params)
150 : pimpl_(new Impl(*this, params))
152 pimpl_->setFile(file);
160 void Loader::reset(string const & file, DisplayType type)
162 pimpl_->unsetOldFile();
164 pimpl_->params_ = GParams();
165 pimpl_->params_.display = type;
166 pimpl_->setFile(file);
170 void Loader::reset(string const & file, GParams const & params)
172 pimpl_->unsetOldFile();
174 pimpl_->params_ = params;
175 pimpl_->setFile(file);
179 void Loader::reset(GParams const & params)
181 pimpl_->params_ = params;
183 if (pimpl_->status_ == Loaded)
184 pimpl_->createPixmap();
188 void Loader::startLoading()
190 if (pimpl_->status_ != WaitingToLoad || !pimpl_->graphic_.get())
193 pimpl_->graphic_->statusChanged.connect(
194 boost::bind(&Loader::Impl::statusChanged,
196 pimpl_->graphic_->startLoading();
200 string const & Loader::filename() const
202 static string const empty;
203 return pimpl_->graphic_.get() ? pimpl_->graphic_->filename() : empty;
207 ImageStatus Loader::status() const
209 return pimpl_->status_;
213 GImage const * Loader::image() const
215 return pimpl_->image_.get();