]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.C
Smart loading of image files for previews and for the graphics inset.
[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_.statusChanged.connect(
113                 boost::bind(&Impl::statusChanged, this));
114 }
115
116
117 PreviewImage::Impl::~Impl()
118 {
119         lyx::unlink(iloader_.filename());
120 }
121
122
123 Image const * PreviewImage::Impl::image(Inset const & inset,
124                                         BufferView const & bv)
125 {
126         if (iloader_.status() == WaitingToLoad)
127                 iloader_.startLoading(inset, bv);
128
129         return iloader_.image();
130 }
131
132
133 void PreviewImage::Impl::statusChanged()
134 {
135         switch (iloader_.status()) {
136         case WaitingToLoad:
137         case Loading:
138         case Converting:
139         case Loaded:
140         case ScalingEtc:
141                 break;
142
143         case ErrorNoFile:
144         case ErrorConverting:
145         case ErrorLoading:
146         case ErrorGeneratingPixmap:
147         case ErrorUnknown:
148                 //lyx::unlink(iloader_.filename());
149                 ploader_.remove(snippet_);
150                 break;
151
152         case Ready:
153                 lyx::unlink(iloader_.filename());
154                 ploader_.imageReady(parent_);
155                 break;
156         }
157 }
158
159 } // namespace grfx