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