]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiView.h
Fix bug #6606: Crash when reverting a document with a not available document class.
[features.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
24 class QCloseEvent;
25 class QDragEnterEvent;
26 class QDropEvent;
27 class QMenu;
28 class QShowEvent;
29
30
31 namespace lyx {
32
33 namespace support { class FileName; }
34
35 class Buffer;
36 class BufferView;
37 class Cursor;
38 class DispatchResult;
39 class FuncStatus;
40 class FuncRequest;
41 class Inset;
42
43 namespace frontend {
44
45 class Dialog;
46 class LayoutBox;
47 class GuiToolbar;
48 class GuiWorkArea;
49 class TabWorkArea;
50 class TocModels;
51 class ToolbarInfo;
52
53 /**
54  * GuiView - Qt4 main LyX window
55  *
56  * This class represents the main LyX window and provides
57  * accessor functions to its content.
58  *
59  * Note: a QObject emits a destroyed(QObject *) Qt signal when it
60  * is deleted. This might be useful for closing other dialogs
61  * depending on a given GuiView.
62  */
63 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
64         public GuiBufferDelegate
65 {
66         Q_OBJECT
67
68 public:
69         /// create a main window of the given dimensions
70         GuiView(int id);
71
72         ~GuiView();
73
74         int id() const { return id_; }
75
76         ///
77         void setBusy(bool);
78         /// are we busy ?
79         bool busy();
80
81         /// \name Generic accessor functions
82         //@{
83         /// The current BufferView refers to the BufferView that has the focus,
84         /// including for example the one that is created when you use the
85         /// advanced search and replace pane.
86         /// \return the currently selected buffer view.
87         BufferView * currentBufferView();
88         BufferView const * currentBufferView() const;
89
90         /// The document BufferView always refers to the view's main document
91         /// BufferView. So, even if the BufferView in e.g., the advanced
92         /// search and replace pane has the focus.
93         /// \return the current document buffer view.
94         BufferView * documentBufferView();
95         BufferView const * documentBufferView() const;
96
97         void newDocument(std::string const & filename,
98                 bool fromTemplate);
99
100         /// display a message in the view
101         /// could be called from any thread
102         void message(docstring const &);
103         
104         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
105         /// dispatch command.
106         /// \return true if the \c FuncRequest has been dispatched.
107         void dispatch(FuncRequest const & cmd, DispatchResult & dr);
108
109         void restartCursor();
110         /// Update the completion popup and the inline completion state.
111         /// If \c start is true, then a new completion might be started.
112         /// If \c keep is true, an active completion will be kept active
113         /// even though the cursor moved. The update flags of \c cur might
114         /// be changed.
115         void updateCompletion(Cursor & cur, bool start, bool keep);
116
117         ///
118         void setFocus();
119
120         ///
121         void focusInEvent(QFocusEvent * e);
122         /// set a buffer to the current workarea.
123         void setBuffer(Buffer * b); ///< \c Buffer to set.
124
125         /// load a document into the current workarea.
126         Buffer * loadDocument(
127                 support::FileName const &  name, ///< File to load.
128                 bool tolastfiles = true  ///< append to the "Open recent" menu?
129                 );
130
131         /// add toolbar, if newline==true, add a toolbar break before the toolbar
132         GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
133         void updateStatusBar();
134
135         /// updates the possible layouts selectable
136         void updateLayoutList();
137         void updateToolbars();
138         QMenu * createPopupMenu();
139
140         ///
141         LayoutBox * getLayoutDialog() const;
142
143         /// hides the workarea and makes sure it is clean
144         bool hideWorkArea(GuiWorkArea * wa);
145         /// closes the workarea
146         bool closeWorkArea(GuiWorkArea * wa);
147         /// closes the buffer
148         bool closeBuffer(Buffer & buf);
149         ///
150         void openDocument(std::string const & filename);
151         ///
152         void importDocument(std::string const &);
153
154         /// \name GuiBufferDelegate.
155         //@{
156         void resetAutosaveTimers();
157         void errors(std::string const &, bool from_master = false);
158         void structureChanged();
159         void updateTocItem(std::string const &, DocIterator const &);
160         //@}
161
162         ///
163         TocModels & tocModels();
164         
165         /// called on timeout
166         void autoSave();
167
168         /// check for external change of any opened buffer, mainly for svn usage
169         void checkExternallyModifiedBuffers();
170
171         /** redraw \c inset in all the BufferViews in which it is currently
172          *  visible. If successful return a pointer to the owning Buffer.
173          */
174         Buffer const * updateInset(Inset const *);
175
176         /// \return the \c Workarea associated to \p  Buffer
177         /// \retval 0 if no \c WorkArea is found.
178         GuiWorkArea * workArea(Buffer & buffer);
179
180         /// Add a \c WorkArea 
181         /// \return the \c Workarea associated to \p  Buffer
182         /// \retval 0 if no \c WorkArea is found.
183         GuiWorkArea * addWorkArea(Buffer & buffer);
184         /// \param work_area The current \c WorkArea, or \c NULL
185         void setCurrentWorkArea(GuiWorkArea * work_area);
186         ///
187         void removeWorkArea(GuiWorkArea * work_area);
188         /// return the current WorkArea (the one that has the focus).
189         GuiWorkArea const * currentWorkArea() const;
190         /// return the current WorkArea (the one that has the focus).
191         GuiWorkArea * currentWorkArea();
192
193         /// return the current document WorkArea (it may not have the focus).
194         GuiWorkArea const * currentMainWorkArea() const;
195         /// return the current document WorkArea (it may not have the focus).
196         GuiWorkArea * currentMainWorkArea();
197
198 Q_SIGNALS:
199         void closing(int);
200         void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
201
202 public Q_SLOTS:
203         /// idle timeout.
204         /// clear any temporary message and replace with current status.
205         void clearMessage();
206
207 private Q_SLOTS:
208         ///
209         void updateWindowTitle(GuiWorkArea * wa);
210         ///
211         void resetWindowTitleAndIconText();
212
213         ///
214         void on_currentWorkAreaChanged(GuiWorkArea *);
215         ///
216         void on_lastWorkAreaRemoved();
217
218         /// slots to change the icon size
219         void smallSizedIcons();
220         void normalSizedIcons();
221         void bigSizedIcons();
222
223         /// For completion of autosave or exporrt threads.
224         void threadFinished();
225
226         /// must be called in GUI thread
227         void doShowDialog(QString const & qname, QString const & qdata,
228         Inset * inset);
229
230         /// must be called from GUI thread
231         void updateStatusBarMessage(QString const & str);
232         void clearMessageText();
233
234 private:
235         /// Open given child document in current buffer directory.
236         void openChildDocument(std::string const & filename);
237         /// Close current document buffer.
238         bool closeBuffer();
239         /// Close all document buffers.
240         bool closeBufferAll();
241         ///
242         TabWorkArea * addTabWorkArea();
243
244         /// connect to signals in the given BufferView
245         void connectBufferView(BufferView & bv);
246         /// disconnect from signals in the given BufferView
247         void disconnectBufferView();
248         /// connect to signals in the given buffer
249         void connectBuffer(Buffer & buf);
250         /// disconnect from signals in the given buffer
251         void disconnectBuffer();
252         ///
253         void dragEnterEvent(QDragEnterEvent * ev);
254         ///
255         void dropEvent(QDropEvent * ev);
256         /// make sure we quit cleanly
257         void closeEvent(QCloseEvent * e);
258         ///
259         void showEvent(QShowEvent *);
260
261         /// in order to catch Tab key press.
262         bool event(QEvent * e);
263         bool focusNextPrevChild(bool);
264
265         ///
266         bool goToFileRow(std::string const & argument);
267
268         ///
269         struct GuiViewPrivate;
270         GuiViewPrivate & d;
271
272 public:
273         ///
274         /// dialogs for this view
275         ///
276
277         ///
278         void resetDialogs();
279
280         /// Hide all visible dialogs
281         void hideAll() const;
282
283         /// Update all visible dialogs.
284         /** 
285          *  Check the status of all visible dialogs and disable or reenable
286          *  them as appropriate.
287          *
288          *  Disabling is needed for example when a dialog is open and the
289          *  cursor moves to a position where the corresponding inset is not
290          *  allowed.
291          */
292         void updateDialogs();
293
294         /** Show dialog could be called from arbitrary threads.
295             \param name == "bibtex", "citation" etc; an identifier used to
296             launch a particular dialog.
297             \param data is a string representation of the Inset contents.
298             It is often little more than the output from Inset::write.
299             It is passed to, and parsed by, the frontend dialog.
300             Several of these dialogs do not need any data,
301             so it defaults to string().
302             \param inset ownership is _not_ passed to the frontend dialog.
303             It is stored internally and used by the kernel to ascertain
304             what to do with the FuncRequest dispatched from the frontend
305             dialog on 'Apply'; should it be used to create a new inset at
306             the current cursor position or modify an existing, 'open' inset?
307         */
308         void showDialog(std::string const & name,
309                 std::string const & data, Inset * inset = 0);
310
311         /** \param name == "citation", "bibtex" etc; an identifier used
312             to reset the contents of a particular dialog with \param data.
313             See the comments to 'show', above.
314         */
315         void updateDialog(std::string const & name, std::string const & data);
316
317         /** All Dialogs of the given \param name will be closed if they are
318             connected to the given \param inset.
319         */
320         void hideDialog(std::string const & name, Inset * inset);
321         ///
322         void disconnectDialog(std::string const & name);
323
324 private:
325         ///
326         void saveLayout() const;
327         ///
328         bool restoreLayout();
329         ///
330         GuiToolbar * toolbar(std::string const & name);
331         ///
332         void constructToolbars();
333         ///
334         void initToolbars();
335         ///
336         bool lfunUiToggle(std::string const & ui_component);
337         ///
338         void toggleFullScreen();
339         ///
340         void insertLyXFile(docstring const & fname);
341         ///
342         void insertPlaintextFile(docstring const & fname,
343                 bool asParagraph);
344
345         /// Save a buffer as a new file. 
346         /**
347         Write a buffer to a new file name and rename the buffer
348     according to the new file name.
349
350     This function is e.g. used by menu callbacks and
351     LFUN_BUFFER_WRITE_AS.
352
353     If 'newname' is empty, the user is asked via a
354     dialog for the buffer's new name and location.
355
356     If 'newname' is non-empty and has an absolute path, that is used.
357     Otherwise the base directory of the buffer is used as the base
358     for any relative path in 'newname'.
359         */
360         bool renameBuffer(Buffer & b, docstring const & newname);
361         ///
362         bool saveBuffer(Buffer & b);
363         /// closes a workarea, if close_buffer is true the buffer will
364         /// also be released, otherwise the buffer will be hidden.
365         bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
366         /// closes the tabworkarea and all tabs. If we are in a close event,
367         /// all buffers will be closed, otherwise they will be hidden.
368         bool closeTabWorkArea(TabWorkArea * twa);
369         /// gives the user the possibility to save his work 
370         /// or to discard the changes. If hiding is true, the
371         /// document will be reloaded.
372         bool saveBufferIfNeeded(Buffer & buf, bool hiding);
373         /// closes all workareas
374         bool closeWorkAreaAll();
375         /// write all open workareas into the session file
376         void writeSession() const;
377         /// is the buffer in this workarea also shown in another tab ?
378         /// This tab can either be in the same view or in another one.
379         bool inMultiTabs(GuiWorkArea * wa);
380         /// is the buffer in this workarea also shown in another view ?
381         bool inMultiViews(GuiWorkArea * wa);
382         ///
383         enum NextOrPrevious {
384                 NEXTBUFFER,
385                 PREVBUFFER
386         };
387         ///
388         void gotoNextOrPreviousBuffer(NextOrPrevious np);
389
390         /// Is the dialog currently visible?
391         bool isDialogVisible(std::string const & name) const;
392         ///
393         Dialog * findOrBuild(std::string const & name, bool hide_it);
394         ///
395         Dialog * build(std::string const & name);
396         ///
397         void reloadBuffer();
398         ///
399         void dispatchVC(FuncRequest const & cmd);
400         ///
401         void showMessage();
402
403         /// This view ID.
404         int id_;
405
406         /// flag to avoid two concurrent close events.
407         bool closing_;
408         /// if the view is busy the cursor shouldn't blink for instance.
409         bool busy_;
410 };
411
412 } // namespace frontend
413 } // namespace lyx
414
415 #endif // GUIVIEW_H