]> jfr.im git - irc/weechat/qweechat.git/blame - src/qweechat/input.py
Use /usr/bin/env in python shebang
[irc/weechat/qweechat.git] / src / qweechat / input.py
CommitLineData
fbb0156d 1#!/usr/bin/env python
7dcf23b1
SH
2# -*- coding: utf-8 -*-
3#
e836cfb0
SH
4# input.py - input line for chat and debug window
5#
da74afdb 6# Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
7dcf23b1
SH
7#
8# This file is part of QWeeChat, a Qt remote GUI for WeeChat.
9#
10# QWeeChat is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 3 of the License, or
13# (at your option) any later version.
14#
15# QWeeChat is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
22#
23
7dcf23b1
SH
24import qt_compat
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()
47 if modifiers == QtCore.Qt.ControlModifier and key == QtCore.Qt.Key_PageUp:
48 self.bufferSwitchPrev.emit()
e59c8988
SH
49 elif modifiers == QtCore.Qt.AltModifier and key in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Up):
50 self.bufferSwitchPrev.emit()
7dcf23b1
SH
51 elif modifiers == QtCore.Qt.ControlModifier and key == QtCore.Qt.Key_PageDown:
52 self.bufferSwitchNext.emit()
e59c8988
SH
53 elif modifiers == QtCore.Qt.AltModifier and key in (QtCore.Qt.Key_Right, QtCore.Qt.Key_Down):
54 self.bufferSwitchNext.emit()
7dcf23b1
SH
55 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_PageUp:
56 bar.setValue(bar.value() - (bar.pageStep() / 10))
57 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_PageDown:
58 bar.setValue(bar.value() + (bar.pageStep() / 10))
59 elif key == QtCore.Qt.Key_PageUp:
60 bar.setValue(bar.value() - bar.pageStep())
61 elif key == QtCore.Qt.Key_PageDown:
62 bar.setValue(bar.value() + bar.pageStep())
63 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_Home:
64 bar.setValue(bar.minimum())
65 elif modifiers == QtCore.Qt.AltModifier and key == QtCore.Qt.Key_End:
66 bar.setValue(bar.maximum())
67 elif key == QtCore.Qt.Key_Up:
68 self._history_navigate(-1)
69 elif key == QtCore.Qt.Key_Down:
70 self._history_navigate(1)
71 else:
72 QtGui.QLineEdit.keyPressEvent(self, event)
73
74 def _input_return_pressed(self):
e09c80ab 75 self._history.append(self.text().encode('utf-8'))
7dcf23b1
SH
76 self._history_index = len(self._history)
77 self.textSent.emit(self.text())
78 self.clear()
79
80 def _history_navigate(self, direction):
81 if self._history:
82 self._history_index += direction
83 if self._history_index < 0:
84 self._history_index = 0
85 return
86 if self._history_index > len(self._history) - 1:
87 self._history_index = len(self._history)
88 self.clear()
89 return
90 self.setText(self._history[self._history_index])