4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjornes
9 * \author Abdelrazak Younes
10 * \author Peter Kümmel
12 * Full author contact details are available in file CREDITS.
18 #include "frontends/Delegates.h"
20 #include "support/strfwd.h"
22 #include <QMainWindow>
27 class QDragEnterEvent;
37 namespace support { class FileName; }
58 * GuiView - Qt main LyX window
60 * This class represents the main LyX window and provides
61 * accessor functions to its content.
63 * Note: a QObject emits a destroyed(QObject *) Qt signal when it
64 * is deleted. This might be useful for closing other dialogs
65 * depending on a given GuiView.
67 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
68 public GuiBufferDelegate
73 /// create a main window of the given dimensions
78 /// closes the view such that the view knows that is closed
79 /// programmatically and not by the user clicking the x.
80 bool closeScheduled();
82 /// Things that need to be done when the OSes session manager
83 /// requests a log out.
84 bool prepareAllBuffersForLogout();
86 int id() const { return id_; }
91 /// Signal that the any "auto" minibuffer can be closed now.
92 void resetCommandExecute();
94 /// \name Generic accessor functions
96 /// The current BufferView refers to the BufferView that has the focus,
97 /// including for example the one that is created when you use the
98 /// advanced search and replace pane.
99 /// \return the currently selected buffer view.
100 BufferView * currentBufferView();
101 BufferView const * currentBufferView() const;
103 /// The document BufferView always refers to the view's main document
104 /// BufferView. So, even if the BufferView in e.g., the advanced
105 /// search and replace pane has the focus.
106 /// \return the current document buffer view.
107 BufferView * documentBufferView();
108 BufferView const * documentBufferView() const;
110 void newDocument(std::string const & filename,
111 std::string templatefile = std::string(),
112 bool fromTemplate = false);
114 /// display a message in the view
115 /// could be called from any thread
116 void message(docstring const &) override;
118 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
119 /// dispatch command.
120 /// \return true if the \c FuncRequest has been dispatched.
121 void dispatch(FuncRequest const & cmd, DispatchResult & dr);
124 /// Update the completion popup and the inline completion state.
125 /// If \c start is true, then a new completion might be started.
126 /// If \c keep is true, an active completion will be kept active
127 /// even though the cursor moved. The update flags of \c cur might
129 void updateCompletion(Cursor & cur, bool start, bool keep);
133 bool hasFocus() const;
136 void focusInEvent(QFocusEvent * e) override;
137 /// Add a Buffer to the View
138 /// \param b Buffer to set.
139 /// \param switch_to Whether to set it to the current workarea.
140 void setBuffer(Buffer * b, bool switch_to = true);
142 /// load a document into the current workarea.
143 Buffer * loadDocument(
144 support::FileName const & name, ///< File to load.
145 bool tolastfiles = true ///< append to the "Open recent" menu?
148 /// add toolbar, if newline==true, add a toolbar break before the toolbar
149 GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
150 void updateStatusBar();
152 /// updates the possible layouts selectable
153 void updateLayoutList();
154 void updateToolbars();
157 LayoutBox * getLayoutDialog() const;
159 /// hides the workarea and makes sure it is clean
160 bool hideWorkArea(GuiWorkArea * wa);
161 /// closes workarea; close buffer only if no other workareas point to it
162 bool closeWorkArea(GuiWorkArea * wa);
163 /// closes the buffer
164 bool closeBuffer(Buffer & buf);
166 void openDocument(std::string const & filename);
168 void importDocument(std::string const &);
170 /// \name GuiBufferDelegate.
172 void resetAutosaveTimers() override;
173 // shows an error list
174 // if from_master is true, show master's error list
175 void errors(std::string const &, bool from_master = false) override;
176 void structureChanged() override;
177 void updateTocItem(std::string const &, DocIterator const &) override;
181 TocModels & tocModels();
183 /// called on timeout
186 /// check for external change of any opened buffer, mainly for svn usage
187 void checkExternallyModifiedBuffers();
189 /** redraw \c inset in all the BufferViews in which it is currently
190 * visible. If successful return a pointer to the owning Buffer.
192 Buffer const * updateInset(Inset const *);
194 /// \return the \c Workarea associated to \p Buffer
195 /// \retval 0 if no \c WorkArea is found.
196 GuiWorkArea * workArea(Buffer & buffer);
197 /// \return the \c Workarea at index \c index
198 GuiWorkArea * workArea(int index);
200 /// Add a \c WorkArea
201 /// \return the \c Workarea associated to \p Buffer
202 /// \retval 0 if no \c WorkArea is found.
203 GuiWorkArea * addWorkArea(Buffer & buffer);
204 /// \param work_area The current \c WorkArea, or \c NULL
205 void setCurrentWorkArea(GuiWorkArea * work_area);
207 void removeWorkArea(GuiWorkArea * work_area);
208 /// return the current WorkArea (the one that has the focus).
209 GuiWorkArea const * currentWorkArea() const;
210 /// return the current WorkArea (the one that has the focus).
211 GuiWorkArea * currentWorkArea();
213 /// return the current document WorkArea (it may not have the focus).
214 GuiWorkArea const * currentMainWorkArea() const;
215 /// return the current document WorkArea (it may not have the focus).
216 GuiWorkArea * currentMainWorkArea();
218 /// Current ratio between physical pixels and device-independent pixels
219 double pixelRatio() const;
223 void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
224 // emitted when the work area or its buffer view changed
225 void bufferViewChanged();
226 /// emitted when zoom is modified
227 void currentZoomChanged(int);
231 void setBusy(bool) override;
233 /// clear any temporary message and replace with current status.
236 void updateWindowTitle(GuiWorkArea * wa);
238 void disableShellEscape();
242 void resetWindowTitle();
244 void flatGroupBoxes(const QObject * object, bool flag);
247 void checkCancelBackground();
249 void zoomSliderMoved(int);
251 void zoomValueChanged(int);
253 void zoomInPressed();
255 void zoomOutPressed();
257 void showZoomContextMenu();
259 void on_currentWorkAreaChanged(GuiWorkArea *);
261 void onBufferViewChanged();
263 void on_lastWorkAreaRemoved();
265 /// For completion of autosave or export threads.
266 void processingThreadStarted();
267 void processingThreadFinished();
268 void autoSaveThreadFinished();
270 /// must be called in GUI thread
271 void doShowDialog(QString const & qname, QString const & qdata,
274 /// must be called from GUI thread
275 void updateStatusBarMessage(QString const & str);
276 void clearMessageText();
279 void toolBarPopup(const QPoint &pos);
282 /// Open given child document in current buffer directory.
283 void openChildDocument(std::string const & filename);
284 /// Close current document buffer.
286 /// Close all document buffers.
287 bool closeBufferAll();
289 TabWorkArea * addTabWorkArea();
291 /// connect to signals in the given BufferView
292 void connectBufferView(BufferView & bv);
293 /// disconnect from signals in the given BufferView
294 void disconnectBufferView();
295 /// connect to signals in the given buffer
296 void connectBuffer(Buffer & buf);
297 /// disconnect from signals in the given buffer
298 void disconnectBuffer();
300 void dragEnterEvent(QDragEnterEvent * ev) override;
302 void dropEvent(QDropEvent * ev) override;
303 /// make sure we quit cleanly
304 void closeEvent(QCloseEvent * e) override;
306 void showEvent(QShowEvent *) override;
308 /// in order to catch Tab key press.
309 bool event(QEvent * e) override;
310 bool focusNextPrevChild(bool) override;
313 bool goToFileRow(std::string const & argument);
316 class GuiViewPrivate;
321 /// dialogs for this view
327 /// Hide all visible dialogs
328 void hideAll() const;
330 /// Update all visible dialogs.
332 * Check the status of all visible dialogs and disable or re-enable
333 * them as appropriate.
335 * Disabling is needed for example when a dialog is open and the
336 * cursor moves to a position where the corresponding inset is not
339 void updateDialogs();
341 /** Show dialog could be called from arbitrary threads.
342 \param name == "bibtex", "citation" etc; an identifier used to
343 launch a particular dialog.
344 \param data is a string representation of the Inset contents.
345 It is often little more than the output from Inset::write.
346 It is passed to, and parsed by, the frontend dialog.
347 Several of these dialogs do not need any data,
348 so it defaults to string().
349 \param inset ownership is _not_ passed to the frontend dialog.
350 It is stored internally and used by the kernel to ascertain
351 what to do with the FuncRequest dispatched from the frontend
352 dialog on 'Apply'; should it be used to create a new inset at
353 the current cursor position or modify an existing, 'open' inset?
355 void showDialog(std::string const & name,
356 std::string const & data, Inset * inset = 0) override;
358 /** \param name == "citation", "bibtex" etc; an identifier used
359 to reset the contents of a particular dialog with \param data.
360 See the comments to 'show', above.
362 void updateDialog(std::string const & name, std::string const & data) override;
364 /** All Dialogs of the given \param name will be closed if they are
365 connected to the given \param inset.
367 void hideDialog(std::string const & name, Inset * inset);
369 void disconnectDialog(std::string const & name);
371 bool develMode() const { return devel_mode_; }
373 void setCurrentZoom(int const v);
376 /// Saves the layout and geometry of the window
377 void saveLayout() const;
378 /// Saves the settings of toolbars and all dialogs
379 void saveUISettings() const;
381 bool restoreLayout();
383 GuiToolbar * toolbar(std::string const & name);
385 void constructToolbars();
389 void initToolbar(std::string const & name);
390 /// Update lock (all) toolbars position
391 void updateLockToolbars();
392 /// refill the toolbars (dark/light mode switch)
393 void refillToolbars();
395 bool lfunUiToggle(std::string const & ui_component);
397 void toggleFullScreen();
398 /// \return whether we did anything
399 bool insertLyXFile(docstring const & fname, bool ignorelang = false);
401 /// Open Export As ... dialog. \p iformat is the format the
402 /// filter is initially set to.
403 bool exportBufferAs(Buffer & b, docstring const & iformat);
408 LV_WRITE_AS_TEMPLATE,
412 /// Get a path for LFUN_BUFFER_WRITE_AS_TEMPLATE
413 std::string const getTemplatesPath(Buffer & buf);
414 /// Save a buffer as a new file.
416 * Write a buffer to a new file name and rename the buffer
417 * according to the new file name.
419 * This function is e.g. used by menu callbacks and
420 * LFUN_BUFFER_WRITE_AS.
422 * If 'newname' is empty, the user is asked via a
423 * dialog for the buffer's new name and location.
425 * If 'newname' is non-empty and has an absolute path, that is used.
426 * Otherwise the base directory of the buffer is used as the base
427 * for any relative path in 'newname'.
429 * \p kind controls what is done besides the pure renaming:
430 * LV_WRITE_AS => The buffer is written without version control actions.
431 * LV_VC_RENAME => The file is renamed in version control.
432 * LV_VC_COPY => The file is copied in version control.
434 bool renameBuffer(Buffer & b, docstring const & newname,
435 RenameKind kind = LV_WRITE_AS);
437 bool saveBuffer(Buffer & b);
438 /// save and rename buffer to fn. If fn is empty, the buffer
439 /// is just saved as the filename it already has.
440 bool saveBuffer(Buffer & b, support::FileName const & fn);
441 /// closes a workarea, if close_buffer is true the buffer will
442 /// also be released, otherwise the buffer will be hidden.
443 bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
444 /// closes the tabworkarea and all tabs. If we are in a close event,
445 /// all buffers will be closed, otherwise they will be hidden.
446 bool closeTabWorkArea(TabWorkArea * twa);
447 /// gives the user the possibility to save their work
448 /// or to discard the changes. If hiding is true, the
449 /// document will be reloaded.
450 bool saveBufferIfNeeded(Buffer & buf, bool hiding);
451 /// closes all workareas
452 bool closeWorkAreaAll();
453 /// write all open workareas into the session file
454 void writeSession() const;
455 /// is the buffer in this workarea also shown in another tab ?
456 /// This tab can either be in the same view or in another one.
457 bool inMultiTabs(GuiWorkArea * wa);
458 /// is the buffer shown in some other view ?
459 bool inOtherView(Buffer & buf);
461 enum NextOrPrevious {
466 void gotoNextOrPreviousBuffer(NextOrPrevious np, bool const move);
468 /// Is the dialog currently visible?
469 bool isDialogVisible(std::string const & name) const;
471 Dialog * findOrBuild(std::string const & name, bool hide_it);
473 Dialog * build(std::string const & name);
475 bool reloadBuffer(Buffer & buffer);
477 void dispatchVC(FuncRequest const & cmd, DispatchResult & dr);
479 void dispatchToBufferView(FuncRequest const & cmd, DispatchResult & dr);
486 /// flag to avoid two concurrent close events.
488 /// if the view is busy the cursor shouldn't blink for instance.
489 /// This counts the number of times more often we called
490 /// setBusy(true) compared to setBusy(false), so we can nest
491 /// functions that call setBusy;
494 /// Request to open the command toolbar if it is "auto"
495 bool command_execute_;
496 /// Request to give focus to minibuffer
497 bool minibuffer_focus_;
499 /// Statusbar widget that shows shell-escape status
500 QLabel * shell_escape_;
501 /// Statusbar widget that shows read-only status
503 /// Statusbar widget that shows version control status
504 QLabel * version_control_;
505 /// Statusbar widget that shows zoom value
506 QLabel * zoom_value_;
507 /// The zoom slider widget
508 QSlider * zoom_slider_;
509 /// Zoom in ("+") Button
510 QPushButton * zoom_in_;
511 /// Zoom out ("-") Button
512 QPushButton * zoom_out_;
514 /// The rate from which the actual zoom value is calculated
515 /// from the default zoom pref
516 double zoom_ratio_ = 1.0;
517 /// Minimum zoom percentage
518 static int const zoom_min_ = 10;
520 // movability flag of all toolbars
521 bool toolbarsMovable_;
528 class SEMenu : public QMenu
532 explicit SEMenu(QWidget * parent);
535 void showMenu(QPoint const &) { exec(QCursor::pos()); }
539 class PressableSvgWidget : public QSvgWidget
543 explicit PressableSvgWidget(const QString &file, QWidget * parent = nullptr)
544 : QSvgWidget(file, parent) {};
546 void mousePressEvent(QMouseEvent *event) override;
551 } // namespace frontend