]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.h
* GuiApplication.cpp: prevent null pointer.
[lyx.git] / src / frontends / qt4 / GuiView.h
1 // -*- C++ -*-
2 /**
3  * \file GuiView.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjornes
8  * \author John Levon
9  * \author Abdelrazak Younes
10  * \author Peter Kümmel
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef GUI_VIEW_H
16 #define GUI_VIEW_H
17
18 #include "frontends/Delegates.h"
19
20 #include "support/strfwd.h"
21
22 #include <QMainWindow>
23
24 class QCloseEvent;
25 class QDragEnterEvent;
26 class QDropEvent;
27 class QMenu;
28 class QShowEvent;
29
30
31 namespace lyx {
32
33 namespace support { class FileName; }
34
35 class Buffer;
36 class BufferView;
37 class Cursor;
38 class DispatchResult;
39 class FuncStatus;
40 class FuncRequest;
41 class Inset;
42
43 namespace frontend {
44
45 class Dialog;
46 class LayoutBox;
47 class GuiToolbar;
48 class GuiWorkArea;
49 class TabWorkArea;
50 class TocModels;
51 class ToolbarInfo;
52
53 /**
54  * GuiView - Qt4 main LyX window
55  *
56  * This class represents the main LyX window and provides
57  * accessor functions to its content.
58  *
59  * Note: a QObject emits a destroyed(QObject *) Qt signal when it
60  * is deleted. This might be useful for closing other dialogs
61  * depending on a given GuiView.
62  */
63 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
64         public GuiBufferDelegate
65 {
66         Q_OBJECT
67 public:
68         /// create a main window of the given dimensions
69         GuiView(int id);
70
71         ~GuiView();
72
73         int id() const { return id_; }
74
75         void setBusy(bool);
76
77         /// \name Generic accessor functions
78         //@{
79         /// The current BufferView refers to the BufferView that has the focus,
80         /// including for example the one that is created when you use the
81         /// advanced search and replace pane.
82         /// \return the currently selected buffer view.
83         BufferView * currentBufferView();
84         BufferView const * currentBufferView() const;
85
86         /// The document BufferView always refers to the view's main document
87         /// BufferView. So, even if the BufferView in e.g., the advanced
88         /// search and replace pane has the focus.
89         /// \return the current document buffer view.
90         BufferView * documentBufferView();
91         BufferView const * documentBufferView() const;
92
93         void newDocument(std::string const & filename,
94                 bool fromTemplate);
95
96         /// display a message in the view
97         /// could be called from any thread
98         void message(docstring const &);
99         /// must be called from GUI thread
100         void updateMessage(QString const & str);
101
102         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
103         /// dispatch command.
104         /// \return true if the \c FuncRequest has been dispatched.
105         void dispatch(FuncRequest const & cmd, DispatchResult & dr);
106
107         void restartCursor();
108         /// Update the completion popup and the inline completion state.
109         /// If \c start is true, then a new completion might be started.
110         /// If \c keep is true, an active completion will be kept active
111         /// even though the cursor moved. The update flags of \c cur might
112         /// be changed.
113         void updateCompletion(Cursor & cur, bool start, bool keep);
114
115         ///
116         void setFocus();
117
118         ///
119         void focusInEvent(QFocusEvent * e);
120         /// set a buffer to the current workarea.
121         void setBuffer(Buffer * b); ///< \c Buffer to set.
122
123         /// load a document into the current workarea.
124         Buffer * loadDocument(
125                 support::FileName const &  name, ///< File to load.
126                 bool tolastfiles = true  ///< append to the "Open recent" menu?
127                 );
128
129         /// add toolbar, if newline==true, add a toolbar break before the toolbar
130         GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
131         void updateStatusBar();
132
133         /// updates the possible layouts selectable
134         void updateLayoutList();
135         void updateToolbars();
136         QMenu * createPopupMenu();
137
138         ///
139         LayoutBox * getLayoutDialog() const;
140
141         /// hides the workarea and makes sure it is clean
142         bool hideWorkArea(GuiWorkArea * wa);
143         /// closes the workarea
144         bool closeWorkArea(GuiWorkArea * wa);
145         /// closes the buffer
146         bool closeBuffer(Buffer & buf);
147         ///
148         void openDocument(std::string const & filename);
149         ///
150         void importDocument(std::string const &);
151
152         /// \name GuiBufferDelegate.
153         //@{
154         void resetAutosaveTimers();
155         void errors(std::string const &, bool from_master = false);
156         void structureChanged();
157         void updateTocItem(std::string const &, DocIterator const &);
158         //@}
159
160         ///
161         TocModels & tocModels();
162         
163         /// called on timeout
164         void autoSave();
165
166         /// check for external change of any opened buffer, mainly for svn usage
167         void checkExternallyModifiedBuffers();
168
169         /** redraw \c inset in all the BufferViews in which it is currently
170          *  visible. If successful return a pointer to the owning Buffer.
171          */
172         Buffer const * updateInset(Inset const *);
173
174         /// \return the \c Workarea associated to \p  Buffer
175         /// \retval 0 if no \c WorkArea is found.
176         GuiWorkArea * workArea(Buffer & buffer);
177
178         /// Add a \c WorkArea 
179         /// \return the \c Workarea associated to \p  Buffer
180         /// \retval 0 if no \c WorkArea is found.
181         GuiWorkArea * addWorkArea(Buffer & buffer);
182         /// \param work_area The current \c WorkArea, or \c NULL
183         void setCurrentWorkArea(GuiWorkArea * work_area);
184         ///
185         void removeWorkArea(GuiWorkArea * work_area);
186         /// return the current WorkArea (the one that has the focus).
187         GuiWorkArea const * currentWorkArea() const;
188         /// return the current WorkArea (the one that has the focus).
189         GuiWorkArea * currentWorkArea();
190
191         /// return the current document WorkArea (it may not have the focus).
192         GuiWorkArea const * currentMainWorkArea() const;
193         /// return the current document WorkArea (it may not have the focus).
194         GuiWorkArea * currentMainWorkArea();
195
196 Q_SIGNALS:
197         void closing(int);
198         void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
199
200 public Q_SLOTS:
201         /// idle timeout.
202         /// clear any temporary message and replace with current status.
203         void clearMessage();
204
205 private Q_SLOTS:
206         ///
207         void updateWindowTitle(GuiWorkArea * wa);
208         ///
209         void resetWindowTitleAndIconText();
210
211         ///
212         void on_currentWorkAreaChanged(GuiWorkArea *);
213         ///
214         void on_lastWorkAreaRemoved();
215
216         /// slots to change the icon size
217         void smallSizedIcons();
218         void normalSizedIcons();
219         void bigSizedIcons();
220
221         /// For completion of autosave or exporrt threads.
222         void threadFinished();
223
224         /// must be called in GUI thread
225         void doShowDialog(QString const & qname, QString const & qdata,
226         Inset * inset);
227
228 private:
229         /// Open given child document in current buffer directory.
230         void openChildDocument(std::string const & filename);
231         /// Close current document buffer.
232         bool closeBuffer();
233         /// Close all document buffers.
234         bool closeBufferAll();
235         ///
236         TabWorkArea * addTabWorkArea();
237
238         /// connect to signals in the given BufferView
239         void connectBufferView(BufferView & bv);
240         /// disconnect from signals in the given BufferView
241         void disconnectBufferView();
242         /// connect to signals in the given buffer
243         void connectBuffer(Buffer & buf);
244         /// disconnect from signals in the given buffer
245         void disconnectBuffer();
246         ///
247         void dragEnterEvent(QDragEnterEvent * ev);
248         ///
249         void dropEvent(QDropEvent * ev);
250         /// make sure we quit cleanly
251         void closeEvent(QCloseEvent * e);
252         ///
253         void showEvent(QShowEvent *);
254
255         /// in order to catch Tab key press.
256         bool event(QEvent * e);
257         bool focusNextPrevChild(bool);
258
259         ///
260         bool goToFileRow(std::string const & argument);
261
262         ///
263         struct GuiViewPrivate;
264         GuiViewPrivate & d;
265
266 public:
267         ///
268         /// dialogs for this view
269         ///
270
271         ///
272         void resetDialogs();
273
274         /// Hide all visible dialogs
275         void hideAll() const;
276
277         /// Update all visible dialogs.
278         /** 
279          *  Check the status of all visible dialogs and disable or reenable
280          *  them as appropriate.
281          *
282          *  Disabling is needed for example when a dialog is open and the
283          *  cursor moves to a position where the corresponding inset is not
284          *  allowed.
285          */
286         void updateDialogs();
287
288         /** Show dialog could be called from arbitrary threads.
289             \param name == "bibtex", "citation" etc; an identifier used to
290             launch a particular dialog.
291             \param data is a string representation of the Inset contents.
292             It is often little more than the output from Inset::write.
293             It is passed to, and parsed by, the frontend dialog.
294             Several of these dialogs do not need any data,
295             so it defaults to string().
296             \param inset ownership is _not_ passed to the frontend dialog.
297             It is stored internally and used by the kernel to ascertain
298             what to do with the FuncRequest dispatched from the frontend
299             dialog on 'Apply'; should it be used to create a new inset at
300             the current cursor position or modify an existing, 'open' inset?
301         */
302         void showDialog(std::string const & name,
303                 std::string const & data, Inset * inset = 0);
304
305         /** \param name == "citation", "bibtex" etc; an identifier used
306             to reset the contents of a particular dialog with \param data.
307             See the comments to 'show', above.
308         */
309         void updateDialog(std::string const & name, std::string const & data);
310
311         /** All Dialogs of the given \param name will be closed if they are
312             connected to the given \param inset.
313         */
314         void hideDialog(std::string const & name, Inset * inset);
315         ///
316         void disconnectDialog(std::string const & name);
317
318 private:
319         ///
320         void saveLayout() const;
321         ///
322         bool restoreLayout();
323         ///
324         GuiToolbar * toolbar(std::string const & name);
325         ///
326         void constructToolbars();
327         ///
328         void initToolbars();
329         ///
330         bool lfunUiToggle(std::string const & ui_component);
331         ///
332         void toggleFullScreen();
333         ///
334         void insertLyXFile(docstring const & fname);
335         ///
336         void insertPlaintextFile(docstring const & fname,
337                 bool asParagraph);
338
339         /// Save a buffer as a new file. 
340         /**
341         Write a buffer to a new file name and rename the buffer
342     according to the new file name.
343
344     This function is e.g. used by menu callbacks and
345     LFUN_BUFFER_WRITE_AS.
346
347     If 'newname' is empty, the user is asked via a
348     dialog for the buffer's new name and location.
349
350     If 'newname' is non-empty and has an absolute path, that is used.
351     Otherwise the base directory of the buffer is used as the base
352     for any relative path in 'newname'.
353         */
354         bool renameBuffer(Buffer & b, docstring const & newname);
355         ///
356         bool saveBuffer(Buffer & b);
357         /// closes a workarea, if close_buffer is true the buffer will
358         /// also be released, otherwise the buffer will be hidden.
359         bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
360         /// closes the tabworkarea and all tabs. If we are in a close event,
361         /// all buffers will be closed, otherwise they will be hidden.
362         bool closeTabWorkArea(TabWorkArea * twa);
363         /// gives the user the possibility to save his work 
364         /// or to discard the changes. If hiding is true, the
365         /// document will be reloaded.
366         bool saveBufferIfNeeded(Buffer & buf, bool hiding);
367         /// closes all workareas
368         bool closeWorkAreaAll();
369         /// write all open workareas into the session file
370         void writeSession() const;
371         /// is the buffer in this workarea also shown in another tab ?
372         /// This tab can either be in the same view or in another one.
373         bool inMultiTabs(GuiWorkArea * wa);
374         /// is the buffer in this workarea also shown in another view ?
375         bool inMultiViews(GuiWorkArea * wa);
376         ///
377         enum NextOrPrevious {
378                 NEXTBUFFER,
379                 PREVBUFFER
380         };
381         ///
382         void gotoNextOrPreviousBuffer(NextOrPrevious np);
383
384         /// Is the dialog currently visible?
385         bool isDialogVisible(std::string const & name) const;
386         ///
387         Dialog * findOrBuild(std::string const & name, bool hide_it);
388         ///
389         Dialog * build(std::string const & name);
390         ///
391         void reloadBuffer();
392         ///
393         void dispatchVC(FuncRequest const & cmd);
394         ///
395         void showMessage();
396
397         /// This view ID.
398         int id_;
399
400         /// flag to avoid two concurrent close events.
401         bool closing_;
402 };
403
404 } // namespace frontend
405 } // namespace lyx
406
407 #endif // GUIVIEW_H