]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewedInset.C
Previews for \input insets.
[lyx.git] / src / graphics / PreviewedInset.C
1 // -*- C++ -*-
2 /**
3  *  \file PreviewedInset.C
4  *  Copyright 2002 the LyX Team
5  *  Read the file COPYING
6  *
7  * \author Angus Leeming <leeming@lyx.org>
8  */
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #endif
13
14 #include <config.h>
15
16 #include "PreviewedInset.h"
17
18 #include "BufferView.h"
19
20 #include "GraphicsImage.h"
21 #include "PreviewLoader.h"
22 #include "PreviewImage.h"
23 #include "Previews.h"
24
25 #include <boost/bind.hpp>
26
27
28 namespace grfx {
29
30 bool PreviewedInset::activated()
31 {
32         return Previews::activated();
33 }
34
35
36 void PreviewedInset::generatePreview() const
37 {
38         if (!Previews::activated() || !previewWanted() ||
39             !view() || !view()->buffer())
40                 return;
41
42         grfx::Previews & previews = grfx::Previews::get();
43         grfx::PreviewLoader & loader = previews.loader(view()->buffer());
44         addPreview(loader);
45         loader.startLoading();
46 }
47
48
49 void PreviewedInset::addPreview(grfx::PreviewLoader & ploader) const
50 {
51         if (!Previews::activated() || !previewWanted())
52                 return;
53
54         // Generate the LaTeX snippet.
55         string const snippet = latexString();
56
57         pimage_ = ploader.preview(snippet);
58         if (pimage_)
59                 return;
60
61         // If this is the first time of calling, connect to the
62         // grfx::PreviewLoader signal that'll inform us when the preview image
63         // is ready for loading.
64         if (!connection_.connected()) {
65                 connection_ = ploader.connect(
66                         boost::bind(&PreviewedInset::imageReady, this, _1));
67         }
68
69         ploader.add(snippet);
70 }
71
72
73 bool PreviewedInset::previewReady() const
74 {
75         if (!grfx::Previews::activated() || !previewWanted() ||
76             !view() || !view()->buffer())
77                 return false;
78
79         // If the cached grfx::PreviewImage is invalid, update it.
80         string const snippet = latexString();
81
82         if (!pimage_ || snippet != pimage_->snippet()) {
83                 grfx::PreviewLoader & ploader =
84                         grfx::Previews::get().loader(view()->buffer());
85                 pimage_ = ploader.preview(snippet);
86         }
87
88         if (!pimage_)
89                 return false;
90
91         return pimage_->image(inset_, *view());
92 }
93
94
95 void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const
96 {
97         // Check snippet against the Inset's current contents
98         if (latexString() != pimage.snippet())
99                 return;
100
101         pimage_ = &pimage;
102         if (view())
103                 view()->updateInset(&inset_, false);
104 }
105
106 } // namespace grfx