]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
Modify the headers of files in src/graphics as discussed on the list.
[lyx.git] / src / graphics / PreviewImage.C
1 /**
2  *  \file PreviewImage.C
3  *  Read the file COPYING
4  *
5  * \author Angus Leeming 
6  *
7  * Full author contact details available in file CREDITS
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "PreviewImage.h"
17 #include "PreviewLoader.h"
18 #include "GraphicsImage.h"
19 #include "GraphicsLoader.h"
20
21 #include "support/lyxlib.h"
22
23 #include <boost/bind.hpp>
24 #include <boost/signals/trackable.hpp>
25
26
27 namespace grfx {
28
29 struct PreviewImage::Impl : public boost::signals::trackable {
30         ///
31         Impl(PreviewImage & p, PreviewLoader & l,
32              string const & s, string const & f, double af);
33         ///
34         ~Impl();
35         ///
36         Image const * image(Inset const &, BufferView const &);
37         ///
38         void statusChanged();
39
40         ///
41         PreviewImage const & parent_;
42         ///
43         PreviewLoader & ploader_;
44         ///
45         Loader iloader_;
46         ///
47         string const snippet_;
48         ///
49         double const ascent_frac_;
50 };
51
52
53 PreviewImage::PreviewImage(PreviewLoader & l,
54                            string const & s,
55                            string const & f,
56                            double af)
57         : pimpl_(new Impl(*this, l, s, f, af))
58 {}
59
60
61 PreviewImage::~PreviewImage()
62 {}
63
64
65 string const & PreviewImage::snippet() const
66 {
67         return pimpl_->snippet_;
68 }
69
70
71 int PreviewImage::ascent() const
72 {
73         Image const * const image = pimpl_->iloader_.image();
74         if (!image)
75                 return 0;
76
77         return int(pimpl_->ascent_frac_ * double(image->getHeight()));
78 }
79
80
81 int PreviewImage::descent() const
82 {
83         Image const * const image = pimpl_->iloader_.image();
84         if (!image)
85                 return 0;
86
87         // Avoids rounding errors.
88         return image->getHeight() - ascent();
89 }
90
91
92 int PreviewImage::width() const
93 {
94         Image const * const image = pimpl_->iloader_.image();
95         return image ? image->getWidth() : 0;
96 }
97
98
99 Image const * PreviewImage::image(Inset const & inset,
100                                   BufferView const & bv) const
101 {
102         return pimpl_->image(inset, bv);
103 }
104
105
106 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
107                          string const & s,
108                          string const & bf,
109                          double af)
110         : parent_(p), ploader_(l), iloader_(bf),
111           snippet_(s), ascent_frac_(af)
112 {
113         iloader_.connect(boost::bind(&Impl::statusChanged, this));
114 }
115
116
117 PreviewImage::Impl::~Impl()
118 {
119         lyx::unlink(iloader_.filename());
120 }
121
122
123 Image const * PreviewImage::Impl::image(Inset const & inset,
124                                         BufferView const & bv)
125 {
126         if (iloader_.status() == WaitingToLoad)
127                 iloader_.startLoading(inset, bv);
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                 //lyx::unlink(iloader_.filename());
149                 ploader_.remove(snippet_);
150                 break;
151
152         case Ready:
153                 lyx::unlink(iloader_.filename());
154                 ploader_.emitSignal(parent_);
155                 break;
156         }
157 }
158
159 } // namespace grfx