From cec327bd4a8a3a77f09b6e465636db2537b86580 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Thu, 20 Dec 2012 15:34:42 +0100 Subject: [PATCH] Close variable chooser when pressing escape. Even if the variable chooser currently doesn't have focus. Task-number: QTCREATORBUG-7155 Change-Id: Ie3a7527e13dd6c3f37c1abe86a596753d2b8e286 Reviewed-by: Robert Loehning --- src/plugins/coreplugin/variablechooser.cpp | 24 +++++++++++++++++++--- src/plugins/coreplugin/variablechooser.h | 1 + 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/plugins/coreplugin/variablechooser.cpp b/src/plugins/coreplugin/variablechooser.cpp index 5a9bbab6d98..96417b0cd80 100644 --- a/src/plugins/coreplugin/variablechooser.cpp +++ b/src/plugins/coreplugin/variablechooser.cpp @@ -87,7 +87,8 @@ void VariableChooser::updateDescription(const QString &variable) void VariableChooser::updateCurrentEditor(QWidget *old, QWidget *widget) { - Q_UNUSED(old) + if (old) + old->removeEventFilter(this); if (!widget) // we might loose focus, but then keep the previous state return; // prevent children of the chooser itself, and limit to children of chooser's parent @@ -104,6 +105,7 @@ void VariableChooser::updateCurrentEditor(QWidget *old, QWidget *widget) } if (!handle) return; + widget->installEventFilter(this); // for intercepting escape key presses QLineEdit *previousLineEdit = m_lineEdit; m_lineEdit = 0; m_textEdit = 0; @@ -182,10 +184,26 @@ void VariableChooser::insertVariable(const QString &variable) } } -void VariableChooser::keyPressEvent(QKeyEvent *ke) +static bool handleEscapePressed(QKeyEvent *ke, QWidget *widget) { if (ke->key() == Qt::Key_Escape && !ke->modifiers()) { ke->accept(); - QTimer::singleShot(0, this, SLOT(close())); + QTimer::singleShot(0, widget, SLOT(close())); + return true; } + return false; +} + +void VariableChooser::keyPressEvent(QKeyEvent *ke) +{ + handleEscapePressed(ke, this); +} + +bool VariableChooser::eventFilter(QObject *, QEvent *event) +{ + if (event->type() == QEvent::KeyPress && isVisible()) { + QKeyEvent *ke = static_cast(event); + return handleEscapePressed(ke, this); + } + return false; } diff --git a/src/plugins/coreplugin/variablechooser.h b/src/plugins/coreplugin/variablechooser.h index fe1e38edb20..2fcf8270e76 100644 --- a/src/plugins/coreplugin/variablechooser.h +++ b/src/plugins/coreplugin/variablechooser.h @@ -58,6 +58,7 @@ public: protected: void keyPressEvent(QKeyEvent *ke); + bool eventFilter(QObject *, QEvent *event); private slots: void updateDescription(const QString &variable);