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 "Application.h"
17 #include "ColorCache.h"
18 #include "QLPainter.h"
19 #include "QLyXKeySym.h"
20 #include "qt_helpers.h"
22 #include "BufferView.h"
24 #include "funcrequest.h"
27 #include "support/os.h"
30 #include <QMainWindow>
33 #include <QDragEnterEvent>
38 #include <boost/bind.hpp>
39 #include <boost/current_function.hpp>
41 // Abdel (26/06/2006):
42 // On windows-XP the UserGuide PageDown scroll test is faster without event pruning (16 s)
43 // than with it (23 s).
45 #define USE_EVENT_PRUNING 0
47 #define USE_EVENT_PRUNING 0
53 namespace os = lyx::support::os;
57 /// return the LyX key state from Qt's
58 key_modifier::state q_key_state(Qt::KeyboardModifiers state)
60 key_modifier::state k = key_modifier::none;
61 if (state & Qt::ControlModifier)
62 k |= key_modifier::ctrl;
63 if (state & Qt::ShiftModifier)
64 k |= key_modifier::shift;
65 if (state & Qt::AltModifier || state & Qt::MetaModifier)
66 k |= key_modifier::alt;
71 /// return the LyX mouse button state from Qt's
72 mouse_button::state q_button_state(Qt::MouseButton button)
74 mouse_button::state b = mouse_button::none;
77 b = mouse_button::button1;
80 b = mouse_button::button2;
83 b = mouse_button::button3;
92 /// return the LyX mouse button state from Qt's
93 mouse_button::state q_motion_state(Qt::MouseButton state)
95 mouse_button::state b = mouse_button::none;
96 if (state & Qt::LeftButton)
97 b |= mouse_button::button1;
98 if (state & Qt::MidButton)
99 b |= mouse_button::button2;
100 if (state & Qt::RightButton)
101 b |= mouse_button::button3;
110 // This is a 'heartbeat' generating synthetic mouse move events when the
111 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
112 SyntheticMouseEvent::SyntheticMouseEvent()
113 : timeout(200), restart_timeout(true),
114 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
118 GuiWorkArea::GuiWorkArea(int w, int h, LyXView & lyx_view)
119 : WorkArea(lyx_view), painter_(this)
121 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
122 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
124 setAcceptDrops(true);
126 setMinimumSize(100, 70);
128 viewport()->setAutoFillBackground(false);
129 viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
131 viewport()->setFocusPolicy(Qt::WheelFocus);
132 viewport()->setFocus();
133 setFocusPolicy(Qt::WheelFocus);
135 viewport()->setCursor(Qt::IBeamCursor);
139 synthetic_mouse_event_.timeout.timeout.connect(
140 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
143 // Initialize the vertical Scroll Bar
144 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
145 this, SLOT(adjustViewWithScrollBar(int)));
147 // PageStep only depends on the viewport height.
148 verticalScrollBar()->setPageStep(viewport()->height());
150 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
151 << "\n Area width\t" << width()
152 << "\n Area height\t" << height()
153 << "\n viewport width\t" << viewport()->width()
154 << "\n viewport height\t" << viewport()->height()
157 if (USE_EVENT_PRUNING) {
158 // This is the keyboard buffering stuff...
159 // I don't see any need for this under windows. The keyboard is reactive
162 if ( !QObject::connect(&step_timer_, SIGNAL(timeout()),
163 this, SLOT(keyeventTimeout())) )
164 lyxerr[Debug::GUI] << "ERROR: keyeventTimeout cannot connect!" << endl;
166 // Start the timer, one-shot.
167 step_timer_.setSingleShot(true);
168 step_timer_.start(50);
171 // Enables input methods for asian languages.
172 // Must be set when creating custom text editing widgets.
173 setAttribute(Qt::WA_InputMethodEnabled, true);
177 GuiWorkArea::~GuiWorkArea()
182 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
184 verticalScrollBar()->setTracking(false);
186 // do what cursor movement does (some grey)
188 int scroll_max_ = std::max(0, h - height());
190 verticalScrollBar()->setRange(0, scroll_max_);
191 verticalScrollBar()->setSliderPosition(scroll_pos);
192 verticalScrollBar()->setSingleStep(scroll_line_step);
194 verticalScrollBar()->setTracking(true);
198 void GuiWorkArea::adjustViewWithScrollBar(int)
200 scrollBufferView(verticalScrollBar()->sliderPosition());
204 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
206 if (event->mimeData()->hasUrls())
208 /// \todo Ask lyx-devel is this is enough:
209 /// if (event->mimeData()->hasFormat("text/plain"))
210 /// event->acceptProposedAction();
214 void GuiWorkArea::dropEvent(QDropEvent* event)
216 QList<QUrl> files = event->mimeData()->urls();
220 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
221 for (int i = 0; i!=files.size(); ++i) {
222 string const file = os::internal_path(fromqstr(files.at(i).toString()));
224 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
229 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
231 if (dc_event_.active && dc_event_ == *e) {
232 dc_event_.active = false;
233 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
234 dc_event_.x, dc_event_.y,
235 q_button_state(dc_event_.state));
240 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
241 q_button_state(e->button()));
246 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
248 if (synthetic_mouse_event_.timeout.running())
249 synthetic_mouse_event_.timeout.stop();
251 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
252 q_button_state(e->button()));
257 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
259 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
260 q_motion_state(e->button()));
262 // If we're above or below the work area...
263 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
264 // Make sure only a synthetic event can cause a page scroll,
265 // so they come at a steady rate:
267 // _Force_ a scroll up:
270 cmd.y = viewport()->height();
271 // Store the event, to be handled when the timeout expires.
272 synthetic_mouse_event_.cmd = cmd;
274 if (synthetic_mouse_event_.timeout.running())
275 // Discard the event. Note that it _may_ be handled
276 // when the timeout expires if
277 // synthetic_mouse_event_.cmd has not been overwritten.
278 // Ie, when the timeout expires, we handle the
279 // most recent event but discard all others that
280 // occurred after the one used to start the timeout
281 // in the first place.
284 synthetic_mouse_event_.restart_timeout = true;
285 synthetic_mouse_event_.timeout.start();
286 // Fall through to handle this event...
289 } else if (synthetic_mouse_event_.timeout.running()) {
290 // Store the event, to be possibly handled when the timeout
292 // Once the timeout has expired, normal control is returned
293 // to mouseMoveEvent (restart_timeout = false).
294 // This results in a much smoother 'feel' when moving the
295 // mouse back into the work area.
296 synthetic_mouse_event_.cmd = cmd;
297 synthetic_mouse_event_.restart_timeout = false;
301 // Has anything changed on-screen since the last QMouseEvent
303 double const scrollbar_value = verticalScrollBar()->value();
304 if (e->x() != synthetic_mouse_event_.x_old ||
305 e->y() != synthetic_mouse_event_.y_old ||
306 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
307 // Yes it has. Store the params used to check this.
308 synthetic_mouse_event_.x_old = e->x();
309 synthetic_mouse_event_.y_old = e->y();
310 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
312 // ... and dispatch the event to the LyX core.
318 void GuiWorkArea::wheelEvent(QWheelEvent * e)
320 // Wheel rotation by one notch results in a delta() of 120 (see
321 // documentation of QWheelEvent)
322 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
323 verticalScrollBar()->setValue(verticalScrollBar()->value() -
324 lines * verticalScrollBar()->singleStep());
325 adjustViewWithScrollBar();
329 void GuiWorkArea::generateSyntheticMouseEvent()
331 // Set things off to generate the _next_ 'pseudo' event.
332 if (synthetic_mouse_event_.restart_timeout)
333 synthetic_mouse_event_.timeout.start();
335 // Has anything changed on-screen since the last timeout signal
337 double const scrollbar_value = verticalScrollBar()->value();
338 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
339 // Yes it has. Store the params used to check this.
340 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
342 // ... and dispatch the event to the LyX core.
343 dispatch(synthetic_mouse_event_.cmd);
348 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
350 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
351 << " count=" << e->count()
352 << " text=" << fromqstr(e->text())
353 << " isAutoRepeat=" << e->isAutoRepeat()
354 << " key=" << e->key()
357 if (USE_EVENT_PRUNING) {
358 keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
361 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
363 processKeySym(sym, q_key_state(e->modifiers()));
368 // This is used only if USE_EVENT_PRUNING is defined...
369 void GuiWorkArea::keyeventTimeout()
371 bool handle_autos = true;
373 while (!keyeventQueue_.empty()) {
374 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
376 // We never handle more than one auto repeated
377 // char in a list of queued up events.
378 if (!handle_autos && ev->isAutoRepeat()) {
379 keyeventQueue_.pop();
383 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
386 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
387 << " count=" << ev->count()
388 << " text=" << fromqstr(ev->text())
389 << " isAutoRepeat=" << ev->isAutoRepeat()
390 << " key=" << ev->key()
393 processKeySym(sym, q_key_state(ev->modifiers()));
394 keyeventQueue_.pop();
396 handle_autos = false;
399 // Restart the timer.
400 step_timer_.setSingleShot(true);
401 step_timer_.start(25);
405 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
407 dc_event_ = double_click(e);
409 if (!dc_event_.active)
412 dc_event_.active = false;
414 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
415 dc_event_.x, dc_event_.y,
416 q_button_state(dc_event_.state));
421 void GuiWorkArea::resizeEvent(QResizeEvent *)
423 verticalScrollBar()->setPageStep(viewport()->height());
424 paint_device_ = QPixmap(viewport()->width(), viewport()->height());
429 void GuiWorkArea::update(int x, int y, int w, int h)
431 viewport()->update(x, y, w, h);
435 void GuiWorkArea::paintEvent(QPaintEvent * e)
438 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
439 << "\n QWidget width\t" << this->width()
440 << "\n QWidget height\t" << this->height()
441 << "\n viewport width\t" << viewport()->width()
442 << "\n viewport height\t" << viewport()->height()
443 << "\n pixmap width\t" << pixmap_->width()
444 << "\n pixmap height\t" << pixmap_->height()
445 << "\n QPaintEvent x\t" << e->rect().x()
446 << "\n QPaintEvent y\t" << e->rect().y()
447 << "\n QPaintEvent w\t" << e->rect().width()
448 << "\n QPaintEvent h\t" << e->rect().height()
452 QPainter q(viewport());
453 q.drawPixmap(e->rect(), paint_device_, e->rect());
456 q.drawPixmap(cursor_x_, cursor_y_, vcursor_);
459 q.drawPixmap(cursor_x_, cursor_y_ + cursor_h_ - 1, hcursor_);
463 QPixmap GuiWorkArea::copyScreen(int x, int y, int w, int h) const
465 return paint_device_.copy(x, y, w, h);
469 void GuiWorkArea::drawScreen(int x, int y, QPixmap pixmap)
471 QPainter q(&paint_device_);
472 q.drawPixmap(x, y, pixmap);
473 update(x, y, pixmap.width(), pixmap.height());
477 void GuiWorkArea::expose(int x, int y, int w, int h)
480 if (x == 0 && y == 0 && w == viewport()->width() && h == viewport()->height()) {
481 viewport()->repaint(x, y, w, h);
490 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
492 if (!qApp->focusWidget())
495 show_vcursor_ = true;
497 QColor const & required_color = lcolorcache.get(LColor::cursor);
499 if (x==cursor_x_ && y==cursor_y_ && h==cursor_h_
500 && cursor_color_ == required_color
501 && cursor_shape_ == shape) {
502 show_hcursor_ = lshape_cursor_;
503 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
507 // Cache the dimensions of the cursor.
511 cursor_color_ = required_color;
512 cursor_shape_ = shape;
514 switch (cursor_shape_) {
516 // FIXME the cursor width shouldn't be hard-coded!
518 lshape_cursor_ = false;
521 cursor_w_ = cursor_h_ / 3;
522 lshape_cursor_ = true;
524 case REVERSED_L_SHAPE:
525 cursor_w_ = cursor_h_ / 3;
526 cursor_x_ -= cursor_w_ - 1;
527 lshape_cursor_ = true;
531 // We cache two pixmaps:
532 // 1 the vertical line of the cursor.
533 // 2 the horizontal line of the L-shaped cursor (if necessary).
535 // Draw the new (vertical) cursor.
536 vcursor_ = QPixmap(cursor_w_, cursor_h_);
537 vcursor_.fill(cursor_color_);
539 // Draw the new (horizontal) cursor if necessary.
540 if (lshape_cursor_) {
541 hcursor_ = QPixmap(cursor_w_, 1);
542 hcursor_.fill(cursor_color_);
543 show_hcursor_ = true;
546 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
550 void GuiWorkArea::removeCursor()
552 show_vcursor_ = false;
553 show_hcursor_ = false;
555 update(cursor_x_, cursor_y_, cursor_w_, cursor_h_);
559 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
561 QString const & text = e->commitString();
562 if (!text.isEmpty()) {
564 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
565 << " preeditString =" << fromqstr(e->preeditString())
566 << " commitString =" << fromqstr(e->commitString())
570 // needed to make math superscript work on some systems
571 // ideally, such special coding should not be necessary
573 key = Qt::Key_AsciiCircum;
574 // FIXME: Needs for investigation, this key is not really used,
575 // the ctor below just check if key is different from 0.
576 QKeyEvent ev(QEvent::KeyPress, key,
577 Qt::NoModifier, text);
583 } // namespace frontend
586 #include "GuiWorkArea_moc.cpp"