13 |
13 |
14 class IrcMessageEdit(QLineEdit): |
14 class IrcMessageEdit(QLineEdit): |
15 """ |
15 """ |
16 Class implementing a specialized line edit for entering IRC messages. |
16 Class implementing a specialized line edit for entering IRC messages. |
17 """ |
17 """ |
|
18 |
18 MaxHistory = 100 |
19 MaxHistory = 100 |
19 |
20 |
20 def __init__(self, parent=None): |
21 def __init__(self, parent=None): |
21 """ |
22 """ |
22 Constructor |
23 Constructor |
23 |
24 |
24 @param parent reference to the parent widget |
25 @param parent reference to the parent widget |
25 @type QWidget |
26 @type QWidget |
26 """ |
27 """ |
27 super().__init__(parent) |
28 super().__init__(parent) |
28 |
29 |
29 self.__historyList = [""] # initialize with one empty line |
30 self.__historyList = [""] # initialize with one empty line |
30 self.__historyLine = 0 |
31 self.__historyLine = 0 |
31 |
32 |
32 def setText(self, text): |
33 def setText(self, text): |
33 """ |
34 """ |
34 Public method to set the text. |
35 Public method to set the text. |
35 |
36 |
36 Note: This reimplementation ensures, that the cursor is at the end of |
37 Note: This reimplementation ensures, that the cursor is at the end of |
37 the text. |
38 the text. |
38 |
39 |
39 @param text text to be set (string) |
40 @param text text to be set (string) |
40 """ |
41 """ |
41 super().setText(text) |
42 super().setText(text) |
42 self.setCursorPosition(len(text)) |
43 self.setCursorPosition(len(text)) |
43 |
44 |
44 def keyPressEvent(self, evt): |
45 def keyPressEvent(self, evt): |
45 """ |
46 """ |
46 Protected method implementing special key handling. |
47 Protected method implementing special key handling. |
47 |
48 |
48 @param evt reference to the event (QKeyEvent) |
49 @param evt reference to the event (QKeyEvent) |
49 """ |
50 """ |
50 key = evt.key() |
51 key = evt.key() |
51 if key == Qt.Key.Key_Up: |
52 if key == Qt.Key.Key_Up: |
52 self.__getHistory(True) |
53 self.__getHistory(True) |
58 if self.text(): |
59 if self.text(): |
59 self.__addHistory(self.text()) |
60 self.__addHistory(self.text()) |
60 elif evt.text() == chr(21): |
61 elif evt.text() == chr(21): |
61 # ^U: clear the text |
62 # ^U: clear the text |
62 self.setText("") |
63 self.setText("") |
63 |
64 |
64 super().keyPressEvent(evt) |
65 super().keyPressEvent(evt) |
65 |
66 |
66 def wheelEvent(self, evt): |
67 def wheelEvent(self, evt): |
67 """ |
68 """ |
68 Protected slot to support wheel events. |
69 Protected slot to support wheel events. |
69 |
70 |
70 @param evt reference to the wheel event (QWheelEvent) |
71 @param evt reference to the wheel event (QWheelEvent) |
71 """ |
72 """ |
72 delta = evt.angleDelta().y() |
73 delta = evt.angleDelta().y() |
73 if delta > 0: |
74 if delta > 0: |
74 self.__getHistory(True) |
75 self.__getHistory(True) |
75 elif delta < 0: |
76 elif delta < 0: |
76 self.__getHistory(False) |
77 self.__getHistory(False) |
77 |
78 |
78 super().wheelEvent(evt) |
79 super().wheelEvent(evt) |
79 |
80 |
80 def __addHistory(self, txt): |
81 def __addHistory(self, txt): |
81 """ |
82 """ |
82 Private method to add an entry to the history. |
83 Private method to add an entry to the history. |
83 |
84 |
84 @param txt text to be added to the history (string) |
85 @param txt text to be added to the history (string) |
85 """ |
86 """ |
86 # Only add the entry, if it is not the same as last time |
87 # Only add the entry, if it is not the same as last time |
87 if ( |
88 if len(self.__historyList) == 1 or ( |
88 len(self.__historyList) == 1 or |
89 len(self.__historyList) > 1 and self.__historyList[1] != txt |
89 (len(self.__historyList) > 1 and self.__historyList[1] != txt) |
|
90 ): |
90 ): |
91 # Replace empty first entry and add new empty first entry |
91 # Replace empty first entry and add new empty first entry |
92 self.__historyList[0] = txt |
92 self.__historyList[0] = txt |
93 self.__historyList.insert(0, "") |
93 self.__historyList.insert(0, "") |
94 # Keep history below the defined limit |
94 # Keep history below the defined limit |
95 del self.__historyList[IrcMessageEdit.MaxHistory:] |
95 del self.__historyList[IrcMessageEdit.MaxHistory :] |
96 |
96 |
97 self.__historyLine = 0 |
97 self.__historyLine = 0 |
98 |
98 |
99 def __getHistory(self, up): |
99 def __getHistory(self, up): |
100 """ |
100 """ |
101 Private method to move in the history. |
101 Private method to move in the history. |
102 |
102 |
103 @param up flag indicating the direction (boolean) |
103 @param up flag indicating the direction (boolean) |
104 """ |
104 """ |
105 # preserve the current text, if it is not empty |
105 # preserve the current text, if it is not empty |
106 if self.text(): |
106 if self.text(): |
107 self.__historyList[self.__historyLine] = self.text() |
107 self.__historyList[self.__historyLine] = self.text() |
108 |
108 |
109 if up: |
109 if up: |
110 self.__historyLine += 1 |
110 self.__historyLine += 1 |
111 # If the position was moved past the end of the history, |
111 # If the position was moved past the end of the history, |
112 # go to the last entry |
112 # go to the last entry |
113 if self.__historyLine == len(self.__historyList): |
113 if self.__historyLine == len(self.__historyList): |