]> jfr.im git - irc/weechat/qweechat.git/blame - qweechat/input.py
Fix pylint errors
[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
1fe88536
SH
23"""Input line for chat and debug window."""
24
f3da694d 25from PySide6 import QtCore, QtWidgets
7dcf23b1
SH
26
27
8e15c19f 28class InputLineEdit(QtWidgets.QLineEdit):
7dcf23b1
SH
29 """Input line."""
30
1f27a20e
AR
31 bufferSwitchPrev = QtCore.Signal()
32 bufferSwitchNext = QtCore.Signal()
33 textSent = QtCore.Signal(str)
7dcf23b1
SH
34
35 def __init__(self, scroll_widget):
35136f99 36 super().__init__()
7dcf23b1
SH
37 self.scroll_widget = scroll_widget
38 self._history = []
39 self._history_index = -1
40 self.returnPressed.connect(self._input_return_pressed)
41
42 def keyPressEvent(self, event):
43 key = event.key()
44 modifiers = event.modifiers()
1fe88536 45 scroll = self.scroll_widget.verticalScrollBar()
77df9d06
SH
46 if modifiers == QtCore.Qt.ControlModifier:
47 if key == QtCore.Qt.Key_PageUp:
48 self.bufferSwitchPrev.emit()
49 elif key == QtCore.Qt.Key_PageDown:
50 self.bufferSwitchNext.emit()
51 else:
8e15c19f 52 QtWidgets.QLineEdit.keyPressEvent(self, event)
77df9d06
SH
53 elif modifiers == QtCore.Qt.AltModifier:
54 if key in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Up):
55 self.bufferSwitchPrev.emit()
56 elif key in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Down):
57 self.bufferSwitchNext.emit()
58 elif key == QtCore.Qt.Key_PageUp:
1fe88536 59 scroll.setValue(scroll.value() - (scroll.pageStep() / 10))
77df9d06 60 elif key == QtCore.Qt.Key_PageDown:
1fe88536 61 scroll.setValue(scroll.value() + (scroll.pageStep() / 10))
77df9d06 62 elif key == QtCore.Qt.Key_Home:
1fe88536 63 scroll.setValue(scroll.minimum())
77df9d06 64 elif key == QtCore.Qt.Key_End:
1fe88536 65 scroll.setValue(scroll.maximum())
77df9d06 66 else:
8e15c19f 67 QtWidgets.QLineEdit.keyPressEvent(self, event)
7dcf23b1 68 elif key == QtCore.Qt.Key_PageUp:
1fe88536 69 scroll.setValue(scroll.value() - scroll.pageStep())
7dcf23b1 70 elif key == QtCore.Qt.Key_PageDown:
1fe88536 71 scroll.setValue(scroll.value() + scroll.pageStep())
7dcf23b1
SH
72 elif key == QtCore.Qt.Key_Up:
73 self._history_navigate(-1)
74 elif key == QtCore.Qt.Key_Down:
75 self._history_navigate(1)
76 else:
8e15c19f 77 QtWidgets.QLineEdit.keyPressEvent(self, event)
7dcf23b1
SH
78
79 def _input_return_pressed(self):
e7b0dbce 80 self._history.append(self.text())
7dcf23b1
SH
81 self._history_index = len(self._history)
82 self.textSent.emit(self.text())
83 self.clear()
84
85 def _history_navigate(self, direction):
86 if self._history:
87 self._history_index += direction
88 if self._history_index < 0:
89 self._history_index = 0
90 return
91 if self._history_index > len(self._history) - 1:
92 self._history_index = len(self._history)
93 self.clear()
94 return
95 self.setText(self._history[self._history_index])