2 * \file PreviewedInset.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "PreviewedInset.h"
14 #include "PreviewImage.h"
15 #include "PreviewLoader.h"
18 #include "BufferView.h"
20 #include "insets/inset.h"
22 #include "support/lstrings.h"
24 #include <boost/bind.hpp>
26 namespace support = lyx::support;
32 bool PreviewedInset::activated()
34 return Previews::activated();
38 BufferView * PreviewedInset::view() const
44 void PreviewedInset::generatePreview()
46 if (!Previews::activated() || !previewWanted() ||
47 !view() || !view()->buffer())
50 Previews & previews = Previews::get();
51 PreviewLoader & loader = previews.loader(*view()->buffer());
53 if (!snippet_.empty())
54 loader.startLoading();
58 void PreviewedInset::addPreview(PreviewLoader & ploader)
60 if (!Previews::activated() || !previewWanted())
63 snippet_ = support::trim(latexString());
67 pimage_ = ploader.preview(snippet_);
71 // If this is the first time of calling, connect to the
72 // 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));
79 ploader.add(snippet_);
83 void PreviewedInset::removePreview()
85 if (!view() || !view()->buffer() || snippet_.empty())
88 Previews & previews = Previews::get();
89 PreviewLoader & loader = previews.loader(*view()->buffer());
90 loader.remove(snippet_);
96 bool PreviewedInset::previewReady() const
98 if (!Previews::activated() || !previewWanted() ||
99 !view() || !view()->buffer())
102 if (!pimage_ || snippet_ != pimage_->snippet()) {
103 PreviewLoader & ploader =
104 Previews::get().loader(*view()->buffer());
105 pimage_ = ploader.preview(snippet_);
111 return pimage_->image();
115 void PreviewedInset::imageReady(PreviewImage const & pimage) const
117 // Check snippet against the Inset's current contents
118 if (snippet_ != pimage.snippet())
124 view()->updateInset(inset());
127 } // namespace graphics