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