3 * \file PreviewedInset.C
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
13 #pragma implementation
18 #include "PreviewedInset.h"
19 #include "GraphicsImage.h"
20 #include "PreviewLoader.h"
21 #include "PreviewImage.h"
25 #include "BufferView.h"
27 #include "frontends/LyXView.h"
29 #include "support/lstrings.h"
31 #include <boost/bind.hpp>
33 #include "debug.h" // temporary
37 bool PreviewedInset::activated()
39 return Previews::activated();
43 void PreviewedInset::generatePreview()
45 if (!Previews::activated() || !previewWanted() ||
46 !view() || !view()->buffer())
49 grfx::Previews & previews = grfx::Previews::get();
50 grfx::PreviewLoader & loader = previews.loader(view()->buffer());
52 if (!snippet_.empty())
53 loader.startLoading();
57 void PreviewedInset::addPreview(grfx::PreviewLoader & ploader)
59 if (!Previews::activated() || !previewWanted())
62 setView(ploader.buffer().getUser());
64 snippet_ = trim(latexString());
68 pimage_ = ploader.preview(snippet_);
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));
80 ploader.add(snippet_);
84 void PreviewedInset::removePreview()
86 if (!view() || !view()->buffer() || snippet_.empty())
89 grfx::Previews & previews = grfx::Previews::get();
90 grfx::PreviewLoader & loader = previews.loader(view()->buffer());
91 loader.remove(snippet_);
97 bool PreviewedInset::previewReady() const
99 if (!Previews::activated() || !previewWanted() ||
100 !view() || !view()->buffer())
103 if (!pimage_ || snippet_ != pimage_->snippet()) {
104 grfx::PreviewLoader & ploader =
105 grfx::Previews::get().loader(view()->buffer());
106 pimage_ = ploader.preview(snippet_);
112 return pimage_->image(inset_, *view());
116 void PreviewedInset::setView(BufferView * bv)
121 view_ = bv->owner()->view();
124 void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const
126 // Check snippet against the Inset's current contents
127 if (snippet_ != pimage.snippet())
133 view()->updateInset(&inset_, false);