]> git.lyx.org Git - lyx.git/blob - src/graphics/Previews.cpp
hand-crafted LyXErr
[lyx.git] / src / graphics / Previews.cpp
1 /**
2  * \file Previews.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Previews.h"
14 #include "PreviewLoader.h"
15
16 #include "Buffer.h"
17 #include "InsetIterator.h"
18 #include "LyXRC.h"
19
20 #include "insets/Inset.h"
21
22
23 namespace lyx {
24
25 namespace graphics {
26
27 LyXRC_PreviewStatus Previews::status()
28 {
29         return lyxrc.preview;
30 }
31
32
33 Previews & Previews::get()
34 {
35         static Previews singleton;
36         return singleton;
37 }
38
39
40 class Previews::Impl {
41 public:
42         ///
43         typedef boost::shared_ptr<PreviewLoader> PreviewLoaderPtr;
44         ///
45         typedef std::map<Buffer const *, PreviewLoaderPtr> CacheType;
46         ///
47         CacheType cache;
48 };
49
50
51 Previews::Previews()
52         : pimpl_(new Impl)
53 {}
54
55
56 Previews::~Previews()
57 {
58         delete pimpl_;
59 }
60
61
62 PreviewLoader & Previews::loader(Buffer const & buffer) const
63 {
64         Impl::CacheType::iterator it = pimpl_->cache.find(&buffer);
65
66         if (it == pimpl_->cache.end()) {
67                 Impl::PreviewLoaderPtr ptr(new PreviewLoader(buffer));
68                 pimpl_->cache[&buffer] = ptr;
69                 return *ptr.get();
70         }
71
72         return *it->second.get();
73 }
74
75
76 void Previews::removeLoader(Buffer const & buffer) const
77 {
78         Impl::CacheType::iterator it = pimpl_->cache.find(&buffer);
79
80         if (it != pimpl_->cache.end())
81                 pimpl_->cache.erase(it);
82 }
83
84
85 void Previews::generateBufferPreviews(Buffer const & buffer) const
86 {
87         PreviewLoader & ploader = loader(buffer);
88
89         Inset & inset = buffer.inset();
90         InsetIterator it = inset_iterator_begin(inset);
91         InsetIterator const end = inset_iterator_end(inset);
92
93         for (; it != end; ++it)
94                 it->addPreview(ploader);
95
96         ploader.startLoading();
97 }
98
99 } // namespace graphics
100 } // namespace lyx