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>
23 namespace support = lyx::support;
29 struct PreviewImage::Impl : public boost::signals::trackable {
31 Impl(PreviewImage & p, PreviewLoader & l,
32 string const & s, string const & f, double af);
36 Image const * image();
41 PreviewImage const & parent_;
43 PreviewLoader & ploader_;
47 string const snippet_;
49 double const ascent_frac_;
53 PreviewImage::PreviewImage(PreviewLoader & l,
57 : pimpl_(new Impl(*this, l, s, f, af))
61 PreviewImage::~PreviewImage()
65 string const & PreviewImage::snippet() const
67 return pimpl_->snippet_;
71 int PreviewImage::ascent() const
73 Image const * const image = pimpl_->iloader_.image();
77 return int(pimpl_->ascent_frac_ * double(image->getHeight()));
81 int PreviewImage::descent() const
83 Image const * const image = pimpl_->iloader_.image();
87 // Avoids rounding errors.
88 return image->getHeight() - ascent();
92 int PreviewImage::width() const
94 Image const * const image = pimpl_->iloader_.image();
95 return image ? image->getWidth() : 0;
99 Image const * PreviewImage::image() const
101 return pimpl_->image();
105 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
109 : parent_(p), ploader_(l), iloader_(bf),
110 snippet_(s), ascent_frac_(af)
112 iloader_.connect(boost::bind(&Impl::statusChanged, this));
116 PreviewImage::Impl::~Impl()
118 support::unlink(iloader_.filename());
122 Image const * PreviewImage::Impl::image()
124 if (iloader_.status() == WaitingToLoad)
125 iloader_.startLoading();
127 return iloader_.image();
131 void PreviewImage::Impl::statusChanged()
133 switch (iloader_.status()) {
142 case ErrorConverting:
144 case ErrorGeneratingPixmap:
146 //lyx::unlink(iloader_.filename());
147 ploader_.remove(snippet_);
151 support::unlink(iloader_.filename());
152 ploader_.emitSignal(parent_);
157 } // namespace graphics