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 class PreviewImage::Impl : public boost::signals::trackable {
33 Impl(PreviewImage & p, PreviewLoader & l,
34 string const & s, string const & f, double af);
38 Image const * image();
43 PreviewImage const & parent_;
45 PreviewLoader & ploader_;
49 string const snippet_;
51 double const ascent_frac_;
55 PreviewImage::PreviewImage(PreviewLoader & l,
59 : pimpl_(new Impl(*this, l, s, f, af))
63 PreviewImage::~PreviewImage()
67 string const & PreviewImage::snippet() const
69 return pimpl_->snippet_;
73 int PreviewImage::ascent() const
75 Image const * const image = pimpl_->iloader_.image();
79 return int(pimpl_->ascent_frac_ * double(image->getHeight()));
83 int PreviewImage::descent() const
85 Image const * const image = pimpl_->iloader_.image();
89 // Avoids rounding errors.
90 return image->getHeight() - ascent();
94 int PreviewImage::width() const
96 Image const * const image = pimpl_->iloader_.image();
97 return image ? image->getWidth() : 0;
101 Image const * PreviewImage::image() const
103 return pimpl_->image();
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 support::unlink(iloader_.filename());
124 Image const * PreviewImage::Impl::image()
126 if (iloader_.status() == WaitingToLoad)
127 iloader_.startLoading();
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 support::unlink(iloader_.filename());
156 ploader_.emitSignal(parent_);
159 } // namespace graphics