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