]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.cpp
Fix dialog handling of Insert Plain Text
[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 int PreviewImage::ascent() const
78 {
79         Image const * const image = pimpl_->iloader_.image();
80         if (!image)
81                 return 0;
82
83         return int(pimpl_->ascent_frac_ * double(image->getHeight()));
84 }
85
86
87 int PreviewImage::descent() const
88 {
89         Image const * const image = pimpl_->iloader_.image();
90         if (!image)
91                 return 0;
92
93         // Avoids rounding errors.
94         return image->getHeight() - ascent();
95 }
96
97
98 int PreviewImage::width() const
99 {
100         Image const * const image = pimpl_->iloader_.image();
101         return image ? image->getWidth() : 0;
102 }
103
104
105 Image const * PreviewImage::image() const
106 {
107         return pimpl_->image();
108 }
109
110
111 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l,
112                          string const & s,
113                          FileName const & bf,
114                          double af)
115         : parent_(p), ploader_(l), iloader_(bf),
116           snippet_(s), ascent_frac_(af)
117 {
118         iloader_.connect(boost::bind(&Impl::statusChanged, this));
119 }
120
121
122 PreviewImage::Impl::~Impl()
123 {
124         support::unlink(iloader_.filename());
125 }
126
127
128 Image const * PreviewImage::Impl::image()
129 {
130         if (iloader_.status() == WaitingToLoad)
131                 iloader_.startLoading();
132
133         return iloader_.image();
134 }
135
136
137 void PreviewImage::Impl::statusChanged()
138 {
139         switch (iloader_.status()) {
140         case WaitingToLoad:
141         case Loading:
142         case Converting:
143         case Loaded:
144         case ScalingEtc:
145                 break;
146
147         case ErrorNoFile:
148         case ErrorConverting:
149         case ErrorLoading:
150         case ErrorGeneratingPixmap:
151         case ErrorUnknown:
152                 //lyx::unlink(iloader_.filename());
153                 ploader_.remove(snippet_);
154                 break;
155
156         case Ready:
157                 support::unlink(iloader_.filename());
158                 break;
159         }
160         ploader_.emitSignal(parent_);
161 }
162
163 } // namespace graphics
164 } // namespace lyx