eric7/Network/IRC/IrcMessageEdit.py

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

eric ide

mercurial