]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiWorkArea.cpp
QDialogButtonBox for the remaining dialogs.
[lyx.git] / src / frontends / qt4 / GuiWorkArea.cpp
index 0df7b65c7880db02d48bdd8b02357f0f907e7f02..e6d993c2735ad8bd16eb43c394b7e91c51ee1c51 100644 (file)
@@ -270,12 +270,14 @@ GuiWorkArea::Private::~Private()
 GuiWorkArea::GuiWorkArea(QWidget * /* w */)
 : d(new Private(this))
 {
+       new CompressorProxy(this); // not a leak
 }
 
 
 GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
 : d(new Private(this))
 {
+       new CompressorProxy(this); // not a leak
        setGuiView(gv);
        buffer.params().display_pixel_ratio = theGuiApp()->pixelRatio();
        setBuffer(buffer);
@@ -658,10 +660,10 @@ void GuiWorkArea::Private::updateScrollbar()
        // the signal valueChanged. (#10311)
        QObject::disconnect(p->verticalScrollBar(), SIGNAL(valueChanged(int)),
                            p, SLOT(scrollTo(int)));
-       ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
-       p->verticalScrollBar()->setRange(scroll_.min, scroll_.max);
-       p->verticalScrollBar()->setPageStep(scroll_.page_step);
-       p->verticalScrollBar()->setSingleStep(scroll_.single_step);
+       ScrollbarParameters const & scroll = buffer_view_->scrollbarParameters();
+       p->verticalScrollBar()->setRange(scroll.min, scroll.max);
+       p->verticalScrollBar()->setPageStep(scroll.page_step);
+       p->verticalScrollBar()->setSingleStep(scroll.single_step);
        p->verticalScrollBar()->setSliderPosition(0);
        // Connect to the vertical scroll bar
        QObject::connect(p->verticalScrollBar(), SIGNAL(valueChanged(int)),
@@ -751,7 +753,7 @@ void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e)
                        if (inset && inset->asInsetMath())
                                --pos.rx();
                        else if (cur.pos() > 0) {
-                               Inset * inset = cur.paragraph().getInset(cur.pos() - 1);
+                               inset = cur.paragraph().getInset(cur.pos() - 1);
                                if (inset)
                                        ++pos.rx();
                        }
@@ -1033,6 +1035,39 @@ void GuiWorkArea::generateSyntheticMouseEvent()
 }
 
 
