3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "GuiWorkArea.h"
16 #include "GuiApplication.h"
17 #include "QLPainter.h"
18 #include "QLyXKeySym.h"
19 #include "qt_helpers.h"
23 #include "BufferView.h"
25 #include "funcrequest.h"
28 #include "support/os.h"
31 #include <QMainWindow>
34 #include <QDragEnterEvent>
39 #include <boost/bind.hpp>
40 #include <boost/current_function.hpp>
42 // Abdel (26/06/2006):
43 // On windows-XP the UserGuide PageDown scroll test is faster without event pruning (16 s)
44 // than with it (23 s).
46 #define USE_EVENT_PRUNING 0
48 #define USE_EVENT_PRUNING 0
54 namespace os = lyx::support::os;
58 /// return the LyX key state from Qt's
59 static key_modifier::state q_key_state(Qt::KeyboardModifiers state)
61 key_modifier::state k = key_modifier::none;
62 if (state & Qt::ControlModifier)
63 k |= key_modifier::ctrl;
64 if (state & Qt::ShiftModifier)
65 k |= key_modifier::shift;
66 if (state & Qt::AltModifier || state & Qt::MetaModifier)
67 k |= key_modifier::alt;
72 /// return the LyX mouse button state from Qt's
73 static mouse_button::state q_button_state(Qt::MouseButton button)
75 mouse_button::state b = mouse_button::none;
78 b = mouse_button::button1;
81 b = mouse_button::button2;
84 b = mouse_button::button3;
93 /// retddurn the LyX mouse button state from Qt's
94 mouse_button::state q_motion_state(Qt::MouseButton state)
96 mouse_button::state b = mouse_button::none;
97 if (state & Qt::LeftButton)
98 b |= mouse_button::button1;
99 if (state & Qt::MidButton)
100 b |= mouse_button::button2;
101 if (state & Qt::RightButton)
102 b |= mouse_button::button3;
109 // This is a 'heartbeat' generating synthetic mouse move events when the
110 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
111 SyntheticMouseEvent::SyntheticMouseEvent()
112 : timeout(200), restart_timeout(true),
113 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
117 GuiWorkArea::GuiWorkArea(int w, int h, LyXView & lyx_view)
118 : WorkArea(lyx_view), painter_(this)
120 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
121 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
123 setAcceptDrops(true);
125 setMinimumSize(100, 70);
127 viewport()->setAutoFillBackground(false);
128 viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
129 setFocusPolicy(Qt::WheelFocus);
131 viewport()->setCursor(Qt::IBeamCursor);
135 synthetic_mouse_event_.timeout.timeout.connect(
136 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
139 // Initialize the vertical Scroll Bar
140 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
141 this, SLOT(adjustViewWithScrollBar(int)));
143 // PageStep only depends on the viewport height.
144 verticalScrollBar()->setPageStep(viewport()->height());
146 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
147 << "\n Area width\t" << width()
148 << "\n Area height\t" << height()
149 << "\n viewport width\t" << viewport()->width()
150 << "\n viewport height\t" << viewport()->height()
153 if (USE_EVENT_PRUNING) {
154 // This is the keyboard buffering stuff...
155 // I don't see any need for this under windows. The keyboard is reactive
158 if ( !QObject::connect(&step_timer_, SIGNAL(timeout()),
159 this, SLOT(keyeventTimeout())) )
160 lyxerr[Debug::GUI] << "ERROR: keyeventTimeout cannot connect!" << endl;
162 // Start the timer, one-shot.
163 step_timer_.setSingleShot(true);
164 step_timer_.start(50);
167 // Enables input methods for asian languages.
168 // Must be set when creating custom text editing widgets.
169 setAttribute(Qt::WA_InputMethodEnabled, true);
173 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
175 verticalScrollBar()->setTracking(false);
177 // do what cursor movement does (some grey)
179 int scroll_max_ = std::max(0, h - height());
181 verticalScrollBar()->setRange(0, scroll_max_);
182 verticalScrollBar()->setSliderPosition(scroll_pos);
183 verticalScrollBar()->setSingleStep(scroll_line_step);
184 verticalScrollBar()->setValue(scroll_pos);
186 verticalScrollBar()->setTracking(true);
190 void GuiWorkArea::adjustViewWithScrollBar(int)
192 scrollBufferView(verticalScrollBar()->sliderPosition());
196 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
198 if (event->mimeData()->hasUrls())
200 /// \todo Ask lyx-devel is this is enough:
201 /// if (event->mimeData()->hasFormat("text/plain"))
202 /// event->acceptProposedAction();
206 void GuiWorkArea::dropEvent(QDropEvent* event)
208 QList<QUrl> files = event->mimeData()->urls();
212 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
213 for (int i = 0; i!=files.size(); ++i) {
214 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
216 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
221 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
223 if (dc_event_.active && dc_event_ == *e) {
224 dc_event_.active = false;
225 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
226 dc_event_.x, dc_event_.y,
227 q_button_state(dc_event_.state));
232 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
233 q_button_state(e->button()));
238 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
240 if (synthetic_mouse_event_.timeout.running())
241 synthetic_mouse_event_.timeout.stop();
243 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
244 q_button_state(e->button()));
249 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
251 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
252 q_motion_state(e->button()));
254 // If we're above or below the work area...
255 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
256 // Make sure only a synthetic event can cause a page scroll,
257 // so they come at a steady rate:
259 // _Force_ a scroll up:
262 cmd.y = viewport()->height();
263 // Store the event, to be handled when the timeout expires.
264 synthetic_mouse_event_.cmd = cmd;
266 if (synthetic_mouse_event_.timeout.running())
267 // Discard the event. Note that it _may_ be handled
268 // when the timeout expires if
269 // synthetic_mouse_event_.cmd has not been overwritten.
270 // Ie, when the timeout expires, we handle the
271 // most recent event but discard all others that
272 // occurred after the one used to start the timeout
273 // in the first place.
276 synthetic_mouse_event_.restart_timeout = true;
277 synthetic_mouse_event_.timeout.start();
278 // Fall through to handle this event...
281 } else if (synthetic_mouse_event_.timeout.running()) {
282 // Store the event, to be possibly handled when the timeout
284 // Once the timeout has expired, normal control is returned
285 // to mouseMoveEvent (restart_timeout = false).
286 // This results in a much smoother 'feel' when moving the
287 // mouse back into the work area.
288 synthetic_mouse_event_.cmd = cmd;
289 synthetic_mouse_event_.restart_timeout = false;
293 // Has anything changed on-screen since the last QMouseEvent
295 double const scrollbar_value = verticalScrollBar()->value();
296 if (e->x() != synthetic_mouse_event_.x_old ||
297 e->y() != synthetic_mouse_event_.y_old ||
298 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
299 // Yes it has. Store the params used to check this.
300 synthetic_mouse_event_.x_old = e->x();
301 synthetic_mouse_event_.y_old = e->y();
302 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
304 // ... and dispatch the event to the LyX core.
310 void GuiWorkArea::wheelEvent(QWheelEvent * e)
312 // Wheel rotation by one notch results in a delta() of 120 (see
313 // documentation of QWheelEvent)
314 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
315 verticalScrollBar()->setValue(verticalScrollBar()->value() -
316 lines * verticalScrollBar()->singleStep());
317 adjustViewWithScrollBar();
321 void GuiWorkArea::generateSyntheticMouseEvent()
323 // Set things off to generate the _next_ 'pseudo' event.
324 if (synthetic_mouse_event_.restart_timeout)
325 synthetic_mouse_event_.timeout.start();
327 // Has anything changed on-screen since the last timeout signal
329 double const scrollbar_value = verticalScrollBar()->value();
330 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
331 // Yes it has. Store the params used to check this.
332 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
334 // ... and dispatch the event to the LyX core.
335 dispatch(synthetic_mouse_event_.cmd);
340 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
342 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
343 << " count=" << e->count()
344 << " text=" << fromqstr(e->text())
345 << " isAutoRepeat=" << e->isAutoRepeat()
346 << " key=" << e->key()
349 if (USE_EVENT_PRUNING) {
350 keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
353 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
355 processKeySym(sym, q_key_state(e->modifiers()));
360 // This is used only if USE_EVENT_PRUNING is defined...
361 void GuiWorkArea::keyeventTimeout()
363 bool handle_autos = true;
365 while (!keyeventQueue_.empty()) {
366 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
368 // We never handle more than one auto repeated
369 // char in a list of queued up events.
370 if (!handle_autos && ev->isAutoRepeat()) {
371 keyeventQueue_.pop();
375 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
378 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
379 << " count=" << ev->count()
380 << " text=" << fromqstr(ev->text())
381 << " isAutoRepeat=" << ev->isAutoRepeat()
382 << " key=" << ev->key()
385 processKeySym(sym, q_key_state(ev->modifiers()));
386 keyeventQueue_.pop();
388 handle_autos = false;
391 // Restart the timer.
392 step_timer_.setSingleShot(true);
393 step_timer_.start(25);
397 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
399 dc_event_ = double_click(e);
401 if (!dc_event_.active)
404 dc_event_.active = false;
406 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
407 dc_event_.x, dc_event_.y,
408 q_button_state(dc_event_.state));
413 void GuiWorkArea::resizeEvent(QResizeEvent *)
415 verticalScrollBar()->setPageStep(viewport()->height());
416 paint_device_ = QPixmap(viewport()->width(), viewport()->height());
421 void GuiWorkArea::update(int x, int y, int w, int h)
423 viewport()->repaint(x, y, w, h);
427 void GuiWorkArea::paintEvent(QPaintEvent * e)
429 lyxerr << "paintEvent begin: x: " << e->rect().x()
430 << " y: " << e->rect().y()
431 << " w: " << e->rect().width()
432 << " h: " << e->rect().height() << endl;
434 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
435 << "\n QWidget width\t" << this->width()
436 << "\n QWidget height\t" << this->height()
437 << "\n viewport width\t" << viewport()->width()
438 << "\n viewport height\t" << viewport()->height()
439 << "\n pixmap width\t" << pixmap_->width()
440 << "\n pixmap height\t" << pixmap_->height()
441 << "\n QPaintEvent x\t" << e->rect().x()
442 << "\n QPaintEvent y\t" << e->rect().y()
443 << "\n QPaintEvent w\t" << e->rect().width()
444 << "\n QPaintEvent h\t" << e->rect().height()
448 QPainter q(viewport());
449 q.drawPixmap(e->rect(), paint_device_, e->rect());
452 q.drawPixmap(cursor_x_, cursor_y_, vcursor_);
455 q.drawPixmap(cursor_x_, cursor_y_ + cursor_h_ - 1, hcursor_);
457 lyxerr << "paintEvent end" << endl;
461 QPixmap GuiWorkArea::copyScreen(int x, int y, int w, int h) const
463 lyxerr << "copyScreen begin: x: " << x << " y: " << y << endl;
464 return paint_device_.copy(x, y, w, h);
465 lyxerr << "copyScreen end " << endl;
469 void GuiWorkArea::drawScreen(int x, int y, const QPixmap & pixmap)
471 lyxerr << "drawScreen begin: x: " << x << " y: " << y << endl;
472 QPainter q(&paint_device_);
473 q.drawPixmap(x, y, pixmap);
474 update(x, y, pixmap.width(), pixmap.height());
475 lyxerr << "drawScreen end" << endl;
479 void GuiWorkArea::expose(int x, int y, int w, int h)
485 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
487 if (!qApp->focusWidget())
490 show_vcursor_ = true;
492 QColor const & required_color = guiApp->colorCache().get(LColor::cursor);
494 if (x==cursor_x_ && y==cursor_y_ && h==cursor_h_
495 && cursor_color_ == required_color
496 && cursor_shape_ == shape) {
497 show_hcursor_ = lshape_cursor_;
498 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
502 // Cache the dimensions of the cursor.
506 cursor_color_ = required_color;
507 cursor_shape_ = shape;
509 switch (cursor_shape_) {
511 // FIXME the cursor width shouldn't be hard-coded!
513 lshape_cursor_ = false;
516 cursor_w_ = cursor_h_ / 3;
517 lshape_cursor_ = true;
519 case REVERSED_L_SHAPE:
520 cursor_w_ = cursor_h_ / 3;
521 cursor_x_ -= cursor_w_ - 1;
522 lshape_cursor_ = true;
526 // We cache two pixmaps:
527 // 1 the vertical line of the cursor.
528 // 2 the horizontal line of the L-shaped cursor (if necessary).
530 // Draw the new (vertical) cursor.
531 vcursor_ = QPixmap(cursor_w_, cursor_h_);
532 vcursor_.fill(cursor_color_);
534 // Draw the new (horizontal) cursor if necessary.
535 if (lshape_cursor_) {
536 hcursor_ = QPixmap(cursor_w_, 1);
537 hcursor_.fill(cursor_color_);
538 show_hcursor_ = true;
541 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
545 void GuiWorkArea::removeCursor()
547 show_vcursor_ = false;
548 show_hcursor_ = false;
550 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
554 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
556 QString const & text = e->commitString();
557 if (!text.isEmpty()) {
559 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
560 << " preeditString =" << fromqstr(e->preeditString())
561 << " commitString =" << fromqstr(e->commitString())
565 // needed to make math superscript work on some systems
566 // ideally, such special coding should not be necessary
568 key = Qt::Key_AsciiCircum;
569 // FIXME: Needs for investigation, this key is not really used,
570 // the ctor below just check if key is different from 0.
571 QKeyEvent ev(QEvent::KeyPress, key,
572 Qt::NoModifier, text);
578 } // namespace frontend
581 #include "GuiWorkArea_moc.cpp"