QmlDesigner: Override shortcuts in text editor

There are a couple of known shortcuts that we have to override
in the text editor. We install an event filter and handle
the shortcut override event for the keys in question.

Change-Id: Icb5483b19e2202d85e99d24c20d05097aaa33da6
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Thomas Hartmann
2017-01-19 15:25:03 +01:00
parent 64b8bd0924
commit f581ad7ded
2 changed files with 22 additions and 0 deletions

View File

@@ -34,8 +34,12 @@
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <QEvent>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <vector>
#include <algorithm>
namespace QmlDesigner { namespace QmlDesigner {
TextEditorWidget::TextEditorWidget(TextEditorView *textEditorView) : QWidget() TextEditorWidget::TextEditorWidget(TextEditorView *textEditorView) : QWidget()
@@ -64,6 +68,7 @@ void TextEditorWidget::setTextEditor(TextEditor::BaseTextEditor *textEditor) {
connect(textEditor->editorWidget(), &QPlainTextEdit::cursorPositionChanged, connect(textEditor->editorWidget(), &QPlainTextEdit::cursorPositionChanged,
&m_updateSelectionTimer, static_cast<void (QTimer::*)()>(&QTimer::start)); &m_updateSelectionTimer, static_cast<void (QTimer::*)()>(&QTimer::start));
textEditor->editorWidget()->installEventFilter(this);
} }
QString TextEditorWidget::contextHelpId() const QString TextEditorWidget::contextHelpId() const
@@ -136,4 +141,18 @@ int TextEditorWidget::currentLine() const
return -1; return -1;
} }
bool TextEditorWidget::eventFilter( QObject *, QEvent *event)
{
static std::vector<int> overrideKeys = { Qt::Key_Delete, Qt::Key_Backspace, Qt::Key_Left, Qt::Key_Right, Qt::Key_Up, Qt::Key_Down };
if (event->type() == QEvent::ShortcutOverride) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (std::find(overrideKeys.begin(), overrideKeys.end(), keyEvent->key()) != overrideKeys.end()) {
keyEvent->accept();
return true;
}
}
return false;
}
} // namespace QmlDesigner } // namespace QmlDesigner

View File

@@ -58,6 +58,9 @@ public:
void clearStatusBar(); void clearStatusBar();
int currentLine() const; int currentLine() const;
protected:
bool eventFilter(QObject *object, QEvent *event) override;
private: private:
void updateSelectionByCursorPosition(); void updateSelectionByCursorPosition();