Install a new compressor

A brand new event compressor based on Kuba Ober's cleverly simple
solution: <https://stackoverflow.com/a/21006207>.

Fix #9362, #9461, #9933: Lyx suddenly gets keyboard keys wrong, and
deadlocks

Fix #9790: LyX should perform key event compression (for improving the
remote X connections one would also need to implement
Qt::WA_KeyCompression)

Fix #10516: slowness on repeated arrow keys with IBus and Qt5

Patch pulled from
https://github.com/gadmm/lyx-unstable/commit/bf5a1efb0db5bfc2b

Signed-off-by: Juergen Spitzmueller <spitz@lyx.org>
(cherry picked from commit 43e4b80734)
This commit is contained in:
Guillaume MM 2018-01-12 10:58:31 +01:00 committed by Juergen Spitzmueller
parent e629a978fd
commit 789617b818
2 changed files with 71 additions and 36 deletions

View File

@ -265,6 +265,7 @@ GuiWorkArea::GuiWorkArea(QWidget * /* w */)
GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv) GuiWorkArea::GuiWorkArea(Buffer & buffer, GuiView & gv)
: d(new Private(this)) : d(new Private(this))
{ {
new CompressorProxy(this); // not a leak
setGuiView(gv); setGuiView(gv);
buffer.params().display_pixel_ratio = theGuiApp()->pixelRatio(); buffer.params().display_pixel_ratio = theGuiApp()->pixelRatio();
setBuffer(buffer); setBuffer(buffer);
@ -1050,6 +1051,37 @@ void GuiWorkArea::generateSyntheticMouseEvent()
} }
// CompressorProxy adapted from Kuba Ober https://stackoverflow.com/a/21006207
CompressorProxy::CompressorProxy(GuiWorkArea * wa) : QObject(wa)
{
qRegisterMetaType<KeySymbol>("KeySymbol");
qRegisterMetaType<KeyModifier>("KeyModifier");
connect(wa, &GuiWorkArea::compressKeySym, this, &CompressorProxy::slot,
Qt::QueuedConnection);
connect(this, &CompressorProxy::signal, wa, &GuiWorkArea::processKeySym);
}
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) void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
{ {
// this is also called for ShortcutOverride events. In this case, one must // this is also called for ShortcutOverride events. In this case, one must
@ -1057,13 +1089,16 @@ void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
bool const act = (ev->type() != QEvent::ShortcutOverride); bool const act = (ev->type() != QEvent::ShortcutOverride);
// Do not process here some keys if dialog_mode_ is set // 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::NoModifier
|| ev->modifiers() == Qt::ShiftModifier) || ev->modifiers() == Qt::ShiftModifier)
&& (ev->key() == Qt::Key_Escape && (ev->key() == Qt::Key_Escape
|| ev->key() == Qt::Key_Enter || 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(); ev->ignore();
return; return;
} }
@ -1080,29 +1115,9 @@ 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()); KeyModifier const m = q_key_state(ev->modifiers());
if (act && lyxerr.debugging(Debug::KEY)) {
std::string str; std::string str;
if (m & ShiftModifier) if (m & ShiftModifier)
str += "Shift-"; str += "Shift-";
@ -1112,19 +1127,19 @@ void GuiWorkArea::keyPressEvent(QKeyEvent * ev)
str += "Alt-"; str += "Alt-";
if (m & MetaModifier) if (m & MetaModifier)
str += "Meta-"; str += "Meta-";
if (act)
LYXERR(Debug::KEY, " count: " << ev->count() << " text: " << ev->text() LYXERR(Debug::KEY, " count: " << ev->count() << " text: " << ev->text()
<< " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key() << " isAutoRepeat: " << ev->isAutoRepeat() << " key: " << ev->key()
<< " keyState: " << str); << " keyState: " << str);
}
KeySymbol sym; KeySymbol sym;
setKeySymbol(&sym, ev); setKeySymbol(&sym, ev);
if (sym.isOK()) { if (sym.isOK()) {
if (act) { if (act) {
processKeySym(sym, m); Q_EMIT compressKeySym(sym, m, autoRepeat);
ev->accept(); ev->accept();
} else } else
// here, !autoRepeat, as determined at the beginning
ev->setAccepted(queryKeySym(sym, m)); ev->setAccepted(queryKeySym(sym, m));
} else { } else {
ev->ignore(); ev->ignore();

View File

@ -16,6 +16,7 @@
#include "ui_WorkAreaUi.h" #include "ui_WorkAreaUi.h"
#include "frontends/WorkArea.h" #include "frontends/WorkArea.h"
#include "frontends/KeySymbol.h"
#include <QAbstractScrollArea> #include <QAbstractScrollArea>
#include <QTabBar> #include <QTabBar>
@ -105,6 +106,8 @@ Q_SIGNALS:
void busy(bool); void busy(bool);
/// ///
void bufferViewChanged(); void bufferViewChanged();
/// send key event to CompressorProxy
void compressKeySym(KeySymbol sym, KeyModifier mod, bool isAutoRepeat);
private Q_SLOTS: private Q_SLOTS:
/// Scroll the BufferView. /// Scroll the BufferView.
@ -165,6 +168,23 @@ private:
}; // GuiWorkArea }; // GuiWorkArea
/// CompressorProxy adapted from Kuba Ober https://stackoverflow.com/a/21006207
class CompressorProxy : public QObject
{
Q_OBJECT
bool emitCheck(bool isAutoRepeat);
bool flag_;
// input: event to compress
Q_SLOT void slot(KeySymbol sym, KeyModifier mod, bool isAutoRepeat);
// output: compressed event
Q_SIGNAL void signal(KeySymbol sym, KeyModifier mod);
public:
// No default constructor, since the proxy must be a child of the
// target object.
explicit CompressorProxy(GuiWorkArea * wa);
};
class EmbeddedWorkArea : public GuiWorkArea class EmbeddedWorkArea : public GuiWorkArea
{ {
Q_OBJECT Q_OBJECT