]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QWorkArea.h
92bd7f57c5d818ec32e82152eade9869aa008fe1
[lyx.git] / src / frontends / qt4 / QWorkArea.h
1 // -*- C++ -*-
2 /**
3  * \file QWorkArea.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author unknown
8  * \author John Levon
9  * \author Abdelrazak Younes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef QWORKAREA_H
15 #define QWORKAREA_H
16
17 #if (defined(Q_WS_X11) && QT_VERSION >= 0x030200)
18 #define USE_INPUT_METHODS 1
19 #endif
20
21 #ifdef emit
22 #undef emit
23 #endif
24
25 #include "WorkArea.h"
26 #include "QLPainter.h"
27 #include "LyXView.h"
28
29 #include "funcrequest.h"
30 #include "frontends/Timeout.h"
31
32 #include <QAbstractScrollArea>
33 #include <QMouseEvent>
34 #include <QWheelEvent>
35 #include <QResizeEvent>
36 #include <QKeyEvent>
37 #include <QPaintEvent>
38 #include <QTimer>
39 #include <QImage>
40 #include <QPixmap>
41
42 #include <queue>
43
44 class Painter;
45
46 class QWidget;
47 class QDragEnterEvent;
48 class QDropEvent;
49 class QMouseEvent;
50
51 /// for emulating triple click
52 class double_click {
53 public:
54         int x;
55         int y;
56         Qt::ButtonState state;
57         bool active;
58
59         bool operator==(QMouseEvent const & e) {
60                 return x == e.x() && y == e.y()
61                         && state == e.button();
62         }
63
64         double_click()
65                 : x(0), y(0), state(Qt::NoButton), active(false) {}
66
67         double_click(QMouseEvent * e)
68                 : x(e->x()), y(e->y()),
69                 state(e->button()), active(true) {}
70 };
71
72
73 /** Qt only emits mouse events when the mouse is being moved, but
74  *  we want to generate 'pseudo' mouse events when the mouse button is
75  *  pressed and the mouse cursor is below the bottom, or above the top
76  *  of the work area. In this way, we'll be able to continue scrolling
77  *  (and selecting) the text.
78  *
79  *  This class stores all the parameters needed to make this happen.
80  */
81 class SyntheticMouseEvent
82 {
83 public:
84         SyntheticMouseEvent();
85
86         FuncRequest cmd;
87         Timeout timeout;
88         bool restart_timeout;
89         int x_old;
90         int y_old;
91         double scrollbar_value_old;
92 };
93
94 /**
95  * Qt-specific implementation of the work area
96  * (buffer view GUI)
97 */
98 class QWorkArea : public QAbstractScrollArea, public WorkArea {
99
100         Q_OBJECT
101
102 public:
103
104         QWorkArea(LyXView & owner, int w, int h);
105
106         virtual ~QWorkArea();
107         /// return the width of the content pane
108         virtual int workWidth() const { return workWidth_; }
109
110         /// return the height of the content pane
111         virtual int workHeight() const { return workHeight_; }
112         ///
113         virtual void setScrollbarParams(int height, int pos, int line_height);
114
115         /// a selection exists
116         virtual void haveSelection(bool) const;
117
118         ///
119         virtual std::string const getClipboard() const;
120
121         ///
122         virtual void putClipboard(std::string const &) const;
123
124         ///
125         virtual void dragEnterEvent(QDragEnterEvent * event);
126
127         ///
128         virtual void dropEvent(QDropEvent* event);
129
130         /// return the widget's painter
131         virtual Painter & getPainter() { return (Painter &) painter_; }
132
133         /// return the backing pixmap
134         QPaintDevice * paintDevice() { return &paint_device_; }
135
136         /// update the passed area.
137         void update(int x, int y, int w, int h);
138
139         /// return a screen copy of the defined area.
140         QPixmap copyScreen(int x, int y, int w, int h) const;
141
142         /// Draw a pixmap onto the backing pixmap.
143         /**
144         QPixmap is implicitely shared so no need to pass by reference.
145         */
146         void drawScreen(int x, int y, QPixmap pixmap);
147
148         LyXView & view()
149         {
150                 return view_;
151         }
152 protected:
153
154         /// repaint part of the widget
155         void paintEvent(QPaintEvent * e);
156         /// widget has been resized
157         void resizeEvent(QResizeEvent * e);
158         /// mouse button press
159         void mousePressEvent(QMouseEvent * e);
160         /// mouse button release
161         void mouseReleaseEvent(QMouseEvent * e);
162         /// mouse double click of button
163         void mouseDoubleClickEvent(QMouseEvent * e);
164         /// mouse motion
165         void mouseMoveEvent(QMouseEvent * e);
166         /// wheel event
167         void wheelEvent(QWheelEvent * e);
168         /// key press
169         void keyPressEvent(QKeyEvent * e);
170
171 #if USE_INPUT_METHODS
172 protected:
173         /// IM events
174         void QWorkArea::inputMethodEvent(QInputMethodEvent * e)
175 #endif
176
177 public slots:
178
179         /// Timeout event Slot for keyboard bufferring.
180         /// \todo This is not used currently in the code, remove?
181         void keyeventTimeout();
182
183         /// Adjust the LyX buffer view with the position of the scrollbar.
184         /**
185         * The action argument is not used in the the code, it is there
186         * only for the connection to the vertical srollbar signal which
187         * emits an 'int' action.
188         */
189         void adjustViewWithScrollBar(int action = 0);
190
191 private:
192         ///
193         LyXView & view_;
194         
195         /// Buffer view width.
196         int workWidth_;
197
198         /// Buffer view height.
199         int workHeight_;
200
201         /// Our painter.
202         QLPainter painter_;
203
204         /// The slot connected to SyntheticMouseEvent::timeout.
205         void generateSyntheticMouseEvent();
206
207         ///
208         SyntheticMouseEvent synthetic_mouse_event_;
209
210         /// Our client side painting device.
211         QImage paint_device_;
212
213         /// Our server side painting device.
214         QPixmap screen_device_;
215
216         /// \todo remove
217         QTimer step_timer_;
218
219         /// \todo remove
220         std::queue<boost::shared_ptr<QKeyEvent> > keyeventQueue_;
221
222         double_click dc_event_;
223 };
224
225 #endif // QWORKAREA_H