]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QContentPane.C
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / qt2 / QContentPane.C
1 /**
2  * \file QContentPane.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Qt defines a macro 'signals' that clashes with a boost namespace.
14 // All is well if the namespace is visible first.
15 #include "QWorkArea.h"
16
17 #include "QContentPane.h"
18 #include "QLyXKeySym.h"
19
20 #include <qapplication.h>
21 #include <qpainter.h>
22 #include <qtimer.h>
23
24 #include <boost/bind.hpp>
25
26 namespace {
27
28 /// return the LyX key state from Qt's
29 key_modifier::state q_key_state(Qt::ButtonState state)
30 {
31         key_modifier::state k = key_modifier::none;
32         if (state & Qt::ControlButton)
33                 k |= key_modifier::ctrl;
34         if (state & Qt::ShiftButton)
35                 k |= key_modifier::shift;
36         if (state & Qt::AltButton)
37                 k |= key_modifier::alt;
38         return k;
39 }
40
41
42 /// return the LyX mouse button state from Qt's
43 mouse_button::state q_button_state(Qt::ButtonState button)
44 {
45         mouse_button::state b = mouse_button::none;
46         switch (button) {
47                 case Qt::LeftButton:
48                         b = mouse_button::button1;
49                         break;
50                 case Qt::MidButton:
51                         b = mouse_button::button2;
52                         break;
53                 case Qt::RightButton:
54                         b = mouse_button::button3;
55                         break;
56                 default:
57                         break;
58         }
59         return b;
60 }
61
62
63 /// return the LyX mouse button state from Qt's
64 mouse_button::state q_motion_state(Qt::ButtonState state)
65 {
66         mouse_button::state b = mouse_button::none;
67         if (state & Qt::LeftButton)
68                 b |= mouse_button::button1;
69         if (state & Qt::MidButton)
70                 b |= mouse_button::button2;
71         if (state & Qt::RightButton)
72                 b |= mouse_button::button3;
73         return b;
74 }
75
76 } // namespace anon
77
78
79 SyntheticMouseEvent::SyntheticMouseEvent()
80         : timeout(200), restart_timeout(true),
81           x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
82 {}
83
84
85 QContentPane::QContentPane(QWorkArea * parent)
86         : QWidget(parent, "content_pane", WRepaintNoErase),
87           track_scrollbar_(true), wa_(parent)
88 {
89         synthetic_mouse_event_.timeout.timeout.connect(
90                 boost::bind(&QContentPane::generateSyntheticMouseEvent,
91                             this));
92
93         setFocusPolicy(QWidget::WheelFocus);
94         setFocus();
95         setCursor(ibeamCursor);
96
97         // stupid moc strikes again
98         connect(wa_->scrollbar_, SIGNAL(valueChanged(int)),
99                 this, SLOT(scrollBarChanged(int)));
100 }
101
102
103 void QContentPane::generateSyntheticMouseEvent()
104 {
105         // Set things off to generate the _next_ 'pseudo' event.
106         if (synthetic_mouse_event_.restart_timeout)
107                 synthetic_mouse_event_.timeout.start();
108
109         // Has anything changed on-screen since the last timeout signal
110         // was received?
111         double const scrollbar_value = wa_->scrollbar_->value();
112         if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
113                 // Yes it has. Store the params used to check this.
114                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
115
116                 // ... and dispatch the event to the LyX core.
117                 wa_->dispatch(synthetic_mouse_event_.cmd);
118         }
119 }
120
121
122 void QContentPane::scrollBarChanged(int val)
123 {
124         if (track_scrollbar_)
125                 wa_->scrollDocView(val);
126 }
127
128
129 void QContentPane::mousePressEvent(QMouseEvent * e)
130 {
131         if (dc_event_.active && dc_event_ == *e) {
132                 dc_event_.active = false;
133                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
134                         dc_event_.x, dc_event_.y,
135                         q_button_state(dc_event_.state));
136                 wa_->dispatch(cmd);
137                 return;
138         }
139
140         FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
141                               q_button_state(e->button()));
142         wa_->dispatch(cmd);
143 }
144
145
146 void QContentPane::mouseReleaseEvent(QMouseEvent * e)
147 {
148         if (synthetic_mouse_event_.timeout.running())
149                 synthetic_mouse_event_.timeout.stop();
150
151         FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
152                               q_button_state(e->button()));
153         wa_->dispatch(cmd);
154 }
155
156
157 void QContentPane::mouseMoveEvent(QMouseEvent * e)
158 {
159         FuncRequest const cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
160                               q_motion_state(e->state()));
161
162         // If we're above or below the work area...
163         if (e->y() <= 0 || e->y() >= height()) {
164                 // Store the event, to be handled when the timeout expires.
165                 synthetic_mouse_event_.cmd = cmd;
166
167                 if (synthetic_mouse_event_.timeout.running())
168                         // Discard the event. Note that it _may_ be handled
169                         // when the timeout expires if
170                         // synthetic_mouse_event_.cmd has not been overwritten.
171                         // Ie, when the timeout expires, we handle the
172                         // most recent event but discard all others that
173                         // occurred after the one used to start the timeout
174                         // in the first place.
175                         return;
176                 else {
177                         synthetic_mouse_event_.restart_timeout = true;
178                         synthetic_mouse_event_.timeout.start();
179                         // Fall through to handle this event...
180                 }
181
182         } else if (synthetic_mouse_event_.timeout.running()) {
183                 // Store the event, to be possibly handled when the timeout
184                 // expires.
185                 // Once the timeout has expired, normal control is returned
186                 // to mouseMoveEvent (restart_timeout = false).
187                 // This results in a much smoother 'feel' when moving the
188                 // mouse back into the work area.
189                 synthetic_mouse_event_.cmd = cmd;
190                 synthetic_mouse_event_.restart_timeout = false;
191                 return;
192         }
193
194         // Has anything changed on-screen since the last QMouseEvent
195         // was received?
196         double const scrollbar_value = wa_->scrollbar_->value();
197         if (e->x() != synthetic_mouse_event_.x_old ||
198             e->y() != synthetic_mouse_event_.y_old ||
199             scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
200                 // Yes it has. Store the params used to check this.
201                 synthetic_mouse_event_.x_old = e->x();
202                 synthetic_mouse_event_.y_old = e->y();
203                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
204
205                 // ... and dispatch the event to the LyX core.
206                 wa_->dispatch(cmd);
207         }
208 }
209
210
211 void QContentPane::wheelEvent(QWheelEvent * e)
212 {
213         wa_->scrollbar_->setValue(wa_->scrollbar_->value() - e->delta());
214 }
215
216
217 void QContentPane::keyPressEvent(QKeyEvent * e)
218 {
219         boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
220         sym->set(e);
221         wa_->workAreaKeyPress(sym, q_key_state(e->state()));
222 }
223
224
225 void QContentPane::doubleClickTimeout()
226 {
227         if (!dc_event_.active)
228                 return;
229
230         dc_event_.active = false;
231
232         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
233                 dc_event_.x, dc_event_.y,
234                 q_button_state(dc_event_.state));
235         wa_->dispatch(cmd);
236 }
237
238
239 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
240 {
241         dc_event_ = double_click(e);
242
243         // doubleClickInterval() is just too long.
244         QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
245                 this, SLOT(doubleClickTimeout()));
246 }
247
248
249 void QContentPane::resizeEvent(QResizeEvent *)
250 {
251         if (!pixmap_.get()) {
252                 pixmap_.reset(new QPixmap(width(), height()));
253         }
254
255         pixmap_->resize(width(), height());
256         wa_->workAreaResize();
257 }
258
259
260 void QContentPane::paintEvent(QPaintEvent * e)
261 {
262         if (!pixmap_.get()) {
263                 pixmap_.reset(new QPixmap(width(), height()));
264                 wa_->workAreaResize();
265                 return;
266         }
267
268         QRect r(e->rect());
269
270         QPainter q(this);
271         q.drawPixmap(QPoint(r.x(), r.y()),
272                 *pixmap_.get(), r);
273 }
274
275
276 void QContentPane::trackScrollbar(bool track_on)
277 {
278         track_scrollbar_ = track_on;
279 }