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 "PreviewLoader.h"
15 #include "GraphicsImage.h"
16 #include "GraphicsLoader.h"
18 #include "support/lyxlib.h"
20 #include <boost/bind.hpp>
21 #include <boost/signals/trackable.hpp>
26 struct PreviewImage::Impl : public boost::signals::trackable {
28 Impl(PreviewImage & p, PreviewLoader & l,
29 string const & s, string const & f, double af);
33 Image const * image();
38 PreviewImage const & parent_;
40 PreviewLoader & ploader_;
44 string const snippet_;
46 double const ascent_frac_;
50 PreviewImage::PreviewImage(PreviewLoader & l,
54 : pimpl_(new Impl(*this, l, s, f, af))
58 PreviewImage::~PreviewImage()
62 string const & PreviewImage::snippet() const
64 return pimpl_->snippet_;
68 int PreviewImage::ascent() const
70 Image const * const image = pimpl_->iloader_.image();
74 return int(pimpl_->ascent_frac_ * double(image->getHeight()));
78 int PreviewImage::descent() const
80 Image const * const image = pimpl_->iloader_.image();
84 // Avoids rounding errors.
85 return image->getHeight() - ascent();
89 int PreviewImage::width() const
91 Image const * const image = pimpl_->iloader_.image();
92 return image ? image->getWidth() : 0;
96 Image const * PreviewImage::image() const
98 return pimpl_->image();
102 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
106 : parent_(p), ploader_(l), iloader_(bf),
107 snippet_(s), ascent_frac_(af)
109 iloader_.connect(boost::bind(&Impl::statusChanged, this));
113 PreviewImage::Impl::~Impl()
115 lyx::unlink(iloader_.filename());
119 Image const * PreviewImage::Impl::image()
121 if (iloader_.status() == WaitingToLoad)
122 iloader_.startLoading();
124 return iloader_.image();
128 void PreviewImage::Impl::statusChanged()
130 switch (iloader_.status()) {
139 case ErrorConverting:
141 case ErrorGeneratingPixmap:
143 //lyx::unlink(iloader_.filename());
144 ploader_.remove(snippet_);
148 lyx::unlink(iloader_.filename());
149 ploader_.emitSignal(parent_);