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"
25 using namespace lyx::support;
30 class PreviewImage::Impl {
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 PreviewLoader & PreviewImage::previewLoader() const
103 return pimpl_->ploader_;
107 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l, string const & s,
108 FileName const & bf, double af)
109 : parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf),
110 snippet_(s), ascent_frac_(af)
112 iloader_.setDisplayPixelRatio(l.displayPixelRatio());
113 // This connection is destroyed at the same time as this.
114 iloader_.connect([this](){ statusChanged(); });
118 PreviewImage::Impl::~Impl()
120 // If these images are generated for a clone, then that may be
121 // because we are previewing. We therefore do not want to delete
122 // them when this Buffer is destroyed.
123 if (!ploader_.buffer().isClone())
124 iloader_.filename().removeFile();
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 //iloader_.filename().removeFile();
153 ploader_.remove(snippet_);
154 // FIXME: We need to return here, because PreviewLoader::remove
155 // removes the preview image from the cache, which deletes this
156 // object, so we should not try to do anything here.
160 iloader_.filename().removeFile();
163 ploader_.emitSignal(parent_);
166 } // namespace graphics