]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
clean code to export between different flavours, output different code for sgml to...
[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 "GraphicsImage.h"
15 #include "GraphicsLoader.h"
16 #include "PreviewLoader.h"
17
18 #include "support/lyxlib.h"
19
20 #include <boost/bind.hpp>
21
22 namespace support = lyx::support;
23
24 using std::string;
25
26
27 namespace lyx {
28 namespace graphics {
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();
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() const
101 {
102         return pimpl_->image();
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         support::unlink(iloader_.filename());
120 }
121
122
123 Image const * PreviewImage::Impl::image()
124 {
125         if (iloader_.status() == WaitingToLoad)
126                 iloader_.startLoading();
127
128         return iloader_.image();
129 }
130
131
132 void PreviewImage::Impl::statusChanged()
133 {
134         switch (iloader_.status()) {
135         case WaitingToLoad:
136         case Loading:
137         case Converting:
138         case Loaded:
139         case ScalingEtc:
140                 break;
141
142         case ErrorNoFile:
143         case ErrorConverting:
144         case ErrorLoading:
145         case ErrorGeneratingPixmap:
146         case ErrorUnknown:
147                 //lyx::unlink(iloader_.filename());
148                 ploader_.remove(snippet_);
149                 break;
150
151         case Ready:
152                 support::unlink(iloader_.filename());
153                 break;
154         }
155         ploader_.emitSignal(parent_);
156 }
157
158 } // namespace graphics
159 } // namespace lyx