]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.cpp
Revert part of c053a9394d1075ecad02ccce2f34c95f08c1f00f
[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 "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 these images are generated for a clone, then that may be
117         // because we are previewing. We therefore do not want to delete
118         // them when this Buffer is destroyed.
119         if (!ploader_.buffer().isClone())
120                 iloader_.filename().removeFile();
121 }
122
123
124 Image const * PreviewImage::Impl::image()
125 {
126         if (iloader_.status() == WaitingToLoad)
127                 iloader_.startLoading();
128
129         return iloader_.image();
130 }
131
132
133 void PreviewImage::Impl::statusChanged()
134 {
135         switch (iloader_.status()) {
136         case WaitingToLoad:
137         case Loading:
138         case Converting:
139         case Loaded:
140         case ScalingEtc:
141                 break;
142
143         case ErrorNoFile:
144         case ErrorConverting:
145         case ErrorLoading:
146         case ErrorGeneratingPixmap:
147         case ErrorUnknown:
148                 //iloader_.filename().removeFile();
149                 ploader_.remove(snippet_);
150                 // FIXME: We need to return here, because PreviewLoader::remove
151                 // removes the preview image from the cache, which deletes this
152                 // object, so we should not try to do anything here.
153                 return;
154
155         case Ready:
156                 iloader_.filename().removeFile();
157                 break;
158         }
159         ploader_.emitSignal(parent_);
160 }
161
162 } // namespace graphics
163 } // namespace lyx