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"
35 #include "graphics/GraphicsImage.h"
36 #include "graphics/GraphicsLoader.h"
39 #include <QMainWindow>
42 #include <QDragEnterEvent>
46 #include <boost/bind.hpp>
47 #include <boost/current_function.hpp>
50 int const CursorWidth = 2;
52 int const CursorWidth = 1;
59 namespace os = lyx::support::os;
64 using support::FileName;
66 /// return the LyX key state from Qt's
67 static key_modifier::state q_key_state(Qt::KeyboardModifiers state)
69 key_modifier::state k = key_modifier::none;
70 if (state & Qt::ControlModifier)
71 k |= key_modifier::ctrl;
72 if (state & Qt::ShiftModifier)
73 k |= key_modifier::shift;
74 if (state & Qt::AltModifier || state & Qt::MetaModifier)
75 k |= key_modifier::alt;
80 /// return the LyX mouse button state from Qt's
81 static mouse_button::state q_button_state(Qt::MouseButton button)
83 mouse_button::state b = mouse_button::none;
86 b = mouse_button::button1;
89 b = mouse_button::button2;
92 b = mouse_button::button3;
101 /// return the LyX mouse button state from Qt's
102 mouse_button::state q_motion_state(Qt::MouseButtons state)
104 mouse_button::state b = mouse_button::none;
105 if (state & Qt::LeftButton)
106 b |= mouse_button::button1;
107 if (state & Qt::MidButton)
108 b |= mouse_button::button2;
109 if (state & Qt::RightButton)
110 b |= mouse_button::button3;
121 void draw(QPainter & painter)
123 // FIXME: do something depending on the cursor shape.
124 if (show_ && rect_.isValid())
125 painter.fillRect(rect_, color_);
128 void update(int x, int y, int h, CursorShape shape)
130 color_ = guiApp->colorCache().get(LColor::cursor);
131 rect_ = QRect(x, y, CursorWidth, h);
135 void show(bool set_show = true) { show_ = set_show; }
136 void hide() { show_ = false; }
138 QRect const & rect() { return rect_; }
152 // This is a 'heartbeat' generating synthetic mouse move events when the
153 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
154 SyntheticMouseEvent::SyntheticMouseEvent()
155 : timeout(200), restart_timeout(true),
156 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
160 GuiWorkArea::GuiWorkArea(int w, int h, int id, LyXView & lyx_view)
161 : WorkArea(id, lyx_view)
163 cursor_ = new frontend::CursorWidget();
166 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
167 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
168 setAcceptDrops(true);
169 setMouseTracking(true);
170 setMinimumSize(100, 70);
172 viewport()->setAutoFillBackground(false);
173 viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
174 setFocusPolicy(Qt::WheelFocus);
176 viewport()->setCursor(Qt::IBeamCursor);
180 synthetic_mouse_event_.timeout.timeout.connect(
181 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
184 // Initialize the vertical Scroll Bar
185 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
186 this, SLOT(adjustViewWithScrollBar(int)));
188 // PageStep only depends on the viewport height.
189 verticalScrollBar()->setPageStep(viewport()->height());
191 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
192 << "\n Area width\t" << width()
193 << "\n Area height\t" << height()
194 << "\n viewport width\t" << viewport()->width()
195 << "\n viewport height\t" << viewport()->height()
198 // Enables input methods for asian languages.
199 // Must be set when creating custom text editing widgets.
200 setAttribute(Qt::WA_InputMethodEnabled, true);
204 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
206 if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
207 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
209 verticalScrollBar()->setTracking(false);
211 // do what cursor movement does (some grey)
213 int scroll_max_ = std::max(0, h - height());
215 verticalScrollBar()->setRange(0, scroll_max_);
216 verticalScrollBar()->setSliderPosition(scroll_pos);
217 verticalScrollBar()->setSingleStep(scroll_line_step);
218 verticalScrollBar()->setValue(scroll_pos);
220 verticalScrollBar()->setTracking(true);
224 void GuiWorkArea::adjustViewWithScrollBar(int)
226 scrollBufferView(verticalScrollBar()->sliderPosition());
230 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
232 if (event->mimeData()->hasUrls())
234 /// \todo Ask lyx-devel is this is enough:
235 /// if (event->mimeData()->hasFormat("text/plain"))
236 /// event->acceptProposedAction();
240 void GuiWorkArea::dropEvent(QDropEvent* event)
242 QList<QUrl> files = event->mimeData()->urls();
246 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
247 for (int i = 0; i!=files.size(); ++i) {
248 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
250 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
255 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
257 // No need to do anything if we didn't change views...
258 if (theApp() == 0 || &lyx_view_ == theApp()->currentView())
261 theApp()->setCurrentView(lyx_view_);
263 // FIXME: it would be better to send a signal "newBuffer()"
264 // in BufferList that could be connected to the different tabbars.
265 lyx_view_.updateTab();
267 startBlinkingCursor();
269 //FIXME: Use case: Two windows share the same buffer.
270 // The first window is resize. This modify the inner Buffer
271 // structure because Paragraph has a notion of line break and
272 // thus line width (this is very bad!).
273 // When switching to the other window which does not have the
274 // same size, LyX crashes because the line break is not adapted
275 // the this BufferView width.
276 // The following line fix the crash by resizing the BufferView
277 // on a focusInEvent(). That is not a good fix but it is a fix
278 // nevertheless. The bad side effect is that when the two
279 // BufferViews show the same portion of the Buffer, the second
280 // BufferView will show the same line breaks as the first one;
281 // even though those line breaks are not adapted to the second
282 // BufferView width... such is life!
287 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
289 // No need to do anything if we didn't change views...
290 if (&lyx_view_ == theApp()->currentView())
293 stopBlinkingCursor();
297 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
299 if (dc_event_.active && dc_event_ == *e) {
300 dc_event_.active = false;
301 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
302 dc_event_.x, dc_event_.y,
303 q_button_state(dc_event_.state));
308 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
309 q_button_state(e->button()));
314 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
316 if (synthetic_mouse_event_.timeout.running())
317 synthetic_mouse_event_.timeout.stop();
319 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
320 q_button_state(e->button()));
325 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
327 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
328 q_motion_state(e->buttons()));
330 // If we're above or below the work area...
331 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
332 // Make sure only a synthetic event can cause a page scroll,
333 // so they come at a steady rate:
335 // _Force_ a scroll up:
338 cmd.y = viewport()->height();
339 // Store the event, to be handled when the timeout expires.
340 synthetic_mouse_event_.cmd = cmd;
342 if (synthetic_mouse_event_.timeout.running())
343 // Discard the event. Note that it _may_ be handled
344 // when the timeout expires if
345 // synthetic_mouse_event_.cmd has not been overwritten.
346 // Ie, when the timeout expires, we handle the
347 // most recent event but discard all others that
348 // occurred after the one used to start the timeout
349 // in the first place.
352 synthetic_mouse_event_.restart_timeout = true;
353 synthetic_mouse_event_.timeout.start();
354 // Fall through to handle this event...
357 } else if (synthetic_mouse_event_.timeout.running()) {
358 // Store the event, to be possibly handled when the timeout
360 // Once the timeout has expired, normal control is returned
361 // to mouseMoveEvent (restart_timeout = false).
362 // This results in a much smoother 'feel' when moving the
363 // mouse back into the work area.
364 synthetic_mouse_event_.cmd = cmd;
365 synthetic_mouse_event_.restart_timeout = false;
369 // Has anything changed on-screen since the last QMouseEvent
371 double const scrollbar_value = verticalScrollBar()->value();
372 if (e->x() != synthetic_mouse_event_.x_old ||
373 e->y() != synthetic_mouse_event_.y_old ||
374 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
375 // Yes it has. Store the params used to check this.
376 synthetic_mouse_event_.x_old = e->x();
377 synthetic_mouse_event_.y_old = e->y();
378 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
380 // ... and dispatch the event to the LyX core.
386 void GuiWorkArea::wheelEvent(QWheelEvent * e)
388 // Wheel rotation by one notch results in a delta() of 120 (see
389 // documentation of QWheelEvent)
390 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
391 verticalScrollBar()->setValue(verticalScrollBar()->value() -
392 lines * verticalScrollBar()->singleStep());
393 adjustViewWithScrollBar();
397 void GuiWorkArea::generateSyntheticMouseEvent()
399 // Set things off to generate the _next_ 'pseudo' event.
400 if (synthetic_mouse_event_.restart_timeout)
401 synthetic_mouse_event_.timeout.start();
403 // Has anything changed on-screen since the last timeout signal
405 double const scrollbar_value = verticalScrollBar()->value();
406 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
407 // Yes it has. Store the params used to check this.
408 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
410 // ... and dispatch the event to the LyX core.
411 dispatch(synthetic_mouse_event_.cmd);
416 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
418 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
419 << " count=" << e->count()
420 << " text=" << fromqstr(e->text())
421 << " isAutoRepeat=" << e->isAutoRepeat()
422 << " key=" << e->key()
425 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
427 processKeySym(sym, q_key_state(e->modifiers()));
431 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
433 dc_event_ = double_click(e);
435 if (!dc_event_.active)
438 dc_event_.active = false;
440 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
441 dc_event_.x, dc_event_.y,
442 q_button_state(dc_event_.state));
447 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
449 stopBlinkingCursor();
450 screen_ = QPixmap(ev->size().width(), ev->size().height());
451 verticalScrollBar()->setPageStep(viewport()->height());
452 QAbstractScrollArea::resizeEvent(ev);
454 startBlinkingCursor();
458 void GuiWorkArea::update(int x, int y, int w, int h)
460 viewport()->repaint(x, y, w, h);
464 void GuiWorkArea::doGreyOut(QLPainter & pain)
466 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
468 pain.fillRectangle(0, 0, width(), height(),
471 //if (!lyxrc.show_banner)
473 lyxerr[Debug::GUI] << "show banner: " << lyxrc.show_banner << endl;
474 /// The text to be written on top of the pixmap
475 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
476 FileName const file = support::libFileSearch("images", "banner", "ppm");
480 QPixmap pm(toqstr(file.absFilename()));
482 lyxerr << "could not load splash screen: '" << file << "'" << endl;
487 // The font used to display the version info
488 font.setStyleHint(QFont::SansSerif);
489 font.setWeight(QFont::Bold);
490 font.setPointSize(LyXFont::SIZE_NORMAL);
492 int const w = pm.width();
493 int const h = pm.height();
495 int x = (width() - w) / 2;
496 int y = (height() - h) / 2;
498 pain.drawPixmap(x, y, pm);
503 pain.setPen(QColor(255, 255, 0));
505 pain.drawText(x, y, text);
509 void GuiWorkArea::paintEvent(QPaintEvent * ev)
511 QRect const rc = ev->rect();
512 lyxerr[Debug::PAINTING] << "paintEvent begin: x: " << rc.x()
514 << " w: " << rc.width()
515 << " h: " << rc.height() << endl;
517 QPainter pain(viewport());
518 pain.drawPixmap(rc, screen_, rc);
523 void GuiWorkArea::expose(int x, int y, int w, int h)
525 QLPainter pain(&screen_);
528 lyxerr[Debug::GUI] << "splash screen requested" << endl;
530 verticalScrollBar()->hide();
531 update(0, 0, width(), height());
535 verticalScrollBar()->show();
536 paintText(*buffer_view_, pain);
541 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
543 cursor_->update(x, y, h, shape);
545 viewport()->update(cursor_->rect());
549 void GuiWorkArea::removeCursor()
552 //if (!qApp->focusWidget())
553 viewport()->update(cursor_->rect());
557 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
559 QString const & text = e->commitString();
560 if (!text.isEmpty()) {
562 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
563 << " preeditString =" << fromqstr(e->preeditString())
564 << " commitString =" << fromqstr(e->commitString())
568 // needed to make math superscript work on some systems
569 // ideally, such special coding should not be necessary
571 key = Qt::Key_AsciiCircum;
572 // FIXME: Needs for investigation, this key is not really used,
573 // the ctor below just check if key is different from 0.
574 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, text);
580 } // namespace frontend
583 #include "GuiWorkArea_moc.cpp"