X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiWorkArea.cpp;h=ef79b02fe3e1c7b0f76ef3e8b53cbf8866bbe2af;hb=8b7584846c4e1a9c87a004fab479722fee7e3013;hp=368fa774b115b98129a594c6e20e743a7b9aa22d;hpb=8bbd8bc44c2f9c290c09cea3f08371d416312dcd;p=lyx.git diff --git a/src/frontends/qt4/GuiWorkArea.cpp b/src/frontends/qt4/GuiWorkArea.cpp index 368fa774b1..ef79b02fe3 100644 --- a/src/frontends/qt4/GuiWorkArea.cpp +++ b/src/frontends/qt4/GuiWorkArea.cpp @@ -37,6 +37,7 @@ #include "LyXVC.h" #include "qt_helpers.h" #include "Text.h" +#include "TextMetrics.h" #include "version.h" #include "graphics/GraphicsImage.h" @@ -224,8 +225,7 @@ private: // This is a 'heartbeat' generating synthetic mouse move events when the // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s SyntheticMouseEvent::SyntheticMouseEvent() - : timeout(200), restart_timeout(true), - x_old(-1), y_old(-1), min_scrollbar_old(-1), max_scrollbar_old(-1) + : timeout(200), restart_timeout(true) {} @@ -288,7 +288,7 @@ void GuiWorkArea::init() setFocusPolicy(Qt::StrongFocus); - viewport()->setCursor(Qt::IBeamCursor); + setCursorShape(Qt::IBeamCursor); synthetic_mouse_event_.timeout.timeout.connect( bind(&GuiWorkArea::generateSyntheticMouseEvent, @@ -320,6 +320,25 @@ GuiWorkArea::~GuiWorkArea() } +Qt::CursorShape GuiWorkArea::cursorShape() const +{ + return viewport()->cursor().shape(); +} + + +void GuiWorkArea::setCursorShape(Qt::CursorShape shape) +{ + viewport()->setCursor(shape); +} + + +void GuiWorkArea::updateCursorShape() +{ + setCursorShape(buffer_view_->clickableInset() + ? Qt::PointingHandCursor : Qt::IBeamCursor); +} + + void GuiWorkArea::setGuiView(GuiView & gv) { lyx_view_ = &gv; @@ -424,6 +443,7 @@ void GuiWorkArea::redraw(bool update_metrics) if (update_metrics || lyx_view_ != guiApp->currentView() || lyx_view_->currentWorkArea() != this) { // FIXME: it would be nice to optimize for the off-screen case. + buffer_view_->cursor().fixIfBroken(); buffer_view_->updateMetrics(); buffer_view_->cursor().fixIfBroken(); } @@ -448,6 +468,8 @@ void GuiWorkArea::redraw(bool update_metrics) buffer_view_->coordCache().dump(); setReadOnly(buffer_view_->buffer().isReadonly()); + + updateCursorShape(); } @@ -517,6 +539,8 @@ void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod) // Show the cursor immediately after any operation startBlinkingCursor(); } + + updateCursorShape(); } @@ -618,7 +642,7 @@ void GuiWorkArea::updateScrollbar() void GuiWorkArea::scrollTo(int value) { stopBlinkingCursor(); - buffer_view_->scrollDocView(value); + buffer_view_->scrollDocView(value, true); if (lyxrc.cursor_follows_scrollbar) { buffer_view_->setCursorFromScrollbar(); @@ -654,11 +678,14 @@ bool GuiWorkArea::event(QEvent * e) // which are otherwise reserved to focus switching between controls // within a dialog. QKeyEvent * ke = static_cast(e); - if ((ke->key() != Qt::Key_Tab && ke->key() != Qt::Key_Backtab) - || ke->modifiers() & Qt::ControlModifier) - return QAbstractScrollArea::event(e); - keyPressEvent(ke); - return true; + if ((ke->key() == Qt::Key_Tab && ke->modifiers() == Qt::NoModifier) + || (ke->key() == Qt::Key_Backtab && ( + ke->modifiers() == Qt::ShiftModifier + || ke->modifiers() == Qt::NoModifier))) { + keyPressEvent(ke); + return true; + } + return QAbstractScrollArea::event(e); } default: @@ -770,18 +797,19 @@ void GuiWorkArea::mouseMoveEvent(QMouseEvent * e) e->accept(); // If we're above or below the work area... - if (e->y() <= 20 || e->y() >= viewport()->height() - 20) { + if ((e->y() <= 20 || e->y() >= viewport()->height() - 20) + && e->buttons() == mouse_button::button1) { // Make sure only a synthetic event can cause a page scroll, // so they come at a steady rate: if (e->y() <= 20) // _Force_ a scroll up: - cmd.set_y(-40); + cmd.set_y(e->y() - 21); else - cmd.set_y(viewport()->height()); + cmd.set_y(e->y() + 21); // Store the event, to be handled when the timeout expires. synthetic_mouse_event_.cmd = cmd; - if (synthetic_mouse_event_.timeout.running()) + if (synthetic_mouse_event_.timeout.running()) { // Discard the event. Note that it _may_ be handled // when the timeout expires if // synthetic_mouse_event_.cmd has not been overwritten. @@ -790,7 +818,8 @@ void GuiWorkArea::mouseMoveEvent(QMouseEvent * e) // occurred after the one used to start the timeout // in the first place. return; - + } + synthetic_mouse_event_.restart_timeout = true; synthetic_mouse_event_.timeout.start(); // Fall through to handle this event... @@ -806,24 +835,6 @@ void GuiWorkArea::mouseMoveEvent(QMouseEvent * e) synthetic_mouse_event_.restart_timeout = false; return; } - - // Has anything changed on-screen since the last QMouseEvent - // was received? - if (e->x() == synthetic_mouse_event_.x_old - && e->y() == synthetic_mouse_event_.y_old - && synthetic_mouse_event_.min_scrollbar_old == verticalScrollBar()->minimum() - && synthetic_mouse_event_.max_scrollbar_old == verticalScrollBar()->maximum()) { - // Nothing changed on-screen since the last QMouseEvent. - return; - } - - // Yes something has changed. Store the params used to check this. - synthetic_mouse_event_.x_old = e->x(); - synthetic_mouse_event_.y_old = e->y(); - synthetic_mouse_event_.min_scrollbar_old = verticalScrollBar()->minimum(); - synthetic_mouse_event_.max_scrollbar_old = verticalScrollBar()->maximum(); - - // ... and dispatch the event to the LyX core. dispatch(cmd); } @@ -832,9 +843,26 @@ void GuiWorkArea::wheelEvent(QWheelEvent * ev) { // Wheel rotation by one notch results in a delta() of 120 (see // documentation of QWheelEvent) - int const delta = ev->delta() / 120; - if (ev->modifiers() & Qt::ControlModifier) { - docstring arg = convert(5 * delta); + double const delta = ev->delta() / 120.0; + bool zoom = false; + switch (lyxrc.scroll_wheel_zoom) { + case LyXRC::SCROLL_WHEEL_ZOOM_CTRL: + zoom = ev->modifiers() & Qt::ControlModifier; + zoom &= !(ev->modifiers() & (Qt::ShiftModifier | Qt::AltModifier)); + break; + case LyXRC::SCROLL_WHEEL_ZOOM_SHIFT: + zoom = ev->modifiers() & Qt::ShiftModifier; + zoom &= !(ev->modifiers() & (Qt::ControlModifier | Qt::AltModifier)); + break; + case LyXRC::SCROLL_WHEEL_ZOOM_ALT: + zoom = ev->modifiers() & Qt::AltModifier; + zoom &= !(ev->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier)); + break; + case LyXRC::SCROLL_WHEEL_ZOOM_OFF: + break; + } + if (zoom) { + docstring arg = convert(int(5 * delta)); lyx::dispatch(FuncRequest(LFUN_BUFFER_ZOOM_IN, arg)); return; } @@ -846,11 +874,8 @@ void GuiWorkArea::wheelEvent(QWheelEvent * ev) int scroll_value = lines > page_step ? page_step : lines * verticalScrollBar()->singleStep(); - // Take into account the rotation. - scroll_value *= delta; - - // Take into account user preference. - scroll_value = int(scroll_value * lyxrc.mouse_wheel_speed); + // Take into account the rotation and the user preferences. + scroll_value = int(scroll_value * delta * lyxrc.mouse_wheel_speed); LYXERR(Debug::SCROLLING, "wheelScrollLines = " << lines << " delta = " << delta << " scroll_value = " << scroll_value << " page_step = " << page_step); @@ -863,23 +888,81 @@ void GuiWorkArea::wheelEvent(QWheelEvent * ev) void GuiWorkArea::generateSyntheticMouseEvent() { + int const e_y = synthetic_mouse_event_.cmd.y(); + int const wh = buffer_view_->workHeight(); + bool const up = e_y < 0; + bool const down = e_y > wh; + // Set things off to generate the _next_ 'pseudo' event. - if (synthetic_mouse_event_.restart_timeout) + int step = 50; + if (synthetic_mouse_event_.restart_timeout) { + // This is some magic formulae to determine the speed + // of scrolling related to the position of the mouse. + int time = 200; + if (up || down) { + int dist = up ? -e_y : e_y - wh; + time = max(min(200, 250000 / (dist * dist)), 1) ; + + if (time < 40) { + step = 80000 / (time * time); + time = 40; + } + } + synthetic_mouse_event_.timeout.setTimeout(time); synthetic_mouse_event_.timeout.start(); + } - // Has anything changed on-screen since the last timeout signal - // was received? - int const min_scrollbar = verticalScrollBar()->minimum(); - int const max_scrollbar = verticalScrollBar()->maximum(); - if (min_scrollbar == synthetic_mouse_event_.min_scrollbar_old - && max_scrollbar == synthetic_mouse_event_.max_scrollbar_old) { + // Can we scroll further ? + int const value = verticalScrollBar()->value(); + if (value == verticalScrollBar()->maximum() + || value == verticalScrollBar()->minimum()) { + synthetic_mouse_event_.timeout.stop(); return; } - // Yes it has. Store the params used to check this. - synthetic_mouse_event_.min_scrollbar_old = min_scrollbar; - synthetic_mouse_event_.max_scrollbar_old = max_scrollbar; - // ... and dispatch the event to the LyX core. - dispatch(synthetic_mouse_event_.cmd); + + // Scroll + if (step <= 2 * wh) { + buffer_view_->scroll(up ? -step : step); + buffer_view_->updateMetrics(); + } else { + buffer_view_->scrollDocView(value + up ? -step : step, false); + } + + // In which paragraph do we have to set the cursor ? + Cursor & cur = buffer_view_->cursor(); + TextMetrics const & tm = buffer_view_->textMetrics(cur.text()); + + pair p = up ? tm.first() : tm.last(); + ParagraphMetrics const & pm = *p.second; + pit_type const pit = p.first; + + if (pm.rows().empty()) + return; + + // Find the row at which we set the cursor. + RowList::const_iterator rit = pm.rows().begin(); + RowList::const_iterator rlast = pm.rows().end(); + int yy = pm.position() - pm.ascent(); + for (--rlast; rit != rlast; ++rit) { + int h = rit->height(); + if ((up && yy + h > 0) + || (!up && yy + h > wh - defaultRowHeight())) + break; + yy += h; + } + + // Find the position of the cursor + bool bound; + int x = synthetic_mouse_event_.cmd.x(); + pos_type const pos = rit->pos() + tm.getColumnNearX(pit, *rit, x, bound); + + // Set the cursor + cur.pit() = pit; + cur.pos() = pos; + cur.boundary(bound); + + buffer_view_->buffer().changed(false); + return; } @@ -1260,7 +1343,7 @@ EmbeddedWorkArea::EmbeddedWorkArea(QWidget * w): GuiWorkArea(w) EmbeddedWorkArea::~EmbeddedWorkArea() { // No need to destroy buffer and bufferview here, because it is done - // in theBuffeerList() destruction loop at application exit + // in theBufferList() destruction loop at application exit } @@ -1406,7 +1489,7 @@ void TabWorkArea::mouseDoubleClickEvent(QMouseEvent * event) void TabWorkArea::setFullScreen(bool full_screen) { for (int i = 0; i != count(); ++i) { - if (GuiWorkArea * wa = dynamic_cast(widget(i))) + if (GuiWorkArea * wa = workArea(i)) wa->setFullScreen(full_screen); } @@ -1437,12 +1520,18 @@ GuiWorkArea * TabWorkArea::currentWorkArea() } +GuiWorkArea * TabWorkArea::workArea(int index) +{ + return dynamic_cast(widget(index)); +} + + GuiWorkArea * TabWorkArea::workArea(Buffer & buffer) { // FIXME: this method doesn't work if we have more than work area // showing the same buffer. for (int i = 0; i != count(); ++i) { - GuiWorkArea * wa = dynamic_cast(widget(i)); + GuiWorkArea * wa = workArea(i); LASSERT(wa, return 0); if (&wa->bufferView().buffer() == &buffer) return wa; @@ -1454,7 +1543,7 @@ GuiWorkArea * TabWorkArea::workArea(Buffer & buffer) void TabWorkArea::closeAll() { while (count()) { - GuiWorkArea * wa = dynamic_cast(widget(0)); + GuiWorkArea * wa = workArea(0); LASSERT(wa, /**/); removeTab(0); delete wa; @@ -1537,7 +1626,7 @@ void TabWorkArea::on_currentTabChanged(int i) // returns e.g. on application destruction if (i == -1) return; - GuiWorkArea * wa = dynamic_cast(widget(i)); + GuiWorkArea * wa = workArea(i); LASSERT(wa, return); wa->setUpdatesEnabled(true); wa->redraw(true); @@ -1556,7 +1645,7 @@ void TabWorkArea::closeCurrentBuffer() if (clicked_tab_ == -1) wa = currentWorkArea(); else { - wa = dynamic_cast(widget(clicked_tab_)); + wa = workArea(clicked_tab_); LASSERT(wa, /**/); } wa->view().closeWorkArea(wa); @@ -1569,7 +1658,7 @@ void TabWorkArea::hideCurrentTab() if (clicked_tab_ == -1) wa = currentWorkArea(); else { - wa = dynamic_cast(widget(clicked_tab_)); + wa = workArea(clicked_tab_); LASSERT(wa, /**/); } wa->view().hideWorkArea(wa); @@ -1583,7 +1672,7 @@ void TabWorkArea::closeTab(int index) if (index == -1) wa = currentWorkArea(); else { - wa = dynamic_cast(widget(index)); + wa = workArea(index); LASSERT(wa, /**/); } wa->view().closeWorkArea(wa); @@ -1694,7 +1783,7 @@ void TabWorkArea::updateTabTexts() // collect full names first: path into postfix, empty prefix and // filename without extension for (size_t i = 0; i < n; ++i) { - GuiWorkArea * i_wa = dynamic_cast(widget(i)); + GuiWorkArea * i_wa = workArea(i); FileName const fn = i_wa->bufferView().buffer().fileName(); paths.push_back(DisplayPath(i, fn)); } @@ -1778,7 +1867,7 @@ void TabWorkArea::updateTabTexts() // set new tab titles for (It it = paths.begin(); it != paths.end(); ++it) { - GuiWorkArea * i_wa = dynamic_cast(widget(it->tab())); + GuiWorkArea * i_wa = workArea(it->tab()); Buffer & buf = i_wa->bufferView().buffer(); if (!buf.fileName().empty() && !buf.isClean()) setTabText(it->tab(), it->displayString() + "*");