]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiWorkArea.cpp
On Linux show in crash message box the backtrace
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
index 10075dcf4ad58b0d1e7c48f3404c0db41470d700..514866ac497ce845b498c6d33014e92f69443757 100644 (file)
 #include "support/gettext.h"
 #include "support/FileName.h"
 #include "support/lassert.h"
+#include "support/TempFile.h"
 
 #include "frontends/Application.h"
 #include "frontends/FontMetrics.h"
 #include "frontends/WorkAreaManager.h"
 
 #include <QContextMenuEvent>
+#if (QT_VERSION < 0x050000)
 #include <QInputContext>
+#endif
+#include <QDrag>
 #include <QHelpEvent>
 #ifdef Q_WS_MACX
 #include <QMacStyle>
 #endif
 #include <QMainWindow>
+#include <QMimeData>
 #include <QMenu>
 #include <QPainter>
 #include <QPalette>
 #include <QPixmapCache>
 #include <QScrollBar>
+#include <QStyleOption>
+#include <QStylePainter>
 #include <QTimer>
 #include <QToolButton>
 #include <QToolTip>
@@ -237,7 +244,7 @@ SyntheticMouseEvent::SyntheticMouseEvent()
 
 
 GuiWorkArea::Private::Private(GuiWorkArea * parent)
-: p(parent), buffer_view_(0), read_only_(false), lyx_view_(0), cursor_visible_(false),
+: p(parent), screen_(0), buffer_view_(0), lyx_view_(0), cursor_visible_(false),
 need_resize_(false), schedule_redraw_(false), preedit_lines_(1),
 completer_(new GuiCompleter(p, p))
 {
@@ -253,7 +260,6 @@ GuiWorkArea::GuiWorkArea(QWidget * /* w */)
 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
 : d(new Private(this))
 {
-       d->read_only_ = buffer.isReadonly();
        setGuiView(gv);
        setBuffer(buffer);
        init();
@@ -275,7 +281,7 @@ void GuiWorkArea::init()
                d->cursor_timeout_.setInterval(500);
        }
 
-       d->screen_ = QPixmap(viewport()->width(), viewport()->height());
+       d->resetScreen();
        // With Qt4.5 a mouse event will happen before the first paint event
        // so make sure that the buffer view has an up to date metrics.
        d->buffer_view_->resize(viewport()->width(), viewport()->height());
@@ -320,6 +326,7 @@ void GuiWorkArea::init()
 GuiWorkArea::~GuiWorkArea()
 {
        d->buffer_view_->buffer().workAreaManager().remove(this);
+       delete d->screen_;
        delete d->buffer_view_;
        delete d->cursor_;
        // Completer has a QObject parent and is thus automatically destroyed.
@@ -476,7 +483,7 @@ void GuiWorkArea::redraw(bool update_metrics)
        if (lyxerr.debugging(Debug::WORKAREA))
                d->buffer_view_->coordCache().dump();
 
-       d->setReadOnly(d->buffer_view_->buffer().isReadonly());
+       updateWindowTitle();
 
        d->updateCursorShape();
 }
@@ -532,6 +539,7 @@ void GuiWorkArea::Private::dispatch(FuncRequest const & cmd0, KeyModifier mod)
        buffer_view_->mouseEventDispatch(cmd);
 
        // Skip these when selecting
+       // FIXME: let GuiView take care of those.
        if (cmd.action() != LFUN_MOUSE_MOTION) {
                completer_->updateVisibility(false, false);
                lyx_view_->updateDialogs();
@@ -543,6 +551,7 @@ void GuiWorkArea::Private::dispatch(FuncRequest const & cmd0, KeyModifier mod)
                // Slight hack: this is only called currently when we
                // clicked somewhere, so we force through the display
                // of the new status here.
+               // FIXME: let GuiView take care of those.
                lyx_view_->clearMessage();
 
                // Show the cursor immediately after any operation
@@ -557,7 +566,10 @@ void GuiWorkArea::Private::resizeBufferView()
 {
        // WARNING: Please don't put any code that will trigger a repaint here!
        // We are already inside a paint event.
-       lyx_view_->setBusy(true);
+       p->stopBlinkingCursor();
+       // Warn our container (GuiView).
+       p->busy(true);
+
        Point point;
        int h = 0;
        buffer_view_->cursorPosAndHeight(point, h);
@@ -573,9 +585,13 @@ void GuiWorkArea::Private::resizeBufferView()
        // as the scrollbar paramters are then set for the first time.
        updateScrollbar();
 
-       lyx_view_->updateLayoutList();
-       lyx_view_->setBusy(false);
        need_resize_ = false;
+       p->busy(false);
+       // Eventually, restart the cursor after the resize event.
+       // We might be resizing even if the focus is on another widget so we only
+       // restart the cursor if we have the focus.
+       if (p->hasFocus())
+               QTimer::singleShot(50, p, SLOT(startBlinkingCursor()));
 }
 
 
@@ -656,11 +672,14 @@ void GuiWorkArea::scrollTo(int value)
 
        if (lyxrc.cursor_follows_scrollbar) {
                d->buffer_view_->setCursorFromScrollbar();
+               // FIXME: let GuiView take care of those.
                d->lyx_view_->updateLayoutList();
        }
        // Show the cursor immediately after any operation.
        startBlinkingCursor();
+#ifdef Q_WS_X11
        QApplication::syncX();
+#endif
 }
 
 
@@ -707,7 +726,7 @@ bool GuiWorkArea::event(QEvent * e)
 
 void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
 {
-       docstring name;
+       string name;
        if (e->reason() == QContextMenuEvent::Mouse)
                // the menu name is set on mouse press
                name = d->context_menu_name_;
@@ -751,8 +770,10 @@ void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
 void GuiWorkArea::focusInEvent(QFocusEvent * e)
 {
        LYXERR(Debug::DEBUG, "GuiWorkArea::focusInEvent(): " << this << endl);
-       if (d->lyx_view_->currentWorkArea() != this)
+       if (d->lyx_view_->currentWorkArea() != this) {
                d->lyx_view_->setCurrentWorkArea(this);
+               d->lyx_view_->currentWorkArea()->bufferView().buffer().updateBuffer();
+       }
 
        startBlinkingCursor();
        QAbstractScrollArea::focusInEvent(e);
@@ -778,7 +799,9 @@ void GuiWorkArea::mousePressEvent(QMouseEvent * e)
                return;
        }
 
+#if (QT_VERSION < 0x050000)
        inputContext()->reset();
+#endif
 
        FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
                q_button_state(e->button()));
@@ -946,7 +969,7 @@ void GuiWorkArea::generateSyntheticMouseEvent()
                d->buffer_view_->scroll(up ? -step : step);
                d->buffer_view_->updateMetrics();
        } else {
-               d->buffer_view_->scrollDocView(value + up ? -step : step, false);
+               d->buffer_view_->scrollDocView(value + (up ? -step : step), false);
        }
 
        // In which paragraph do we have to set the cursor ?
