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