3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming <leeming@lyx.org>
12 #pragma implementation
15 #include "PreviewImage.h"
16 #include "PreviewLoader.h"
17 #include "GraphicsImage.h"
18 #include "GraphicsLoader.h"
20 #include "support/lyxlib.h"
22 #include <boost/bind.hpp>
23 #include <boost/signals/trackable.hpp>
28 struct PreviewImage::Impl : public boost::signals::trackable {
30 Impl(PreviewImage & p, PreviewLoader & l,
31 string const & s, string const & f, double af);
35 Image const * image(Inset const &, BufferView const &);
40 PreviewImage const & parent_;
42 PreviewLoader & ploader_;
46 string const snippet_;
48 double const ascent_frac_;
52 PreviewImage::PreviewImage(PreviewLoader & l,
56 : pimpl_(new Impl(*this, l, s, f, af))
60 PreviewImage::~PreviewImage()
64 string const & PreviewImage::snippet() const
66 return pimpl_->snippet_;
70 int PreviewImage::ascent() const
72 Image const * const image = pimpl_->iloader_.image();
76 return int(pimpl_->ascent_frac_ * double(image->getHeight()));
80 int PreviewImage::descent() const
82 Image const * const image = pimpl_->iloader_.image();
86 // Avoids rounding errors.
87 return image->getHeight() - ascent();
91 int PreviewImage::width() const
93 Image const * const image = pimpl_->iloader_.image();
94 return image ? image->getWidth() : 0;
98 Image const * PreviewImage::image(Inset const & inset,
99 BufferView const & bv) const
101 return pimpl_->image(inset, bv);
105 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
109 : parent_(p), ploader_(l), iloader_(bf),
110 snippet_(s), ascent_frac_(af)
112 iloader_.statusChanged.connect(
113 boost::bind(&Impl::statusChanged, this));
117 PreviewImage::Impl::~Impl()
119 lyx::unlink(iloader_.filename());
123 Image const * PreviewImage::Impl::image(Inset const & inset,
124 BufferView const & bv)
126 if (iloader_.status() == WaitingToLoad)
127 iloader_.startLoading(inset, bv);
129 return iloader_.image();
133 void PreviewImage::Impl::statusChanged()
135 switch (iloader_.status()) {
144 case ErrorConverting:
146 case ErrorGeneratingPixmap:
148 //lyx::unlink(iloader_.filename());
149 ploader_.remove(snippet_);
153 lyx::unlink(iloader_.filename());
154 ploader_.imageReady(parent_);