]> jfr.im git - irc/weechat/qweechat.git/blame - qweechat/input.py
More fixes.
[irc/weechat/qweechat.git] / qweechat / input.py
CommitLineData
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
1f27a20e 23from PySide6 import QtCore
8e15c19f 24from PySide6 import QtWidgets
7dcf23b1
SH
25
26
8e15c19f 27class InputLineEdit(QtWidgets.QLineEdit):
7dcf23b1
SH
28 """Input line."""
29
1f27a20e
AR
30 bufferSwitchPrev = QtCore.Signal()
31 bufferSwitchNext = QtCore.Signal()
32 textSent = QtCore.Signal(str)
7dcf23b1
SH
33
34 def __init__(self, scroll_widget):
1f27a20e 35 super().__init__(scroll_widget)
7dcf23b1
SH
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()
77df9d06
SH
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:
8e15c19f 51 QtWidgets.QLineEdit.keyPressEvent(self, event)
77df9d06
SH
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:
8e15c19f 66 QtWidgets.QLineEdit.keyPressEvent(self, event)
7dcf23b1
SH
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())
7dcf23b1
SH
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:
8e15c19f 76 QtWidgets.QLineEdit.keyPressEvent(self, event)
7dcf23b1
SH
77
78 def _input_return_pressed(self):
e7b0dbce 79 self._history.append(self.text())
7dcf23b1
SH
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])