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