3 * \file PreviewedInset.C
4 * Copyright 2002 the LyX Team
5 * Read the file COPYING
7 * \author Angus Leeming <leeming@lyx.org>
11 #pragma implementation
16 #include "PreviewedInset.h"
18 #include "BufferView.h"
20 #include "GraphicsImage.h"
21 #include "PreviewLoader.h"
22 #include "PreviewImage.h"
25 #include <boost/bind.hpp>
30 bool PreviewedInset::activated()
32 return Previews::activated();
36 void PreviewedInset::generatePreview() const
38 if (!Previews::activated() || !previewWanted() ||
39 !view() || !view()->buffer())
42 grfx::Previews & previews = grfx::Previews::get();
43 grfx::PreviewLoader & loader = previews.loader(view()->buffer());
45 loader.startLoading();
49 void PreviewedInset::addPreview(grfx::PreviewLoader & ploader) const
51 if (!Previews::activated() || !previewWanted())
54 // Generate the LaTeX snippet.
55 string const snippet = latexString();
57 pimage_ = ploader.preview(snippet);
61 // If this is the first time of calling, connect to the
62 // grfx::PreviewLoader signal that'll inform us when the preview image
63 // is ready for loading.
64 if (!connection_.connected()) {
65 connection_ = ploader.connect(
66 boost::bind(&PreviewedInset::imageReady, this, _1));
73 bool PreviewedInset::previewReady() const
75 if (!grfx::Previews::activated() || !previewWanted() ||
76 !view() || !view()->buffer())
79 // If the cached grfx::PreviewImage is invalid, update it.
80 string const snippet = latexString();
82 if (!pimage_ || snippet != pimage_->snippet()) {
83 grfx::PreviewLoader & ploader =
84 grfx::Previews::get().loader(view()->buffer());
85 pimage_ = ploader.preview(snippet);
91 return pimage_->image(inset_, *view());
95 void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const
97 // Check snippet against the Inset's current contents
98 if (latexString() != pimage.snippet())
103 view()->updateInset(&inset_, false);