]> jfr.im git - irc/weechat/qweechat.git/blob - src/qweechat/input.py
Add support of object type "arr" (array) in protocol
[irc/weechat/qweechat.git] / src / qweechat / input.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2011-2012 Sebastien Helleu <flashcode@flashtux.org>
5 #
6 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
7 #
8 # QWeeChat is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # QWeeChat is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
20 #
21
22 #
23 # Input line (for chat or debug window).
24 #
25
26 import qt_compat
27 QtCore = qt_compat.import_module('QtCore')
28 QtGui = qt_compat.import_module('QtGui')
29
30
31 class InputLineEdit(QtGui.QLineEdit):
32 """Input line."""
33
34 bufferSwitchPrev = qt_compat.Signal()
35 bufferSwitchNext = qt_compat.Signal()
36 textSent = qt_compat.Signal(str)
37
38 def __init__(self, scroll_widget):
39 QtGui.QLineEdit.__init__(self)
40 self.scroll_widget = scroll_widget
41 self._history = []
42 self._history_index = -1
43 self.returnPressed.connect(self._input_return_pressed)
44
45 def keyPressEvent(self, event):
46 key = event.key()
47 modifiers = event.modifiers()
48 bar = self.scroll_widget.verticalScrollBar()
49 if modifiers == QtCore.Qt.ControlModifier and key == QtCore.Qt.Key_PageUp:
50 self.bufferSwitchPrev.emit()
51 elif modifiers == QtCore.Qt.AltModifier and key in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Up):
52 self.bufferSwitchPrev.emit()
53 elif modifiers == QtCore.Qt.ControlModifier and key == QtCore.Qt.Key_PageDown:
54 self.bufferSwitchNext.emit()
55 elif modifiers == QtCore.Qt.AltModifier and key in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Down):
56 self.bufferSwitchNext.emit()
57 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_PageUp:
58 bar.setValue(bar.value() - (bar.pageStep() / 10))
59 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_PageDown:
60 bar.setValue(bar.value() + (bar.pageStep() / 10))
61 elif key == QtCore.Qt.Key_PageUp:
62 bar.setValue(bar.value() - bar.pageStep())
63 elif key == QtCore.Qt.Key_PageDown:
64 bar.setValue(bar.value() + bar.pageStep())
65 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_Home:
66 bar.setValue(bar.minimum())
67 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_End:
68 bar.setValue(bar.maximum())
69 elif key == QtCore.Qt.Key_Up:
70 self._history_navigate(-1)
71 elif key == QtCore.Qt.Key_Down:
72 self._history_navigate(1)
73 else:
74 QtGui.QLineEdit.keyPressEvent(self, event)
75
76 def _input_return_pressed(self):
77 self._history.append(str(self.text()))
78 self._history_index = len(self._history)
79 self.textSent.emit(self.text())
80 self.clear()
81
82 def _history_navigate(self, direction):
83 if self._history:
84 self._history_index += direction
85 if self._history_index < 0:
86 self._history_index = 0
87 return
88 if self._history_index > len(self._history) - 1:
89 self._history_index = len(self._history)
90 self.clear()
91 return
92 self.setText(self._history[self._history_index])