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"
24 #include "BufferView.h"
25 #include "rowpainter.h"
27 #include "funcrequest.h"
32 #include "support/filetools.h" // LibFileSearch
33 #include "support/os.h"
34 #include "support/convert.h"
36 #include "graphics/GraphicsImage.h"
37 #include "graphics/GraphicsLoader.h"
40 #include <QMainWindow>
43 #include <QDragEnterEvent>
48 #include <boost/bind.hpp>
49 #include <boost/current_function.hpp>
52 int const CursorWidth = 2;
54 int const CursorWidth = 1;
61 namespace os = lyx::support::os;
66 using support::FileName;
68 /// return the LyX key state from Qt's
69 static key_modifier::state q_key_state(Qt::KeyboardModifiers state)
71 key_modifier::state k = key_modifier::none;
72 if (state & Qt::ControlModifier)
73 k |= key_modifier::ctrl;
74 if (state & Qt::ShiftModifier)
75 k |= key_modifier::shift;
76 if (state & Qt::AltModifier || state & Qt::MetaModifier)
77 k |= key_modifier::alt;
82 /// return the LyX mouse button state from Qt's
83 static mouse_button::state q_button_state(Qt::MouseButton button)
85 mouse_button::state b = mouse_button::none;
88 b = mouse_button::button1;
91 b = mouse_button::button2;
94 b = mouse_button::button3;
103 /// return the LyX mouse button state from Qt's
104 mouse_button::state q_motion_state(Qt::MouseButtons state)
106 mouse_button::state b = mouse_button::none;
107 if (state & Qt::LeftButton)
108 b |= mouse_button::button1;
109 if (state & Qt::MidButton)
110 b |= mouse_button::button2;
111 if (state & Qt::RightButton)
112 b |= mouse_button::button3;
123 void draw(QPainter & painter)
125 // FIXME: do something depending on the cursor shape.
126 if (show_ && rect_.isValid())
127 painter.fillRect(rect_, color_);
130 void update(int x, int y, int h, CursorShape shape)
132 color_ = guiApp->colorCache().get(LColor::cursor);
133 rect_ = QRect(x, y, CursorWidth, h);
137 void show(bool set_show = true) { show_ = set_show; }
138 void hide() { show_ = false; }
140 QRect const & rect() { return rect_; }
154 // This is a 'heartbeat' generating synthetic mouse move events when the
155 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
156 SyntheticMouseEvent::SyntheticMouseEvent()
157 : timeout(200), restart_timeout(true),
158 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
162 GuiWorkArea::GuiWorkArea(int w, int h, int id, LyXView & lyx_view)
163 : WorkArea(id, lyx_view), need_resize_(false), schedule_redraw_(false)
165 screen_ = QPixmap(viewport()->width(), viewport()->height());
166 cursor_ = new frontend::CursorWidget();
169 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
170 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
171 setAcceptDrops(true);
172 setMouseTracking(true);
173 setMinimumSize(100, 70);
175 viewport()->setAutoFillBackground(false);
176 // We don't need double-buffering nor SystemBackground on
177 // the viewport because we have our own backing pixmap.
178 viewport()->setAttribute(Qt::WA_NoSystemBackground);
180 setFocusPolicy(Qt::WheelFocus);
182 viewport()->setCursor(Qt::IBeamCursor);
186 synthetic_mouse_event_.timeout.timeout.connect(
187 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
190 // Initialize the vertical Scroll Bar
191 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
192 this, SLOT(adjustViewWithScrollBar(int)));
194 // disable context menu for the scrollbar
195 verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);
197 // PageStep only depends on the viewport height.
198 verticalScrollBar()->setPageStep(viewport()->height());
200 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
201 << "\n Area width\t" << width()
202 << "\n Area height\t" << height()
203 << "\n viewport width\t" << viewport()->width()
204 << "\n viewport height\t" << viewport()->height()
207 // Enables input methods for asian languages.
208 // Must be set when creating custom text editing widgets.
209 setAttribute(Qt::WA_InputMethodEnabled, true);
213 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
215 if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
216 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
218 verticalScrollBar()->setTracking(false);
220 // do what cursor movement does (some grey)
222 int scroll_max_ = std::max(0, h - height());
224 verticalScrollBar()->setRange(0, scroll_max_);
225 verticalScrollBar()->setSliderPosition(scroll_pos);
226 verticalScrollBar()->setSingleStep(scroll_line_step);
227 verticalScrollBar()->setValue(scroll_pos);
229 verticalScrollBar()->setTracking(true);
233 void GuiWorkArea::adjustViewWithScrollBar(int)
235 scrollBufferView(verticalScrollBar()->sliderPosition());
239 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
241 if (event->mimeData()->hasUrls())
243 /// \todo Ask lyx-devel is this is enough:
244 /// if (event->mimeData()->hasFormat("text/plain"))
245 /// event->acceptProposedAction();
249 void GuiWorkArea::dropEvent(QDropEvent* event)
251 QList<QUrl> files = event->mimeData()->urls();
255 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
256 for (int i = 0; i!=files.size(); ++i) {
257 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
259 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
264 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
266 // No need to do anything if we didn't change views...
267 // if (theApp() == 0 || &lyx_view_ == theApp()->currentView())
270 theApp()->setCurrentView(lyx_view_);
272 // Repaint the whole screen.
273 // Note: this is different from redraw() as only the backing pixmap
274 // will be redrawn, which is cheap.
275 viewport()->repaint();
277 // FIXME: it would be better to send a signal "newBuffer()"
278 // in BufferList that could be connected to the different tabbars.
279 lyx_view_.updateTab();
281 startBlinkingCursor();
285 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
287 stopBlinkingCursor();
291 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
293 if (dc_event_.active && dc_event_ == *e) {
294 dc_event_.active = false;
295 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
297 q_button_state(e->button()));
302 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
303 q_button_state(e->button()));
304 dispatch(cmd, q_key_state(e->modifiers()));
308 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
310 if (synthetic_mouse_event_.timeout.running())
311 synthetic_mouse_event_.timeout.stop();
313 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
314 q_button_state(e->button()));
319 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
321 //we kill the triple click if we move
322 doubleClickTimeout();
323 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
324 q_motion_state(e->buttons()));
326 // If we're above or below the work area...
327 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
328 // Make sure only a synthetic event can cause a page scroll,
329 // so they come at a steady rate:
331 // _Force_ a scroll up:
334 cmd.y = viewport()->height();
335 // Store the event, to be handled when the timeout expires.
336 synthetic_mouse_event_.cmd = cmd;
338 if (synthetic_mouse_event_.timeout.running())
339 // Discard the event. Note that it _may_ be handled
340 // when the timeout expires if
341 // synthetic_mouse_event_.cmd has not been overwritten.
342 // Ie, when the timeout expires, we handle the
343 // most recent event but discard all others that
344 // occurred after the one used to start the timeout
345 // in the first place.
348 synthetic_mouse_event_.restart_timeout = true;
349 synthetic_mouse_event_.timeout.start();
350 // Fall through to handle this event...
353 } else if (synthetic_mouse_event_.timeout.running()) {
354 // Store the event, to be possibly handled when the timeout
356 // Once the timeout has expired, normal control is returned
357 // to mouseMoveEvent (restart_timeout = false).
358 // This results in a much smoother 'feel' when moving the
359 // mouse back into the work area.
360 synthetic_mouse_event_.cmd = cmd;
361 synthetic_mouse_event_.restart_timeout = false;
365 // Has anything changed on-screen since the last QMouseEvent
367 double const scrollbar_value = verticalScrollBar()->value();
368 if (e->x() != synthetic_mouse_event_.x_old ||
369 e->y() != synthetic_mouse_event_.y_old ||
370 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
371 // Yes it has. Store the params used to check this.
372 synthetic_mouse_event_.x_old = e->x();
373 synthetic_mouse_event_.y_old = e->y();
374 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
376 // ... and dispatch the event to the LyX core.
382 void GuiWorkArea::wheelEvent(QWheelEvent * e)
384 // Wheel rotation by one notch results in a delta() of 120 (see
385 // documentation of QWheelEvent)
386 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
387 verticalScrollBar()->setValue(verticalScrollBar()->value() -
388 lines * verticalScrollBar()->singleStep());
389 adjustViewWithScrollBar();
393 void GuiWorkArea::generateSyntheticMouseEvent()
395 // Set things off to generate the _next_ 'pseudo' event.
396 if (synthetic_mouse_event_.restart_timeout)
397 synthetic_mouse_event_.timeout.start();
399 // Has anything changed on-screen since the last timeout signal
401 double const scrollbar_value = verticalScrollBar()->value();
402 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
403 // Yes it has. Store the params used to check this.
404 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
406 // ... and dispatch the event to the LyX core.
407 dispatch(synthetic_mouse_event_.cmd);
412 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
414 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
415 << " count=" << e->count()
416 << " text=" << fromqstr(e->text())
417 << " isAutoRepeat=" << e->isAutoRepeat()
418 << " key=" << e->key()
421 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
423 processKeySym(sym, q_key_state(e->modifiers()));
426 void GuiWorkArea::doubleClickTimeout() {
427 dc_event_.active = false;
430 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
432 dc_event_ = double_click(e);
433 QTimer::singleShot(QApplication::doubleClickInterval(), this, SLOT(doubleClickTimeout()));
434 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
436 q_button_state(e->button()));
441 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
443 QAbstractScrollArea::resizeEvent(ev);
448 void GuiWorkArea::update(int x, int y, int w, int h)
450 viewport()->repaint(x, y, w, h);
454 void GuiWorkArea::doGreyOut(QLPainter & pain)
456 pain.fillRectangle(0, 0, width(), height(),
459 //if (!lyxrc.show_banner)
461 lyxerr[Debug::GUI] << "show banner: " << lyxrc.show_banner << endl;
462 /// The text to be written on top of the pixmap
463 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
464 FileName const file = support::libFileSearch("images", "banner", "ppm");
468 QPixmap pm(toqstr(file.absFilename()));
470 lyxerr << "could not load splash screen: '" << file << "'" << endl;
475 // The font used to display the version info
476 font.setStyleHint(QFont::SansSerif);
477 font.setWeight(QFont::Bold);
478 font.setPointSize(convert<int>(lyxrc.font_sizes[LyXFont::SIZE_LARGE]));
480 int const w = pm.width();
481 int const h = pm.height();
483 int x = (width() - w) / 2;
484 int y = (height() - h) / 2;
486 pain.drawPixmap(x, y, pm);
491 pain.setPen(QColor(255, 255, 0));
493 pain.drawText(x, y, text);
497 void GuiWorkArea::paintEvent(QPaintEvent * ev)
499 QRect const rc = ev->rect();
501 lyxerr[Debug::PAINTING] << "paintEvent begin: x: " << rc.x()
503 << " w: " << rc.width()
504 << " h: " << rc.height() << endl;
508 verticalScrollBar()->setPageStep(viewport()->height());
509 screen_ = QPixmap(viewport()->width(), viewport()->height());
512 need_resize_ = false;
515 QPainter pain(viewport());
516 pain.drawPixmap(rc, screen_, rc);
521 void GuiWorkArea::expose(int x, int y, int w, int h)
528 void GuiWorkArea::updateScreen()
530 QLPainter pain(&screen_);
533 lyxerr[Debug::GUI] << "splash screen requested" << endl;
534 verticalScrollBar()->hide();
539 verticalScrollBar()->show();
540 paintText(*buffer_view_, pain);
544 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
546 if (schedule_redraw_) {
547 if (buffer_view_ && buffer_view_->buffer()) {
548 buffer_view_->update(Update::Force);
550 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
552 schedule_redraw_ = false;
553 // Show the cursor immediately after the update.
559 cursor_->update(x, y, h, shape);
561 viewport()->update(cursor_->rect());
565 void GuiWorkArea::removeCursor()
568 //if (!qApp->focusWidget())
569 viewport()->update(cursor_->rect());
573 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
575 QString const & text = e->commitString();
576 if (!text.isEmpty()) {
578 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
579 << " preeditString =" << fromqstr(e->preeditString())
580 << " commitString =" << fromqstr(e->commitString())
585 // FIXME Abdel 10/02/07: Remove?
586 // needed to make math superscript work on some systems
587 // ideally, such special coding should not be necessary
589 key = Qt::Key_AsciiCircum;
591 // FIXME Abdel 10/02/07: Minimal support for CJK, aka systems
592 // with input methods. What should we do with e->preeditString()?
593 // Do we need an inputMethodQuery() method?
594 // FIXME 2: we should take care also of UTF16 surrogates here.
595 for (int i = 0; i < text.size(); ++i) {
596 // FIXME: Needs for investigation, this key is not really used,
597 // the ctor below just check if key is different from 0.
598 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, text[i]);
605 } // namespace frontend
608 #include "GuiWorkArea_moc.cpp"