@@ -1102,7 +1125,7 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
        //      << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
 
        if (d->need_resize_) {
-               d->screen_ = QPixmap(viewport()->width(), viewport()->height());
+               d->resetScreen();
                d->resizeBufferView();
                if (d->cursor_visible_) {
                        d->hideCursor();
@@ -1111,7 +1134,11 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
        }
 
        QPainter pain(viewport());
-       pain.drawPixmap(rc, d->screen_, rc);
+       if (lyxrc.use_qimage) {
+               pain.drawImage(rc, static_cast<QImage const &>(*d->screen_), rc);
+       } else {
+               pain.drawPixmap(rc, static_cast<QPixmap const &>(*d->screen_), rc);
+       }
        d->cursor_->draw(pain);
        ev->accept();
 }
@@ -1119,7 +1146,7 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
 
 void GuiWorkArea::Private::updateScreen()
 {
-       GuiPainter pain(&screen_);
+       GuiPainter pain(screen_);
        buffer_view_->draw(pain);
 }
 
@@ -1184,6 +1211,8 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
                stopBlinkingCursor();
 
        // last_width : for checking if last preedit string was/wasn't empty.
+       // FIXME THREAD
+       // We could have more than one work area, right?
        static bool last_width = false;
        if (!last_width && preedit_string.empty()) {
                // if last_width is last length of preedit string.
@@ -1191,7 +1220,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
                return;
        }
 
-       GuiPainter pain(&d->screen_);
+       GuiPainter pain(d->screen_);
        d->buffer_view_->updateMetrics();
        d->buffer_view_->draw(pain);
        FontInfo font = d->buffer_view_->cursor().getFont().fontInfo();
@@ -1349,17 +1378,6 @@ void GuiWorkArea::updateWindowTitle()
 }
 
 
