]> jfr.im git - irc/weechat/qweechat.git/blob - qweechat/input.py
192e5d4ac776143b06925d15d8b0f68eb7aad8b7
[irc/weechat/qweechat.git] / qweechat / input.py
1 # -*- coding: utf-8 -*-
2 #
3 # input.py - input line for chat and debug window
4 #
5 # Copyright (C) 2011-2021 Sébastien Helleu <flashcode@flashtux.org>
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
23 from PySide6 import QtCore
24 from PySide6 import QtWidgets as QtGui
25
26
27 class InputLineEdit(QtGui.QLineEdit):
28 """Input line."""
29
30 bufferSwitchPrev = QtCore.Signal()
31 bufferSwitchNext = QtCore.Signal()
32 textSent = QtCore.Signal(str)
33
34 def __init__(self, scroll_widget):
35 super().__init__(scroll_widget)
36 self.scroll_widget = scroll_widget
37 self._history = []
38 self._history_index = -1
39 self.returnPressed.connect(self._input_return_pressed)
40
41 def keyPressEvent(self, event):
42 key = event.key()
43 modifiers = event.modifiers()
44 bar = self.scroll_widget.verticalScrollBar()
45 if modifiers == QtCore.Qt.ControlModifier:
46 if key == QtCore.Qt.Key_PageUp:
47 self.bufferSwitchPrev.emit()
48 elif key == QtCore.Qt.Key_PageDown:
49 self.bufferSwitchNext.emit()
50 else:
51 QtGui.QLineEdit.keyPressEvent(self, event)
52 elif modifiers == QtCore.Qt.AltModifier:
53 if key in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Up):
54 self.bufferSwitchPrev.emit()
55 elif key in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Down):
56 self.bufferSwitchNext.emit()
57 elif key == QtCore.Qt.Key_PageUp:
58 bar.setValue(bar.value() - (bar.pageStep() / 10))
59 elif key == QtCore.Qt.Key_PageDown:
60 bar.setValue(bar.value() + (bar.pageStep() / 10))
61 elif key == QtCore.Qt.Key_Home:
62 bar.setValue(bar.minimum())
63 elif key == QtCore.Qt.Key_End:
64 bar.setValue(bar.maximum())
65 else:
66 QtGui.QLineEdit.keyPressEvent(self, event)
67 elif key == QtCore.Qt.Key_PageUp:
68 bar.setValue(bar.value() - bar.pageStep())
69 elif key == QtCore.Qt.Key_PageDown:
70 bar.setValue(bar.value() + bar.pageStep())
71 elif key == QtCore.Qt.Key_Up:
72 self._history_navigate(-1)
73 elif key == QtCore.Qt.Key_Down:
74 self._history_navigate(1)
75 else:
76 QtGui.QLineEdit.keyPressEvent(self, event)
77
78 def _input_return_pressed(self):
79 self._history.append(self.text())
80 self._history_index = len(self._history)
81 self.textSent.emit(self.text())
82 self.clear()
83
84 def _history_navigate(self, direction):
85 if self._history:
86 self._history_index += direction
87 if self._history_index < 0:
88 self._history_index = 0
89 return
90 if self._history_index > len(self._history) - 1:
91 self._history_index = len(self._history)
92 self.clear()
93 return
94 self.setText(self._history[self._history_index])