]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
* Make the graphics files conform strictly to the Pimpl idiom by moving
[lyx.git] / src / graphics / PreviewImage.C
1 /**
2  *  \file PreviewImage.C
3  *  Copyright 2002 the LyX Team
4  *  Read the file COPYING
5  *
6  * \author Angus Leeming <leeming@lyx.org>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "PreviewImage.h"
16 #include "PreviewLoader.h"
17 #include "GraphicsImage.h"
18 #include "GraphicsLoader.h"
19
20 #include "support/lyxlib.h"
21
22 #include <boost/bind.hpp>
23 #include <boost/signals/trackable.hpp>
24
25
26 namespace grfx {
27
28 struct PreviewImage::Impl : public boost::signals::trackable {
29         ///
30         Impl(PreviewImage & p, PreviewLoader & l,
31              string const & s, string const & f, double af);
32         ///
33         ~Impl();
34         ///
35         Image const * image(Inset const &, BufferView const &);
36         ///
37         void statusChanged();
38
39         ///
40         PreviewImage const & parent_;
41         ///
42         PreviewLoader & ploader_;
43         ///
44         Loader iloader_;
45         ///
46         string const snippet_;
47         ///
48         double const ascent_frac_;
49 };
50
51
52 PreviewImage::PreviewImage(PreviewLoader & l,
53                            string const & s,
54                            string const & f,
55                            double af)
56         : pimpl_(new Impl(*this, l, s, f, af))
57 {}
58
59
60 PreviewImage::~PreviewImage()
61 {}
62
63
64 string const & PreviewImage::snippet() const
65 {
66         return pimpl_->snippet_;
67 }
68
69
70 int PreviewImage::ascent() const
71 {
72         Image const * const image = pimpl_->iloader_.image();
73         if (!image)
74                 return 0;
75
76         return int(pimpl_->ascent_frac_ * double(image->getHeight()));
77 }
78
79
80 int PreviewImage::descent() const
81 {
82         Image const * const image = pimpl_->iloader_.image();
83         if (!image)
84                 return 0;
85
86         // Avoids rounding errors.
87         return image->getHeight() - ascent();
88 }
89
90
91 int PreviewImage::width() const
92 {
93         Image const * const image = pimpl_->iloader_.image();
94         return image ? image->getWidth() : 0;
95 }
96
97
98 Image const * PreviewImage::image(Inset const & inset,
99                                   BufferView const & bv) const
100 {
101         return pimpl_->image(inset, bv);
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         lyx::unlink(iloader_.filename());
119 }
120
121
122 Image const * PreviewImage::Impl::image(Inset const & inset,
123                                         BufferView const & bv)
124 {
125         if (iloader_.status() == WaitingToLoad)
126                 iloader_.startLoading(inset, bv);
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                 lyx::unlink(iloader_.filename());
153                 ploader_.emitSignal(parent_);
154                 break;
155         }
156 }
157
158 } // namespace grfx