]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiWorkArea.cpp
Code cleanup
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
index d0c04b7d3de6a7f91db81e78083c93a570cbc69b..410f5e97dd6642e18e331ca08999ac6f2119f5c8 100644 (file)
@@ -231,7 +231,7 @@ private:
 // 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.0), max_scrollbar_old(-1.0)
+         x_old(-1), y_old(-1), min_scrollbar_old(-1), max_scrollbar_old(-1)
 {}
 
 
@@ -239,7 +239,7 @@ GuiWorkArea::GuiWorkArea(QWidget *)
        : buffer_view_(0), lyx_view_(0),
        cursor_visible_(false),
        need_resize_(false), schedule_redraw_(false),
-       preedit_lines_(1), completer_(new GuiCompleter(this)),
+       preedit_lines_(1), completer_(new GuiCompleter(this, this)),
        context_target_pos_()
 {
 }
@@ -249,7 +249,7 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
        : buffer_view_(0), lyx_view_(0),
        cursor_visible_(false),
        need_resize_(false), schedule_redraw_(false),
-       preedit_lines_(1), completer_(new GuiCompleter(this)),
+       preedit_lines_(1), completer_(new GuiCompleter(this, this)),
        context_target_pos_()
 {
        setGuiView(gv);
@@ -321,6 +321,7 @@ GuiWorkArea::~GuiWorkArea()
        delete buffer_view_;
        delete cursor_;
        // Completer has a QObject parent and is thus automatically destroyed.
+       // See #4758.
        // delete completer_;
 }
 
@@ -334,7 +335,7 @@ void GuiWorkArea::setGuiView(GuiView & gv)
 void GuiWorkArea::setBuffer(Buffer & buffer)
 {
        delete buffer_view_;
-       buffer_view_ = new BufferView(buffer),
+       buffer_view_ = new BufferView(buffer);
        buffer.workAreaManager().add(this);
 
        // HACK: Prevents an additional redraw when the scrollbar pops up
@@ -406,7 +407,7 @@ void GuiWorkArea::startBlinkingCursor()
 }
 
 
-void GuiWorkArea::redraw()
+void GuiWorkArea::redraw(bool update_metrics)
 {
        if (!isVisible())
                // No need to redraw in this case.
@@ -414,7 +415,7 @@ void GuiWorkArea::redraw()
 
        // No need to do anything if this is the current view. The BufferView
        // metrics are already up to date.
-       if (lyx_view_ != guiApp->currentView()
+       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_->updateMetrics();
@@ -455,9 +456,7 @@ void GuiWorkArea::processKeySym(KeySymbol const & key, KeyModifier mod)
        // we better stop the blinking cursor...
        // the cursor gets restarted in GuiView::restartCursor()
        stopBlinkingCursor();
-
-       theLyXFunc().setLyXView(lyx_view_);
-       theLyXFunc().processKeySym(key, mod);
+       guiApp->processKeySym(key, mod);
 }
 
 
@@ -469,8 +468,6 @@ void GuiWorkArea::dispatch(FuncRequest const & cmd0, KeyModifier mod)
                return;
        }
 
-       theLyXFunc().setLyXView(lyx_view_);
-
        FuncRequest cmd;
 
        if (cmd0.action == LFUN_MOUSE_PRESS) {
@@ -701,14 +698,7 @@ void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
 
 void GuiWorkArea::focusInEvent(QFocusEvent * e)
 {
-       /*
-       LYXERR(Debug::DEBUG, "GuiWorkArea::focusInEvent(): " << this << std::endl);
-       GuiWorkArea * old_gwa = theGuiApp()->currentView()->currentWorkArea();
-       if (old_gwa)
-               old_gwa->stopBlinkingCursor();
-       lyx_view_->setCurrentWorkArea(this);
-       */
-
+       LYXERR(Debug::DEBUG, "GuiWorkArea::focusInEvent(): " << this << endl);
        if (lyx_view_->currentWorkArea() != this)
                lyx_view_->setCurrentWorkArea(this);
 
@@ -719,7 +709,7 @@ void GuiWorkArea::focusInEvent(QFocusEvent * e)
 
 void GuiWorkArea::focusOutEvent(QFocusEvent * e)
 {
-       LYXERR(Debug::DEBUG, "GuiWorkArea::focusOutEvent(): " << this << std::endl);
+       LYXERR(Debug::DEBUG, "GuiWorkArea::focusOutEvent(): " << this << endl);
        stopBlinkingCursor();
        QAbstractScrollArea::focusOutEvent(e);
 }
@@ -850,7 +840,7 @@ void GuiWorkArea::wheelEvent(QWheelEvent * ev)
        scroll_value *= delta;
 
        // Take into account user preference.
-       scroll_value *= lyxrc.mouse_wheel_speed;
+       scroll_value = int(scroll_value * lyxrc.mouse_wheel_speed);
        LYXERR(Debug::SCROLLING, "wheelScrollLines = " << lines
                        << " delta = " << delta << " scroll_value = " << scroll_value
                        << " page_step = " << page_step);
@@ -976,8 +966,10 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
        if (need_resize_) {
                screen_ = QPixmap(viewport()->width(), viewport()->height());
                resizeBufferView();
-               hideCursor();
-               showCursor();
+               if (cursor_visible_) {
+                       hideCursor();
+                       showCursor();
+               }
        }
 
        QPainter pain(viewport());
@@ -1279,7 +1271,9 @@ void EmbeddedWorkArea::disable()
        stopBlinkingCursor();
        if (view().currentWorkArea() != this)
                return;
-       LASSERT(view().currentMainWorkArea(), /* */);
+       // No problem if currentMainWorkArea() is 0 (setCurrentWorkArea()
+       // tolerates it and shows the background logo), what happens if
+       // an EmbeddedWorkArea is closed after closing all document WAs
        view().setCurrentWorkArea(view().currentMainWorkArea());
 }
 
@@ -1323,6 +1317,9 @@ TabWorkArea::TabWorkArea(QWidget * parent)
 #ifdef Q_WS_MACX
        setStyle(&noTabFrameMacStyle);
 #endif
+#if QT_VERSION < 0x040500
+       lyxrc.single_close_tab_button = true;
+#endif
 
        QPalette pal = palette();
        pal.setColor(QPalette::Active, QPalette::Button,
@@ -1335,7 +1332,6 @@ TabWorkArea::TabWorkArea(QWidget * parent)
        QObject::connect(this, SIGNAL(currentChanged(int)),
                this, SLOT(on_currentTabChanged(int)));
 
-#if QT_VERSION < 0x040500
        closeBufferButton = new QToolButton(this);
        closeBufferButton->setPalette(pal);
        // FIXME: rename the icon to closebuffer.png
@@ -1348,7 +1344,6 @@ TabWorkArea::TabWorkArea(QWidget * parent)
        QObject::connect(closeBufferButton, SIGNAL(clicked()),
                this, SLOT(closeCurrentBuffer()));
        setCornerWidget(closeBufferButton, Qt::TopRightCorner);
-#endif
 
        // setup drag'n'drop
        QTabBar* tb = new DragTabBar;
@@ -1363,7 +1358,7 @@ TabWorkArea::TabWorkArea(QWidget * parent)
                this, SLOT(showContextMenu(const QPoint &)));
 #if QT_VERSION >= 0x040500
        connect(tb, SIGNAL(tabCloseRequested(int)),
-               tb, SLOT(on_tabCloseRequested(int)));
+               this, SLOT(closeTab(int)));
 #endif
 
        setUsesScrollButtons(true);
