]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | # -*- coding: utf-8 -*- | |
3 | # | |
4 | # Copyright (C) 2011 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.ControlModifier and key == QtCore.Qt.Key_PageDown: | |
52 | self.bufferSwitchNext.emit() | |
53 | elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_PageUp: | |
54 | bar.setValue(bar.value() - (bar.pageStep() / 10)) | |
55 | elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_PageDown: | |
56 | bar.setValue(bar.value() + (bar.pageStep() / 10)) | |
57 | elif key == QtCore.Qt.Key_PageUp: | |
58 | bar.setValue(bar.value() - bar.pageStep()) | |
59 | elif key == QtCore.Qt.Key_PageDown: | |
60 | bar.setValue(bar.value() + bar.pageStep()) | |
61 | elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_Home: | |
62 | bar.setValue(bar.minimum()) | |
63 | elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_End: | |
64 | bar.setValue(bar.maximum()) | |
65 | elif key == QtCore.Qt.Key_Up: | |
66 | self._history_navigate(-1) | |
67 | elif key == QtCore.Qt.Key_Down: | |
68 | self._history_navigate(1) | |
69 | else: | |
70 | QtGui.QLineEdit.keyPressEvent(self, event) | |
71 | ||
72 | def _input_return_pressed(self): | |
73 | self._history.append(str(self.text())) | |
74 | self._history_index = len(self._history) | |
75 | self.textSent.emit(self.text()) | |
76 | self.clear() | |
77 | ||
78 | def _history_navigate(self, direction): | |
79 | if self._history: | |
80 | self._history_index += direction | |
81 | if self._history_index < 0: | |
82 | self._history_index = 0 | |
83 | return | |
84 | if self._history_index > len(self._history) - 1: | |
85 | self._history_index = len(self._history) | |
86 | self.clear() | |
87 | return | |
88 | self.setText(self._history[self._history_index]) |