]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
Remove all generated files that still exist (perhaps because the process was
[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() const { return iloader_->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         return int((1.0 - pimpl_->ascent_frac_) * double(image->getHeight()));
96 }
97
98
99 int PreviewImage::width() const
100 {
101         Image const * const image = pimpl_->image();
102         return image ? image->getWidth() : 0;
103 }
104
105
106 Image const * PreviewImage::image() const
107 {
108         return pimpl_->image();
109 }
110
111
112 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
113                          string const & s,
114                          string const & bf,
115                          double af)
116         : parent_(p), ploader_(l), iloader_(new Loader(bf)),
117           snippet_(s), ascent_frac_(af)
118 {}
119
120
121 PreviewImage::Impl::~Impl()
122 {
123         lyx::unlink(iloader_->filename());
124 }
125
126
127 void PreviewImage::Impl::startLoading()
128 {
129         if (iloader_->status() != WaitingToLoad)
130                 return;
131
132         iloader_->statusChanged.connect(
133                 boost::bind(&Impl::statusChanged, this));
134         iloader_->startLoading();
135 }
136
137
138 void PreviewImage::Impl::statusChanged()
139 {
140         switch (iloader_->status()) {
141         case WaitingToLoad:
142         case Loading:
143         case Converting:
144         case Loaded:
145         case ScalingEtc:
146                 break;
147
148         case ErrorNoFile:
149         case ErrorConverting:
150         case ErrorLoading:
151         case ErrorGeneratingPixmap:
152         case ErrorUnknown:
153                 //lyx::unlink(iloader_->filename());
154                 ploader_.remove(snippet_);
155                 break;
156
157         case Ready:
158                 lyx::unlink(iloader_->filename());
159                 ploader_.imageReady(parent_);
160                 break;
161         }
162 }
163
164 } // namespace grfx