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 /// return the LyX key state from Qt's
65 static key_modifier::state q_key_state(Qt::KeyboardModifiers state)
67 key_modifier::state k = key_modifier::none;
68 if (state & Qt::ControlModifier)
69 k |= key_modifier::ctrl;
70 if (state & Qt::ShiftModifier)
71 k |= key_modifier::shift;
72 if (state & Qt::AltModifier || state & Qt::MetaModifier)
73 k |= key_modifier::alt;
78 /// return the LyX mouse button state from Qt's
79 static mouse_button::state q_button_state(Qt::MouseButton button)
81 mouse_button::state b = mouse_button::none;
84 b = mouse_button::button1;
87 b = mouse_button::button2;
90 b = mouse_button::button3;
99 /// return the LyX mouse button state from Qt's
100 mouse_button::state q_motion_state(Qt::MouseButtons state)
102 mouse_button::state b = mouse_button::none;
103 if (state & Qt::LeftButton)
104 b |= mouse_button::button1;
105 if (state & Qt::MidButton)
106 b |= mouse_button::button2;
107 if (state & Qt::RightButton)
108 b |= mouse_button::button3;
119 void draw(QPainter & painter)
121 // FIXME: do something depending on the cursor shape.
122 if (show_ && rect_.isValid())
123 painter.fillRect(rect_, color_);
126 void update(int x, int y, int h, CursorShape shape)
128 color_ = guiApp->colorCache().get(LColor::cursor);
129 rect_ = QRect(x, y, CursorWidth, h);
133 void show(bool set_show = true) { show_ = set_show; }
134 void hide() { show_ = false; }
136 QRect const & rect() { return rect_; }
150 // This is a 'heartbeat' generating synthetic mouse move events when the
151 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
152 SyntheticMouseEvent::SyntheticMouseEvent()
153 : timeout(200), restart_timeout(true),
154 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
158 GuiWorkArea::GuiWorkArea(int w, int h, int id, LyXView & lyx_view)
159 : WorkArea(id, lyx_view)
161 cursor_ = new frontend::CursorWidget();
164 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
165 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
166 setAcceptDrops(true);
167 setMinimumSize(100, 70);
169 viewport()->setAutoFillBackground(false);
170 viewport()->setAttribute(Qt::WA_OpaquePaintEvent);
171 setFocusPolicy(Qt::WheelFocus);
173 viewport()->setCursor(Qt::IBeamCursor);
177 synthetic_mouse_event_.timeout.timeout.connect(
178 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
181 // Initialize the vertical Scroll Bar
182 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
183 this, SLOT(adjustViewWithScrollBar(int)));
185 // PageStep only depends on the viewport height.
186 verticalScrollBar()->setPageStep(viewport()->height());
188 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
189 << "\n Area width\t" << width()
190 << "\n Area height\t" << height()
191 << "\n viewport width\t" << viewport()->width()
192 << "\n viewport height\t" << viewport()->height()
195 // Enables input methods for asian languages.
196 // Must be set when creating custom text editing widgets.
197 setAttribute(Qt::WA_InputMethodEnabled, true);
201 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
203 if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
204 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
206 verticalScrollBar()->setTracking(false);
208 // do what cursor movement does (some grey)
210 int scroll_max_ = std::max(0, h - height());
212 verticalScrollBar()->setRange(0, scroll_max_);
213 verticalScrollBar()->setSliderPosition(scroll_pos);
214 verticalScrollBar()->setSingleStep(scroll_line_step);
215 verticalScrollBar()->setValue(scroll_pos);
217 verticalScrollBar()->setTracking(true);
221 void GuiWorkArea::adjustViewWithScrollBar(int)
223 scrollBufferView(verticalScrollBar()->sliderPosition());
227 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
229 if (event->mimeData()->hasUrls())
231 /// \todo Ask lyx-devel is this is enough:
232 /// if (event->mimeData()->hasFormat("text/plain"))
233 /// event->acceptProposedAction();
237 void GuiWorkArea::dropEvent(QDropEvent* event)
239 QList<QUrl> files = event->mimeData()->urls();
243 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
244 for (int i = 0; i!=files.size(); ++i) {
245 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
247 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
252 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
254 // No need to do anything if we didn't change views...
255 if (theApp == 0 || &lyx_view_ == &theApp->currentView())
258 theApp->setCurrentView(lyx_view_);
260 // FIXME: it would be better to send a signal "newBuffer()"
261 // in BufferList that could be connected to the different tabbars.
262 lyx_view_.updateTab();
264 startBlinkingCursor();
266 //FIXME: Use case: Two windows share the same buffer.
267 // The first window is resize. This modify the inner Buffer
268 // structure because Paragraph has a notion of line break and
269 // thus line width (this is very bad!).
270 // When switching to the other window which does not have the
271 // same size, LyX crashes because the line break is not adapted
272 // the this BufferView width.
273 // The following line fix the crash by resizing the BufferView
274 // on a focusInEvent(). That is not a good fix but it is a fix
275 // nevertheless. The bad side effect is that when the two
276 // BufferViews show the same portion of the Buffer, the second
277 // BufferView will show the same line breaks as the first one;
278 // even though those line breaks are not adapted to the second
279 // BufferView width... such is life!
284 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
286 // No need to do anything if we didn't change views...
287 if (&lyx_view_ == &theApp->currentView())
290 stopBlinkingCursor();
294 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
296 if (dc_event_.active && dc_event_ == *e) {
297 dc_event_.active = false;
298 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
299 dc_event_.x, dc_event_.y,
300 q_button_state(dc_event_.state));
305 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
306 q_button_state(e->button()));
311 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
313 if (synthetic_mouse_event_.timeout.running())
314 synthetic_mouse_event_.timeout.stop();
316 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
317 q_button_state(e->button()));
322 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
324 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
325 q_motion_state(e->buttons()));
327 // If we're above or below the work area...
328 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
329 // Make sure only a synthetic event can cause a page scroll,
330 // so they come at a steady rate:
332 // _Force_ a scroll up:
335 cmd.y = viewport()->height();
336 // Store the event, to be handled when the timeout expires.
337 synthetic_mouse_event_.cmd = cmd;
339 if (synthetic_mouse_event_.timeout.running())
340 // Discard the event. Note that it _may_ be handled
341 // when the timeout expires if
342 // synthetic_mouse_event_.cmd has not been overwritten.
343 // Ie, when the timeout expires, we handle the
344 // most recent event but discard all others that
345 // occurred after the one used to start the timeout
346 // in the first place.
349 synthetic_mouse_event_.restart_timeout = true;
350 synthetic_mouse_event_.timeout.start();
351 // Fall through to handle this event...
354 } else if (synthetic_mouse_event_.timeout.running()) {
355 // Store the event, to be possibly handled when the timeout
357 // Once the timeout has expired, normal control is returned
358 // to mouseMoveEvent (restart_timeout = false).
359 // This results in a much smoother 'feel' when moving the
360 // mouse back into the work area.
361 synthetic_mouse_event_.cmd = cmd;
362 synthetic_mouse_event_.restart_timeout = false;
366 // Has anything changed on-screen since the last QMouseEvent
368 double const scrollbar_value = verticalScrollBar()->value();
369 if (e->x() != synthetic_mouse_event_.x_old ||
370 e->y() != synthetic_mouse_event_.y_old ||
371 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
372 // Yes it has. Store the params used to check this.
373 synthetic_mouse_event_.x_old = e->x();
374 synthetic_mouse_event_.y_old = e->y();
375 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
377 // ... and dispatch the event to the LyX core.
383 void GuiWorkArea::wheelEvent(QWheelEvent * e)
385 // Wheel rotation by one notch results in a delta() of 120 (see
386 // documentation of QWheelEvent)
387 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
388 verticalScrollBar()->setValue(verticalScrollBar()->value() -
389 lines * verticalScrollBar()->singleStep());
390 adjustViewWithScrollBar();
394 void GuiWorkArea::generateSyntheticMouseEvent()
396 // Set things off to generate the _next_ 'pseudo' event.
397 if (synthetic_mouse_event_.restart_timeout)
398 synthetic_mouse_event_.timeout.start();
400 // Has anything changed on-screen since the last timeout signal
402 double const scrollbar_value = verticalScrollBar()->value();
403 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
404 // Yes it has. Store the params used to check this.
405 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
407 // ... and dispatch the event to the LyX core.
408 dispatch(synthetic_mouse_event_.cmd);
413 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
415 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
416 << " count=" << e->count()
417 << " text=" << fromqstr(e->text())
418 << " isAutoRepeat=" << e->isAutoRepeat()
419 << " key=" << e->key()
422 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
424 processKeySym(sym, q_key_state(e->modifiers()));
428 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
430 dc_event_ = double_click(e);
432 if (!dc_event_.active)
435 dc_event_.active = false;
437 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
438 dc_event_.x, dc_event_.y,
439 q_button_state(dc_event_.state));
444 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
446 stopBlinkingCursor();
447 screen_ = QPixmap(ev->size().width(), ev->size().height());
448 verticalScrollBar()->setPageStep(viewport()->height());
449 QAbstractScrollArea::resizeEvent(ev);
451 startBlinkingCursor();
455 void GuiWorkArea::update(int x, int y, int w, int h)
457 viewport()->update(x, y, w, h);
461 void GuiWorkArea::doGreyOut(QLPainter & pain)
463 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
465 pain.fillRectangle(0, 0, width(), height(),
468 //if (!lyxrc.show_banner)
470 lyxerr[Debug::GUI] << "show banner: " << lyxrc.show_banner << endl;
471 /// The text to be written on top of the pixmap
472 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
473 string const file = support::libFileSearch("images", "banner", "ppm");
477 QPixmap pm(toqstr(file));
479 lyxerr << "could not load splash screen: '" << file << "'" << endl;
484 // The font used to display the version info
485 font.setStyleHint(QFont::SansSerif);
486 font.setWeight(QFont::Bold);
487 font.setPointSize(LyXFont::SIZE_NORMAL);
489 int const w = pm.width();
490 int const h = pm.height();
492 int x = (width() - w) / 2;
493 int y = (height() - h) / 2;
495 pain.drawPixmap(x, y, pm);
500 pain.setPen(QColor(255, 255, 0));
502 pain.drawText(x, y, text);
506 void GuiWorkArea::paintEvent(QPaintEvent * ev)
508 QRect const rc = ev->rect();
509 lyxerr[Debug::PAINTING] << "paintEvent begin: x: " << rc.x()
511 << " w: " << rc.width()
512 << " h: " << rc.height() << endl;
514 QPainter pain(viewport());
515 pain.drawPixmap(rc, screen_, rc);
520 void GuiWorkArea::expose(int x, int y, int w, int h)
522 QLPainter pain(&screen_);
525 lyxerr[Debug::GUI] << "splash screen requested" << endl;
527 verticalScrollBar()->hide();
528 update(0, 0, width(), height());
532 verticalScrollBar()->show();
533 paintText(*buffer_view_, pain);
538 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
540 cursor_->update(x, y, h, shape);
542 viewport()->update(cursor_->rect());
546 void GuiWorkArea::removeCursor()
549 //if (!qApp->focusWidget())
550 viewport()->update(cursor_->rect());
554 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
556 QString const & text = e->commitString();
557 if (!text.isEmpty()) {
559 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
560 << " preeditString =" << fromqstr(e->preeditString())
561 << " commitString =" << fromqstr(e->commitString())
565 // needed to make math superscript work on some systems
566 // ideally, such special coding should not be necessary
568 key = Qt::Key_AsciiCircum;
569 // FIXME: Needs for investigation, this key is not really used,
570 // the ctor below just check if key is different from 0.
571 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, text);
577 } // namespace frontend
580 #include "GuiWorkArea_moc.cpp"