| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com) |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "shortcutinputedit.h" |
| 18 | |
| 19 | #include <QKeyEvent> |
| 20 | #include <QKeySequence> |
| 21 | #include <QDebug> |
| 22 | #include <QAction> |
| 23 | |
| 24 | ShortcutInputEdit::ShortcutInputEdit(QWidget* parent):QLineEdit(parent) |
| 25 | { |
| 26 | QList<QAction *> acts = actions(); |
| 27 | foreach (const QAction* action, acts) { |
| 28 | qDebug()<<action->shortcut()[0]; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void ShortcutInputEdit::keyPressEvent(QKeyEvent *event) |
| 33 | { |
| 34 | if (event->key()==Qt::Key_Delete && event->modifiers()==Qt::NoModifier) { |
| 35 | setText(""); |
| 36 | } else if (event->key()==Qt::Key_Backspace && event->modifiers()==Qt::NoModifier) { |
| 37 | setText(""); |
| 38 | } else if (event->key()==Qt::Key_Control || |
| 39 | event->key()==Qt::Key_Alt || |
| 40 | event->key()==Qt::Key_Shift || |
| 41 | event->key()==Qt::Key_Meta |
| 42 | ) { |
| 43 | //setText(""); |
| 44 | return; |
| 45 | } else if (event->modifiers()==Qt::ShiftModifier |
| 46 | && !event->text().isEmpty() |
| 47 | && event->text().at(0).unicode()>32 |
| 48 | && event->text().at(0).unicode()<127){ |
| 49 | //setText(""); |
| 50 | return; |
| 51 | } else if (event->modifiers()==Qt::NoModifier |
| 52 | && !event->text().isEmpty() |
| 53 | && event->text().at(0).unicode()>32 |
| 54 | && event->text().at(0).unicode()<127){ |
| 55 | //setText(""); |
| 56 | return; |
| 57 | } else { |
| 58 | int key = event->key(); |
| 59 | if (key==Qt::Key_Backtab) |
| 60 | key = Qt::Key_Tab; |
| 61 | QKeySequence seq(event->modifiers()|key); |
| 62 | QString s=seq.toString(); |
| 63 | if (event->modifiers().testFlag(Qt::ShiftModifier) |
| 64 | && !event->text().isEmpty() |
| 65 | && event->text().at(0).unicode()>32 |
| 66 | && event->text().at(0).unicode()<127) { |
| 67 | s = s.mid(0,s.lastIndexOf('+')+1) + event->text().at(0); |
| 68 | } |
| 69 | setText(s); |
| 70 | if (key!=Qt::Key_Tab |
| 71 | && key!=Qt::Key_Enter |
| 72 | && key!=Qt::Key_Return) |
| 73 | emit inputFinished(this); |
| 74 | } |
| 75 | event->accept(); |
| 76 | } |
| 77 | |
| 78 | bool ShortcutInputEdit::event(QEvent *event) |
| 79 | { |
| 80 | if (event->type()==QEvent::ShortcutOverride) { |
| 81 | keyPressEvent((QKeyEvent*)event); |
| 82 | event->accept(); |
| 83 | return true; |
| 84 | } else if (event->type()==QEvent::KeyPress) { |
| 85 | keyPressEvent((QKeyEvent*)event); |
| 86 | return true; |
| 87 | } |
| 88 | return QLineEdit::event(event); |
| 89 | } |
| 90 |