]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWorkArea.h
Cleanup and simplify WorkArea code
[lyx.git] / src / frontends / qt4 / GuiWorkArea.h
1 // -*- C++ -*-
2 /**
3  * \file GuiWorkArea.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef WORKAREA_H
14 #define WORKAREA_H
15
16 #include "ui_WorkAreaUi.h"
17
18 #include "frontends/WorkArea.h"
19 #include "frontends/KeySymbol.h"
20
21 #include <QAbstractScrollArea>
22 #include <QTabBar>
23 #include <QTabWidget>
24
25 class QDragEnterEvent;
26 class QDropEvent;
27 class QToolButton;
28 class QWidget;
29
30 namespace lyx {
31
32 class Buffer;
33 class FuncRequest;
34
35 namespace frontend {
36
37 class GuiCompleter;
38 class GuiView;
39
40 class GuiWorkArea : public QAbstractScrollArea, public WorkArea
41 {
42         Q_OBJECT
43
44 public:
45         ///
46         GuiWorkArea(QWidget *);
47         ///
48         GuiWorkArea(Buffer & buffer, GuiView & gv);
49         ///
50         ~GuiWorkArea();
51
52         ///
53         void init();
54         ///
55         void setBuffer(Buffer &);
56         ///
57         void setGuiView(GuiView &);
58         ///
59         void setFullScreen(bool full_screen);
60         /// is GuiView in fullscreen mode?
61         bool isFullScreen() const;
62         ///
63         void scheduleRedraw();
64         ///
65         BufferView & bufferView();
66         ///
67         BufferView const & bufferView() const;
68         ///
69         void redraw(bool update_metrics);
70
71         /// return true if the key is part of a shortcut
72         bool queryKeySym(KeySymbol const & key, KeyModifier mod) const;
73
74         bool inDialogMode() const;
75         void setDialogMode(bool mode);
76
77         ///
78         GuiCompleter & completer();
79
80         /// Return the GuiView this workArea belongs to
81         GuiView const & view() const;
82         GuiView & view();
83
84         /// Current ratio between physical pixels and device-independent pixels
85         double pixelRatio() const;
86
87 public Q_SLOTS:
88         /// Process Key pressed event.
89         /// This needs to be public because it is accessed externally by GuiView.
90         void processKeySym(KeySymbol const & key, KeyModifier mod);
91         ///
92         void stopBlinkingCaret();
93         ///
94         void startBlinkingCaret();
95
96 Q_SIGNALS:
97         ///
98         void titleChanged(GuiWorkArea *);
99         ///
100         void busy(bool);
101         ///
102         void bufferViewChanged();
103         /// send key event to CompressorProxy
104         void compressKeySym(KeySymbol sym, KeyModifier mod, bool isAutoRepeat);
105
106 private Q_SLOTS:
107         /// Scroll the BufferView.
108         /**
109           * This is a slot for the valueChanged() signal of the vertical scrollbar.
110           * \p value value of the scrollbar.
111         */
112         void scrollTo(int value);
113         /// timer to limit triple clicks
114         void doubleClickTimeout();
115         /// toggle the cursor's visibility
116         void toggleCaret();
117         /// close this work area.
118         /// Slot for Buffer::closing signal.
119         void close();
120         /// Slot to restore proper scrollbar behaviour.
121         void fixVerticalScrollBar();
122
123 private:
124         /// Update window titles of all users.
125         void updateWindowTitle();
126         ///
127         bool event(QEvent *);
128         ///
129         void contextMenuEvent(QContextMenuEvent *);
130         ///
131         void focusInEvent(QFocusEvent *);
132         ///
133         void focusOutEvent(QFocusEvent *);
134         /// repaint part of the widget
135         void paintEvent(QPaintEvent * ev);
136         /// widget has been resized
137         void resizeEvent(QResizeEvent * ev);
138         /// mouse button press
139         void mousePressEvent(QMouseEvent * ev);
140         /// mouse button release
141         void mouseReleaseEvent(QMouseEvent * ev);
142         /// mouse double click of button
143         void mouseDoubleClickEvent(QMouseEvent * ev);
144         /// mouse motion
145         void mouseMoveEvent(QMouseEvent * ev);
146         /// wheel event
147         void wheelEvent(QWheelEvent * ev);
148         /// key press event. It also knows how to handle ShortcutOverride events to
149         /// avoid code duplication.
150         void keyPressEvent(QKeyEvent * ev);
151         /// IM events
152         void inputMethodEvent(QInputMethodEvent * ev);
153         /// IM query
154         QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
155
156         /// The slot connected to SyntheticMouseEvent::timeout.
157         void generateSyntheticMouseEvent();
158
159         friend class GuiCompleter;
160         struct Private;
161         Private * const d;
162 }; // GuiWorkArea
163
164
165 /// CompressorProxy adapted from Kuba Ober https://stackoverflow.com/a/21006207
166 class CompressorProxy : public QObject
167 {
168     Q_OBJECT
169         bool emitCheck(bool isAutoRepeat);
170         bool flag_;
171         // input: event to compress
172         Q_SLOT void slot(KeySymbol sym, KeyModifier mod, bool isAutoRepeat);
173         // output: compressed event
174     Q_SIGNAL void signal(KeySymbol sym, KeyModifier mod);
175 public:
176     // No default constructor, since the proxy must be a child of the
177     // target object.
178         explicit CompressorProxy(GuiWorkArea * wa);
179 };
180
181
182 class EmbeddedWorkArea : public GuiWorkArea
183 {
184         Q_OBJECT
185 public:
186         ///
187         EmbeddedWorkArea(QWidget *);
188         ~EmbeddedWorkArea();
189
190         /// Dummy methods for Designer.
191         void setWidgetResizable(bool) {}
192         void setWidget(QWidget *) {}
193
194         QSize sizeHint () const;
195         ///
196         void disable();
197
198 protected:
199         ///
200         void closeEvent(QCloseEvent * ev);
201         ///
202         void hideEvent(QHideEvent *ev);
203
204 private:
205         /// Embedded Buffer.
206         Buffer * buffer_;
207 }; // EmbeddedWorkArea
208
209
210 class GuiWorkAreaContainer;
211
212 /// A tabbed set of GuiWorkAreas.
213 class TabWorkArea : public QTabWidget
214 {
215         Q_OBJECT
216 public:
217         TabWorkArea(QWidget * parent = 0);
218
219         /// hide QTabWidget methods
220         GuiWorkAreaContainer * currentWidget() const;
221         GuiWorkAreaContainer * widget(int index) const;
222
223         ///
224         void setFullScreen(bool full_screen);
225         void showBar(bool show);
226         void closeAll();
227         bool setCurrentWorkArea(GuiWorkArea *);
228         GuiWorkArea * addWorkArea(Buffer & buffer, GuiView & view);
229         bool removeWorkArea(GuiWorkArea *);
230         GuiWorkArea * currentWorkArea() const;
231         GuiWorkArea * workArea(Buffer & buffer) const;
232         GuiWorkArea * workArea(int index) const;
233         void paintEvent(QPaintEvent *);
234
235 Q_SIGNALS:
236         ///
237         void currentWorkAreaChanged(GuiWorkArea *);
238         ///
239         void lastWorkAreaRemoved();
240
241 public Q_SLOTS:
242         /// close current buffer, or the one given by \c clicked_tab_
243         void closeCurrentBuffer();
244         /// hide current tab, or the one given by \c clicked_tab_
245         void hideCurrentTab();
246         /// close the tab given by \c index
247         void closeTab(int index);
248         ///
249         void moveTab(int fromIndex, int toIndex);
250         ///
251         void updateTabTexts();
252
253 private Q_SLOTS:
254         ///
255         void on_currentTabChanged(int index);
256         ///
257         void showContextMenu(const QPoint & pos);
258         /// enable closing tab on middle-click
259         void mousePressEvent(QMouseEvent * me);
260         void mouseReleaseEvent(QMouseEvent * me);
261         ///
262         void mouseDoubleClickEvent(QMouseEvent * event);
263         ///
264         int indexOfWorkArea(GuiWorkArea * w) const;
265
266 private:
267         using QTabWidget::addTab;
268         using QTabWidget::insertTab;
269
270         /// true if position is a tab (rather than the blank space in tab bar)
271         bool posIsTab(QPoint position);
272
273         int clicked_tab_;
274         ///
275         int midpressed_tab_;
276         ///
277         QToolButton * closeBufferButton;
278 }; // TabWorkArea
279
280
281 class DragTabBar : public QTabBar
282 {
283         Q_OBJECT
284 public:
285         ///
286         DragTabBar(QWidget * parent = 0);
287
288 protected:
289         ///
290         void mousePressEvent(QMouseEvent * event);
291         ///
292         void mouseMoveEvent(QMouseEvent * event);
293         ///
294         void dragEnterEvent(QDragEnterEvent * event);
295         ///
296         void dropEvent(QDropEvent * event);
297
298 private:
299         ///
300         QPoint dragStartPos_;
301
302 Q_SIGNALS:
303         ///
304         void tabMoveRequested(int fromIndex, int toIndex);
305 };
306
307
308 class GuiWorkAreaContainer : public QWidget, public Ui::WorkAreaUi
309 {
310         Q_OBJECT
311         // non-null
312         GuiWorkArea * const wa_;
313         void dispatch(FuncRequest f) const;
314
315 private Q_SLOTS:
316         void updateDisplay();
317         void reload() const;
318         void ignore() const;
319
320 protected:
321         void mouseDoubleClickEvent(QMouseEvent * event); //override
322
323 public:
324         /// wa != 0
325         GuiWorkAreaContainer(GuiWorkArea * wa, QWidget * parent = 0);
326         /// non-null
327         GuiWorkArea * workArea() const { return wa_; }
328 };
329
330
331
332 } // namespace frontend
333 } // namespace lyx
334
335 #endif // WORKAREA_H