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>
47 #include <boost/bind.hpp>
48 #include <boost/current_function.hpp>
51 int const CursorWidth = 2;
53 int const CursorWidth = 1;
60 namespace os = lyx::support::os;
65 using support::FileName;
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::MouseButtons 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;
122 void draw(QPainter & painter)
124 // FIXME: do something depending on the cursor shape.
125 if (show_ && rect_.isValid())
126 painter.fillRect(rect_, color_);
129 void update(int x, int y, int h, CursorShape shape)
131 color_ = guiApp->colorCache().get(LColor::cursor);
132 rect_ = QRect(x, y, CursorWidth, h);
136 void show(bool set_show = true) { show_ = set_show; }
137 void hide() { show_ = false; }
139 QRect const & rect() { return rect_; }
153 // This is a 'heartbeat' generating synthetic mouse move events when the
154 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
155 SyntheticMouseEvent::SyntheticMouseEvent()
156 : timeout(200), restart_timeout(true),
157 x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
161 GuiWorkArea::GuiWorkArea(int w, int h, int id, LyXView & lyx_view)
162 : WorkArea(id, lyx_view), need_resize_(false), shedule_redraw_(false)
164 cursor_ = new frontend::CursorWidget();
167 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
168 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
169 setAcceptDrops(true);
170 setMouseTracking(true);
171 setMinimumSize(100, 70);
173 viewport()->setAutoFillBackground(false);
174 // We don't need double-buffering nor SystemBackground on
175 // the viewport because we have our own backing pixmap.
176 viewport()->setAttribute(Qt::WA_NoSystemBackground);
178 setFocusPolicy(Qt::WheelFocus);
180 viewport()->setCursor(Qt::IBeamCursor);
184 synthetic_mouse_event_.timeout.timeout.connect(
185 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
188 // Initialize the vertical Scroll Bar
189 QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
190 this, SLOT(adjustViewWithScrollBar(int)));
192 // PageStep only depends on the viewport height.
193 verticalScrollBar()->setPageStep(viewport()->height());
195 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
196 << "\n Area width\t" << width()
197 << "\n Area height\t" << height()
198 << "\n viewport width\t" << viewport()->width()
199 << "\n viewport height\t" << viewport()->height()
202 // Enables input methods for asian languages.
203 // Must be set when creating custom text editing widgets.
204 setAttribute(Qt::WA_InputMethodEnabled, true);
208 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
210 if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
211 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
213 verticalScrollBar()->setTracking(false);
215 // do what cursor movement does (some grey)
217 int scroll_max_ = std::max(0, h - height());
219 verticalScrollBar()->setRange(0, scroll_max_);
220 verticalScrollBar()->setSliderPosition(scroll_pos);
221 verticalScrollBar()->setSingleStep(scroll_line_step);
222 verticalScrollBar()->setValue(scroll_pos);
224 verticalScrollBar()->setTracking(true);
228 void GuiWorkArea::adjustViewWithScrollBar(int)
230 scrollBufferView(verticalScrollBar()->sliderPosition());
234 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
236 if (event->mimeData()->hasUrls())
238 /// \todo Ask lyx-devel is this is enough:
239 /// if (event->mimeData()->hasFormat("text/plain"))
240 /// event->acceptProposedAction();
244 void GuiWorkArea::dropEvent(QDropEvent* event)
246 QList<QUrl> files = event->mimeData()->urls();
250 lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
251 for (int i = 0; i!=files.size(); ++i) {
252 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
254 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
259 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
261 // No need to do anything if we didn't change views...
262 // if (theApp() == 0 || &lyx_view_ == theApp()->currentView())
265 theApp()->setCurrentView(lyx_view_);
267 // FIXME: it would be better to send a signal "newBuffer()"
268 // in BufferList that could be connected to the different tabbars.
269 lyx_view_.updateTab();
271 startBlinkingCursor();
275 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
277 stopBlinkingCursor();
281 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
283 if (dc_event_.active && dc_event_ == *e) {
284 dc_event_.active = false;
285 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
286 dc_event_.x, dc_event_.y,
287 q_button_state(dc_event_.state));
292 FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
293 q_button_state(e->button()));
298 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
300 if (synthetic_mouse_event_.timeout.running())
301 synthetic_mouse_event_.timeout.stop();
303 FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
304 q_button_state(e->button()));
309 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
311 FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
312 q_motion_state(e->buttons()));
314 // If we're above or below the work area...
315 if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
316 // Make sure only a synthetic event can cause a page scroll,
317 // so they come at a steady rate:
319 // _Force_ a scroll up:
322 cmd.y = viewport()->height();
323 // Store the event, to be handled when the timeout expires.
324 synthetic_mouse_event_.cmd = cmd;
326 if (synthetic_mouse_event_.timeout.running())
327 // Discard the event. Note that it _may_ be handled
328 // when the timeout expires if
329 // synthetic_mouse_event_.cmd has not been overwritten.
330 // Ie, when the timeout expires, we handle the
331 // most recent event but discard all others that
332 // occurred after the one used to start the timeout
333 // in the first place.
336 synthetic_mouse_event_.restart_timeout = true;
337 synthetic_mouse_event_.timeout.start();
338 // Fall through to handle this event...
341 } else if (synthetic_mouse_event_.timeout.running()) {
342 // Store the event, to be possibly handled when the timeout
344 // Once the timeout has expired, normal control is returned
345 // to mouseMoveEvent (restart_timeout = false).
346 // This results in a much smoother 'feel' when moving the
347 // mouse back into the work area.
348 synthetic_mouse_event_.cmd = cmd;
349 synthetic_mouse_event_.restart_timeout = false;
353 // Has anything changed on-screen since the last QMouseEvent
355 double const scrollbar_value = verticalScrollBar()->value();
356 if (e->x() != synthetic_mouse_event_.x_old ||
357 e->y() != synthetic_mouse_event_.y_old ||
358 scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
359 // Yes it has. Store the params used to check this.
360 synthetic_mouse_event_.x_old = e->x();
361 synthetic_mouse_event_.y_old = e->y();
362 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
364 // ... and dispatch the event to the LyX core.
370 void GuiWorkArea::wheelEvent(QWheelEvent * e)
372 // Wheel rotation by one notch results in a delta() of 120 (see
373 // documentation of QWheelEvent)
374 int const lines = qApp->wheelScrollLines() * e->delta() / 120;
375 verticalScrollBar()->setValue(verticalScrollBar()->value() -
376 lines * verticalScrollBar()->singleStep());
377 adjustViewWithScrollBar();
381 void GuiWorkArea::generateSyntheticMouseEvent()
383 // Set things off to generate the _next_ 'pseudo' event.
384 if (synthetic_mouse_event_.restart_timeout)
385 synthetic_mouse_event_.timeout.start();
387 // Has anything changed on-screen since the last timeout signal
389 double const scrollbar_value = verticalScrollBar()->value();
390 if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
391 // Yes it has. Store the params used to check this.
392 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
394 // ... and dispatch the event to the LyX core.
395 dispatch(synthetic_mouse_event_.cmd);
400 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
402 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
403 << " count=" << e->count()
404 << " text=" << fromqstr(e->text())
405 << " isAutoRepeat=" << e->isAutoRepeat()
406 << " key=" << e->key()
409 boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
411 processKeySym(sym, q_key_state(e->modifiers()));
415 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
417 dc_event_ = double_click(e);
419 if (!dc_event_.active)
422 dc_event_.active = false;
424 FuncRequest cmd(LFUN_MOUSE_DOUBLE,
425 dc_event_.x, dc_event_.y,
426 q_button_state(dc_event_.state));
431 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
433 QAbstractScrollArea::resizeEvent(ev);
438 void GuiWorkArea::update(int x, int y, int w, int h)
440 viewport()->repaint(x, y, w, h);
444 void GuiWorkArea::doGreyOut(QLPainter & pain)
446 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
448 pain.fillRectangle(0, 0, width(), height(),
451 //if (!lyxrc.show_banner)
453 lyxerr[Debug::GUI] << "show banner: " << lyxrc.show_banner << endl;
454 /// The text to be written on top of the pixmap
455 QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
456 FileName const file = support::libFileSearch("images", "banner", "ppm");
460 QPixmap pm(toqstr(file.absFilename()));
462 lyxerr << "could not load splash screen: '" << file << "'" << endl;
467 // The font used to display the version info
468 font.setStyleHint(QFont::SansSerif);
469 font.setWeight(QFont::Bold);
470 font.setPointSize(convert<int>(lyxrc.font_sizes[LyXFont::SIZE_LARGE]));
472 int const w = pm.width();
473 int const h = pm.height();
475 int x = (width() - w) / 2;
476 int y = (height() - h) / 2;
478 pain.drawPixmap(x, y, pm);
483 pain.setPen(QColor(255, 255, 0));
485 pain.drawText(x, y, text);
489 void GuiWorkArea::paintEvent(QPaintEvent * ev)
491 QRect const rc = ev->rect();
493 lyxerr[Debug::PAINTING] << "paintEvent begin: x: " << rc.x()
495 << " w: " << rc.width()
496 << " h: " << rc.height() << endl;
500 verticalScrollBar()->setPageStep(viewport()->height());
501 screen_ = QPixmap(viewport()->width(), viewport()->height());
504 need_resize_ = false;
507 QPainter pain(viewport());
508 pain.drawPixmap(rc, screen_, rc);
513 void GuiWorkArea::expose(int x, int y, int w, int h)
520 void GuiWorkArea::updateScreen()
522 QLPainter pain(&screen_);
525 lyxerr[Debug::GUI] << "splash screen requested" << endl;
526 verticalScrollBar()->hide();
531 verticalScrollBar()->show();
532 paintText(*buffer_view_, pain);
536 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
538 if (shedule_redraw_) {
539 if (buffer_view_ && buffer_view_->buffer()) {
540 buffer_view_->update(Update::Force);
542 viewport()->update(QRect(0, 0, viewport()->width(), viewport()->height()));
544 shedule_redraw_ = false;
545 // Show the cursor immediately after the update.
551 cursor_->update(x, y, h, shape);
553 viewport()->update(cursor_->rect());
557 void GuiWorkArea::removeCursor()
560 //if (!qApp->focusWidget())
561 viewport()->update(cursor_->rect());
565 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
567 QString const & text = e->commitString();
568 if (!text.isEmpty()) {
570 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
571 << " preeditString =" << fromqstr(e->preeditString())
572 << " commitString =" << fromqstr(e->commitString())
576 // needed to make math superscript work on some systems
577 // ideally, such special coding should not be necessary
579 key = Qt::Key_AsciiCircum;
580 // FIXME: Needs for investigation, this key is not really used,
581 // the ctor below just check if key is different from 0.
582 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, text);
588 } // namespace frontend
591 #include "GuiWorkArea_moc.cpp"