]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiWorkArea.cpp
Amend f441590c
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
index 4d5d2591770d7155f843040c825f94a1a1e0986e..2bd7c8e5b1825fcbede7f02b1a9b69819c70dd4c 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>
+#ifdef Q_OS_MAC
+#include <QProxyStyle>
 #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>
@@ -125,7 +132,9 @@ namespace frontend {
 
 class CursorWidget {
 public:
-       CursorWidget() {
+       CursorWidget() : rtl_(false), l_shape_(false), completable_(false),
+               show_(false), x_(0), cursor_width_(0)
+       {
                recomputeWidth();
        }
 
@@ -237,9 +246,11 @@ SyntheticMouseEvent::SyntheticMouseEvent()
 
 
 GuiWorkArea::Private::Private(GuiWorkArea * parent)
-: p(parent), buffer_view_(0), lyx_view_(0), cursor_visible_(false),
+: p(parent), screen_(0), buffer_view_(0), read_only_(false), lyx_view_(0),
+cursor_visible_(false), cursor_(0),
 need_resize_(false), schedule_redraw_(false), preedit_lines_(1),
-completer_(new GuiCompleter(p, p))
+pixel_ratio_(1.0),
+completer_(new GuiCompleter(p, p)), dialog_mode_(false)
 {
 }
 
@@ -254,11 +265,22 @@ GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
 : d(new Private(this))
 {
        setGuiView(gv);
+       buffer.params().display_pixel_ratio = theGuiApp()->pixelRatio();
        setBuffer(buffer);
        init();
 }
 
 
+double GuiWorkArea::pixelRatio() const
+{
+#if QT_VERSION >= 0x050000
+       return devicePixelRatio();
+#else
+       return 1.0;
+#endif
+}
+
+
 void GuiWorkArea::init()
 {
        // Setup the signals
@@ -319,6 +341,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.
@@ -531,6 +554,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();
@@ -542,6 +566,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
@@ -556,7 +581,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);
@@ -572,9 +600,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()));
 }
 
 
@@ -611,6 +643,12 @@ void GuiWorkArea::Private::showCursor()
                && !completer_->inlineVisible();
        cursor_visible_ = true;
        cursor_->recomputeWidth();
+
+       //int cur_x = buffer_view_->getPos(cur).x_;
+       // We may have decided to slide the cursor row so that cursor
+       // is visible.
+       p.x_ -= buffer_view_->horizScrollOffset();
+
        showCursor(p.x_, p.y_, h, l_shape, isrtl, completable);
 }
 
@@ -655,11 +693,15 @@ 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();
+       // FIXME QT5
+#ifdef Q_WS_X11
        QApplication::syncX();
+#endif
 }
 
 
@@ -706,7 +748,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_;
@@ -750,8 +792,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);
@@ -777,7 +821,9 @@ void GuiWorkArea::mousePressEvent(QMouseEvent * e)
                return;
        }
 
+#if (QT_VERSION < 0x050000) && !defined(__HAIKU__)
        inputContext()->reset();
+#endif
 
        FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
                q_button_state(e->button()));
@@ -945,7 +991,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 ?
@@ -974,11 +1020,11 @@ void GuiWorkArea::generateSyntheticMouseEvent()
                        break;
                yy += h;
        }
-       
+
        // Find the position of the cursor
        bool bound;
        int x = d->synthetic_mouse_event_.cmd.x();
-       pos_type const pos = rit->pos() + tm.getColumnNearX(pit, *rit, x, bound);
+       pos_type const pos = tm.getPosNearX(*rit, x, bound);
 
        // Set the cursor
        cur.pit() = pit;
@@ -1018,7 +1064,7 @@ void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
        // do nothing if there are other events
        // (the auto repeated events come too fast)
        // it looks like this is only needed on X11
