]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.h
QDialogButtonBox for the remaining dialogs.
[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 #include <QMenu>
24
25 class QCloseEvent;
26 class QDragEnterEvent;
27 class QDropEvent;
28 class QLabel;
29 class QShowEvent;
30
31
32 namespace lyx {
33
34 namespace support { class FileName; }
35
36 class Buffer;
37 class BufferView;
38 class Cursor;
39 class DispatchResult;
40 class FuncStatus;
41 class FuncRequest;
42 class Inset;
43
44 namespace frontend {
45
46 class Dialog;
47 class LayoutBox;
48 class GuiToolbar;
49 class GuiWorkArea;
50 class TabWorkArea;
51 class TocModels;
52 class ToolbarInfo;
53
54 /**
55  * GuiView - Qt main LyX window
56  *
57  * This class represents the main LyX window and provides
58  * accessor functions to its content.
59  *
60  * Note: a QObject emits a destroyed(QObject *) Qt signal when it
61  * is deleted. This might be useful for closing other dialogs
62  * depending on a given GuiView.
63  */
64 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
65         public GuiBufferDelegate
66 {
67         Q_OBJECT
68
69 public:
70         /// create a main window of the given dimensions
71         GuiView(int id);
72
73         ~GuiView();
74
75         /// closes the view such that the view knows that is closed
76         /// programmatically and not by the user clicking the x.
77         bool closeScheduled();
78
79         /// Things that need to be done when the OSes session manager
80         /// requests a log out.
81         bool prepareAllBuffersForLogout();
82
83         int id() const { return id_; }
84
85         /// are we busy ?
86         bool busy() const;
87
88         /// Signal that the any "auto" minibuffer can be closed now.
89         void resetCommandExecute();
90
91         /// \name Generic accessor functions
92         //@{
93         /// The current BufferView refers to the BufferView that has the focus,
94         /// including for example the one that is created when you use the
95         /// advanced search and replace pane.
96         /// \return the currently selected buffer view.
97         BufferView * currentBufferView();
98         BufferView const * currentBufferView() const;
99
100         /// The document BufferView always refers to the view's main document
101         /// BufferView. So, even if the BufferView in e.g., the advanced
102         /// search and replace pane has the focus.
103         /// \return the current document buffer view.
104         BufferView * documentBufferView();
105         BufferView const * documentBufferView() const;
106
107         void newDocument(std::string const & filename,
108                 bool fromTemplate);
109
110         /// display a message in the view
111         /// could be called from any thread
112         void message(docstring const &);
113
114         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
115         /// dispatch command.
116         /// \return true if the \c FuncRequest has been dispatched.
117         void dispatch(FuncRequest const & cmd, DispatchResult & dr);
118
119         void restartCaret();
120         /// Update the completion popup and the inline completion state.
121         /// If \c start is true, then a new completion might be started.
122         /// If \c keep is true, an active completion will be kept active
123         /// even though the cursor moved. The update flags of \c cur might
124         /// be changed.
125         void updateCompletion(Cursor & cur, bool start, bool keep);
126
127         ///
128         void setFocus();
129         bool hasFocus() const;
130
131         ///
132         void focusInEvent(QFocusEvent * e);
133         /// Add a Buffer to the View
134         /// \param b Buffer to set.
135         /// \param switch_to Whether to set it to the current workarea.
136         void setBuffer(Buffer * b, bool switch_to = true);
137
138         /// load a document into the current workarea.
139         Buffer * loadDocument(
140                 support::FileName const &  name, ///< File to load.
141                 bool tolastfiles = true  ///< append to the "Open recent" menu?
142                 );
143
144         /// add toolbar, if newline==true, add a toolbar break before the toolbar
145         GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
146         void updateStatusBar();
147
148         /// updates the possible layouts selectable
149         void updateLayoutList();
150         void updateToolbars();
151
152         ///
153         LayoutBox * getLayoutDialog() const;
154
155         /// hides the workarea and makes sure it is clean
156         bool hideWorkArea(GuiWorkArea * wa);
157         /// closes workarea; close buffer only if no other workareas point to it
158         bool closeWorkArea(GuiWorkArea * wa);
159         /// closes the buffer
160         bool closeBuffer(Buffer & buf);
161         ///
162         void openDocument(std::string const & filename);
163         ///
164         void importDocument(std::string const &);
165
166         /// \name GuiBufferDelegate.
167         //@{
168         void resetAutosaveTimers();
169         // shows an error list
170         // if from_master is true, show master's error list
171         void errors(std::string const &, bool from_master = false);
172         void structureChanged();
173         void updateTocItem(std::string const &, DocIterator const &);
174         //@}
175
176         ///
177         TocModels & tocModels();
178
179         /// called on timeout
180         void autoSave();
181
182         /// check for external change of any opened buffer, mainly for svn usage
183         void checkExternallyModifiedBuffers();
184
185         /** redraw \c inset in all the BufferViews in which it is currently
186          *  visible. If successful return a pointer to the owning Buffer.
187          */
188         Buffer const * updateInset(Inset const *);
189
190         /// \return the \c Workarea associated to \p  Buffer
191         /// \retval 0 if no \c WorkArea is found.
192         GuiWorkArea * workArea(Buffer & buffer);
193         /// \return the \c Workarea at index \c index
194         GuiWorkArea * workArea(int index);
195
196         /// Add a \c WorkArea
197         /// \return the \c Workarea associated to \p  Buffer
198         /// \retval 0 if no \c WorkArea is found.
199         GuiWorkArea * addWorkArea(Buffer & buffer);
200         /// \param work_area The current \c WorkArea, or \c NULL
201         void setCurrentWorkArea(GuiWorkArea * work_area);
202         ///
203         void removeWorkArea(GuiWorkArea * work_area);
204         /// return the current WorkArea (the one that has the focus).
205         GuiWorkArea const * currentWorkArea() const;
206         /// return the current WorkArea (the one that has the focus).
207         GuiWorkArea * currentWorkArea();
208
209         /// return the current document WorkArea (it may not have the focus).
210         GuiWorkArea const * currentMainWorkArea() const;
211         /// return the current document WorkArea (it may not have the focus).
212         GuiWorkArea * currentMainWorkArea();
213
214         /// Current ratio between physical pixels and device-independent pixels
215         double pixelRatio() const;
216
217 Q_SIGNALS:
218         void closing(int);
219         void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
220         // emitted when the work area or its buffer view changed
221         void bufferViewChanged();
222
223 public Q_SLOTS:
224         ///
225         void setBusy(bool);
226         /// idle timeout.
227         /// clear any temporary message and replace with current status.
228         void clearMessage();
229         ///
230         void updateWindowTitle(GuiWorkArea * wa);
231         ///
232         void disableShellEscape();
233
234 private Q_SLOTS:
235         ///
236         void resetWindowTitle();
237
238         ///
239         void on_currentWorkAreaChanged(GuiWorkArea *);
240         ///
241         void onBufferViewChanged();
242         ///
243         void on_lastWorkAreaRemoved();
244
245         /// For completion of autosave or export threads.
246         void processingThreadStarted();
247         void processingThreadFinished();
248         void autoSaveThreadFinished();
249
250         /// must be called in GUI thread
251         void doShowDialog(QString const & qname, QString const & qdata,
252         Inset * inset);
253
254         /// must be called from GUI thread
255         void updateStatusBarMessage(QString const & str);
256         void clearMessageText();
257
258         ///
259         void toolBarPopup(const QPoint &pos);
260
261 private:
262         /// Open given child document in current buffer directory.
263         void openChildDocument(std::string const & filename);
264         /// Close current document buffer.
265         bool closeBuffer();
266         /// Close all document buffers.
267         bool closeBufferAll();
268         ///
269         TabWorkArea * addTabWorkArea();
270
271         /// connect to signals in the given BufferView
272         void connectBufferView(BufferView & bv);
273         /// disconnect from signals in the given BufferView
274         void disconnectBufferView();
275         /// connect to signals in the given buffer
276         void connectBuffer(Buffer & buf);
277         /// disconnect from signals in the given buffer
278         void disconnectBuffer();
279         ///
280         void dragEnterEvent(QDragEnterEvent * ev);
281         ///
282         void dropEvent(QDropEvent * ev);
283         /// make sure we quit cleanly
284         void closeEvent(QCloseEvent * e);
285         ///
286         void showEvent(QShowEvent *);
287
288         /// in order to catch Tab key press.
289         bool event(QEvent * e);
290         bool focusNextPrevChild(bool);
291
292         ///
293         bool goToFileRow(std::string const & argument);
294
295         ///
296         class GuiViewPrivate;
297         GuiViewPrivate & d;
298
299 public:
300         ///
301         /// dialogs for this view
302         ///
303
304         ///
305         void resetDialogs();
306
307         /// Hide all visible dialogs
308         void hideAll() const;
309
310         /// Update all visible dialogs.
311         /**
312          *  Check the status of all visible dialogs and disable or reenable
313          *  them as appropriate.
314          *
315          *  Disabling is needed for example when a dialog is open and the
316          *  cursor moves to a position where the corresponding inset is not
317          *  allowed.
318          */
319         void updateDialogs();
320
321         /** Show dialog could be called from arbitrary threads.
322             \param name == "bibtex", "citation" etc; an identifier used to
323             launch a particular dialog.
324             \param data is a string representation of the Inset contents.
325             It is often little more than the output from Inset::write.
326             It is passed to, and parsed by, the frontend dialog.
327             Several of these dialogs do not need any data,
328             so it defaults to string().
329             \param inset ownership is _not_ passed to the frontend dialog.
330             It is stored internally and used by the kernel to ascertain
331             what to do with the FuncRequest dispatched from the frontend
332             dialog on 'Apply'; should it be used to create a new inset at
333             the current cursor position or modify an existing, 'open' inset?
334         */
335         void showDialog(std::string const & name,
336                 std::string const & data, Inset * inset = 0);
337
338         /** \param name == "citation", "bibtex" etc; an identifier used
339             to reset the contents of a particular dialog with \param data.
340             See the comments to 'show', above.
341         */
342         void updateDialog(std::string const & name, std::string const & data);
343
344         /** All Dialogs of the given \param name will be closed if they are
345             connected to the given \param inset.
346         */
347         void hideDialog(std::string const & name, Inset * inset);
348         ///
349         void disconnectDialog(std::string const & name);
350         ///
351         bool develMode() const { return devel_mode_; }
352
353 private:
354         /// Saves the layout and geometry of the window
355         void saveLayout() const;
356         /// Saves the settings of toolbars and all dialogs
357         void saveUISettings() const;
358         ///
359         bool restoreLayout();
360         ///
361         GuiToolbar * toolbar(std::string const & name);
362         ///
363         void constructToolbars();
364         ///
365         void initToolbars();
366         ///
367         void initToolbar(std::string const & name);
368         /// Update lock (all) toolbars position
369         void updateLockToolbars();
370         ///
371         bool lfunUiToggle(std::string const & ui_component);
372         ///
373         void toggleFullScreen();
374         ///
375         void insertLyXFile(docstring const & fname);
376         ///
377         /// Open Export As ... dialog. \p iformat is the format the
378         /// filter is initially set to.
379         bool exportBufferAs(Buffer & b, docstring const & iformat);
380
381         ///
382         enum RenameKind { LV_WRITE_AS, LV_VC_RENAME, LV_VC_COPY };
383         /// Save a buffer as a new file.
384         /**
385         Write a buffer to a new file name and rename the buffer
386     according to the new file name.
387
388     This function is e.g. used by menu callbacks and
389     LFUN_BUFFER_WRITE_AS.
390
391     If 'newname' is empty, the user is asked via a
392     dialog for the buffer's new name and location.
393
394     If 'newname' is non-empty and has an absolute path, that is used.
395     Otherwise the base directory of the buffer is used as the base
396     for any relative path in 'newname'.
397
398          \p kind controls what is done besides the pure renaming:
399          * LV_WRITE_AS  => The buffer is written without version control actions.
400          * LV_VC_RENAME => The file is renamed in version control.
401          * LV_VC_COPY   => The file is copied in version control.
402          */
403         bool renameBuffer(Buffer & b, docstring const & newname,
404                           RenameKind kind = LV_WRITE_AS);
405         ///
406         bool saveBuffer(Buffer & b);
407         /// save and rename buffer to fn. If fn is empty, the buffer
408         /// is just saved as the filename it already has.
409         bool saveBuffer(Buffer & b, support::FileName const & fn);
410         /// closes a workarea, if close_buffer is true the buffer will
411         /// also be released, otherwise the buffer will be hidden.
412         bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
413         /// closes the tabworkarea and all tabs. If we are in a close event,
414         /// all buffers will be closed, otherwise they will be hidden.
415         bool closeTabWorkArea(TabWorkArea * twa);
416         /// gives the user the possibility to save his work
417         /// or to discard the changes. If hiding is true, the
418         /// document will be reloaded.
419         bool saveBufferIfNeeded(Buffer & buf, bool hiding);
420         /// closes all workareas
421         bool closeWorkAreaAll();
422         /// write all open workareas into the session file
423         void writeSession() const;
424         /// is the buffer in this workarea also shown in another tab ?
425         /// This tab can either be in the same view or in another one.
426         bool inMultiTabs(GuiWorkArea * wa);
427         /// is the buffer shown in some other view ?
428         bool inOtherView(Buffer & buf);
429         ///
430         enum NextOrPrevious {
431                 NEXTBUFFER,
432                 PREVBUFFER
433         };
434         ///
435         void gotoNextOrPreviousBuffer(NextOrPrevious np, bool const move);
436
437         /// Is the dialog currently visible?
438         bool isDialogVisible(std::string const & name) const;
439         ///
440         Dialog * findOrBuild(std::string const & name, bool hide_it);
441         ///
442         Dialog * build(std::string const & name);
443         ///
444         bool reloadBuffer(Buffer & buffer);
445         ///
446         void dispatchVC(FuncRequest const & cmd, DispatchResult & dr);
447         ///
448         void dispatchToBufferView(FuncRequest const & cmd, DispatchResult & dr);
449         ///
450         void showMessage();
451
452         /// This view ID.
453         int id_;
454
455         /// flag to avoid two concurrent close events.
456         bool closing_;
457         /// if the view is busy the cursor shouldn't blink for instance.
458         /// This counts the number of times more often we called
459         /// setBusy(true) compared to setBusy(false), so we can nest
460         /// functions that call setBusy;
461         int busy_;
462
463         /// Request to open the command toolbar if it is "auto"
464         bool command_execute_;
465         /// Request to give focus to minibuffer
466         bool minibuffer_focus_;
467
468         /// Statusbar widget that shows shell-escape status
469         QLabel * shell_escape_;
470         /// Statusbar widget that shows read-only status
471         QLabel * read_only_;
472         /// Statusbar widget that shows version control status
473         QLabel * version_control_;
474
475         /// The rate from which the actual zoom value is calculated
476         /// from the default zoom pref
477         double zoom_ratio_ = 1.0;
478         /// Minimum zoom percentage
479         static int const zoom_min_ = 10;
480
481         // movability flag of all toolbars
482         bool toolbarsMovable_;
483
484         // developer mode
485         bool devel_mode_;
486 };
487
488
489 class SEMenu : public QMenu
490 {
491         Q_OBJECT
492 public:
493         explicit SEMenu(QWidget * parent);
494
495 public Q_SLOTS:
496         void showMenu(QPoint const &) { exec(QCursor::pos()); }
497 };
498
499 } // namespace frontend
500 } // namespace lyx
501
502 #endif // GUIVIEW_H