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
14 #include "PreviewedInset.h"
15 #include "GraphicsImage.h"
16 #include "PreviewLoader.h"
17 #include "PreviewImage.h"
21 #include "BufferView.h"
23 #include "frontends/LyXView.h"
25 #include "support/lstrings.h"
27 #include <boost/bind.hpp>
29 #include "debug.h" // temporary
33 bool PreviewedInset::activated()
35 return Previews::activated();
39 void PreviewedInset::generatePreview()
41 if (!Previews::activated() || !previewWanted() ||
42 !view() || !view()->buffer())
45 grfx::Previews & previews = grfx::Previews::get();
46 grfx::PreviewLoader & loader = previews.loader(view()->buffer());
48 if (!snippet_.empty())
49 loader.startLoading();
53 void PreviewedInset::addPreview(grfx::PreviewLoader & ploader)
55 if (!Previews::activated() || !previewWanted())
58 setView(ploader.buffer().getUser());
60 snippet_ = trim(latexString());
64 pimage_ = ploader.preview(snippet_);
68 // If this is the first time of calling, connect to the
69 // grfx::PreviewLoader signal that'll inform us when the preview image
70 // is ready for loading.
71 if (!connection_.connected()) {
72 connection_ = ploader.connect(
73 boost::bind(&PreviewedInset::imageReady, this, _1));
76 ploader.add(snippet_);
80 void PreviewedInset::removePreview()
82 if (!view() || !view()->buffer() || snippet_.empty())
85 grfx::Previews & previews = grfx::Previews::get();
86 grfx::PreviewLoader & loader = previews.loader(view()->buffer());
87 loader.remove(snippet_);
93 bool PreviewedInset::previewReady() const
95 if (!Previews::activated() || !previewWanted() ||
96 !view() || !view()->buffer())
99 if (!pimage_ || snippet_ != pimage_->snippet()) {
100 grfx::PreviewLoader & ploader =
101 grfx::Previews::get().loader(view()->buffer());
102 pimage_ = ploader.preview(snippet_);
108 return pimage_->image(inset_, *view());
112 void PreviewedInset::setView(BufferView * bv)
117 view_ = bv->owner()->view();
120 void PreviewedInset::imageReady(grfx::PreviewImage const & pimage) const
122 // Check snippet against the Inset's current contents
123 if (snippet_ != pimage.snippet())
129 view()->updateInset(&inset_, false);