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