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>
28 class QDragEnterEvent;
37 namespace support { class FileName; }
51 class GuiClickableLabel;
59 * GuiView - Qt main LyX window
61 * This class represents the main LyX window and provides
62 * accessor functions to its content.
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.
68 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
69 public GuiBufferDelegate
74 /// create a main window of the given dimensions
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();
83 /// Things that need to be done when the OSes session manager
84 /// requests a log out.
85 bool prepareAllBuffersForLogout();
87 int id() const { return id_; }
92 /// Signal that the any "auto" minibuffer can be closed now.
93 void resetCommandExecute();
95 /// \name Generic accessor functions
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;
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;
111 void newDocument(std::string const & filename,
112 std::string templatefile = std::string(),
113 bool fromTemplate = false);
115 /// display a message in the view
116 /// could be called from any thread
117 void message(docstring const &) override;
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);
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
130 void updateCompletion(Cursor & cur, bool start, bool keep);
134 bool hasFocus() const;
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);
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?
149 /// add toolbar, if newline==true, add a toolbar break before the toolbar
150 GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
151 void updateStatusBar();
153 /// updates the possible layouts selectable
154 void updateLayoutList();
155 void updateToolbars();
158 LayoutBox * getLayoutDialog() const;
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);
167 void openDocument(std::string const & filename);
169 void importDocument(std::string const &);
171 /// \name GuiBufferDelegate.
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;
182 TocModels & tocModels();
184 /// called on timeout
187 /// check for external change of any opened buffer, mainly for svn usage
188 void checkExternallyModifiedBuffers();
190 /** redraw \c inset in all the BufferViews in which it is currently
191 * visible. If successful return a pointer to the owning Buffer.
193 Buffer const * updateInset(Inset const *);
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);
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);
208 void removeWorkArea(GuiWorkArea * work_area);
209 /// return the current WorkArea (the one that has the focus).
210 GuiWorkArea const * currentWorkArea() const;
211 /// return the current WorkArea (the one that has the focus).
212 GuiWorkArea * currentWorkArea();
214 /// return the current document WorkArea (it may not have the focus).
215 GuiWorkArea const * currentMainWorkArea() const;
216 /// return the current document WorkArea (it may not have the focus).
217 GuiWorkArea * currentMainWorkArea();
219 /// Current ratio between physical pixels and device-independent pixels
220 double pixelRatio() const;
224 void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
225 // emitted when the work area or its buffer view changed
226 void bufferViewChanged();
227 /// emitted when zoom is modified
228 void currentZoomChanged(int);
232 void setBusy(bool) override;
234 /// clear any temporary message and replace with current status.
237 void updateWindowTitle(GuiWorkArea * wa);
239 void disableShellEscape();
243 void resetWindowTitle();
245 void flatGroupBoxes(const QObject * object, bool flag);
248 void checkCancelBackground();
250 void zoomSliderMoved(int);
252 void zoomValueChanged(int);
254 void zoomInPressed();
256 void zoomOutPressed();
258 void showZoomContextMenu();
260 void on_currentWorkAreaChanged(GuiWorkArea *);
262 void onBufferViewChanged();
264 void on_lastWorkAreaRemoved();
266 /// For completion of autosave or export threads.
267 void processingThreadStarted();
268 void processingThreadFinished();
269 void autoSaveThreadFinished();
271 /// must be called in GUI thread
272 void doShowDialog(QString const & qname, QString const & qdata,
275 /// must be called from GUI thread
276 void updateStatusBarMessage(QString const & str);
277 void clearMessageText();
280 void toolBarPopup(const QPoint &pos);
283 /// Open given child document in current buffer directory.
284 void openChildDocument(std::string const & filename);
285 /// Close current document buffer.
287 /// Close all document buffers.
288 bool closeBufferAll();
290 TabWorkArea * addTabWorkArea();
293 void scheduleRedrawWorkAreas();
295 /// connect to signals in the given BufferView
296 void connectBufferView(BufferView & bv);
297 /// disconnect from signals in the given BufferView
298 void disconnectBufferView();
299 /// connect to signals in the given buffer
300 void connectBuffer(Buffer & buf);
301 /// disconnect from signals in the given buffer
302 void disconnectBuffer();
304 void dragEnterEvent(QDragEnterEvent * ev) override;
306 void dropEvent(QDropEvent * ev) override;
307 /// make sure we quit cleanly
308 void closeEvent(QCloseEvent * e) override;
310 void showEvent(QShowEvent *) override;
312 /// in order to catch Tab key press.
313 bool event(QEvent * e) override;
314 bool focusNextPrevChild(bool) override;
317 bool goToFileRow(std::string const & argument);
320 class GuiViewPrivate;
325 /// dialogs for this view
331 /// Hide all visible dialogs
332 void hideAll() const;
334 /// Update all visible dialogs.
336 * Check the status of all visible dialogs and disable or re-enable
337 * them as appropriate.
339 * Disabling is needed for example when a dialog is open and the
340 * cursor moves to a position where the corresponding inset is not
343 void updateDialogs();
345 /** Show dialog could be called from arbitrary threads.
346 \param name == "bibtex", "citation" etc; an identifier used to
347 launch a particular dialog.
348 \param data is a string representation of the Inset contents.
349 It is often little more than the output from Inset::write.
350 It is passed to, and parsed by, the frontend dialog.
351 Several of these dialogs do not need any data,
352 so it defaults to string().
353 \param inset ownership is _not_ passed to the frontend dialog.
354 It is stored internally and used by the kernel to ascertain
355 what to do with the FuncRequest dispatched from the frontend
356 dialog on 'Apply'; should it be used to create a new inset at
357 the current cursor position or modify an existing, 'open' inset?
359 void showDialog(std::string const & name,
360 std::string const & data, Inset * inset = 0) override;
362 /** \param name == "citation", "bibtex" etc; an identifier used
363 to reset the contents of a particular dialog with \param data.
364 See the comments to 'show', above.
366 void updateDialog(std::string const & name, std::string const & data) override;
368 /** All Dialogs of the given \param name will be closed if they are
369 connected to the given \param inset.
371 void hideDialog(std::string const & name, Inset * inset);
373 void disconnectDialog(std::string const & name);
375 bool develMode() const { return devel_mode_; }
377 void setCurrentZoom(int const v);
380 /// Saves the layout and geometry of the window
381 void saveLayout() const;
382 /// Saves the settings of toolbars and all dialogs
383 void saveUISettings() const;
385 bool restoreLayout();
387 GuiToolbar * toolbar(std::string const & name);
389 void constructToolbars();
393 void initToolbar(std::string const & name);
394 /// Update lock (all) toolbars position
395 void updateLockToolbars();
396 /// refill the toolbars (dark/light mode switch)
397 void refillToolbars();
399 bool lfunUiToggle(std::string const & ui_component);
401 void toggleFullScreen();
402 /// \return whether we did anything
403 bool insertLyXFile(docstring const & fname, bool ignorelang = false);
405 /// Open Export As ... dialog. \p iformat is the format the
406 /// filter is initially set to.
407 bool exportBufferAs(Buffer & b, docstring const & iformat);
412 LV_WRITE_AS_TEMPLATE,
416 /// Get a path for LFUN_BUFFER_WRITE_AS_TEMPLATE
417 std::string const getTemplatesPath(Buffer & buf);
418 /// Save a buffer as a new file.
420 * Write a buffer to a new file name and rename the buffer
421 * according to the new file name.
423 * This function is e.g. used by menu callbacks and
424 * LFUN_BUFFER_WRITE_AS.
426 * If 'newname' is empty, the user is asked via a
427 * dialog for the buffer's new name and location.
429 * If 'newname' is non-empty and has an absolute path, that is used.
430 * Otherwise the base directory of the buffer is used as the base
431 * for any relative path in 'newname'.
433 * \p kind controls what is done besides the pure renaming:
434 * LV_WRITE_AS => The buffer is written without version control actions.
435 * LV_VC_RENAME => The file is renamed in version control.
436 * LV_VC_COPY => The file is copied in version control.
438 bool renameBuffer(Buffer & b, docstring const & newname,
439 RenameKind kind = LV_WRITE_AS);
441 bool saveBuffer(Buffer & b);
442 /// save and rename buffer to fn. If fn is empty, the buffer
443 /// is just saved as the filename it already has.
444 bool saveBuffer(Buffer & b, support::FileName const & fn);
445 /// closes a workarea, if close_buffer is true the buffer will
446 /// also be released, otherwise the buffer will be hidden.
447 bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
448 /// closes the tabworkarea and all tabs. If we are in a close event,
449 /// all buffers will be closed, otherwise they will be hidden.
450 bool closeTabWorkArea(TabWorkArea * twa);
451 /// gives the user the possibility to save their work
452 /// or to discard the changes. If hiding is true, the
453 /// document will be reloaded.
454 bool saveBufferIfNeeded(Buffer & buf, bool hiding);
455 /// closes all workareas
456 bool closeWorkAreaAll();
457 /// write all open workareas into the session file
458 void writeSession() const;
459 /// is the buffer in this workarea also shown in another tab ?
460 /// This tab can either be in the same view or in another one.
461 bool inMultiTabs(GuiWorkArea * wa);
462 /// is the buffer shown in some other view ?
463 bool inOtherView(Buffer & buf);
465 enum NextOrPrevious {
470 void gotoNextOrPreviousBuffer(NextOrPrevious np, bool const move);
472 /// Is the dialog currently visible?
473 bool isDialogVisible(std::string const & name) const;
475 Dialog * findOrBuild(std::string const & name, bool hide_it);
477 Dialog * build(std::string const & name);
479 bool reloadBuffer(Buffer & buffer);
481 void dispatchVC(FuncRequest const & cmd, DispatchResult & dr);
483 void dispatchToBufferView(FuncRequest const & cmd, DispatchResult & dr);
490 /// flag to avoid two concurrent close events.
492 /// if the view is busy the cursor shouldn't blink for instance.
493 /// This counts the number of times more often we called
494 /// setBusy(true) compared to setBusy(false), so we can nest
495 /// functions that call setBusy;
498 /// Request to open the command toolbar if it is "auto"
499 bool command_execute_;
500 /// Request to give focus to minibuffer
501 bool minibuffer_focus_;
503 /// Statusbar widget that shows shell-escape status
504 QLabel * shell_escape_;
505 /// Statusbar widget that shows read-only status
507 /// Statusbar widget that shows version control status
508 QLabel * version_control_;
509 /// Statusbar widget that shows zoom value
510 QLabel * zoom_value_;
511 /// The zoom slider widget
512 QSlider * zoom_slider_;
513 /// Zoom in ("+") Button
514 GuiClickableLabel * zoom_in_;
515 /// Zoom out ("-") Button
516 GuiClickableLabel * zoom_out_;
518 /// The rate from which the actual zoom value is calculated
519 /// from the default zoom pref
520 double zoom_ratio_ = 1.0;
521 /// Minimum zoom percentage
522 static int const zoom_min_ = 10;
524 // movability flag of all toolbars
525 bool toolbarsMovable_;
532 class SEMenu : public QMenu
536 explicit SEMenu(QWidget * parent);
539 void showMenu(QPoint const &) { exec(QCursor::pos()); }
543 class PressableSvgWidget : public QSvgWidget
547 explicit PressableSvgWidget(const QString &file, QWidget * parent = nullptr)
548 : QSvgWidget(file, parent) {};
550 void mousePressEvent(QMouseEvent *event) override;
555 } // namespace frontend