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"
15 #include "Dimension.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsLoader.h"
18 #include "PreviewLoader.h"
20 #include "support/FileName.h"
22 #include "support/bind.h"
25 using namespace lyx::support;
30 class PreviewImage::Impl : public boost::signals::trackable {
33 Impl(PreviewImage & p, PreviewLoader & l,
34 string const & s, FileName 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()
69 string const & PreviewImage::snippet() const
71 return pimpl_->snippet_;
75 support::FileName const & PreviewImage::filename() const
77 return pimpl_->iloader_.filename();
81 Dimension PreviewImage::dim() const
84 Image const * const image = pimpl_->iloader_.image();
88 dim.asc = int(pimpl_->ascent_frac_ * double(image->height()) + 0.5);
89 dim.des = image->height() - dim.asc;
90 dim.wid = image->width();
95 Image const * PreviewImage::image() const
97 return pimpl_->image();
101 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
105 : parent_(p), ploader_(l), iloader_(bf),
106 snippet_(s), ascent_frac_(af)
108 iloader_.setDisplayPixelRatio(l.displayPixelRatio());
109 iloader_.connect(bind(&Impl::statusChanged, this));
113 PreviewImage::Impl::~Impl()
115 iloader_.filename().removeFile();
119 Image const * PreviewImage::Impl::image()
121 if (iloader_.status() == WaitingToLoad)
122 iloader_.startLoading();
124 return iloader_.image();
128 void PreviewImage::Impl::statusChanged()
130 switch (iloader_.status()) {
139 case ErrorConverting:
141 case ErrorGeneratingPixmap:
143 //iloader_.filename().removeFile();
144 ploader_.remove(snippet_);
145 // FIXME: We need to return here, because PreviewLoader::remove
146 // removes the preview image from the cache, which deletes this
147 // object, so we should not try to do anything here.
151 iloader_.filename().removeFile();
154 ploader_.emitSignal(parent_);
157 } // namespace graphics