]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.cpp
Avoid an exception when the lyxpreview2bitmap.py script fails completely
[lyx.git] / src / graphics / PreviewImage.cpp
1 /**
2  * \file PreviewImage.cpp
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  */
10
11 #include <config.h>
12
13 #include "PreviewImage.h"
14
15 #include "Dimension.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsLoader.h"
18 #include "PreviewLoader.h"
19
20 #include "support/FileName.h"
21
22 #include "support/bind.h"
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28 namespace graphics {
29
30 class PreviewImage::Impl : public boost::signals::trackable {
31 public:
32         ///
33         Impl(PreviewImage & p, PreviewLoader & l,
34              string const & s, FileName const & f, double af);
35         ///
36         ~Impl();
37         ///
38         Image const * image();
39         ///
40         void statusChanged();
41
42         ///
43         PreviewImage const & parent_;
44         ///
45         PreviewLoader & ploader_;
46         ///
47         Loader iloader_;
48         ///
49         string const snippet_;
50         ///
51         double const ascent_frac_;
52 };
53
54
55 PreviewImage::PreviewImage(PreviewLoader & l,
56                            string const & s,
57                            FileName const & f,
58                            double af)
59         : pimpl_(new Impl(*this, l, s, f, af))
60 {}
61
62
63 PreviewImage::~PreviewImage()
64 {
65         delete pimpl_;
66 }
67
68
69 string const & PreviewImage::snippet() const
70 {
71         return pimpl_->snippet_;
72 }
73
74
75 support::FileName const & PreviewImage::filename() const
76 {
77         return pimpl_->iloader_.filename();
78 }
79
80
81 Dimension PreviewImage::dim() const
82 {
83         Dimension dim;
84         Image const * const image = pimpl_->iloader_.image();
85         if (!image)
86                 return dim;
87
88         dim.asc = int(pimpl_->ascent_frac_ * double(image->height()) + 0.5);
89         dim.des = image->height() - dim.asc;
90         dim.wid = image->width();
91         return dim;
92 }
93
94
95 Image const * PreviewImage::image() const
96 {
97         return pimpl_->image();
98 }
99
100
101 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
102                          string const & s,
103                          FileName const & bf,
104                          double af)
105         : parent_(p), ploader_(l), iloader_(bf),
106           snippet_(s), ascent_frac_(af)
107 {
108         iloader_.setDisplayPixelRatio(l.displayPixelRatio());
109         iloader_.connect(bind(&Impl::statusChanged, this));
110 }
111
112
113 PreviewImage::Impl::~Impl()
114 {
115         iloader_.filename().removeFile();
116 }
117
118
119 Image const * PreviewImage::Impl::image()
120 {
121         if (iloader_.status() == WaitingToLoad)
122                 iloader_.startLoading();
123
124         return iloader_.image();
125 }
126
127
128 void PreviewImage::Impl::statusChanged()
129 {
130         switch (iloader_.status()) {
131         case WaitingToLoad:
132         case Loading:
133         case Converting:
134         case Loaded:
135         case ScalingEtc:
136                 break;
137
138         case ErrorNoFile:
139         case ErrorConverting:
140         case ErrorLoading:
141         case ErrorGeneratingPixmap:
142         case ErrorUnknown:
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.
148                 return;
149
150         case Ready:
151                 iloader_.filename().removeFile();
152                 break;
153         }
154         ploader_.emitSignal(parent_);
155 }
156
157 } // namespace graphics
158 } // namespace lyx