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