]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.cpp
installer: further preparation
[lyx.git] / src / graphics / PreviewImage.cpp
1 /**
2  * \file PreviewImage.cpp
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
15 #include "Dimension.h"
16 #include "GraphicsImage.h"
17 #include "GraphicsLoader.h"
18 #include "PreviewLoader.h"
19
20 #include "support/FileName.h"
21
22 #include "support/bind.h"
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28 namespace graphics {
29
30 class PreviewImage::Impl : public boost::signals::trackable {
31 public:
32         ///
33         Impl(PreviewImage & p, PreviewLoader & l,
34              string const & s, FileName const & f, double af);
35         ///
36         ~Impl();
37         ///
38         Image const * image();
39         ///
40         void statusChanged();
41
42         ///
43         PreviewImage const & parent_;
44         ///
45         PreviewLoader & ploader_;
46         ///
47         Loader iloader_;
48         ///
49         string const snippet_;
50         ///
51         double const ascent_frac_;
52 };
53
54
55 PreviewImage::PreviewImage(PreviewLoader & l,
56                            string const & s,
57                            FileName const & f,
58                            double af)
59         : pimpl_(new Impl(*this, l, s, f, af))
60 {}
61
62
63 PreviewImage::~PreviewImage()
64 {
65         delete pimpl_;
66 }
67
68
69 string const & PreviewImage::snippet() const
70 {
71         return pimpl_->snippet_;
72 }
73
74
75 support::FileName const & PreviewImage::filename() const
76 {
77         return pimpl_->iloader_.filename();
78 }
79
80
81 Dimension PreviewImage::dim() const
82 {
83         Dimension dim;
84         Image const * const image = pimpl_->iloader_.image();
85         if (!image)
86                 return dim;
87
88         dim.asc = int(pimpl_->ascent_frac_ * double(image->height()));
89         dim.des = image->height() - dim.asc;
90         dim.wid = image->width();
91         return dim;
92 }
93
94
95 Image const * PreviewImage::image() const
96 {
97         return pimpl_->image();
98 }
99
100
101 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
102                          string const & s,
103                          FileName const & bf,
104                          double af)
105         : parent_(p), ploader_(l), iloader_(bf),
106           snippet_(s), ascent_frac_(af)
107 {
108         iloader_.connect(bind(&Impl::statusChanged, this));
109 }
110
111
112 PreviewImage::Impl::~Impl()
113 {
114         iloader_.filename().removeFile();
115 }
116
117
118 Image const * PreviewImage::Impl::image()
119 {
120         if (iloader_.status() == WaitingToLoad)
121                 iloader_.startLoading();
122
123         return iloader_.image();
124 }
125
126
127 void PreviewImage::Impl::statusChanged()
128 {
129         switch (iloader_.status()) {
130         case WaitingToLoad:
131         case Loading:
132         case Converting:
133         case Loaded:
134         case ScalingEtc:
135                 break;
136
137         case ErrorNoFile:
138         case ErrorConverting:
139         case ErrorLoading:
140         case ErrorGeneratingPixmap:
141         case ErrorUnknown:
142                 //iloader_.filename().removeFile();
143                 ploader_.remove(snippet_);
144                 // FIXME: We need to return here, because PreviewLoader::remove
145                 // removes the preview image from the cache, which deletes this
146                 // object, so we should not try to do anything here.
147                 return;
148
149         case Ready:
150                 iloader_.filename().removeFile();
151                 break;
152         }
153         ploader_.emitSignal(parent_);
154 }
155
156 } // namespace graphics
157 } // namespace lyx