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