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;
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();
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() const
100 return pimpl_->image();
104 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
108 : parent_(p), ploader_(l), iloader_(bf),
109 snippet_(s), ascent_frac_(af)
111 iloader_.connect(boost::bind(&Impl::statusChanged, this));
115 PreviewImage::Impl::~Impl()
117 support::unlink(iloader_.filename());
121 Image const * PreviewImage::Impl::image()
123 if (iloader_.status() == WaitingToLoad)
124 iloader_.startLoading();
126 return iloader_.image();
130 void PreviewImage::Impl::statusChanged()
132 switch (iloader_.status()) {
141 case ErrorConverting:
143 case ErrorGeneratingPixmap:
145 //lyx::unlink(iloader_.filename());
146 ploader_.remove(snippet_);
150 support::unlink(iloader_.filename());
151 ploader_.emitSignal(parent_);
156 } // namespace graphics