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"
24 #include "rowpainter.h"
26 #include "funcrequest.h"
31 #include "support/filetools.h" // LibFileSearch
32 #include "support/os.h"
34 #include "graphics/GraphicsImage.h"
35 #include "graphics/GraphicsLoader.h"
38 #include <QMainWindow>
41 #include <QDragEnterEvent>
46 #include <boost/bind.hpp>
47 #include <boost/current_function.hpp>
49 // Abdel (26/06/2006):
50 // On windows-XP the UserGuide PageDown scroll test is faster without event pruning (16 s)
51 // than with it (23 s).
53 #define USE_EVENT_PRUNING 0
55 #define USE_EVENT_PRUNING 0
62 namespace os = lyx::support::os;
67 /// return the LyX key state from Qt's
68 static key_modifier::state q_key_state(Qt::KeyboardModifiers state)
70 key_modifier::state k = key_modifier::none;
71 if (state & Qt::ControlModifier)
72 k |= key_modifier::ctrl;
73 if (state & Qt::ShiftModifier)
74 k |= key_modifier::shift;
75 if (state & Qt::AltModifier || state & Qt::MetaModifier)
76 k |= key_modifier::alt;
81 /// return the LyX mouse button state from Qt's
82 static mouse_button::state q_button_state(Qt::MouseButton button)
84 mouse_button::state b = mouse_button::none;
87 b = mouse_button::button1;
90 b = mouse_button::button2;
93 b = mouse_button::button3;
102 /// return the LyX mouse button state from Qt's
103 mouse_button::state q_motion_state(Qt::MouseButton state)
105 mouse_button::state b = mouse_button::none;
106 if (state & Qt::LeftButton)
107 b |= mouse_button::button1;
108 if (state & Qt::MidButton)
109 b |= mouse_button::button2;
110 if (state & Qt::RightButton)
111 b |= mouse_button::button3;
118 class CursorWidget : public QWidget {
120 CursorWidget(QWidget * parent)
126 void paintEvent(QPaintEvent *)
128 QColor const & col = guiApp->colorCache().get(LColor::cursor);
134 switch (cursor_shape_) {
136 // FIXME the cursor width shouldn't be hard-coded!
138 lshape_cursor_ = false;
141 cursor_w_ = cursor_h_ / 3;
142 lshape_cursor_ = true;
144 case REVERSED_L_SHAPE:
145 cursor_w_ = cursor_h_ / 3;
146 //cursor_x_ -= cursor_w_ - 1;
147 lshape_cursor_ = true;
152 // We cache two pixmaps:
153 // 1 the vertical line of the cursor.
154 // 2 the horizontal line of the L-shaped cursor (if necessary).
156 // Draw the new (vertical) cursor.
158 pain.fillRect(rect(), col);
160 // Draw the new (horizontal) cursor if necessary.
161 if (lshape_cursor_) {
162 hcursor_ = QPixmap(cursor_w_, 1);
164 show_hcursor_ = true;
175 // This is a 'heartbeat' generating synthetic mouse move events when the
176 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
177 SyntheticMouseEvent::SyntheticMouseEvent()
178 : timeout(200), restart_timeout(true),
179 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
183 GuiWorkArea::GuiWorkArea(int w, int h, int id, LyXView & lyx_view)
184 : WorkArea(id, lyx_view)
186 cursor_ = new frontend::CursorWidget(this);
189 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
190 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
191 setAcceptDrops(true);
192 setMinimumSize(100, 70);
194 //viewport()->setAutoFillBackground(false);
195 //viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
196 setFocusPolicy(Qt::WheelFocus);
198 viewport()->setCursor(Qt::IBeamCursor);
202 synthetic_mouse_event_.timeout.timeout.connect(
203 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
206 // Initialize the vertical Scroll Bar
207 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
208 this, SLOT(adjustViewWithScrollBar(int)));
210 // PageStep only depends on the viewport height.
211 verticalScrollBar()->setPageStep(viewport()->height());
213 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
214 << "\n Area width\t" << width()
215 << "\n Area height\t" << height()
216 << "\n viewport width\t" << viewport()->width()
217 << "\n viewport height\t" << viewport()->height()
220 if (USE_EVENT_PRUNING) {
221 // This is the keyboard buffering stuff...
222 // I don't see any need for this under windows. The keyboard is reactive
225 if ( !QObject::connect(&step_timer_, SIGNAL(timeout()),
226 this, SLOT(keyeventTimeout())) )
227 lyxerr[Debug::GUI] << "ERROR: keyeventTimeout cannot connect!" << endl;
229 // Start the timer, one-shot.
230 step_timer_.setSingleShot(true);
231 step_timer_.start(50);
234 // Enables input methods for asian languages.
235 // Must be set when creating custom text editing widgets.
236 setAttribute(Qt::WA_InputMethodEnabled, true);
240 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
242 verticalScrollBar()->setTracking(false);
244 // do what cursor movement does (some grey)
246 int scroll_max_ = std::max(0, h - height());
248 verticalScrollBar()->setRange(0, scroll_max_);
249 verticalScrollBar()->setSliderPosition(scroll_pos);
250 verticalScrollBar()->setSingleStep(scroll_line_step);
251 verticalScrollBar()->setValue(scroll_pos);
253 verticalScrollBar()->setTracking(true);
257 void GuiWorkArea::adjustViewWithScrollBar(int)
259 scrollBufferView(verticalScrollBar()->sliderPosition());
263 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
265 if (event->mimeData()->hasUrls())
267 /// \todo Ask lyx-devel is this is enough:
268 /// if (event->mimeData()->hasFormat("text/plain"))
269 /// event->acceptProposedAction();
273 void GuiWorkArea::dropEvent(QDropEvent* event)
275 QList<QUrl> files = event->mimeData()->urls();
279 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
280 for (int i = 0; i!=files.size(); ++i) {
281 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
283 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
288 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
290 if (dc_event_.active && dc_event_ == *e) {
291 dc_event_.active = false;
292 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
293 dc_event_.x, dc_event_.y,
294 q_button_state(dc_event_.state));
299 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
300 q_button_state(e->button()));
305 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
307 if (synthetic_mouse_event_.timeout.running())
308 synthetic_mouse_event_.timeout.stop();
310 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
311 q_button_state(e->button()));
316 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
318 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
319 q_motion_state(e->button()));
321 // If we're above or below the work area...
322 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
323 // Make sure only a synthetic event can cause a page scroll,
324 // so they come at a steady rate:
326 // _Force_ a scroll up:
329 cmd.y = viewport()->height();
330 // Store the event, to be handled when the timeout expires.
331 synthetic_mouse_event_.cmd = cmd;
333 if (synthetic_mouse_event_.timeout.running())
334 // Discard the event. Note that it _may_ be handled
335 // when the timeout expires if
336 // synthetic_mouse_event_.cmd has not been overwritten.
337 // Ie, when the timeout expires, we handle the
338 // most recent event but discard all others that
339 // occurred after the one used to start the timeout
340 // in the first place.
343 synthetic_mouse_event_.restart_timeout = true;
344 synthetic_mouse_event_.timeout.start();
345 // Fall through to handle this event...
348 } else if (synthetic_mouse_event_.timeout.running()) {
349 // Store the event, to be possibly handled when the timeout
351 // Once the timeout has expired, normal control is returned
352 // to mouseMoveEvent (restart_timeout = false).
353 // This results in a much smoother 'feel' when moving the
354 // mouse back into the work area.
355 synthetic_mouse_event_.cmd = cmd;
356 synthetic_mouse_event_.restart_timeout = false;
360 // Has anything changed on-screen since the last QMouseEvent
362 double const scrollbar_value = verticalScrollBar()->value();
363 if (e->x() != synthetic_mouse_event_.x_old ||
364 e->y() != synthetic_mouse_event_.y_old ||
365 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
366 // Yes it has. Store the params used to check this.
367 synthetic_mouse_event_.x_old = e->x();
368 synthetic_mouse_event_.y_old = e->y();
369 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
371 // ... and dispatch the event to the LyX core.
377 void GuiWorkArea::wheelEvent(QWheelEvent * e)
379 // Wheel rotation by one notch results in a delta() of 120 (see
380 // documentation of QWheelEvent)
381 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
382 verticalScrollBar()->setValue(verticalScrollBar()->value() -
383 lines * verticalScrollBar()->singleStep());
384 adjustViewWithScrollBar();
388 void GuiWorkArea::generateSyntheticMouseEvent()
390 // Set things off to generate the _next_ 'pseudo' event.
391 if (synthetic_mouse_event_.restart_timeout)
392 synthetic_mouse_event_.timeout.start();
394 // Has anything changed on-screen since the last timeout signal
396 double const scrollbar_value = verticalScrollBar()->value();
397 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
398 // Yes it has. Store the params used to check this.
399 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
401 // ... and dispatch the event to the LyX core.
402 dispatch(synthetic_mouse_event_.cmd);
407 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
409 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
410 << " count=" << e->count()
411 << " text=" << fromqstr(e->text())
412 << " isAutoRepeat=" << e->isAutoRepeat()
413 << " key=" << e->key()
416 if (USE_EVENT_PRUNING) {
417 keyeventQueue_.push(boost::shared_ptr<QKeyEvent>(new QKeyEvent(*e)));
420 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
422 processKeySym(sym, q_key_state(e->modifiers()));
427 // This is used only if USE_EVENT_PRUNING is defined...
428 void GuiWorkArea::keyeventTimeout()
430 bool handle_autos = true;
432 while (!keyeventQueue_.empty()) {
433 boost::shared_ptr<QKeyEvent> ev = keyeventQueue_.front();
435 // We never handle more than one auto repeated
436 // char in a list of queued up events.
437 if (!handle_autos && ev->isAutoRepeat()) {
438 keyeventQueue_.pop();
442 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
445 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
446 << " count=" << ev->count()
447 << " text=" << fromqstr(ev->text())
448 << " isAutoRepeat=" << ev->isAutoRepeat()
449 << " key=" << ev->key()
452 processKeySym(sym, q_key_state(ev->modifiers()));
453 keyeventQueue_.pop();
455 handle_autos = false;
458 // Restart the timer.
459 step_timer_.setSingleShot(true);
460 step_timer_.start(25);
464 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
466 dc_event_ = double_click(e);
468 if (!dc_event_.active)
471 dc_event_.active = false;
473 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
474 dc_event_.x, dc_event_.y,
475 q_button_state(dc_event_.state));
480 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
483 verticalScrollBar()->setPageStep(viewport()->height());
484 //paint_device_ = QPixmap(viewport()->width(), viewport()->height());
485 QAbstractScrollArea::resizeEvent(ev);
490 void GuiWorkArea::update(int x, int y, int w, int h)
492 viewport()->update(x, y, w, h);
496 void GuiWorkArea::doGreyOut(QLPainter & pain)
499 pain.fillRectangle(0, 0, width(), height(),
502 //if (!lyxrc.show_banner)
504 lyxerr << "show banner: " << lyxrc.show_banner << endl;
505 /// The text to be written on top of the pixmap
506 string const text = lyx_version ? lyx_version : "unknown";
507 string const file = support::libFileSearch("images", "banner", "ppm");
511 QPixmap pm(toqstr(file));
513 lyxerr << "could not load splash screen: '" << file << "'" << endl;
518 // The font used to display the version info
519 font.setStyleHint(QFont::SansSerif);
520 font.setWeight(QFont::Bold);
521 font.setPointSize(LyXFont::SIZE_NORMAL);
523 int const w = pm.width();
524 int const h = pm.height();
526 int x = (width() - w) / 2;
527 int y = (height() - h) / 2;
529 pain.drawPixmap(x, y, pm);
534 pain.setPen(QColor(255, 255, 0));
536 pain.drawText(x, y, toqstr(text));
540 void GuiWorkArea::paintEvent(QPaintEvent * ev)
543 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
544 << "\n QWidget width\t" << this->width()
545 << "\n QWidget height\t" << this->height()
546 << "\n viewport width\t" << viewport()->width()
547 << "\n viewport height\t" << viewport()->height()
548 << "\n pixmap width\t" << pixmap_->width()
549 << "\n pixmap height\t" << pixmap_->height()
550 << "\n QPaintEvent x\t" << e->rect().x()
551 << "\n QPaintEvent y\t" << e->rect().y()
552 << "\n QPaintEvent w\t" << e->rect().width()
553 << "\n QPaintEvent h\t" << e->rect().height()
557 QRect const rc = ev->rect();
558 //lyxerr << "paintEvent begin: x: " << rc.x()
559 // << " y: " << rc.y()
560 // << " w: " << rc.width()
561 // << " h: " << rc.height() << endl;
564 lyxerr << "no bufferview" << endl;
569 if (rc.width() == 3) { // FIXME HACK
570 // Assume splash screen drawing is requested when
572 lyxerr << "splash screen requested" << endl;
573 QLPainter pain(viewport());
578 if (!buffer_view_->buffer()) {
579 lyxerr << "no buffer: " << endl;
580 QLPainter pain(viewport());
586 //lyxerr << "real drawing" << endl;
587 QLPainter pain(viewport());
588 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
589 paintText(*buffer_view_, vi, pain);
594 void GuiWorkArea::expose(int x, int y, int w, int h)
600 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
602 // FIXME: the BufferView (via Cursor) should provide the correct positioning
603 // for the moment we put the cursor 2 pixels to the right
604 //cursor_->setGeometry(x, y, 2, h);
605 cursor_->setGeometry(x + 2, y, 2, h);
606 cursor_->shape_ = shape;
612 void GuiWorkArea::removeCursor()
614 if (!qApp->focusWidget())
620 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
622 QString const & text = e->commitString();
623 if (!text.isEmpty()) {
625 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
626 << " preeditString =" << fromqstr(e->preeditString())
627 << " commitString =" << fromqstr(e->commitString())
631 // needed to make math superscript work on some systems
632 // ideally, such special coding should not be necessary
634 key = Qt::Key_AsciiCircum;
635 // FIXME: Needs for investigation, this key is not really used,
636 // the ctor below just check if key is different from 0.
637 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, text);
643 } // namespace frontend
646 #include "GuiWorkArea_moc.cpp"