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"
16 #include "Dimension.h"
17 #include "GraphicsImage.h"
18 #include "GraphicsLoader.h"
19 #include "PreviewLoader.h"
21 #include "support/FileName.h"
23 #include "support/bind.h"
26 using namespace lyx::support;
31 class PreviewImage::Impl : public boost::signals2::trackable {
34 Impl(PreviewImage & p, PreviewLoader & l,
35 string const & s, FileName const & f, double af);
39 Image const * image();
44 PreviewImage const & parent_;
46 PreviewLoader & ploader_;
50 string const snippet_;
52 double const ascent_frac_;
56 PreviewImage::PreviewImage(PreviewLoader & l,
60 : pimpl_(new Impl(*this, l, s, f, af))
64 PreviewImage::~PreviewImage()
70 string const & PreviewImage::snippet() const
72 return pimpl_->snippet_;
76 support::FileName const & PreviewImage::filename() const
78 return pimpl_->iloader_.filename();
82 Dimension PreviewImage::dim() const
85 Image const * const image = pimpl_->iloader_.image();
89 dim.asc = int(pimpl_->ascent_frac_ * double(image->height()) + 0.5);
90 dim.des = image->height() - dim.asc;
91 dim.wid = image->width();
96 Image const * PreviewImage::image() const
98 return pimpl_->image();
102 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
106 : parent_(p), ploader_(l), iloader_(bf),
107 snippet_(s), ascent_frac_(af)
109 iloader_.setDisplayPixelRatio(l.displayPixelRatio());
110 iloader_.connect(bind(&Impl::statusChanged, this));
114 PreviewImage::Impl::~Impl()
116 // If these images are generated for a clone, then that may be
117 // because we are previewing. We therefore do not want to delete
118 // them when this Buffer is destroyed.
119 if (!ploader_.buffer().isClone())
120 iloader_.filename().removeFile();
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 //iloader_.filename().removeFile();
149 ploader_.remove(snippet_);
150 // FIXME: We need to return here, because PreviewLoader::remove
151 // removes the preview image from the cache, which deletes this
152 // object, so we should not try to do anything here.
156 iloader_.filename().removeFile();
159 ploader_.emitSignal(parent_);
162 } // namespace graphics