]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
If I ever see another licence blurb again, it'll be too soon...
[lyx.git] / src / graphics / PreviewImage.C
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  */
10
11 #include <config.h>
12
13 #include "PreviewImage.h"
14 #include "PreviewLoader.h"
15 #include "GraphicsImage.h"
16 #include "GraphicsLoader.h"
17
18 #include "support/lyxlib.h"
19
20 #include <boost/bind.hpp>
21 #include <boost/signals/trackable.hpp>
22
23 using namespace lyx::support;
24
25 namespace lyx {
26 namespace graphics {
27
28 struct PreviewImage::Impl : public boost::signals::trackable {
29         ///
30         Impl(PreviewImage & p, PreviewLoader & l,
31              string const & s, string const & f, double af);
32         ///
33         ~Impl();
34         ///
35         Image const * image();
36         ///
37         void statusChanged();
38
39         ///
40         PreviewImage const & parent_;
41         ///
42         PreviewLoader & ploader_;
43         ///
44         Loader iloader_;
45         ///
46         string const snippet_;
47         ///
48         double const ascent_frac_;
49 };
50
51
52 PreviewImage::PreviewImage(PreviewLoader & l,
53                            string const & s,
54                            string const & f,
55                            double af)
56         : pimpl_(new Impl(*this, l, s, f, af))
57 {}
58
59
60 PreviewImage::~PreviewImage()
61 {}
62
63
64 string const & PreviewImage::snippet() const
65 {
66         return pimpl_->snippet_;
67 }
68
69
70 int PreviewImage::ascent() const
71 {
72         Image const * const image = pimpl_->iloader_.image();
73         if (!image)
74                 return 0;
75
76         return int(pimpl_->ascent_frac_ * double(image->getHeight()));
77 }
78
79
80 int PreviewImage::descent() const
81 {
82         Image const * const image = pimpl_->iloader_.image();
83         if (!image)
84                 return 0;
85
86         // Avoids rounding errors.
87         return image->getHeight() - ascent();
88 }
89
90
91 int PreviewImage::width() const
92 {
93         Image const * const image = pimpl_->iloader_.image();
94         return image ? image->getWidth() : 0;
95 }
96
97
98 Image const * PreviewImage::image() const
99 {
100         return pimpl_->image();
101 }
102
103
104 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
105                          string const & s,
106                          string const & bf,
107                          double af)
108         : parent_(p), ploader_(l), iloader_(bf),
109           snippet_(s), ascent_frac_(af)
110 {
111         iloader_.connect(boost::bind(&Impl::statusChanged, this));
112 }
113
114
115 PreviewImage::Impl::~Impl()
116 {
117         unlink(iloader_.filename());
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                 //lyx::unlink(iloader_.filename());
146                 ploader_.remove(snippet_);
147                 break;
148
149         case Ready:
150                 unlink(iloader_.filename());
151                 ploader_.emitSignal(parent_);
152                 break;
153         }
154 }
155
156 } // namespace graphics
157 } // namespace lyx