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