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"
22 #include "support/lyxlib.h"
26 using namespace lyx::support;
31 class PreviewImage::Impl {
34 Impl(PreviewImage & p, PreviewLoader & l,
35 string const & s, FileName const & f, double af);
39 Image const * image() const;
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 = support::iround(pimpl_->ascent_frac_ * double(image->height()));
90 dim.des = image->height() - dim.asc;
91 dim.wid = image->width();
96 Image const * PreviewImage::image() const
98 return pimpl_->image();
102 PreviewLoader & PreviewImage::previewLoader() const
104 return pimpl_->ploader_;
108 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l, string const & s,
109 FileName const & bf, double af)
110 : parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf),
111 snippet_(s), ascent_frac_(af)
113 iloader_.setDisplayPixelRatio(l.displayPixelRatio());
114 // This connection is destroyed at the same time as this.
115 iloader_.connect([this](){ statusChanged(); });
119 PreviewImage::Impl::~Impl()
121 // If these images are generated for a clone, then that may be
122 // because we are previewing. We therefore do not want to delete
123 // them when this Buffer is destroyed.
124 if (!ploader_.buffer().isClone())
125 iloader_.filename().removeFile();
129 Image const * PreviewImage::Impl::image() const
131 if (iloader_.status() == WaitingToLoad)
132 iloader_.startLoading();
134 return iloader_.image();
138 void PreviewImage::Impl::statusChanged()
140 switch (iloader_.status()) {
149 case ErrorConverting:
151 case ErrorGeneratingPixmap:
153 //iloader_.filename().removeFile();
154 ploader_.remove(snippet_);
155 // FIXME: We need to return here, because PreviewLoader::remove
156 // removes the preview image from the cache, which deletes this
157 // object, so we should not try to do anything here.
161 iloader_.filename().removeFile();
164 ploader_.emitSignal(parent_);
167 } // namespace graphics