3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
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"
17 #include "QContentPane.h"
18 #include "QLyXKeySym.h"
20 #include <qapplication.h>
24 #include <boost/bind.hpp>
28 /// return the LyX key state from Qt's
29 key_modifier::state q_key_state(Qt::ButtonState state)
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;
42 /// return the LyX mouse button state from Qt's
43 mouse_button::state q_button_state(Qt::ButtonState button)
45 mouse_button::state b = mouse_button::none;
48 b = mouse_button::button1;
51 b = mouse_button::button2;
54 b = mouse_button::button3;
63 /// return the LyX mouse button state from Qt's
64 mouse_button::state q_motion_state(Qt::ButtonState state)
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;
79 SyntheticMouseEvent::SyntheticMouseEvent()
80 : timeout(200), restart_timeout(true),
81 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
85 QContentPane::QContentPane(QWorkArea * parent)
86 : QWidget(parent, "content_pane", WRepaintNoErase),
87 track_scrollbar_(true), wa_(parent)
89 synthetic_mouse_event_.timeout.timeout.connect(
90 boost::bind(&QContentPane::generateSyntheticMouseEvent,
93 setFocusPolicy(QWidget::WheelFocus);
95 setCursor(ibeamCursor);
97 // stupid moc strikes again
98 connect(wa_->scrollbar_, SIGNAL(valueChanged(int)),
99 this, SLOT(scrollBarChanged(int)));
103 void QContentPane::generateSyntheticMouseEvent()
105 // Set things off to generate the _next_ 'pseudo' event.
106 if (synthetic_mouse_event_.restart_timeout)
107 synthetic_mouse_event_.timeout.start();
109 // Has anything changed on-screen since the last timeout signal
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;
116 // ... and dispatch the event to the LyX core.
117 wa_->dispatch(synthetic_mouse_event_.cmd);
122 void QContentPane::scrollBarChanged(int val)
124 if (track_scrollbar_)
125 wa_->scrollDocView(val);
129 void QContentPane::mousePressEvent(QMouseEvent * e)
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));
140 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
141 q_button_state(e->button()));
146 void QContentPane::mouseReleaseEvent(QMouseEvent * e)
148 if (synthetic_mouse_event_.timeout.running())
149 synthetic_mouse_event_.timeout.stop();
151 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
152 q_button_state(e->button()));
157 void QContentPane::mouseMoveEvent(QMouseEvent * e)
159 FuncRequest const cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
160 q_motion_state(e->state()));
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;
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.
177 synthetic_mouse_event_.restart_timeout = true;
178 synthetic_mouse_event_.timeout.start();
179 // Fall through to handle this event...
182 } else if (synthetic_mouse_event_.timeout.running()) {
183 // Store the event, to be possibly handled when the timeout
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;
194 // Has anything changed on-screen since the last QMouseEvent
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;
205 // ... and dispatch the event to the LyX core.
211 void QContentPane::wheelEvent(QWheelEvent * e)
213 wa_->scrollbar_->setValue(wa_->scrollbar_->value() - e->delta());
217 void QContentPane::keyPressEvent(QKeyEvent * e)
219 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
221 wa_->workAreaKeyPress(sym, q_key_state(e->state()));
225 void QContentPane::doubleClickTimeout()
227 if (!dc_event_.active)
230 dc_event_.active = false;
232 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
233 dc_event_.x, dc_event_.y,
234 q_button_state(dc_event_.state));
239 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
241 dc_event_ = double_click(e);
243 // doubleClickInterval() is just too long.
244 QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
245 this, SLOT(doubleClickTimeout()));
249 void QContentPane::resizeEvent(QResizeEvent *)
251 if (!pixmap_.get()) {
252 pixmap_.reset(new QPixmap(width(), height()));
255 pixmap_->resize(width(), height());
256 wa_->workAreaResize();
260 void QContentPane::paintEvent(QPaintEvent * e)
262 if (!pixmap_.get()) {
263 pixmap_.reset(new QPixmap(width(), height()));
264 wa_->workAreaResize();
271 q.drawPixmap(QPoint(r.x(), r.y()),
276 void QContentPane::trackScrollbar(bool track_on)
278 track_scrollbar_ = track_on;