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
14 #pragma implementation
17 #include "PreviewImage.h"
18 #include "PreviewLoader.h"
19 #include "GraphicsImage.h"
20 #include "GraphicsLoader.h"
22 #include "support/lyxlib.h"
24 #include <boost/bind.hpp>
25 #include <boost/signals/trackable.hpp>
30 struct PreviewImage::Impl : public boost::signals::trackable {
32 Impl(PreviewImage & p, PreviewLoader & l,
33 string const & s, string const & f, double af);
37 Image const * image(Inset const &, BufferView const &);
42 PreviewImage const & parent_;
44 PreviewLoader & ploader_;
48 string const snippet_;
50 double const ascent_frac_;
54 PreviewImage::PreviewImage(PreviewLoader & l,
58 : pimpl_(new Impl(*this, l, s, f, af))
62 PreviewImage::~PreviewImage()
66 string const & PreviewImage::snippet() const
68 return pimpl_->snippet_;
72 int PreviewImage::ascent() const
74 Image const * const image = pimpl_->iloader_.image();
78 return int(pimpl_->ascent_frac_ * double(image->getHeight()));
82 int PreviewImage::descent() const
84 Image const * const image = pimpl_->iloader_.image();
88 // Avoids rounding errors.
89 return image->getHeight() - ascent();
93 int PreviewImage::width() const
95 Image const * const image = pimpl_->iloader_.image();
96 return image ? image->getWidth() : 0;
100 Image const * PreviewImage::image(Inset const & inset,
101 BufferView const & bv) const
103 return pimpl_->image(inset, bv);
107 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
111 : parent_(p), ploader_(l), iloader_(bf),
112 snippet_(s), ascent_frac_(af)
114 iloader_.connect(boost::bind(&Impl::statusChanged, this));
118 PreviewImage::Impl::~Impl()
120 lyx::unlink(iloader_.filename());
124 Image const * PreviewImage::Impl::image(Inset const & inset,
125 BufferView const & bv)
127 if (iloader_.status() == WaitingToLoad)
128 iloader_.startLoading(inset, bv);
130 return iloader_.image();
134 void PreviewImage::Impl::statusChanged()
136 switch (iloader_.status()) {
145 case ErrorConverting:
147 case ErrorGeneratingPixmap:
149 //lyx::unlink(iloader_.filename());
150 ploader_.remove(snippet_);
154 lyx::unlink(iloader_.filename());
155 ploader_.emitSignal(parent_);