File indexing completed on 2024-05-12 16:15:48

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