Consider the case where there is not any layout name.
[lyx.git] / src / graphics / PreviewImage.C
blob6a057d27f3e8409cd8bb70cf53287426f35957f3
1 /**
2  * \file PreviewImage.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
11 #include <config.h>
13 #include "PreviewImage.h"
14 #include "GraphicsImage.h"
15 #include "GraphicsLoader.h"
16 #include "PreviewLoader.h"
18 #include "support/lyxlib.h"
20 #include <boost/bind.hpp>
22 namespace support = lyx::support;
24 using std::string;
27 namespace lyx {
28 namespace graphics {
30 class PreviewImage::Impl : public boost::signals::trackable {
31 public:
32         ///
33         Impl(PreviewImage & p, PreviewLoader & l,
34              string const & s, string const & f, double af);
35         ///
36         ~Impl();
37         ///
38         Image const * image();
39         ///
40         void statusChanged();
42         ///
43         PreviewImage const & parent_;
44         ///
45         PreviewLoader & ploader_;
46         ///
47         Loader iloader_;
48         ///
49         string const snippet_;
50         ///
51         double const ascent_frac_;
55 PreviewImage::PreviewImage(PreviewLoader & l,
56                            string const & s,
57                            string const & f,
58                            double af)
59         : pimpl_(new Impl(*this, l, s, f, af))
63 PreviewImage::~PreviewImage()
67 string const & PreviewImage::snippet() const
69         return pimpl_->snippet_;
73 int PreviewImage::ascent() const
75         Image const * const image = pimpl_->iloader_.image();
76         if (!image)
77                 return 0;
79         return int(pimpl_->ascent_frac_ * double(image->getHeight()));
83 int PreviewImage::descent() const
85         Image const * const image = pimpl_->iloader_.image();
86         if (!image)
87                 return 0;
89         // Avoids rounding errors.
90         return image->getHeight() - ascent();
94 int PreviewImage::width() const
96         Image const * const image = pimpl_->iloader_.image();
97         return image ? image->getWidth() : 0;
101 Image const * PreviewImage::image() const
103         return pimpl_->image();
107 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
108                          string const & s,
109                          string const & bf,
110                          double af)
111         : parent_(p), ploader_(l), iloader_(bf),
112           snippet_(s), ascent_frac_(af)
114         iloader_.connect(boost::bind(&Impl::statusChanged, this));
118 PreviewImage::Impl::~Impl()
120         support::unlink(iloader_.filename());
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()) {
136         case WaitingToLoad:
137         case Loading:
138         case Converting:
139         case Loaded:
140         case ScalingEtc:
141                 break;
143         case ErrorNoFile:
144         case ErrorConverting:
145         case ErrorLoading:
146         case ErrorGeneratingPixmap:
147         case ErrorUnknown:
148                 //lyx::unlink(iloader_.filename());
149                 ploader_.remove(snippet_);
150                 break;
152         case Ready:
153                 support::unlink(iloader_.filename());
154                 break;
155         }
156         ploader_.emitSignal(parent_);
159 } // namespace graphics
160 } // namespace lyx