|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a specialized line edit for entering IRC messages. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import Qt |
|
13 |
|
14 from E5Gui.E5LineEdit import E5LineEdit, E5ClearableLineEdit |
|
15 |
|
16 from Globals import qVersionTuple |
|
17 |
|
18 |
|
19 class IrcMessageEdit(E5ClearableLineEdit): |
|
20 """ |
|
21 Class implementing a specialized line edit for entering IRC messages. |
|
22 """ |
|
23 MaxHistory = 100 |
|
24 |
|
25 def __init__(self, parent=None, inactiveText="", |
|
26 side=E5LineEdit.RightSide): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param parent reference to the parent widget (QWidget) |
|
31 @keyparam inactiveText text to be shown on inactivity (string) |
|
32 @keyparam side side the clear button should be shown at |
|
33 (E5LineEdit.RightSide, E5LineEdit.LeftSide) |
|
34 """ |
|
35 super(IrcMessageEdit, self).__init__(parent, inactiveText, side) |
|
36 |
|
37 self.__historyList = [""] # initialize with one empty line |
|
38 self.__historyLine = 0 |
|
39 |
|
40 def setText(self, text): |
|
41 """ |
|
42 Public method to set the text. |
|
43 |
|
44 Note: This reimplementation ensures, that the cursor is at the end of |
|
45 the text. |
|
46 |
|
47 @param text text to be set (string) |
|
48 """ |
|
49 super(IrcMessageEdit, self).setText(text) |
|
50 self.setCursorPosition(len(text)) |
|
51 |
|
52 def keyPressEvent(self, evt): |
|
53 """ |
|
54 Protected method implementing special key handling. |
|
55 |
|
56 @param evt reference to the event (QKeyEvent) |
|
57 """ |
|
58 key = evt.key() |
|
59 if key == Qt.Key_Up: |
|
60 self.__getHistory(True) |
|
61 return |
|
62 elif key == Qt.Key_Down: |
|
63 self.__getHistory(False) |
|
64 return |
|
65 elif key in [Qt.Key_Return, Qt.Key_Enter]: |
|
66 if self.text(): |
|
67 self.__addHistory(self.text()) |
|
68 elif evt.text() == chr(21): |
|
69 # ^U: clear the text |
|
70 self.setText("") |
|
71 |
|
72 super(IrcMessageEdit, self).keyPressEvent(evt) |
|
73 |
|
74 def wheelEvent(self, evt): |
|
75 """ |
|
76 Protected slot to support wheel events. |
|
77 |
|
78 @param evt reference to the wheel event (QWheelEvent) |
|
79 """ |
|
80 if qVersionTuple() >= (5, 0, 0): |
|
81 delta = evt.angleDelta().y() |
|
82 else: |
|
83 delta = evt.delta() |
|
84 if delta > 0: |
|
85 self.__getHistory(True) |
|
86 elif delta < 0: |
|
87 self.__getHistory(False) |
|
88 |
|
89 super(IrcMessageEdit, self).wheelEvent(evt) |
|
90 |
|
91 def __addHistory(self, txt): |
|
92 """ |
|
93 Private method to add an entry to the history. |
|
94 |
|
95 @param txt text to be added to the history (string) |
|
96 """ |
|
97 # Only add the entry, if it is not the same as last time |
|
98 if len(self.__historyList) == 1 or \ |
|
99 (len(self.__historyList) > 1 and self.__historyList[1] != txt): |
|
100 # Replace empty first entry and add new empty first entry |
|
101 self.__historyList[0] = txt |
|
102 self.__historyList.insert(0, "") |
|
103 # Keep history below the defined limit |
|
104 del self.__historyList[IrcMessageEdit.MaxHistory:] |
|
105 |
|
106 self.__historyLine = 0 |
|
107 |
|
108 def __getHistory(self, up): |
|
109 """ |
|
110 Private method to move in the history. |
|
111 |
|
112 @param up flag indicating the direction (boolean) |
|
113 """ |
|
114 # preserve the current text, if it is not empty |
|
115 if self.text(): |
|
116 self.__historyList[self.__historyLine] = self.text() |
|
117 |
|
118 if up: |
|
119 self.__historyLine += 1 |
|
120 # If the position was moved past the end of the history, |
|
121 # go to the last entry |
|
122 if self.__historyLine == len(self.__historyList): |
|
123 self.__historyLine -= 1 |
|
124 return |
|
125 else: |
|
126 # If the position is at the top of the history, arrow-down shall |
|
127 # add the text to the history and clear the line edit for new input |
|
128 if self.__historyLine == 0: |
|
129 if self.text(): |
|
130 self.__addHistory(self.text()) |
|
131 self.setText("") |
|
132 else: |
|
133 # If the position is not at the top of the history, |
|
134 # decrement it |
|
135 self.__historyLine -= 1 |
|
136 |
|
137 # replace the text of the line edit with the selected history entry |
|
138 self.setText(self.__historyList[self.__historyLine]) |