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