]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.cpp
Natbib authoryear uses (Ref1; Ref2) by default.
[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 "support/bind.h"
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 support::FileName const & PreviewImage::filename() const
74 {
75         return pimpl_->iloader_.filename();
76 }
77
78
79 Dimension PreviewImage::dim() const
80 {
81         Dimension dim;
82         Image const * const image = pimpl_->iloader_.image();
83         if (!image)
84                 return dim;
85
86         dim.asc = int(pimpl_->ascent_frac_ * double(image->height()));
87         dim.des = image->height() - dim.asc;
88         dim.wid = image->width();
89         return dim;
90 }
91
92
93 Image const * PreviewImage::image() const
94 {
95         return pimpl_->image();
96 }
97
98
99 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
100                          string const & s,
101                          FileName const & bf,
102                          double af)
103         : parent_(p), ploader_(l), iloader_(bf),
104           snippet_(s), ascent_frac_(af)
105 {
106         iloader_.connect(bind(&Impl::statusChanged, this));
107 }
108
109
110 PreviewImage::Impl::~Impl()
111 {
112         iloader_.filename().removeFile();
113 }
114
115
116 Image const * PreviewImage::Impl::image()
117 {
118         if (iloader_.status() == WaitingToLoad)
119                 iloader_.startLoading();
120
121         return iloader_.image();
122 }
123
124
125 void PreviewImage::Impl::statusChanged()
126 {
127         switch (iloader_.status()) {
128         case WaitingToLoad:
129         case Loading:
130         case Converting:
131         case Loaded:
132         case ScalingEtc:
133                 break;
134
135         case ErrorNoFile:
136         case ErrorConverting:
137         case ErrorLoading:
138         case ErrorGeneratingPixmap:
139         case ErrorUnknown:
140                 //iloader_.filename().removeFile();
141                 ploader_.remove(snippet_);
142                 // FIXME: We need to return here, because PreviewLoader::remove
143                 // removes the preview image from the cache, which deletes this
144                 // object, so we should not try to do anything here.
145                 return;
146
147         case Ready:
148                 iloader_.filename().removeFile();
149                 break;
150         }
151         ploader_.emitSignal(parent_);
152 }
153
154 } // namespace graphics
155 } // namespace lyx