]> git.lyx.org Git - lyx.git/blob - src/insets/render_preview.h
the exception safety patch
[lyx.git] / src / insets / render_preview.h
1 // -*- C++ -*-
2 /**
3  * \file render_preview.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * lyx::graphics::RenderPreview is an abstract base class that can help
12  * insets to generate previews. The daughter class must instantiate two small
13  * methods. The Inset would own an instance of this daughter class.
14  */
15
16 #ifndef RENDER_PREVIEW_H
17 #define RENDER_PREVIEW_H
18
19 #include "render_base.h"
20
21 #include "support/FileMonitor.h"
22
23 #include <boost/signals/signal0.hpp>
24 #include <boost/signals/trackable.hpp>
25 #include <boost/signals/connection.hpp>
26
27 class Buffer;
28 class BufferView;
29 class MetricsInfo;
30 class PainterInfo;
31
32 namespace lyx {
33 namespace graphics {
34
35 class PreviewImage;
36 class PreviewLoader;
37
38 } // namespace graphics
39 } // namespace lyx
40
41
42 class RenderPreview : public RenderBase, public boost::signals::trackable {
43 public:
44         /// a wrapper for Previews::activated()
45         static bool activated();
46
47         RenderPreview();
48         RenderPreview(RenderPreview const &);
49         std::auto_ptr<RenderBase> clone() const;
50
51         /// Compute the size of the object, returned in dim
52         void metrics(MetricsInfo &, Dimension & dim) const;
53         ///
54         void draw(PainterInfo & pi, int x, int y) const;
55
56         /** Find the PreviewLoader and add a LaTeX snippet to it.
57          *  Do not start the loading process.
58          */
59         void addPreview(std::string const & latex_snippet, Buffer const &);
60
61         /** Add a LaTeX snippet to the PreviewLoader.
62          *  Do not start the loading process.
63          */
64         void addPreview(std::string const & latex_snippet,
65                         lyx::graphics::PreviewLoader & ploader);
66
67         /// Begin the loading process.
68         void startLoading(Buffer const & buffer) const;
69
70         /** Remove a snippet from the cache of previews.
71          *  Useful if previewing the contents of a file that has changed.
72          */
73         void removePreview(Buffer const &);
74
75         /// The preview has been generated and is ready to use.
76         bool previewReady() const;
77
78         /// Connect and you'll be informed when the preview is ready.
79         typedef boost::signal0<void>::slot_type slot_type;
80         boost::signals::connection connect(slot_type const &);
81
82         /// equivalent to dynamic_cast
83         virtual RenderPreview * asPreview() { return this; }
84
85 private:
86         /// Not implemented.
87         void operator=(RenderPreview const &);
88
89         /// This method is connected to the PreviewLoader::imageReady signal.
90         void imageReady(lyx::graphics::PreviewImage const &);
91
92         /// The thing that we're trying to generate a preview of.
93         std::string snippet_;
94
95         /// We don't own this. Cached for efficiency reasons.
96         lyx::graphics::PreviewImage const * pimage_;
97
98         /** Store the connection to the preview loader so that we connect
99          *  only once.
100          */
101         boost::signals::connection ploader_connection_;
102
103         /// This signal is emitted when the preview is ready for display.
104         boost::signal0<void> preview_ready_signal_;
105 };
106
107
108 class RenderMonitoredPreview : public RenderPreview {
109 public:
110         RenderMonitoredPreview() : monitor_(std::string(), 2000) {}
111         ///
112         void draw(PainterInfo & pi, int x, int y) const;
113         ///
114         void setAbsFile(std::string const & file);
115         ///
116         bool monitoring() const { return monitor_.monitoring(); }
117         void startMonitoring() const { monitor_.start(); }
118         void stopMonitoring() const { monitor_.stop(); }
119
120         /// Connect and you'll be informed when the file changes.
121         boost::signals::connection fileChanged(slot_type const &);
122
123         /// equivalent to dynamic_cast
124         virtual RenderMonitoredPreview * asMonitoredPreview() { return this; }
125
126 private:
127         ///
128         mutable lyx::support::FileMonitor monitor_;
129 };
130
131 #endif // RENDERPREVIEW_H