-#ifdef Q_WS_X11
+#if defined(Q_WS_X11) || defined(QPA_XCB)
        if (qApp->hasPendingEvents() && ev->isAutoRepeat()) {
                switch (ev->key()) {
                case Qt::Key_PageDown:
@@ -1096,11 +1142,11 @@ void GuiWorkArea::Private::update(int x, int y, int w, int h)
 
 void GuiWorkArea::paintEvent(QPaintEvent * ev)
 {
-       QRect const rc = ev->rect();
+       QRectF const rc = ev->rect();
        // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
        //      << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
 
-       if (d->need_resize_) {
+       if (d->needResize()) {
                d->resetScreen();
                d->resizeBufferView();
                if (d->cursor_visible_) {
@@ -1110,11 +1156,16 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
        }
 
        QPainter pain(viewport());
-#ifdef USE_QIMAGE
-       pain.drawImage(rc, d->screen_, rc);
-#else
-       pain.drawPixmap(rc, d->screen_, rc);
-#endif
+       double const pr = pixelRatio();
+       QRectF const rcs = QRectF(rc.x() * pr, rc.y() * pr, rc.width() * pr, rc.height() * pr);
+
+       if (lyxrc.use_qimage) {
+               QImage const & image = static_cast<QImage const &>(*d->screen_);
+               pain.drawImage(rc, image, rcs);
+       } else {
+               QPixmap const & pixmap = static_cast<QPixmap const &>(*d->screen_);
+               pain.drawPixmap(rc, pixmap, rcs);
+       }
        d->cursor_->draw(pain);
        ev->accept();
 }
@@ -1122,7 +1173,7 @@ void GuiWorkArea::paintEvent(QPaintEvent * ev)
 
 void GuiWorkArea::Private::updateScreen()
 {
-       GuiPainter pain(&screen_);
+       GuiPainter pain(screen_, p->pixelRatio());
        buffer_view_->draw(pain);
 }
 
@@ -1187,6 +1238,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.
@@ -1194,7 +1247,7 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
                return;
        }
 
-       GuiPainter pain(&d->screen_);
+       GuiPainter pain(d->screen_, pixelRatio());
        d->buffer_view_->updateMetrics();
        d->buffer_view_->draw(pain);
        FontInfo font = d->buffer_view_->cursor().getFont().fontInfo();
@@ -1401,8 +1454,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_);
@@ -1460,14 +1514,14 @@ void EmbeddedWorkArea::disable()
 //
 ////////////////////////////////////////////////////////////////////
 
-#ifdef Q_WS_MACX
-class NoTabFrameMacStyle : public QMacStyle {
+#ifdef Q_OS_MAC
+class NoTabFrameMacStyle : public QProxyStyle {
 public:
        ///
        QRect subElementRect(SubElement element, const QStyleOption * option,
                             const QWidget * widget = 0) const
        {
-               QRect rect = QMacStyle::subElementRect(element, option, widget);
+               QRect rect = QProxyStyle::subElementRect(element, option, widget);
                bool noBar = static_cast<QTabWidget const *>(widget)->count() <= 1;
 
                // The Qt Mac style puts the contents into a 3 pixel wide box
@@ -1491,12 +1545,9 @@ NoTabFrameMacStyle noTabFrameMacStyle;
 TabWorkArea::TabWorkArea(QWidget * parent)
        : QTabWidget(parent), clicked_tab_(-1)
 {
-#ifdef Q_WS_MACX
+#ifdef Q_OS_MAC
        setStyle(&noTabFrameMacStyle);
 #endif
-#if QT_VERSION < 0x040500
-       lyxrc.single_close_tab_button = true;
-#endif
 
        QPalette pal = palette();
        pal.setColor(QPalette::Active, QPalette::Button,
@@ -1512,7 +1563,7 @@ TabWorkArea::TabWorkArea(QWidget * parent)
        closeBufferButton = new QToolButton(this);
        closeBufferButton->setPalette(pal);
        // FIXME: rename the icon to closebuffer.png
-       closeBufferButton->setIcon(QIcon(getPixmap("images/", "closetab", "png")));
+       closeBufferButton->setIcon(QIcon(getPixmap("images/", "closetab", "svgz,png")));
        closeBufferButton->setText("Close File");
        closeBufferButton->setAutoRaise(true);
        closeBufferButton->setCursor(Qt::ArrowCursor);
@@ -1533,15 +1584,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)
@@ -1565,6 +1637,8 @@ void TabWorkArea::setFullScreen(bool full_screen)
 
        if (lyxrc.full_screen_tabbar)
                showBar(!full_screen && count() > 1);
+       else
+               showBar(count() > 1);
 }
 
 
@@ -1573,9 +1647,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
 }
 
 
@@ -1585,7 +1657,7 @@ GuiWorkArea * TabWorkArea::currentWorkArea()
                return 0;
 
        GuiWorkArea * wa = dynamic_cast<GuiWorkArea *>(currentWidget());
-       LASSERT(wa, /**/);
+       LATTEST(wa);
        return wa;
 }
 
@@ -1614,7 +1686,7 @@ void TabWorkArea::closeAll()
 {
        while (count()) {
                GuiWorkArea * wa = workArea(0);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
                removeTab(0);
                delete wa;
        }
@@ -1623,7 +1695,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;
@@ -1716,7 +1788,7 @@ void TabWorkArea::closeCurrentBuffer()
                wa = currentWorkArea();
        else {
                wa = workArea(clicked_tab_);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
        }
        wa->view().closeWorkArea(wa);
 }
@@ -1729,7 +1801,7 @@ void TabWorkArea::hideCurrentTab()
                wa = currentWorkArea();
        else {
                wa = workArea(clicked_tab_);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
        }
        wa->view().hideWorkArea(wa);
 }
@@ -1743,7 +1815,7 @@ void TabWorkArea::closeTab(int index)
                wa = currentWorkArea();
        else {
                wa = workArea(index);
-               LASSERT(wa, /**/);
+               LASSERT(wa, return);
        }
        wa->view().closeWorkArea(wa);
 }
@@ -1783,7 +1855,7 @@ public:
 
                if (!dotted) {
                        if (dottedPrefix_ && !prefix_.isEmpty())
-                               prefix_ += ".../";
+                               prefix_ += ellipsisSlash_;
                        prefix_ += postfix_.front() + "/";
                }
                dottedPrefix_ = dotted && !prefix_.isEmpty();
@@ -1796,7 +1868,7 @@ public:
                        return filename_;
 
                bool dots = dottedPrefix_ || !postfix_.isEmpty();
-               return prefix_ + (dots ? ".../" : "") + filename_;
+               return prefix_ + (dots ? ellipsisSlash_ : "") + filename_;
        }
        ///
        QString forecastPathString() const