+// CompressorProxy adapted from Kuba Ober https://stackoverflow.com/a/21006207
+CompressorProxy::CompressorProxy(GuiWorkArea * wa) : QObject(wa), flag_(false)
+{
+       qRegisterMetaType<KeySymbol>("KeySymbol");
+       qRegisterMetaType<KeyModifier>("KeyModifier");
+       connect(wa, SIGNAL(compressKeySym(KeySymbol, KeyModifier, bool)),
+               this, SLOT(slot(KeySymbol, KeyModifier, bool)),
+               Qt::QueuedConnection);
+       connect(this, SIGNAL(signal(KeySymbol, KeyModifier)),
+               wa, SLOT(processKeySym(KeySymbol, KeyModifier)));
+}
+
+
+bool CompressorProxy::emitCheck(bool isAutoRepeat)
+{
+       flag_ = true;
+       if (isAutoRepeat)
+               QCoreApplication::sendPostedEvents(this, QEvent::MetaCall); // recurse
+       bool result = flag_;
+       flag_ = false;
+       return result;
+}
+
+
+void CompressorProxy::slot(KeySymbol sym, KeyModifier mod, bool isAutoRepeat)
+{
+       if (emitCheck(isAutoRepeat))
+               Q_EMIT signal(sym, mod);
+       else
+               LYXERR(Debug::KEY, "system is busy: autoRepeat key event ignored");
+}
+
+
 void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
 {
        // this is also called for ShortcutOverride events. In this case, one must
@@ -1040,13 +1075,16 @@ void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
        bool const act = (ev->type() != QEvent::ShortcutOverride);
 
        // Do not process here some keys if dialog_mode_ is set
-       if (d->dialog_mode_
+       bool const for_dialog_mode = d->dialog_mode_
                && (ev->modifiers() == Qt::NoModifier
                    || ev->modifiers() == Qt::ShiftModifier)
                && (ev->key() == Qt::Key_Escape
                    || ev->key() == Qt::Key_Enter
-                   || ev->key() == Qt::Key_Return)
-           ) {
+                   || ev->key() == Qt::Key_Return);
+       // also do not use autoRepeat to input shortcuts
+       bool const autoRepeat = ev->isAutoRepeat();
+
+       if (for_dialog_mode || (!act && autoRepeat)) {
                ev->ignore();
                return;
        }
@@ -1063,51 +1101,31 @@ 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
-#if defined(Q_WS_X11) || defined(QPA_XCB)
-       // FIXME: this is a weird way to implement event compression. Also, this is
-       // broken with IBus.
-       if (act && qApp->hasPendingEvents() && ev->isAutoRepeat()) {
-               switch (ev->key()) {
-               case Qt::Key_PageDown:
-               case Qt::Key_PageUp:
-               case Qt::Key_Left:
-               case Qt::Key_Right:
-               case Qt::Key_Up:
-               case Qt::Key_Down:
-                       LYXERR(Debug::KEY, "system is busy: scroll key event ignored");
-                       ev->ignore();
-                       return;
-               }
-       }
-#endif
-
        KeyModifier const m = q_key_state(ev->modifiers());
 
-       std::string str;
-       if (m & ShiftModifier)
-               str += "Shift-";
-       if (m & ControlModifier)
-               str += "Control-";
-       if (m & AltModifier)
-               str += "Alt-";
-       if (m & MetaModifier)
-               str += "Meta-";
-
-       if (act)
+       if (act && lyxerr.debugging(Debug::KEY)) {
+               std::string str;
+               if (m & ShiftModifier)
+                       str += "Shift-";
+               if (m & ControlModifier)
+                       str += "Control-";
+               if (m & AltModifier)
+                       str += "Alt-";
+               if (m & MetaModifier)
+                       str += "Meta-";
                LYXERR(Debug::KEY, " count: " << ev->count() << " text: " << ev->text()
                       << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key()
                       << " keyState: " << str);
+       }
 
        KeySymbol sym;
        setKeySymbol(&sym, ev);
        if (sym.isOK()) {
                if (act) {
-                       processKeySym(sym, m);
+                       Q_EMIT compressKeySym(sym, m, autoRepeat);
                        ev->accept();
                } else
+                       // here, !autoRepeat, as determined at the beginning
                        ev->setAccepted(queryKeySym(sym, m));
        } else {
                ev->ignore();
@@ -1229,6 +1247,18 @@ void GuiWorkArea::Private::paintPreeditText(GuiPainter & pain)
 
 void GuiWorkArea::paintEvent(QPaintEvent * ev)
 {
+       // Do not trigger the painting machinery if we are not ready (see
+       // bug #10989). The second test triggers when in the middle of a
+       // dispatch operation.
+       if (view().busy() || d->buffer_view_->buffer().undo().activeUndoGroup()) {
+               // Since macOS has turned the screen black at this point, our
+               // backing store has to be copied to screen (this is a no-op
+               // except on macOS).
+               d->updateScreen(ev->rect());
+               ev->accept();
+               return;
+       }
+
        // LYXERR(Debug::PAINTING, "paintEvent begin: x: " << rc.x()
        //      << " y: " << rc.y() << " w: " << rc.width() << " h: " << rc.height());
 
@@ -1263,11 +1293,13 @@ void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
 
        // insert the processed text in the document (handles undo)
        if (!e->commitString().isEmpty()) {
-               d->buffer_view_->cursor().beginUndoGroup();
-               d->buffer_view_->cursor().insert(qstring_to_ucs4(e->commitString()));
+               FuncRequest cmd(LFUN_SELF_INSERT,
+                               qstring_to_ucs4(e->commitString()),
+                               FuncRequest::KEYBOARD);
+               dispatch(cmd);
+               // FIXME: this is supposed to remove traces from preedit
+               // string. Can we avoid calling it explicitely?
                d->buffer_view_->updateMetrics();
-               d->buffer_view_->cursor().endUndoGroup();
-               viewport()->update();
        }
 
        // Hide the caret during the test transformation.