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