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.
17 #include "GuiWorkArea.h"
19 #include "QLPainter.h"
20 #include "QLyXKeySym.h"
22 #include "ColorCache.h"
23 #include "qt_helpers.h"
24 #include "Application.h"
25 #include "BufferView.h"
27 #include "funcrequest.h"
29 #include "support/os.h"
32 #include <QMainWindow>
35 #include <QDragEnterEvent>
40 #include <boost/bind.hpp>
41 #include <boost/current_function.hpp>
43 // Abdel (26/06/2006):
44 // On windows-XP the UserGuide PageDown scroll test is faster without event pruning (16 s)
45 // than with it (23 s).
47 #define USE_EVENT_PRUNING 0
49 #define USE_EVENT_PRUNING 0
55 namespace os = lyx::support::os;
59 /// return the LyX key state from Qt's
60 key_modifier::state q_key_state(Qt::KeyboardModifiers state)
62 key_modifier::state k = key_modifier::none;
63 if (state & Qt::ControlModifier)
64 k |= key_modifier::ctrl;
65 if (state & Qt::ShiftModifier)
66 k |= key_modifier::shift;
67 if (state & Qt::AltModifier || state & Qt::MetaModifier)
68 k |= key_modifier::alt;
73 /// return the LyX mouse button state from Qt's
74 mouse_button::state q_button_state(Qt::MouseButton button)
76 mouse_button::state b = mouse_button::none;
79 b = mouse_button::button1;
82 b = mouse_button::button2;
85 b = mouse_button::button3;
94 /// return the LyX mouse button state from Qt's
95 mouse_button::state q_motion_state(Qt::MouseButton state)
97 mouse_button::state b = mouse_button::none;
98 if (state & Qt::LeftButton)
99 b |= mouse_button::button1;
100 if (state & Qt::MidButton)
101 b |= mouse_button::button2;
102 if (state & Qt::RightButton)
103 b |= mouse_button::button3;
112 // This is a 'heartbeat' generating synthetic mouse move events when the
113 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
114 SyntheticMouseEvent::SyntheticMouseEvent()
115 : timeout(200), restart_timeout(true),
116 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
120 GuiWorkArea::GuiWorkArea(int w, int h, LyXView & lyx_view)
121 : WorkArea(lyx_view), painter_(this)
123 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
124 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
126 setAcceptDrops(true);
128 setMinimumSize(100, 70);
130 viewport()->setAutoFillBackground(false);
131 viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
133 viewport()->setFocusPolicy(Qt::WheelFocus);
134 viewport()->setFocus();
135 setFocusPolicy(Qt::WheelFocus);
137 viewport()->setCursor(Qt::IBeamCursor);
143 synthetic_mouse_event_.timeout.timeout.connect(
144 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
147 // Initialize the vertical Scroll Bar
148 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
149 this, SLOT(adjustViewWithScrollBar(int)));
151 // PageStep only depends on the viewport height.
152 verticalScrollBar()->setPageStep(workHeight_);
154 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
155 << "\n Area width\t" << width()
156 << "\n Area height\t" << height()
157 << "\n viewport width\t" << viewport()->width()
158 << "\n viewport height\t" << viewport()->height()
161 if (USE_EVENT_PRUNING) {
162 // This is the keyboard buffering stuff...
163 // I don't see any need for this under windows. The keyboard is reactive
166 if ( !QObject::connect(&step_timer_, SIGNAL(timeout()),
167 this, SLOT(keyeventTimeout())) )
168 lyxerr[Debug::GUI] << "ERROR: keyeventTimeout cannot connect!" << endl;
170 // Start the timer, one-shot.
171 step_timer_.setSingleShot(true);
172 step_timer_.start(50);
175 // Enables input methods for asian languages.
176 // Must be set when creating custom text editing widgets.
177 setAttribute(Qt::WA_InputMethodEnabled, true);
180 GuiWorkArea::~GuiWorkArea()
185 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
187 // do what cursor movement does (some grey)
189 int scroll_max_ = std::max(0, h - height());
191 verticalScrollBar()->setRange(0, scroll_max_);
192 verticalScrollBar()->setSliderPosition(scroll_pos);
193 verticalScrollBar()->setSingleStep(scroll_line_step);
197 void GuiWorkArea::adjustViewWithScrollBar(int)
199 scrollBufferView(verticalScrollBar()->sliderPosition());
203 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
205 if (event->mimeData()->hasUrls())
207 /// \todo Ask lyx-devel is this is enough:
208 /// if (event->mimeData()->hasFormat("text/plain"))
209 /// event->acceptProposedAction();
213 void GuiWorkArea::dropEvent(QDropEvent* event)
215 QList<QUrl> files = event->mimeData()->urls();
219 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
220 for (int i = 0; i!=files.size(); ++i) {
221 string const file = os::internal_path(fromqstr(files.at(i).toString()));
223 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
228 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
230 if (dc_event_.active && dc_event_ == *e) {
231 dc_event_.active = false;
232 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
233 dc_event_.x, dc_event_.y,
234 q_button_state(dc_event_.state));
239 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
240 q_button_state(e->button()));
245 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
247 if (synthetic_mouse_event_.timeout.running())
248 synthetic_mouse_event_.timeout.stop();
250 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
251 q_button_state(e->button()));
256 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
258 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
259 q_motion_state(e->button()));
261 // If we're above or below the work area...
262 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
263 // Make sure only a synthetic event can cause a page scroll,
264 // so they come at a steady rate:
266 // _Force_ a scroll up:
269 cmd.y = viewport()->height();
270 // Store the event, to be handled when the timeout expires.
271 synthetic_mouse_event_.cmd = cmd;
273 if (synthetic_mouse_event_.timeout.running())
274 // Discard the event. Note that it _may_ be handled
275 // when the timeout expires if
276 // synthetic_mouse_event_.cmd has not been overwritten.
277 // Ie, when the timeout expires, we handle the
278 // most recent event but discard all others that
279 // occurred after the one used to start the timeout
280 // in the first place.
283 synthetic_mouse_event_.restart_timeout = true;
284 synthetic_mouse_event_.timeout.start();
285 // Fall through to handle this event...
288 } else if (synthetic_mouse_event_.timeout.running()) {
289 // Store the event, to be possibly handled when the timeout
291 // Once the timeout has expired, normal control is returned
292 // to mouseMoveEvent (restart_timeout = false).
293 // This results in a much smoother 'feel' when moving the
294 // mouse back into the work area.
295 synthetic_mouse_event_.cmd = cmd;
296 synthetic_mouse_event_.restart_timeout = false;
300 // Has anything changed on-screen since the last QMouseEvent
302 double const scrollbar_value = verticalScrollBar()->value();
303 if (e->x() != synthetic_mouse_event_.x_old ||
304 e->y() != synthetic_mouse_event_.y_old ||
305 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
306 // Yes it has. Store the params used to check this.
307 synthetic_mouse_event_.x_old = e->x();
308 synthetic_mouse_event_.y_old = e->y();
309 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
311 // ... and dispatch the event to the LyX core.
317 void GuiWorkArea::wheelEvent(QWheelEvent * e)
319 // Wheel rotation by one notch results in a delta() of 120 (see
320 // documentation of QWheelEvent)
321 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
322 verticalScrollBar()->setValue(verticalScrollBar()->value() -
323 lines * verticalScrollBar()->singleStep());
324 adjustViewWithScrollBar();
328 void GuiWorkArea::generateSyntheticMouseEvent()
330 // Set things off to generate the _next_ 'pseudo' event.
331 if (synthetic_mouse_event_.restart_timeout)
332 synthetic_mouse_event_.timeout.start();
334 // Has anything changed on-screen since the last timeout signal
336 double const scrollbar_value = verticalScrollBar()->value();
337 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
338 // Yes it has. Store the params used to check this.
339 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
341 // ... and dispatch the event to the LyX core.
342 dispatch(synthetic_mouse_event_.cmd);
347 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
349 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
350 << " count=" << e->count()
351 << " text=" << fromqstr(e->text())
352 << " isAutoRepeat=" << e->isAutoRepeat()
353 << " key=" << e->key()
356 if (USE_EVENT_PRUNING) {
357 keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
360 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
362 processKeySym(sym, q_key_state(e->modifiers()));
367 // This is used only if USE_EVENT_PRUNING is defined...
368 void GuiWorkArea::keyeventTimeout()
370 bool handle_autos = true;
372 while (!keyeventQueue_.empty()) {
373 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
375 // We never handle more than one auto repeated
376 // char in a list of queued up events.
377 if (!handle_autos && ev->isAutoRepeat()) {
378 keyeventQueue_.pop();
382 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
385 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
386 << " count=" << ev->count()
387 << " text=" << fromqstr(ev->text())
388 << " isAutoRepeat=" << ev->isAutoRepeat()
389 << " key=" << ev->key()
392 processKeySym(sym, q_key_state(ev->modifiers()));
393 keyeventQueue_.pop();
395 handle_autos = false;
398 // Restart the timer.
399 step_timer_.setSingleShot(true);
400 step_timer_.start(25);
404 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
406 dc_event_ = double_click(e);
408 if (!dc_event_.active)
411 dc_event_.active = false;
413 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
414 dc_event_.x, dc_event_.y,
415 q_button_state(dc_event_.state));
420 void GuiWorkArea::resizeEvent(QResizeEvent *)
422 workWidth_ = viewport()->width();
423 workHeight_ = viewport()->height();
425 verticalScrollBar()->setPageStep(viewport()->height());
427 // screen_device_ = QPixmap(viewport()->width(), viewport()->height());
428 // paint_device_ = QImage(viewport()->width(), viewport()->height(), QImage::Format_RGB32);
429 paint_device_ = QPixmap(viewport()->width(), viewport()->height());
434 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
435 << "\n QWidget width\t" << this->QWidget::width()
436 << "\n QWidget height\t" << this->QWidget::height()
437 << "\n viewport width\t" << viewport()->width()
438 << "\n viewport height\t" << viewport()->height()
439 << "\n QResizeEvent rect left\t" << rect().left()
440 << "\n QResizeEvent rect right\t" << rect().right()
446 void GuiWorkArea::update(int x, int y, int w, int h)
448 //screen_device_.fromImage(paint_device_);
449 //QPainter q(&screen_device_);
450 //q.drawImage(x, y, paint_device_.copy(x, y, w, h));
452 viewport()->update(x, y, w, h);
456 void GuiWorkArea::paintEvent(QPaintEvent * e)
459 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
460 << "\n QWidget width\t" << this->width()
461 << "\n QWidget height\t" << this->height()
462 << "\n viewport width\t" << viewport()->width()
463 << "\n viewport height\t" << viewport()->height()
464 << "\n pixmap width\t" << pixmap_->width()
465 << "\n pixmap height\t" << pixmap_->height()
466 << "\n QPaintEvent x\t" << e->rect().x()
467 << "\n QPaintEvent y\t" << e->rect().y()
468 << "\n QPaintEvent w\t" << e->rect().width()
469 << "\n QPaintEvent h\t" << e->rect().height()
473 QPainter q(viewport());
474 q.drawPixmap(e->rect(), paint_device_, e->rect());
477 q.drawPixmap(cursor_x_, cursor_y_, vcursor_);
480 q.drawPixmap(cursor_x_, cursor_y_ + cursor_h_ - 1, hcursor_);
482 buffer_view_->updateScrollbar();
484 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
486 verticalScrollBar()->setTracking(false);
487 setScrollbarParams(scroll_.height, scroll_.position,
488 scroll_.lineScrollHeight);
489 verticalScrollBar()->setTracking(true);
493 QPixmap GuiWorkArea::copyScreen(int x, int y, int w, int h) const
495 return paint_device_.copy(x, y, w, h);
499 void GuiWorkArea::drawScreen(int x, int y, QPixmap pixmap)
501 QPainter q(&paint_device_);
502 q.drawPixmap(x, y, pixmap);
503 update(x, y, pixmap.width(), pixmap.height());
507 void GuiWorkArea::expose(int x, int y, int w, int h)
510 if (x == 0 && y == 0 && w == viewport()->width() && h == viewport()->height()) {
511 viewport()->repaint(x, y, w, h);
520 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
522 if (!qApp->focusWidget())
525 show_vcursor_ = true;
527 QColor const & required_color = lcolorcache.get(LColor::cursor);
529 if (x==cursor_x_ && y==cursor_y_ && h==cursor_h_
530 && cursor_color_ == required_color
531 && cursor_shape_ == shape) {
532 show_hcursor_ = lshape_cursor_;
533 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
537 // Cache the dimensions of the cursor.
541 cursor_color_ = required_color;
542 cursor_shape_ = shape;
544 switch (cursor_shape_) {
546 // FIXME the cursor width shouldn't be hard-coded!
548 lshape_cursor_ = false;
551 cursor_w_ = cursor_h_ / 3;
552 lshape_cursor_ = true;
554 case REVERSED_L_SHAPE:
555 cursor_w_ = cursor_h_ / 3;
556 cursor_x_ -= cursor_w_ - 1;
557 lshape_cursor_ = true;
561 // We cache two pixmaps:
562 // 1 the vertical line of the cursor.
563 // 2 the horizontal line of the L-shaped cursor (if necessary).
565 // Draw the new (vertical) cursor.
566 vcursor_ = QPixmap(cursor_w_, cursor_h_);
567 vcursor_.fill(cursor_color_);
569 // Draw the new (horizontal) cursor if necessary.
570 if (lshape_cursor_) {
571 hcursor_ = QPixmap(cursor_w_, 1);
572 hcursor_.fill(cursor_color_);
573 show_hcursor_ = true;
576 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
580 void GuiWorkArea::removeCursor()
582 show_vcursor_ = false;
583 show_hcursor_ = false;
585 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
589 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
591 QString const & text = e->commitString();
592 if (!text.isEmpty()) {
594 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
595 << " preeditString =" << fromqstr(e->preeditString())
596 << " commitString =" << fromqstr(e->commitString())
600 // needed to make math superscript work on some systems
601 // ideally, such special coding should not be necessary
603 key = Qt::Key_AsciiCircum;
604 QKeyEvent ev(QEvent::KeyPress, key, 0, text);
611 } // namespace frontend
614 #include "GuiWorkArea_moc.cpp"