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