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