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