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