]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
This file is part of LyX, the document processor.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "PreviewImage.h"
18 #include "PreviewLoader.h"
19 #include "GraphicsImage.h"
20 #include "GraphicsLoader.h"
21
22 #include "support/lyxlib.h"
23
24 #include <boost/bind.hpp>
25 #include <boost/signals/trackable.hpp>
26
27
28 namespace grfx {
29
30 struct PreviewImage::Impl : public boost::signals::trackable {
31         ///
32         Impl(PreviewImage & p, PreviewLoader & l,
33              string const & s, string const & f, double af);
34         ///
35         ~Impl();
36         ///
37         Image const * image(Inset const &, BufferView const &);
38         ///
39         void statusChanged();
40
41         ///
42         PreviewImage const & parent_;
43         ///
44         PreviewLoader & ploader_;
45         ///
46         Loader iloader_;
47         ///
48         string const snippet_;
49         ///
50         double const ascent_frac_;
51 };
52
53
54 PreviewImage::PreviewImage(PreviewLoader & l,
55                            string const & s,
56                            string const & f,
57                            double af)
58         : pimpl_(new Impl(*this, l, s, f, af))
59 {}
60
61
62 PreviewImage::~PreviewImage()
63 {}
64
65
66 string const & PreviewImage::snippet() const
67 {
68         return pimpl_->snippet_;
69 }
70
71
72 int PreviewImage::ascent() const
73 {
74         Image const * const image = pimpl_->iloader_.image();
75         if (!image)
76                 return 0;
77
78         return int(pimpl_->ascent_frac_ * double(image->getHeight()));
79 }
80
81
82 int PreviewImage::descent() const
83 {
84         Image const * const image = pimpl_->iloader_.image();
85         if (!image)
86                 return 0;
87
88         // Avoids rounding errors.
89         return image->getHeight() - ascent();
90 }
91
92
93 int PreviewImage::width() const
94 {
95         Image const * const image = pimpl_->iloader_.image();
96         return image ? image->getWidth() : 0;
97 }
98
99
100 Image const * PreviewImage::image(Inset const & inset,
101                                   BufferView const & bv) const
102 {
103         return pimpl_->image(inset, bv);
104 }
105
106
107 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
108                          string const & s,
109                          string const & bf,
110                          double af)
111         : parent_(p), ploader_(l), iloader_(bf),
112           snippet_(s), ascent_frac_(af)
113 {
114         iloader_.connect(boost::bind(&Impl::statusChanged, this));
115 }
116
117
118 PreviewImage::Impl::~Impl()
119 {
120         lyx::unlink(iloader_.filename());
121 }
122
123
124 Image const * PreviewImage::Impl::image(Inset const & inset,
125                                         BufferView const & bv)
126 {
127         if (iloader_.status() == WaitingToLoad)
128                 iloader_.startLoading(inset, bv);
129
130         return iloader_.image();
131 }
132
133
134 void PreviewImage::Impl::statusChanged()
135 {
136         switch (iloader_.status()) {
137         case WaitingToLoad:
138         case Loading:
139         case Converting:
140         case Loaded:
141         case ScalingEtc:
142                 break;
143
144         case ErrorNoFile:
145         case ErrorConverting:
146         case ErrorLoading:
147         case ErrorGeneratingPixmap:
148         case ErrorUnknown:
149                 //lyx::unlink(iloader_.filename());
150                 ploader_.remove(snippet_);
151                 break;
152
153         case Ready:
154                 lyx::unlink(iloader_.filename());
155                 ploader_.emitSignal(parent_);
156                 break;
157         }
158 }
159
160 } // namespace grfx