@@ -1805,7 +1877,7 @@ public:
                        return displayString();
 
                return prefix_
-                       + (dottedPrefix_ ? ".../" : "")
+                       + (dottedPrefix_ ? ellipsisSlash_ : "")
                        + postfix_.front() + "/";
        }
        ///
@@ -1814,6 +1886,8 @@ public:
        int tab() const { return tab_; }
 
 private:
+       /// ".../"
+       static QString const ellipsisSlash_;
        ///
        QString prefix_;
        ///
@@ -1829,6 +1903,9 @@ private:
 };
 
 
+QString const DisplayPath::ellipsisSlash_ = QString(QChar(0x2026)) + "/";
+
+
 ///
 bool operator<(DisplayPath const & a, DisplayPath const & b)
 {
@@ -1939,11 +2016,17 @@ 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 += "*";
+               QString tab_tooltip = it->abs();
+               if (buf.isReadonly()) {
+                       setTabIcon(tab_index, QIcon(getPixmap("images/", "emblem-readonly", "svgz,png")));
+                       tab_tooltip = qt_("%1 (read only)").arg(it->abs());
+               } else
+                       setTabIcon(tab_index, QIcon());
                setTabText(tab_index, tab_text);
-               setTabToolTip(tab_index, it->abs());
+               setTabToolTip(tab_index, tab_tooltip);
        }
 }
 
@@ -1957,9 +2040,9 @@ void TabWorkArea::showContextMenu(const QPoint & pos)
 
        // show tab popup
        QMenu popup;
-       popup.addAction(QIcon(getPixmap("images/", "hidetab", "png")),
+       popup.addAction(QIcon(getPixmap("images/", "hidetab", "svgz,png")),
                qt_("Hide tab"), this, SLOT(hideCurrentTab()));
-       popup.addAction(QIcon(getPixmap("images/", "closetab", "png")),
+       popup.addAction(QIcon(getPixmap("images/", "closetab", "svgz,png")),
                qt_("Close tab"), this, SLOT(closeCurrentBuffer()));
        popup.exec(tabBar()->mapToGlobal(pos));
 
@@ -1984,23 +2067,8 @@ 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)
@@ -2041,17 +2109,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
-
 }