ExtensionIrc/IrcMessageEdit.py

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

eric ide

mercurial