-void GuiWorkArea::Private::setReadOnly(bool read_only)
-{
-       if (read_only_ == read_only)
-               return;
-       read_only_ = read_only;
-       p->updateWindowTitle();
-       if (p == lyx_view_->currentWorkArea())
-               lyx_view_->updateDialogs();
-}
-
-
 bool GuiWorkArea::isFullScreen() const
 {
        return d->lyx_view_ && d->lyx_view_->isFullScreen();
@@ -1409,8 +1427,9 @@ GuiView & GuiWorkArea::view()
 
 EmbeddedWorkArea::EmbeddedWorkArea(QWidget * w): GuiWorkArea(w)
 {
-       buffer_ = theBufferList().newBuffer(
-               support::FileName::tempName().absFileName() + "_embedded.internal");
+       support::TempFile tempfile("embedded.internal");
+       tempfile.setAutoRemove(false);
+       buffer_ = theBufferList().newInternalBuffer(tempfile.name().absFileName());
        buffer_->setUnnamed(true);
        buffer_->setFullyLoaded(true);
        setBuffer(*buffer_);
@@ -1502,9 +1521,6 @@ 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,
@@ -1541,15 +1557,36 @@ TabWorkArea::TabWorkArea(QWidget * parent)
        tb->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(tb, SIGNAL(customContextMenuRequested(const QPoint &)),
                this, SLOT(showContextMenu(const QPoint &)));
-#if QT_VERSION >= 0x040500
        connect(tb, SIGNAL(tabCloseRequested(int)),
                this, SLOT(closeTab(int)));
-#endif
 
        setUsesScrollButtons(true);
 }
 
 
+void TabWorkArea::paintEvent(QPaintEvent * event)
+{
+       if (tabBar()->isVisible()) {
+               QTabWidget::paintEvent(event);
+       } else {
+               // Prevent the selected tab to influence the 
+               // painting of the frame of the tab widget.
+               // This is needed for gtk style in Qt.
+               QStylePainter p(this);
+#if QT_VERSION < 0x050000
+               QStyleOptionTabWidgetFrameV2 opt;
+#else
+               QStyleOptionTabWidgetFrame opt;
+#endif
+               initStyleOption(&opt);
+               opt.rect = style()->subElementRect(QStyle::SE_TabWidgetTabPane,
+                       &opt, this);
+               opt.selectedTabRect = QRect();
+               p.drawPrimitive(QStyle::PE_FrameTabWidget, opt);
+       }
+}
+
+
 void TabWorkArea::mouseDoubleClickEvent(QMouseEvent * event)
 {
        if (event->button() != Qt::LeftButton)
@@ -1573,6 +1610,8 @@ void TabWorkArea::setFullScreen(bool full_screen)
 
        if (lyxrc.full_screen_tabbar)
                showBar(!full_screen && count() > 1);
+       else
+               showBar(count() > 1);
 }
 
 
@@ -1581,9 +1620,7 @@ void TabWorkArea::showBar(bool show)
        tabBar()->setEnabled(show);
        tabBar()->setVisible(show);
        closeBufferButton->setVisible(show && lyxrc.single_close_tab_button);
-#if QT_VERSION >= 0x040500
        setTabsClosable(!lyxrc.single_close_tab_button);
-#endif
 }
 
 
@@ -1593,7 +1630,7 @@ GuiWorkArea * TabWorkArea::currentWorkArea()
                return 0;
 
        GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
-       LASSERT(wa, /**/);
+       LATTEST(wa);
        return wa;
 }
 
@@ -1622,7 +1659,7 @@ void TabWorkArea::closeAll()
 {
        while (count()) {
                GuiWorkArea * wa = workArea(0);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
                removeTab(0);
                delete wa;
        }
@@ -1631,7 +1668,7 @@ void TabWorkArea::closeAll()
 
 bool TabWorkArea::setCurrentWorkArea(GuiWorkArea * work_area)
 {
-       LASSERT(work_area, /**/);
+       LASSERT(work_area, return false);
        int index = indexOf(work_area);
        if (index == -1)
                return false;
@@ -1724,7 +1761,7 @@ void TabWorkArea::closeCurrentBuffer()
                wa = currentWorkArea();
        else {
                wa = workArea(clicked_tab_);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
        }
        wa->view().closeWorkArea(wa);
 }
@@ -1737,7 +1774,7 @@ void TabWorkArea::hideCurrentTab()
                wa = currentWorkArea();
        else {
                wa = workArea(clicked_tab_);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
        }
        wa->view().hideWorkArea(wa);
 }
@@ -1751,7 +1788,7 @@ void TabWorkArea::closeTab(int index)
                wa = currentWorkArea();
        else {
                wa = workArea(index);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
        }
        wa->view().closeWorkArea(wa);
 }
@@ -1947,7 +1984,7 @@ void TabWorkArea::updateTabTexts()
        for (It it = paths.begin(); it != paths.end(); ++it) {
                int const tab_index = it->tab();
                Buffer const & buf = workArea(tab_index)->bufferView().buffer();
-               QString tab_text = it->displayString();
+               QString tab_text = it->displayString().replace("&", "&&");
                if (!buf.fileName().empty() && !buf.isClean())
                        tab_text += "*";
                setTabText(tab_index, tab_text);
@@ -1992,25 +2029,10 @@ DragTabBar::DragTabBar(QWidget* parent)
        : QTabBar(parent)
 {
        setAcceptDrops(true);
-#if QT_VERSION >= 0x040500
        setTabsClosable(!lyxrc.single_close_tab_button);
-#endif
 }
 
 
-#if QT_VERSION < 0x040300
-int DragTabBar::tabAt(QPoint const & position) const
-{
-       const int max = count();
-       for (int i = 0; i < max; ++i) {
-               if (tabRect(i).contains(position))
-                       return i;
-       }
-       return -1;
-}
-#endif
-
-
 void DragTabBar::mousePressEvent(QMouseEvent * event)
 {
        if (event->button() == Qt::LeftButton)
@@ -2049,17 +2071,12 @@ void DragTabBar::mouseMoveEvent(QMouseEvent * event)
        mimeData->setData("action", "tab-reordering") ;
        drag->setMimeData(mimeData);
 
-#if QT_VERSION >= 0x040300
        // get tab pixmap as cursor
        QRect r = tabRect(tab);
        QPixmap pixmap(r.size());
        render(&pixmap, - r.topLeft());
        drag->setPixmap(pixmap);
        drag->exec();
-#else
-       drag->start(Qt::MoveAction);
-#endif
-
 }