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()
69 string const & PreviewImage::snippet() const
71 return pimpl_->snippet_;
75 int PreviewImage::ascent() const
77 Image const * const image = pimpl_->iloader_.image();
81 return int(pimpl_->ascent_frac_ * double(image->getHeight()));
85 int PreviewImage::descent() const
87 Image const * const image = pimpl_->iloader_.image();
91 // Avoids rounding errors.
92 return image->getHeight() - ascent();
96 int PreviewImage::width() const
98 Image const * const image = pimpl_->iloader_.image();
99 return image ? image->getWidth() : 0;
103 Image const * PreviewImage::image() const
105 return pimpl_->image();
109 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
113 : parent_(p), ploader_(l), iloader_(bf),
114 snippet_(s), ascent_frac_(af)
116 iloader_.connect(boost::bind(&Impl::statusChanged, this));
120 PreviewImage::Impl::~Impl()
122 support::unlink(iloader_.filename());
126 Image const * PreviewImage::Impl::image()
128 if (iloader_.status() == WaitingToLoad)
129 iloader_.startLoading();
131 return iloader_.image();
135 void PreviewImage::Impl::statusChanged()
137 switch (iloader_.status()) {
146 case ErrorConverting:
148 case ErrorGeneratingPixmap:
150 //lyx::unlink(iloader_.filename());
151 ploader_.remove(snippet_);
155 support::unlink(iloader_.filename());
158 ploader_.emitSignal(parent_);
161 } // namespace graphics