1/*
2 SPDX-FileCopyrightText: 2020-2023 Laurent Montel <montel@kde.org>
3 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "klineediteventhandler.h"
8#include "klineediturldropeventfilter.h"
9
10#include <QKeyEvent>
11#include <QLineEdit>
12
13class LineEditCatchReturnKey : public QObject
14{
15 Q_OBJECT
16public:
17 explicit LineEditCatchReturnKey(QLineEdit *lineEdit);
18
19protected:
20 bool eventFilter(QObject *obj, QEvent *event) override;
21
22private:
23 QLineEdit *const m_lineEdit;
24};
25
26LineEditCatchReturnKey::LineEditCatchReturnKey(QLineEdit *lineEdit)
27 : QObject(lineEdit)
28 , m_lineEdit(lineEdit)
29{
30 m_lineEdit->installEventFilter(filterObj: this);
31}
32
33bool LineEditCatchReturnKey::eventFilter(QObject *obj, QEvent *event)
34{
35 if (obj == m_lineEdit) {
36 if (event->type() == QEvent::KeyPress) {
37 auto e = static_cast<QKeyEvent *>(event);
38 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
39 const bool stopEvent = (e->modifiers() == Qt::NoButton || e->modifiers() == Qt::KeypadModifier);
40 if (stopEvent) {
41 Q_EMIT m_lineEdit->returnPressed();
42 }
43 return true;
44 }
45 }
46 }
47 return QObject::eventFilter(watched: obj, event);
48}
49
50void KLineEditEventHandler::catchReturnKey(QObject *lineEdit)
51{
52 if (auto le = qobject_cast<QLineEdit *>(object: lineEdit)) {
53 new LineEditCatchReturnKey(le);
54 }
55}
56
57void KLineEditEventHandler::handleUrlDrops(QObject *lineEdit)
58{
59 auto filter = new KLineEditUrlDropEventFilter(lineEdit);
60 lineEdit->installEventFilter(filterObj: filter);
61}
62
63#include "klineediteventhandler.moc"
64

source code of kwidgetsaddons/src/klineediteventhandler.cpp