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