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