]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.cpp
Add 'needauth' option to converters that need explicit user authorization.
[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::signals2::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 PreviewLoader & PreviewImage::previewLoader() const
103 {
104         return pimpl_->ploader_;
105 }
106
107
108 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
109                          string const & s,
110                          FileName const & bf,
111                          double af)
112         : parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf),
113           snippet_(s), ascent_frac_(af)
114 {
115         iloader_.setDisplayPixelRatio(l.displayPixelRatio());
116         iloader_.connect(bind(&Impl::statusChanged, this));
117 }
118
119
120 PreviewImage::Impl::~Impl()
121 {
122         // If these images are generated for a clone, then that may be
123         // because we are previewing. We therefore do not want to delete
124         // them when this Buffer is destroyed.
125         if (!ploader_.buffer().isClone())
126                 iloader_.filename().removeFile();
127 }
128
129
130 Image const * PreviewImage::Impl::image()
131 {
132         if (iloader_.status() == WaitingToLoad)
133                 iloader_.startLoading();
134
135         return iloader_.image();
136 }
137
138
139 void PreviewImage::Impl::statusChanged()
140 {
141         switch (iloader_.status()) {
142         case WaitingToLoad:
143         case Loading:
144         case Converting:
145         case Loaded:
146         case ScalingEtc:
147                 break;
148
149         case ErrorNoFile:
150         case ErrorConverting:
151         case ErrorLoading:
152         case ErrorGeneratingPixmap:
153         case ErrorUnknown:
154                 //iloader_.filename().removeFile();
155                 ploader_.remove(snippet_);
156                 // FIXME: We need to return here, because PreviewLoader::remove
157                 // removes the preview image from the cache, which deletes this
158                 // object, so we should not try to do anything here.
159                 return;
160
161         case Ready:
162                 iloader_.filename().removeFile();
163                 break;
164         }
165         ploader_.emitSignal(parent_);
166 }
167
168 } // namespace graphics
169 } // namespace lyx