]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
Be more specific that 'using lyx::support'.
[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 namespace support = lyx::support;
24
25
26 namespace lyx {
27 namespace graphics {
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();
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() const
100 {
101         return pimpl_->image();
102 }
103
104
105 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
106                          string const & s,
107                          string const & bf,
108                          double af)
109         : parent_(p), ploader_(l), iloader_(bf),
110           snippet_(s), ascent_frac_(af)
111 {
112         iloader_.connect(boost::bind(&Impl::statusChanged, this));
113 }
114
115
116 PreviewImage::Impl::~Impl()
117 {
118         support::unlink(iloader_.filename());
119 }
120
121
122 Image const * PreviewImage::Impl::image()
123 {
124         if (iloader_.status() == WaitingToLoad)
125                 iloader_.startLoading();
126
127         return iloader_.image();
128 }
129
130
131 void PreviewImage::Impl::statusChanged()
132 {
133         switch (iloader_.status()) {
134         case WaitingToLoad:
135         case Loading:
136         case Converting:
137         case Loaded:
138         case ScalingEtc:
139                 break;
140
141         case ErrorNoFile:
142         case ErrorConverting:
143         case ErrorLoading:
144         case ErrorGeneratingPixmap:
145         case ErrorUnknown:
146                 //lyx::unlink(iloader_.filename());
147                 ploader_.remove(snippet_);
148                 break;
149
150         case Ready:
151                 support::unlink(iloader_.filename());
152                 ploader_.emitSignal(parent_);
153                 break;
154         }
155 }
156
157 } // namespace graphics
158 } // namespace lyx