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