@@ -1386,8 +1381,9 @@ void TabWorkArea::showBar(bool show)
 {
        tabBar()->setEnabled(show);
        tabBar()->setVisible(show);
-#if QT_VERSION < 0x040500
-       closeBufferButton->setVisible(show);    
+       closeBufferButton->setVisible(show && lyxrc.single_close_tab_button);
+#if QT_VERSION >= 0x040500
+       setTabsClosable(!lyxrc.single_close_tab_button);
 #endif
 }
 
@@ -1503,34 +1499,31 @@ void TabWorkArea::on_currentTabChanged(int i)
                return;
        GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(widget(i));
        LASSERT(wa, return);
-       BufferView & bv = wa->bufferView();
-       bv.cursor().fixIfBroken();
-       bv.updateMetrics();
        wa->setUpdatesEnabled(true);
-       wa->redraw();
+       wa->redraw(true);
        wa->setFocus();
        ///
        currentWorkAreaChanged(wa);
 
        LYXERR(Debug::GUI, "currentTabChanged " << i
-               << "File" << bv.buffer().absFileName());
+               << " File: " << wa->bufferView().buffer().absFileName());
 }
 
 
 void TabWorkArea::closeCurrentBuffer()
 {
-       if (clicked_tab_ != -1)
-               setCurrentIndex(clicked_tab_);
-       else
-               // Before dispatching the LFUN we should be sure this
-               // is the current workarea.
-               currentWorkAreaChanged(currentWorkArea());
-
-       lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
+       GuiWorkArea * wa;
+       if (clicked_tab_ == -1)
+               wa = currentWorkArea();
+       else {
+               wa = dynamic_cast<GuiWorkArea *>(widget(clicked_tab_));
+               LASSERT(wa, /**/);
+       }
+       wa->view().closeWorkArea(wa);
 }
 
 
-void TabWorkArea::closeCurrentTab()
+void TabWorkArea::hideCurrentTab()
 {
        GuiWorkArea * wa;
        if (clicked_tab_ == -1)
@@ -1542,6 +1535,21 @@ void TabWorkArea::closeCurrentTab()
        wa->view().hideWorkArea(wa);
 }
 
+
+void TabWorkArea::closeTab(int index)
+{
+       on_currentTabChanged(index);
+       GuiWorkArea * wa;
+       if (index == -1)
+               wa = currentWorkArea();
+       else {
+               wa = dynamic_cast<GuiWorkArea *>(widget(index));
+               LASSERT(wa, /**/);
+       }
+       wa->view().closeWorkArea(wa);
+}
+
+
 ///
 class DisplayPath {
 public:
@@ -1750,7 +1758,7 @@ void TabWorkArea::showContextMenu(const QPoint & pos)
        // show tab popup
        QMenu popup;
        popup.addAction(QIcon(getPixmap("images/", "hidetab", "png")),
-               qt_("Hide tab"), this, SLOT(closeCurrentTab()));
+               qt_("Hide tab"), this, SLOT(hideCurrentTab()));
        popup.addAction(QIcon(getPixmap("images/", "closetab", "png")),
                qt_("Close tab"), this, SLOT(closeCurrentBuffer()));
        popup.exec(tabBar()->mapToGlobal(pos));
@@ -1777,18 +1785,11 @@ DragTabBar::DragTabBar(QWidget* parent)
 {
        setAcceptDrops(true);
 #if QT_VERSION >= 0x040500
-       setTabsClosable(true);
+       setTabsClosable(!lyxrc.single_close_tab_button);
 #endif
 }
 
 
-void DragTabBar::on_tabCloseRequested(int index)
-{
-       setCurrentIndex(index);
-       lyx::dispatch(FuncRequest(LFUN_BUFFER_CLOSE));
-}
-
-
 #if QT_VERSION < 0x040300
 int DragTabBar::tabAt(QPoint const & position) const
 {