File indexing completed on 2024-04-28 03:59:06

0001 /*
0002    SPDX-FileCopyrightText: 2020-2023 Laurent Montel <montel@kde.org>
0003    SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "klineediteventhandler.h"
0008 #include "klineediturldropeventfilter.h"
0009 
0010 #include <QKeyEvent>
0011 #include <QLineEdit>
0012 
0013 class LineEditCatchReturnKey : public QObject
0014 {
0015     Q_OBJECT
0016 public:
0017     explicit LineEditCatchReturnKey(QLineEdit *lineEdit);
0018 
0019 protected:
0020     bool eventFilter(QObject *obj, QEvent *event) override;
0021 
0022 private:
0023     QLineEdit *const m_lineEdit;
0024 };
0025 
0026 LineEditCatchReturnKey::LineEditCatchReturnKey(QLineEdit *lineEdit)
0027     : QObject(lineEdit)
0028     , m_lineEdit(lineEdit)
0029 {
0030     m_lineEdit->installEventFilter(this);
0031 }
0032 
0033 bool LineEditCatchReturnKey::eventFilter(QObject *obj, QEvent *event)
0034 {
0035     if (obj == m_lineEdit) {
0036         if (event->type() == QEvent::KeyPress) {
0037             auto e = static_cast<QKeyEvent *>(event);
0038             if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
0039                 const bool stopEvent = (e->modifiers() == Qt::NoButton || e->modifiers() == Qt::KeypadModifier);
0040                 if (stopEvent) {
0041                     Q_EMIT m_lineEdit->returnPressed();
0042                 }
0043                 return true;
0044             }
0045         }
0046     }
0047     return QObject::eventFilter(obj, event);
0048 }
0049 
0050 void KLineEditEventHandler::catchReturnKey(QObject *lineEdit)
0051 {
0052     if (auto le = qobject_cast<QLineEdit *>(lineEdit)) {
0053         new LineEditCatchReturnKey(le);
0054     }
0055 }
0056 
0057 void KLineEditEventHandler::handleUrlDrops(QObject *lineEdit)
0058 {
0059     auto filter = new KLineEditUrlDropEventFilter(lineEdit);
0060     lineEdit->installEventFilter(filter);
0061 }
0062 
0063 #include "klineediteventhandler.moc"