2 * \file PreviewImage.cpp
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/FileName.h"
19 #include "support/lyxlib.h"
21 #include <boost/bind.hpp>
28 using support::FileName;
32 class PreviewImage::Impl : public boost::signals::trackable {
35 Impl(PreviewImage & p, PreviewLoader & l,
36 string const & s, FileName const & f, double af);
40 Image const * image();
45 PreviewImage const & parent_;
47 PreviewLoader & ploader_;
51 string const snippet_;
53 double const ascent_frac_;
57 PreviewImage::PreviewImage(PreviewLoader & l,
61 : pimpl_(new Impl(*this, l, s, f, af))
65 PreviewImage::~PreviewImage()
71 string const & PreviewImage::snippet() const
73 return pimpl_->snippet_;
77 int PreviewImage::ascent() const
79 Image const * const image = pimpl_->iloader_.image();
83 return int(pimpl_->ascent_frac_ * double(image->height()));
87 int PreviewImage::descent() const
89 Image const * const image = pimpl_->iloader_.image();
93 // Avoids rounding errors.
94 return image->height() - ascent();
98 int PreviewImage::width() const
100 Image const * const image = pimpl_->iloader_.image();
101 return image ? image->width() : 0;
105 Image const * PreviewImage::image() const
107 return pimpl_->image();
111 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
115 : parent_(p), ploader_(l), iloader_(bf),
116 snippet_(s), ascent_frac_(af)
118 iloader_.connect(boost::bind(&Impl::statusChanged, this));
122 PreviewImage::Impl::~Impl()
124 support::unlink(iloader_.filename());
128 Image const * PreviewImage::Impl::image()
130 if (iloader_.status() == WaitingToLoad)
131 iloader_.startLoading();
133 return iloader_.image();
137 void PreviewImage::Impl::statusChanged()
139 switch (iloader_.status()) {
148 case ErrorConverting:
150 case ErrorGeneratingPixmap:
152 //lyx::unlink(iloader_.filename());
153 ploader_.remove(snippet_);
157 support::unlink(iloader_.filename());
160 ploader_.emitSignal(parent_);
163 } // namespace graphics