swift
Loading...
Searching...
No Matches
eventfilter.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "eventfilter.h"
5
6#include <QKeyEvent>
7#include <QPlainTextEdit>
8
9#include "misc/stringutils.h"
10
11namespace swift::gui
12{
14 {
15 if (event->type() == QEvent::KeyPress)
16 {
17 if (auto *e = dynamic_cast<QKeyEvent *>(event))
18 {
19 // If QKeyEvent::text() returns an empty QString then let normal
20 // processing proceed as it may be a control (e.g. cursor movement)
21 // key. Otherwise convert the text to upper case and insert it at
22 // the current cursor position.
23 auto *pte = qobject_cast<QPlainTextEdit *>(object);
24 if (!pte)
25 {
26 return false;
27 }
28 if (pte->isReadOnly())
29 {
30 return false;
31 }
32
33 if (e->text().length() == 1)
34 {
35 const QChar c = e->text().front();
36 if (c.isLetter())
37 {
38 const ushort unicode = c.unicode();
39 if (unicode > 127)
40 {
41 return true;
42 }
43
44 pte->insertPlainText(e->text().toUpper());
45 // return true to prevent further processing
46 return true;
47 }
48 // all codes like backspace etc.
49 return false;
50 } // length
51 } // key event
52 }
53 return false;
54 }
55} // namespace swift::gui
bool eventFilter(QObject *object, QEvent *event)
Filter.
GUI related classes.
bool isLetter(char32_t ucs4)
char16_t & unicode()
virtual bool event(QEvent *e)
T qobject_cast(QObject *object)