]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
Trivial fixes to some warnings thrown up by MSVS.Net 2003.
[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 class PreviewImage::Impl : public boost::signals::trackable {
31 public:
32         ///
33         Impl(PreviewImage & p, PreviewLoader & l,
34              string const & s, string const & f, double af);
35         ///
36         ~Impl();
37         ///
38         Image const * image();
39         ///
40         void statusChanged();
41
42         ///
43         PreviewImage const & parent_;
44         ///
45         PreviewLoader & ploader_;
46         ///
47         Loader iloader_;
48         ///
49         string const snippet_;
50         ///
51         double const ascent_frac_;
52 };
53
54
55 PreviewImage::PreviewImage(PreviewLoader & l,
56                            string const & s,
57                            string const & f,
58                            double af)
59         : pimpl_(new Impl(*this, l, s, f, af))
60 {}
61
62
63 PreviewImage::~PreviewImage()
64 {}
65
66
67 string const & PreviewImage::snippet() const
68 {
69         return pimpl_->snippet_;
70 }
71
72
73 int PreviewImage::ascent() const
74 {
75         Image const * const image = pimpl_->iloader_.image();
76         if (!image)
77                 return 0;
78
79         return int(pimpl_->ascent_frac_ * double(image->getHeight()));
80 }
81
82
83 int PreviewImage::descent() const
84 {
85         Image const * const image = pimpl_->iloader_.image();
86         if (!image)
87                 return 0;
88
89         // Avoids rounding errors.
90         return image->getHeight() - ascent();
91 }
92
93
94 int PreviewImage::width() const
95 {
96         Image const * const image = pimpl_->iloader_.image();
97         return image ? image->getWidth() : 0;
98 }
99
100
101 Image const * PreviewImage::image() const
102 {
103         return pimpl_->image();
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         support::unlink(iloader_.filename());
121 }
122
123
124 Image const * PreviewImage::Impl::image()
125 {
126         if (iloader_.status() == WaitingToLoad)
127                 iloader_.startLoading();
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                 support::unlink(iloader_.filename());
154                 break;
155         }
156         ploader_.emitSignal(parent_);
157 }
158
159 } // namespace graphics
160 } // namespace lyx