]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.h
Crash fix.
[lyx.git] / src / frontends / qt4 / Dialog.h
1 // -*- C++ -*-
2 /**
3  * \file Dialog.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
12 #ifndef DIALOG_H
13 #define DIALOG_H
14
15 #include "lfuns.h"
16
17 #include <string>
18
19 class QString;
20 class QWidget;
21
22 namespace lyx {
23
24 class Buffer;
25 class BufferView;
26 class FuncRequest;
27
28 namespace frontend {
29
30 class GuiView;
31
32 /** \enum KernelDocType used to flag the different kinds of buffer
33  *  without making the kernel header files available to the
34  *  dialog's Controller or View.
35  */
36 enum KernelDocType
37 {
38         LATEX,
39         LITERATE,
40         DOCBOOK
41 };
42
43
44 /** \c Dialog collects the different parts of a Model-Controller-View
45  *  split of a generic dialog together.
46  */
47 class Dialog 
48 {
49 public:
50         /// \param lv is the access point for the dialog to the LyX kernel.
51         /// \param name is the identifier given to the dialog by its parent
52         /// container.
53         Dialog(GuiView & lv, std::string const & name);
54
55         virtual ~Dialog();
56
57         virtual QWidget * asQWidget() = 0;
58         virtual QWidget const * asQWidget() const = 0;
59
60         /// Session key.
61         /**
62          * This key must be used for any session setting.
63          **/
64         QString sessionKey() const;
65
66         /// Save session settings.
67         /**
68          * This default implementation saves the geometry state.
69          * Reimplement to save more settings.
70          **/
71         virtual void saveSession() const;
72
73         /// Restore session settings.
74         /**
75          * This default implementation restores the geometry state.
76          * Reimplement to restore more settings.
77          **/
78         virtual void restoreSession();
79
80         /** \name Container Access
81          *  These methods are publicly accessible because they are invoked
82          *  by the parent container acting on commands from the LyX kernel.
83          */
84         //@{
85         /// \param data is a string encoding of the data to be displayed.
86         /// It is passed to the Controller to be translated into a useable form.
87         virtual void showData(std::string const & data);
88         virtual void updateData(std::string const & data);
89         //@}
90
91         /** Check whether we may apply our data.
92          *
93          *  The buttons are disabled if not and (re-)enabled if yes.
94          */
95         virtual void checkStatus();
96
97         /** When applying, it's useful to know whether the dialog is about
98          *  to close or not (no point refreshing the display for example).
99          */
100         virtual bool isClosing() const { return false; }
101
102         /** \c View part
103          *  of a Model-Controller-View split of a generic dialog.
104          *  These few methods are all that a generic dialog needs of a
105          *  view.
106          */
107         //@{
108         /** A request to modify the data structures stored by the
109          *  accompanying Controller in preparation for their dispatch to
110          *  the LyX kernel.
111          */
112         virtual void applyView() = 0;
113
114         /// Hide the dialog from sight
115         void hideView();
116
117         /// Create the dialog if necessary, update it and display it.
118         void showView();
119
120         /// Update the display of the dialog whilst it is still visible.
121         virtual void updateView() = 0;
122
123         // Default Implementation does nothing.
124         // Each dialog has to choose what control to enable or disable.
125         virtual void enableView(bool /*enable*/) {}
126
127         /// \return true if the dialog is visible.
128         virtual bool isVisibleView() const;
129         //@}
130
131         /// Dialog identifier.
132         /// FIXME for Andre': Now that Dialog is entirely within qt4/
133         /// We can use QString instead in order to avoid <string> inclusion
134         /// or we can pimpl name_.
135         std::string const & name() const;
136
137         //@{
138         /** Enable the controller to initialise its data structures.
139          *  \param data is a string encoding of the parameters to be displayed.
140          *  \return true if the translation was successful.
141          */
142         virtual bool initialiseParams(std::string const & data) = 0;
143
144         /// Enable the controller to clean up its data structures.
145         virtual void clearParams() = 0;
146
147         /// Enable the Controller to dispatch its data back to the LyX kernel.
148         virtual void dispatchParams() = 0;
149
150         /** \return true if the dialog should be shown only when
151          *  a buffer is open.
152          */
153         virtual bool isBufferDependent() const = 0;
154
155         /** \return true if the dialog can apply data also
156          *  for ReadOnly buffers.
157          *  This has to be distinguished from isBufferDependent()
158          */
159         virtual bool canApplyToReadOnly() const { return false; }
160
161         /** The lfun that is sent for applying the data.
162          *
163          * This method is used by the default implementation of canApply()
164          * for buffer dependent dialogs that send one lfun when applying the
165          * data.
166          * It should be used in dispatchParams(), too for consistency reasons.
167          *  \returns the lfun that is sent for applying the data.
168          */
169         virtual kb_action getLfun() const { return LFUN_INSET_APPLY; }
170
171         /** Check whether we may apply our data.
172          *
173          * The default implementation works for all dialogs that send one
174          * lfun when applying the data. Dialogs that send none or more than
175          * one lfun need to reimplement it.
176          *  \returns whether the data can be applied or not.
177          */
178         virtual bool canApply() const;
179
180         /** \return true if the kernel should disconnect the dialog from
181          *  a particular inset after the data has been applied to it.
182          *  Clearly this makes sense only for dialogs modifying the contents
183          *  of an inset :-)
184          *  In practise, only a very few dialogs (e.g. the citation dialog)
185          *  return true.
186          */
187         virtual bool disconnectOnApply() const { return false; }
188
189         /** \return true if Dialog::View::show() should not display the dialog
190          *   after running update. Currently, only ControlSpellchecker
191          *   makes use of that.
192         */
193         virtual bool exitEarly() const { return false; }
194         //@}
195
196         /** \c Kernel part: a wrapper making the LyX kernel available to the dialog.
197          * (Ie, it provides an interface to the Model part of the Model-Controller-
198          *  View split.
199          *  In an ideal world, it will shrink as more info is passed to the
200          *  Dialog::show() and Dialog::update() methods.
201          */
202
203
204         /** This method is the primary purpose of the class. It provides
205          *  the "gateway" by which the dialog can send a request (of a
206          *  change in the data, for more information) to the kernel.
207          *  \param fr is the encoding of the request.
208          */
209         void dispatch(FuncRequest const & fr) const;
210
211         /** The dialog has received a request from the user
212          *  (who pressed the "Restore" button) to update contents.
213          *  It must, therefore, ask the kernel to provide this information.
214          *  \param name is used to identify the dialog to the kernel.
215          */
216         void updateDialog() const;
217
218         /** A request from the Controller that future changes to the data
219          *  stored by the dialog are not applied to the inset currently
220          *  connected to the dialog. Instead, they will be used to generate
221          *  a new inset at the cursor position.
222          *  \param name is used to identify the dialog to the kernel.
223          */
224         void disconnect() const;
225
226         /** \name Kernel Wrappers
227          *  Simple wrapper functions to Buffer methods.
228          */
229         //@{
230         bool isBufferAvailable() const;
231         bool isBufferReadonly() const;
232         std::string const bufferFilepath() const;
233         //@}
234
235         /// The type of the current buffer.
236         KernelDocType docType() const;
237
238         /** \name Kernel Nasties
239          *  Unpleasantly public internals of the LyX kernel.
240          *  We should aim to reduce/remove these from the interface.
241          */
242         //@{
243         GuiView & lyxview() { return *lyxview_; }
244         GuiView const & lyxview() const { return *lyxview_; }
245
246         Buffer & buffer();
247         Buffer const & buffer() const;
248
249         BufferView * bufferview();
250         BufferView const * bufferview() const;
251         //@}
252
253 protected:
254         virtual void apply();
255
256 private:
257         /** The Dialog's name is the means by which a dialog identifies
258          *  itself to the LyXView.
259          */
260         std::string const name_;
261         ///
262         GuiView * lyxview_;
263
264         /// intentionally unimplemented, therefore uncopiable
265         Dialog(Dialog const &);
266         void operator=(Dialog const &);
267
268 };
269
270
271 } // namespace frontend
272 } // namespace lyx
273
274 #endif // DIALOG_H