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