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