]> git.lyx.org Git - lyx.git/blob - src/graphics/PreviewedInset.C
Modify the headers of files in src/graphics as discussed on the list.
[lyx.git] / src / graphics / PreviewedInset.C
1 // -*- C++ -*-
2 /**
3  *  \file PreviewedInset.C
4  *  Read the file COPYING
5  *
6  * \author Angus Leeming 
7  *
8  * Full author contact details available in file CREDITS
9  */
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <config.h>
16
17 #include "PreviewedInset.h"
18 #include "GraphicsImage.h"
19 #include "PreviewLoader.h"
20 #include "PreviewImage.h"
21 #include "Previews.h"
22
23 #include "buffer.h"
24 #include "BufferView.h"
25
26 #include "frontends/LyXView.h"
27
28 #include "support/lstrings.h"
29
30 #include <boost/bind.hpp>
31
32 #include "debug.h"    // temporary
33
34 namespace grfx {
35
36 bool PreviewedInset::activated()
37 {
38         return Previews::activated();
39 }
40
41
42 void PreviewedInset::generatePreview()
43 {
44         if (!Previews::activated() || !previewWanted() ||
45             !view() || !view()->buffer())
46                 return;
47
48         grfx::Previews & previews = grfx::Previews::get();
49         grfx::PreviewLoader & loader = previews.loader(view()->buffer());
50         addPreview(loader);
51         if (!snippet_.empty())
52                 loader.startLoading();
53 }
54
55
56 void PreviewedInset::addPreview(grfx::PreviewLoader & ploader)
57 {
58         if (!Previews::activated() || !previewWanted())
59                 return;
60
61         setView(ploader.buffer().getUser());
62
63         snippet_ = 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         // grfx::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         grfx::Previews & previews = grfx::Previews::get();
89         grfx::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                 grfx::PreviewLoader & ploader =
104                         grfx::Previews::get().loader(view()->buffer());
105                 pimage_ = ploader.preview(snippet_);
106         }
107
108         if (!pimage_)
109                 return false;
110
111         return pimage_->image(inset_, *view());
112 }
113
114
115 void PreviewedInset::setView(BufferView * bv)
116 {
117         if (!bv)
118                 return;
119         
120         view_ = bv->owner()->view();
121 }
122
123 void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const
124 {
125         // Check snippet against the Inset's current contents
126         if (snippet_ != pimage.snippet())
127                 return;
128
129         pimage_ = &pimage;
130
131         if (view())
132                 view()->updateInset(&inset_, false);
133 }
134
135 } // namespace grfx