1 /**
2  * \file RenderPreview.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 "insets/RenderPreview.h"
14 #include "insets/Inset.h"
15 
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "Dimension.h"
19 #include "LyX.h"
20 #include "LyXRC.h"
21 #include "MetricsInfo.h"
22 
23 #include "frontends/FontMetrics.h"
24 #include "frontends/Painter.h"
25 
26 #include "graphics/PreviewImage.h"
27 #include "graphics/PreviewLoader.h"
28 
29 #include "support/FileName.h"
30 #include "support/gettext.h"
31 #include "support/lassert.h"
32 #include "support/lstrings.h"
33 
34 using namespace std;
35 using namespace lyx::support;
36 
37 namespace lyx {
38 
39 
previewText()40 bool RenderPreview::previewText()
41 {
42 	// Use a switch to trigger a warning if the enum is changed.
43 	switch(lyxrc.preview) {
44 	case LyXRC::PREVIEW_ON:
45 	case LyXRC::PREVIEW_NO_MATH:
46 		return true;
47 	case LyXRC::PREVIEW_OFF:
48 		break;
49 	}
50 
51 	return false;
52 }
53 
54 
previewMath()55 bool RenderPreview::previewMath()
56 {
57 	// Use a switch to trigger a warning if the enum is changed.
58 	switch(lyxrc.preview) {
59 	case LyXRC::PREVIEW_ON:
60 		return true;
61 	case LyXRC::PREVIEW_NO_MATH:
62 	case LyXRC::PREVIEW_OFF:
63 		break;
64 	}
65 
66 	return false;
67 }
68 
69 
RenderPreview(Inset const * inset)70 RenderPreview::RenderPreview(Inset const * inset)
71 	: parent_(inset)
72 {}
73 
74 
RenderPreview(RenderPreview const & other,Inset const * inset)75 RenderPreview::RenderPreview(RenderPreview const & other,
76 			     Inset const * inset)
77 	: RenderBase(other),
78 	  snippet_(other.snippet_),
79 	  parent_(inset)
80 {}
81 
82 
clone(Inset const * inset) const83 RenderBase * RenderPreview::clone(Inset const * inset) const
84 {
85 	return new RenderPreview(*this, inset);
86 }
87 
88 
89 namespace {
90 
statusMessage(BufferView const * bv,string const & snippet)91 docstring const statusMessage(BufferView const * bv, string const & snippet)
92 {
93 	LASSERT(bv, return docstring());
94 
95 	Buffer const & buffer = bv->buffer();
96 	graphics::PreviewLoader const * loader = buffer.loader();
97 	// please coverity (probably worth the check anyway)
98 	if (!loader)
99 		return docstring();
100 	graphics::PreviewLoader::Status const status = loader->status(snippet);
101 
102 	docstring message;
103 	switch (status) {
104 	case graphics::PreviewLoader::InQueue:
105 	case graphics::PreviewLoader::Processing:
106 		message = _("Preview loading");
107 		break;
108 	case graphics::PreviewLoader::Ready:
109 		message = _("Preview ready");
110 		break;
111 	case graphics::PreviewLoader::NotFound:
112 		message = _("Preview failed");
113 		break;
114 	}
115 
116 	return message;
117 }
118 
119 } // namespace
120 
121 
122 graphics::PreviewImage const *
getPreviewImage(Buffer const & buffer) const123 RenderPreview::getPreviewImage(Buffer const & buffer) const
124 {
125 	graphics::PreviewLoader const * loader = buffer.loader();
126 	LASSERT(loader, return 0);
127 	return loader->preview(snippet_);
128 }
129 
130 
metrics(MetricsInfo & mi,Dimension & dim) const131 void RenderPreview::metrics(MetricsInfo & mi, Dimension & dim) const
132 {
133 	LBUFERR(mi.base.bv);
134 
135 	graphics::PreviewImage const * const pimage =
136 		getPreviewImage(mi.base.bv->buffer());
137 
138 	if (pimage) {
139 		// If prepared, load the picture before dim calculation. See bug #5627.
140 		pimage->image();
141 		dim = pimage->dim();
142 	} else {
143 		dim.asc = 50;
144 		dim.des = 0;
145 
146 		FontInfo font(mi.base.font);
147 		font.setFamily(SANS_FAMILY);
148 		font.setSize(FONT_SIZE_FOOTNOTE);
149 		docstring const stat = statusMessage(mi.base.bv, snippet_);
150 		dim.wid = 15 + theFontMetrics(font).width(stat);
151 	}
152 
153 	dim_ = dim;
154 }
155 
156 
draw(PainterInfo & pi,int x,int y) const157 void RenderPreview::draw(PainterInfo & pi, int x, int y) const
158 {
159 	LBUFERR(pi.base.bv);
160 
161 	graphics::PreviewImage const * const pimage =
162 		getPreviewImage(pi.base.bv->buffer());
163 	graphics::Image const * const image = pimage ? pimage->image() : 0;
164 
165 	if (image) {
166 		pi.pain.image(x, y - dim_.asc, dim_.wid, dim_.height(),
167 			      *image);
168 	} else {
169 		int const offset = Inset::TEXT_TO_INSET_OFFSET;
170 
171 		pi.pain.rectangle(x + offset,
172 				  y - dim_.asc,
173 				  dim_.wid - 2 * offset,
174 				  dim_.asc + dim_.des,
175 				  Color_foreground);
176 
177 		FontInfo font(pi.base.font);
178 		font.setFamily(SANS_FAMILY);
179 		font.setSize(FONT_SIZE_FOOTNOTE);
180 
181 		docstring const stat = statusMessage(pi.base.bv, snippet_);
182 		pi.pain.text(x + offset + 6,
183 			     y - theFontMetrics(font).maxAscent() - 4,
184 			     stat, font);
185 	}
186 	pi.change_.paintCue(pi, x, y - dim_.asc,
187 	                    x + dim_.width(), y - dim_.asc + dim_.height());
188 }
189 
190 
startLoading(Buffer const & buffer,bool forexport) const191 void RenderPreview::startLoading(Buffer const & buffer, bool forexport) const
192 {
193 	if (!forexport && (lyxrc.preview == LyXRC::PREVIEW_OFF || snippet_.empty()))
194 		return;
195 
196 	graphics::PreviewLoader * loader = buffer.loader();
197 	LASSERT(loader, return);
198 	loader->startLoading(forexport);
199 }
200 
201 
addPreview(docstring const & latex_snippet,Buffer const & buffer,bool ignore_lyxrc)202 void RenderPreview::addPreview(docstring const & latex_snippet,
203                                Buffer const & buffer,
204                                bool ignore_lyxrc)
205 {
206 	if (lyxrc.preview == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
207 		return;
208 
209 	graphics::PreviewLoader * loader = buffer.loader();
210 	LASSERT(loader, return);
211 	addPreview(latex_snippet, *loader, ignore_lyxrc);
212 }
213 
214 
addPreview(docstring const & latex_snippet,graphics::PreviewLoader & ploader,bool ignore_lyxrc)215 void RenderPreview::addPreview(docstring const & latex_snippet,
216                                graphics::PreviewLoader & ploader,
217                                bool ignore_lyxrc)
218 {
219 	if (lyxrc.preview == LyXRC::PREVIEW_OFF && !ignore_lyxrc)
220 		return;
221 
222 	// FIXME UNICODE
223 	// We have to make sure that we call latex with the right encoding
224 	snippet_ = support::trim(to_utf8(latex_snippet));
225 	if (snippet_.empty())
226 		return;
227 
228 	if (ploader.preview(snippet_))
229 		return;
230 
231 	// If this is the first time of calling, connect to the
232 	// PreviewLoader signal that'll inform us when the preview image
233 	// is ready for loading.
234 	if (!ploader_connection_.connected())
235 		// This is a scoped connection.
236 		ploader_connection_ =
237 			ploader.connect([this](graphics::PreviewImage const & pi){
238 				imageReady(pi);
239 			});
240 
241 	ploader.add(snippet_);
242 }
243 
244 
removePreview(Buffer const & buffer)245 void RenderPreview::removePreview(Buffer const & buffer)
246 {
247 	if (snippet_.empty())
248 		return;
249 
250 	graphics::PreviewLoader * loader = buffer.loader();
251 	LASSERT(loader, return);
252 	loader->remove(snippet_);
253 	snippet_.erase();
254 }
255 
256 
imageReady(graphics::PreviewImage const & pimage)257 void RenderPreview::imageReady(graphics::PreviewImage const & pimage)
258 {
259 	// Check the current snippet is the same as that previewed.
260 	if (snippet_ == pimage.snippet())
261 		parent_->updateFrontend();
262 }
263 
264 
RenderMonitoredPreview(Inset const * inset)265 RenderMonitoredPreview::RenderMonitoredPreview(Inset const * inset)
266 	: RenderPreview(inset)
267 {
268 	setAbsFile(FileName());
269 }
270 
271 
setAbsFile(FileName const & file)272 void RenderMonitoredPreview::setAbsFile(FileName const & file)
273 {
274 	bool mon = monitoring();
275 	if (mon)
276 		stopMonitoring();
277 	filename_ = file;
278 	if (mon)
279 		startMonitoring();
280 }
281 
282 
draw(PainterInfo & pi,int x,int y) const283 void RenderMonitoredPreview::draw(PainterInfo & pi, int x, int y) const
284 {
285 	RenderPreview::draw(pi, x, y);
286 	startMonitoring();
287 	monitor_->checkModifiedAsync();
288 }
289 
290 
connect(slot const & slot)291 signals2::connection RenderMonitoredPreview::connect(slot const & slot)
292 {
293 	return changed_.connect(slot);
294 }
295 
296 
monitoring() const297 bool RenderMonitoredPreview::monitoring() const
298 {
299 	return (bool) monitor_;
300 }
301 
302 
startMonitoring() const303 void RenderMonitoredPreview::startMonitoring() const
304 {
305 	if (!monitoring()) {
306 		monitor_ = FileSystemWatcher::activeMonitor(filename_);
307 		// Disconnected at the same time as this is destroyed.
308 		monitor_->connect([this](bool /* exists */){ changed_(); });
309 	}
310 }
311 
312 
stopMonitoring() const313 void RenderMonitoredPreview::stopMonitoring() const
314 {
315 	monitor_ = nullptr;
316 }
317 
318 
319 
320 
321 } // namespace lyx
322