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 "PreviewImage.h"
14 #include "GraphicsImage.h"
15 #include "GraphicsLoader.h"
16 #include "PreviewLoader.h"
18 #include "support/lyxlib.h"
20 #include <boost/bind.hpp>
22 namespace support = lyx::support;
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();
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() const
102 return pimpl_->image();
106 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
110 : parent_(p), ploader_(l), iloader_(bf),
111 snippet_(s), ascent_frac_(af)
113 iloader_.connect(boost::bind(&Impl::statusChanged, this));
117 PreviewImage::Impl::~Impl()
119 support::unlink(iloader_.filename());
123 Image const * PreviewImage::Impl::image()
125 if (iloader_.status() == WaitingToLoad)
126 iloader_.startLoading();
128 return iloader_.image();
132 void PreviewImage::Impl::statusChanged()
134 switch (iloader_.status()) {
143 case ErrorConverting:
145 case ErrorGeneratingPixmap:
147 //lyx::unlink(iloader_.filename());
148 ploader_.remove(snippet_);
152 support::unlink(iloader_.filename());
153 ploader_.emitSignal(parent_);
158 } // namespace graphics