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