]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewedInset.C
Move #includes out of header files.
[lyx.git] / src / graphics / PreviewedInset.C
1 /**
2  * \file PreviewedInset.C
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 "PreviewedInset.h"
14 #include "GraphicsImage.h"
15 #include "PreviewLoader.h"
16 #include "PreviewImage.h"
17 #include "Previews.h"
18
19 #include "buffer.h"
20 #include "BufferView.h"
21
22 #include "frontends/LyXView.h"
23
24 #include "support/lstrings.h"
25
26 #include <boost/bind.hpp>
27
28 namespace support = lyx::support;
29
30
31 namespace lyx {
32 namespace graphics {
33
34 bool PreviewedInset::activated()
35 {
36         return Previews::activated();
37 }
38
39
40 BufferView * PreviewedInset::view() const
41 {
42         return inset_.view();
43 }
44
45
46 void PreviewedInset::generatePreview()
47 {
48         if (!Previews::activated() || !previewWanted() ||
49             !view() || !view()->buffer())
50                 return;
51
52         Previews & previews = Previews::get();
53         PreviewLoader & loader = previews.loader(*view()->buffer());
54         addPreview(loader);
55         if (!snippet_.empty())
56                 loader.startLoading();
57 }
58
59
60 void PreviewedInset::addPreview(PreviewLoader & ploader)
61 {
62         if (!Previews::activated() || !previewWanted())
63                 return;
64
65         snippet_ = support::trim(latexString());
66         if (snippet_.empty())
67                 return;
68
69         pimage_ = ploader.preview(snippet_);
70         if (pimage_)
71                 return;
72
73         // If this is the first time of calling, connect to the
74         // PreviewLoader signal that'll inform us when the preview image
75         // is ready for loading.
76         if (!connection_.connected()) {
77                 connection_ = ploader.connect(
78                         boost::bind(&PreviewedInset::imageReady, this, _1));
79         }
80
81         ploader.add(snippet_);
82 }
83
84
85 void PreviewedInset::removePreview()
86 {
87         if (!view() || !view()->buffer() || snippet_.empty())
88                 return;
89
90         Previews & previews = Previews::get();
91         PreviewLoader & loader = previews.loader(*view()->buffer());
92         loader.remove(snippet_);
93         snippet_.erase();
94         pimage_ = 0;
95 }
96
97
98 bool PreviewedInset::previewReady() const
99 {
100         if (!Previews::activated() || !previewWanted() ||
101             !view() || !view()->buffer())
102                 return false;
103
104         if (!pimage_ || snippet_ != pimage_->snippet()) {
105                 PreviewLoader & ploader =
106                         Previews::get().loader(*view()->buffer());
107                 pimage_ = ploader.preview(snippet_);
108         }
109
110         if (!pimage_)
111                 return false;
112
113         return pimage_->image();
114 }
115
116
117 void PreviewedInset::imageReady(PreviewImage const & pimage) const
118 {
119         // Check snippet against the Inset's current contents
120         if (snippet_ != pimage.snippet())
121                 return;
122
123         pimage_ = &pimage;
124
125         if (view())
126                 view()->updateInset(inset());
127 }
128
129 } // namespace graphics
130 } // namespace lyx