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