File indexing completed on 2024-05-12 16:27:21

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "lineeditcatchreturnkey.h"
0008 #include <QEvent>
0009 #include <QKeyEvent>
0010 #include <QLineEdit>
0011 
0012 LineEditCatchReturnKey::LineEditCatchReturnKey(QLineEdit *lineEdit, QObject *parent)
0013     : QObject(parent)
0014     , mLineEdit(lineEdit)
0015 {
0016     mLineEdit->installEventFilter(this);
0017 }
0018 
0019 LineEditCatchReturnKey::~LineEditCatchReturnKey() = default;
0020 
0021 bool LineEditCatchReturnKey::eventFilter(QObject *obj, QEvent *event)
0022 {
0023     if (obj == mLineEdit) {
0024         if (event->type() == QEvent::KeyPress) {
0025             auto e = static_cast<QKeyEvent *>(event);
0026             if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
0027                 const bool stopEvent = (e->modifiers() == Qt::NoButton || e->modifiers() == Qt::KeypadModifier);
0028                 if (stopEvent) {
0029                     Q_EMIT mLineEdit->returnPressed();
0030                 }
0031                 return true;
0032             }
0033         }
0034     }
0035     return QObject::eventFilter(obj, event);
0036 }
0037 
0038 #include "moc_lineeditcatchreturnkey.cpp"