]> jfr.im git - irc/weechat/qweechat.git/blob - qweechat/input.py
Remove python shebangs
[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-2014 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 import qt_compat
24 QtCore = qt_compat.import_module('QtCore')
25 QtGui = qt_compat.import_module('QtGui')
26
27
28 class InputLineEdit(QtGui.QLineEdit):
29 """Input line."""
30
31 bufferSwitchPrev = qt_compat.Signal()
32 bufferSwitchNext = qt_compat.Signal()
33 textSent = qt_compat.Signal(str)
34
35 def __init__(self, scroll_widget):
36 QtGui.QLineEdit.__init__(self)
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()
45 bar = self.scroll_widget.verticalScrollBar()
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:
52 QtGui.QLineEdit.keyPressEvent(self, event)
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:
59 bar.setValue(bar.value() - (bar.pageStep() / 10))
60 elif key == QtCore.Qt.Key_PageDown:
61 bar.setValue(bar.value() + (bar.pageStep() / 10))
62 elif key == QtCore.Qt.Key_Home:
63 bar.setValue(bar.minimum())
64 elif key == QtCore.Qt.Key_End:
65 bar.setValue(bar.maximum())
66 else:
67 QtGui.QLineEdit.keyPressEvent(self, event)
68 elif key == QtCore.Qt.Key_PageUp:
69 bar.setValue(bar.value() - bar.pageStep())
70 elif key == QtCore.Qt.Key_PageDown:
71 bar.setValue(bar.value() + bar.pageStep())
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:
77 QtGui.QLineEdit.keyPressEvent(self, event)
78
79 def _input_return_pressed(self):
80 self._history.append(self.text().encode('utf-8'))
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])