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