]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewImage.cpp
Include standard <regex>
[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
15 #include "Buffer.h"
16 #include "Dimension.h"
17 #include "GraphicsImage.h"
18 #include "GraphicsLoader.h"
19 #include "PreviewLoader.h"
20
21 #include "support/FileName.h"
22 #include "support/lyxlib.h"
23
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace lyx {
29 namespace graphics {
30
31 class PreviewImage::Impl {
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() const;
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 support::FileName const & PreviewImage::filename() const
77 {
78         return pimpl_->iloader_.filename();
79 }
80
81
82 Dimension PreviewImage::dim() const
83 {
84         Dimension dim;
85         Image const * const image = pimpl_->iloader_.image();
86         if (!image)
87                 return dim;
88
89         dim.asc = support::iround(pimpl_->ascent_frac_ * double(image->height()));
90         dim.des = image->height() - dim.asc;
91         dim.wid = image->width();
92         return dim;
93 }
94
95
96 Image const * PreviewImage::image() const
97 {
98         return pimpl_->image();
99 }
100
101
102 PreviewLoader & PreviewImage::previewLoader() const
103 {
104         return pimpl_->ploader_;
105 }
106
107
108 PreviewImage::Impl::Impl(PreviewImage & p, PreviewLoader & l, string const & s,
109                          FileName const & bf, double af)
110         : parent_(p), ploader_(l), iloader_(l.buffer().fileName(), bf),
111           snippet_(s), ascent_frac_(af)
112 {
113         iloader_.setDisplayPixelRatio(l.displayPixelRatio());
114         // This connection is destroyed at the same time as this.
115         iloader_.connect([this](){ statusChanged(); });
116 }
117
118
119 PreviewImage::Impl::~Impl()
120 {
121         // If these images are generated for a clone, then that may be
122         // because we are previewing. We therefore do not want to delete
123         // them when this Buffer is destroyed.
124         if (!ploader_.buffer().isClone())
125                 iloader_.filename().removeFile();
126 }
127
128
129 Image const * PreviewImage::Impl::image() const
130 {
131         if (iloader_.status() == WaitingToLoad)
132                 iloader_.startLoading();
133
134         return iloader_.image();
135 }
136
137
138 void PreviewImage::Impl::statusChanged()
139 {
140         switch (iloader_.status()) {
141         case WaitingToLoad:
142         case Loading:
143         case Converting:
144         case Loaded:
145         case ScalingEtc:
146                 break;
147
148         case ErrorNoFile:
149         case ErrorConverting:
150         case ErrorLoading:
151         case ErrorGeneratingPixmap:
152         case ErrorUnknown:
153                 //iloader_.filename().removeFile();
154                 ploader_.remove(snippet_);
155                 // FIXME: We need to return here, because PreviewLoader::remove
156                 // removes the preview image from the cache, which deletes this
157                 // object, so we should not try to do anything here.
158                 return;
159
160         case Ready:
161                 iloader_.filename().removeFile();
162                 break;
163         }
164         ploader_.emitSignal(parent_);
165 }
166
167 } // namespace graphics
168 } // namespace lyx