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