]>
Commit | Line | Data |
---|---|---|
7dcf23b1 SH |
1 | # -*- coding: utf-8 -*- |
2 | # | |
e836cfb0 SH |
3 | # input.py - input line for chat and debug window |
4 | # | |
8335009d | 5 | # Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org> |
7dcf23b1 SH |
6 | # |
7 | # This file is part of QWeeChat, a Qt remote GUI for WeeChat. | |
8 | # | |
9 | # QWeeChat is free software; you can redistribute it and/or modify | |
10 | # it under the terms of the GNU General Public License as published by | |
11 | # the Free Software Foundation; either version 3 of the License, or | |
12 | # (at your option) any later version. | |
13 | # | |
14 | # QWeeChat is distributed in the hope that it will be useful, | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | # GNU General Public License for more details. | |
18 | # | |
19 | # You should have received a copy of the GNU General Public License | |
20 | # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>. | |
21 | # | |
22 | ||
f3da694d | 23 | from PySide6 import QtCore, QtWidgets |
7dcf23b1 SH |
24 | |
25 | ||
8e15c19f | 26 | class InputLineEdit(QtWidgets.QLineEdit): |
7dcf23b1 SH |
27 | """Input line.""" |
28 | ||
1f27a20e AR |
29 | bufferSwitchPrev = QtCore.Signal() |
30 | bufferSwitchNext = QtCore.Signal() | |
31 | textSent = QtCore.Signal(str) | |
7dcf23b1 SH |
32 | |
33 | def __init__(self, scroll_widget): | |
35136f99 | 34 | super().__init__() |
7dcf23b1 SH |
35 | self.scroll_widget = scroll_widget |
36 | self._history = [] | |
37 | self._history_index = -1 | |
38 | self.returnPressed.connect(self._input_return_pressed) | |
39 | ||
40 | def keyPressEvent(self, event): | |
41 | key = event.key() | |
42 | modifiers = event.modifiers() | |
43 | bar = self.scroll_widget.verticalScrollBar() | |
77df9d06 SH |
44 | if modifiers == QtCore.Qt.ControlModifier: |
45 | if key == QtCore.Qt.Key_PageUp: | |
46 | self.bufferSwitchPrev.emit() | |
47 | elif key == QtCore.Qt.Key_PageDown: | |
48 | self.bufferSwitchNext.emit() | |
49 | else: | |
8e15c19f | 50 | QtWidgets.QLineEdit.keyPressEvent(self, event) |
77df9d06 SH |
51 | elif modifiers == QtCore.Qt.AltModifier: |
52 | if key in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Up): | |
53 | self.bufferSwitchPrev.emit() | |
54 | elif key in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Down): | |
55 | self.bufferSwitchNext.emit() | |
56 | elif key == QtCore.Qt.Key_PageUp: | |
57 | bar.setValue(bar.value() - (bar.pageStep() / 10)) | |
58 | elif key == QtCore.Qt.Key_PageDown: | |
59 | bar.setValue(bar.value() + (bar.pageStep() / 10)) | |
60 | elif key == QtCore.Qt.Key_Home: | |
61 | bar.setValue(bar.minimum()) | |
62 | elif key == QtCore.Qt.Key_End: | |
63 | bar.setValue(bar.maximum()) | |
64 | else: | |
8e15c19f | 65 | QtWidgets.QLineEdit.keyPressEvent(self, event) |
7dcf23b1 SH |
66 | elif key == QtCore.Qt.Key_PageUp: |
67 | bar.setValue(bar.value() - bar.pageStep()) | |
68 | elif key == QtCore.Qt.Key_PageDown: | |
69 | bar.setValue(bar.value() + bar.pageStep()) | |
7dcf23b1 SH |
70 | elif key == QtCore.Qt.Key_Up: |
71 | self._history_navigate(-1) | |
72 | elif key == QtCore.Qt.Key_Down: | |
73 | self._history_navigate(1) | |
74 | else: | |
8e15c19f | 75 | QtWidgets.QLineEdit.keyPressEvent(self, event) |
7dcf23b1 SH |
76 | |
77 | def _input_return_pressed(self): | |
e7b0dbce | 78 | self._history.append(self.text()) |
7dcf23b1 SH |
79 | self._history_index = len(self._history) |
80 | self.textSent.emit(self.text()) | |
81 | self.clear() | |
82 | ||
83 | def _history_navigate(self, direction): | |
84 | if self._history: | |
85 | self._history_index += direction | |
86 | if self._history_index < 0: | |
87 | self._history_index = 0 | |
88 | return | |
89 | if self._history_index > len(self._history) - 1: | |
90 | self._history_index = len(self._history) | |
91 | self.clear() | |
92 | return | |
93 | self.setText(self._history[self._history_index]) |