]> git.lyx.org Git - lyx.git/blob - src/insets/RenderPreview.h
e9e3cb10c2440f3d868f5a2893ebbe13e3a8f8be
[lyx.git] / src / insets / RenderPreview.h
1 // -*- C++ -*-
2 /**
3  * \file RenderPreview.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  * 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 RENDERPREVIEW_H
17 #define RENDERPREVIEW_H
18
19 #include "RenderBase.h"
20
21 #include "support/docstring.h"
22 #include "support/FileMonitor.h"
23
24 #include <boost/signal.hpp>
25 #include <boost/signals/trackable.hpp>
26 #include <boost/signals/connection.hpp>
27
28 namespace lyx {
29
30 class Buffer;
31 class LyXRC_PreviewStatus;
32 class MetricsInfo;
33 class PainterInfo;
34
35 namespace support { class FileName; }
36
37 namespace graphics {
38
39 class PreviewImage;
40 class PreviewLoader;
41
42 } // namespace graphics
43
44
45 class RenderPreview : public RenderBase, public boost::signals::trackable {
46 public:
47         /// Return true if preview is enabled in text (from LyXRC::preview)
48         static bool previewText();
49         /// Return true if preview is enabled in mathed (from LyXRC::preview)
50         static bool previewMath();
51
52         RenderPreview(Inset const *);
53         RenderPreview(RenderPreview const &, Inset const *);
54         ~RenderPreview();
55         RenderBase * clone(Inset const *) const;
56
57         /// Compute the size of the object, returned in dim
58         void metrics(MetricsInfo &, Dimension & dim) const;
59         ///
60         void draw(PainterInfo & pi, int x, int y) const;
61
62         /** Find the PreviewLoader and add a LaTeX snippet to it.
63          *  Do not start the loading process.
64          *  \param ignore_lyxrc: generate the preview no matter what LyXRC says
65          */
66         void addPreview(docstring const & latex_snippet, Buffer const &,
67                         bool ignore_lyxrc = false);
68
69         /** Add a LaTeX snippet to the PreviewLoader.
70          *  Do not start the loading process.
71          *  \param ignore_lyxrc: generate the preview no matter what LyXRC says
72          */
73         void addPreview(docstring const & latex_snippet,
74                         graphics::PreviewLoader & ploader,
75                         bool ignore_lyxrc = false);
76
77         /// Record math macro definitions added to the preview loader
78         void addMacroDef(docstring const & latex_snippet, Buffer const & buffer);
79         /// Has a math macro definition already been added to the preview loader?
80         bool hasMacroDef(docstring const & latex_snippet, Buffer const & buffer);
81
82         /// Begin the loading process.
83         /// \param forexport : whether this is intended for export. if so,
84         /// then we ignore LyXRC and wait for the image to be generated.
85         void startLoading(Buffer const & buffer, bool forexport = false) const;
86
87         /** Remove a snippet from the cache of previews.
88          *  Useful if previewing the contents of a file that has changed.
89          */
90         void removePreview(Buffer const &);
91
92         /** \returns a pointer to the PreviewImage associated with this snippet
93          *  of latex.
94          */
95         graphics::PreviewImage const *
96         getPreviewImage(Buffer const & buffer) const;
97
98         /// equivalent to dynamic_cast
99         virtual RenderPreview * asPreview() { return this; }
100
101 private:
102         /// Not implemented.
103         RenderPreview & operator=(RenderPreview const &);
104
105         /// This method is connected to the PreviewLoader::imageReady signal.
106         void imageReady(graphics::PreviewImage const &);
107
108         /// The thing that we're trying to generate a preview of.
109         std::string snippet_;
110
111         /** Store the connection to the preview loader so that we connect
112          *  only once.
113          */
114         boost::signals::connection ploader_connection_;
115
116         /// Inform the core that the inset has changed.
117         Inset const * parent_;
118 };
119
120
121 class RenderMonitoredPreview : public RenderPreview {
122 public:
123         RenderMonitoredPreview(Inset const *);
124         ///
125         void draw(PainterInfo & pi, int x, int y) const;
126         ///
127         void setAbsFile(support::FileName const & file);
128         ///
129         bool monitoring() const { return monitor_.monitoring(); }
130         void startMonitoring() const { monitor_.start(); }
131         void stopMonitoring() const { monitor_.stop(); }
132
133         /// Connect and you'll be informed when the file changes.
134         typedef support::FileMonitor::slot_type slot_type;
135         boost::signals::connection fileChanged(slot_type const &);
136
137         /// equivalent to dynamic_cast
138         virtual RenderMonitoredPreview * asMonitoredPreview() { return this; }
139
140 private:
141         ///
142         mutable support::FileMonitor monitor_;
143 };
144
145 } // namespace lyx
146
147 #endif // RENDERPREVIEW_H