src/eric7/HexEdit/HexEditGotoWidget.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
19 19
20 class HexEditGotoWidget(QWidget, Ui_HexEditGotoWidget): 20 class HexEditGotoWidget(QWidget, Ui_HexEditGotoWidget):
21 """ 21 """
22 Class implementing a movement (goto) widget for the hex editor. 22 Class implementing a movement (goto) widget for the hex editor.
23 """ 23 """
24
24 def __init__(self, editor, parent=None): 25 def __init__(self, editor, parent=None):
25 """ 26 """
26 Constructor 27 Constructor
27 28
28 @param editor reference to the hex editor widget 29 @param editor reference to the hex editor widget
29 @type HexEditWidget 30 @type HexEditWidget
30 @param parent reference to the parent widget 31 @param parent reference to the parent widget
31 @type QWidget 32 @type QWidget
32 """ 33 """
33 super().__init__(parent) 34 super().__init__(parent)
34 self.setupUi(self) 35 self.setupUi(self)
35 36
36 self.__editor = editor 37 self.__editor = editor
37 38
38 # keep this in sync with the logic in on_gotoButton_clicked() 39 # keep this in sync with the logic in on_gotoButton_clicked()
39 self.__formatAndValidators = { 40 self.__formatAndValidators = {
40 "hex": (self.tr("Hex"), QRegularExpressionValidator( 41 "hex": (
41 QRegularExpression("[0-9a-f:]*"))), 42 self.tr("Hex"),
42 "dec": (self.tr("Dec"), QRegularExpressionValidator( 43 QRegularExpressionValidator(QRegularExpression("[0-9a-f:]*")),
43 QRegularExpression("[0-9]*"))), 44 ),
45 "dec": (
46 self.tr("Dec"),
47 QRegularExpressionValidator(QRegularExpression("[0-9]*")),
48 ),
44 } 49 }
45 formatOrder = ["hex", "dec"] 50 formatOrder = ["hex", "dec"]
46 51
47 self.__currentFormat = "" 52 self.__currentFormat = ""
48 53
49 self.closeButton.setIcon(UI.PixmapCache.getIcon("close")) 54 self.closeButton.setIcon(UI.PixmapCache.getIcon("close"))
50 55
51 for dataFormat in formatOrder: 56 for dataFormat in formatOrder:
52 formatStr, validator = self.__formatAndValidators[dataFormat] 57 formatStr, validator = self.__formatAndValidators[dataFormat]
53 self.formatCombo.addItem(formatStr, dataFormat) 58 self.formatCombo.addItem(formatStr, dataFormat)
54 59
55 self.formatCombo.setCurrentIndex(0) 60 self.formatCombo.setCurrentIndex(0)
56 61
57 @pyqtSlot() 62 @pyqtSlot()
58 def on_closeButton_clicked(self): 63 def on_closeButton_clicked(self):
59 """ 64 """
60 Private slot to close the widget. 65 Private slot to close the widget.
61 """ 66 """
62 self.__editor.setFocus(Qt.FocusReason.OtherFocusReason) 67 self.__editor.setFocus(Qt.FocusReason.OtherFocusReason)
63 self.close() 68 self.close()
64 69
65 @pyqtSlot(int) 70 @pyqtSlot(int)
66 def on_formatCombo_currentIndexChanged(self, idx): 71 def on_formatCombo_currentIndexChanged(self, idx):
67 """ 72 """
68 Private slot to handle a selection of the format. 73 Private slot to handle a selection of the format.
69 74
70 @param idx index of the selected entry 75 @param idx index of the selected entry
71 @type int 76 @type int
72 """ 77 """
73 if idx >= 0: 78 if idx >= 0:
74 dataFormat = self.formatCombo.itemData(idx) 79 dataFormat = self.formatCombo.itemData(idx)
75 80
76 if dataFormat != self.__currentFormat: 81 if dataFormat != self.__currentFormat:
77 txt = self.offsetEdit.text() 82 txt = self.offsetEdit.text()
78 newTxt = self.__convertText( 83 newTxt = self.__convertText(txt, self.__currentFormat, dataFormat)
79 txt, self.__currentFormat, dataFormat)
80 self.__currentFormat = dataFormat 84 self.__currentFormat = dataFormat
81 85
82 self.offsetEdit.setValidator( 86 self.offsetEdit.setValidator(self.__formatAndValidators[dataFormat][1])
83 self.__formatAndValidators[dataFormat][1]) 87
84
85 self.offsetEdit.setText(newTxt) 88 self.offsetEdit.setText(newTxt)
86 89
87 @pyqtSlot(str) 90 @pyqtSlot(str)
88 def on_offsetEdit_textChanged(self, offset): 91 def on_offsetEdit_textChanged(self, offset):
89 """ 92 """
90 Private slot handling a change of the entered offset. 93 Private slot handling a change of the entered offset.
91 94
92 @param offset entered offset 95 @param offset entered offset
93 @type str 96 @type str
94 """ 97 """
95 self.gotoButton.setEnabled(bool(offset)) 98 self.gotoButton.setEnabled(bool(offset))
96 99
97 @pyqtSlot() 100 @pyqtSlot()
98 def on_gotoButton_clicked(self): 101 def on_gotoButton_clicked(self):
99 """ 102 """
100 Private slot to move the cursor and extend the selection. 103 Private slot to move the cursor and extend the selection.
101 """ 104 """
104 offset = self.offsetEdit.text().replace(":", "") 107 offset = self.offsetEdit.text().replace(":", "")
105 # get rid of ':' address separators 108 # get rid of ':' address separators
106 offset = int(offset, 16) 109 offset = int(offset, 16)
107 else: 110 else:
108 offset = int(self.offsetEdit.text(), 10) 111 offset = int(self.offsetEdit.text(), 10)
109 112
110 fromCursor = self.cursorCheckBox.isChecked() 113 fromCursor = self.cursorCheckBox.isChecked()
111 backwards = self.backCheckBox.isChecked() 114 backwards = self.backCheckBox.isChecked()
112 extendSelection = self.selectionCheckBox.isChecked() 115 extendSelection = self.selectionCheckBox.isChecked()
113 116
114 self.__editor.goto(offset, fromCursor=fromCursor, backwards=backwards, 117 self.__editor.goto(
115 extendSelection=extendSelection) 118 offset,
116 119 fromCursor=fromCursor,
120 backwards=backwards,
121 extendSelection=extendSelection,
122 )
123
117 def show(self): 124 def show(self):
118 """ 125 """
119 Public slot to show the widget. 126 Public slot to show the widget.
120 """ 127 """
121 self.offsetEdit.selectAll() 128 self.offsetEdit.selectAll()
122 self.offsetEdit.setFocus() 129 self.offsetEdit.setFocus()
123 super().show() 130 super().show()
124 131
125 def reset(self): 132 def reset(self):
126 """ 133 """
127 Public slot to reset the input widgets. 134 Public slot to reset the input widgets.
128 """ 135 """
129 self.offsetEdit.clear() 136 self.offsetEdit.clear()
133 self.selectionCheckBox.setChecked(False) 140 self.selectionCheckBox.setChecked(False)
134 141
135 def keyPressEvent(self, event): 142 def keyPressEvent(self, event):
136 """ 143 """
137 Protected slot to handle key press events. 144 Protected slot to handle key press events.
138 145
139 @param event reference to the key press event 146 @param event reference to the key press event
140 @type QKeyEvent 147 @type QKeyEvent
141 """ 148 """
142 if event.key() == Qt.Key.Key_Escape: 149 if event.key() == Qt.Key.Key_Escape:
143 self.close() 150 self.close()
144 151
145 def __convertText(self, txt, oldFormat, newFormat): 152 def __convertText(self, txt, oldFormat, newFormat):
146 """ 153 """
147 Private method to convert text from one format into another. 154 Private method to convert text from one format into another.
148 155
149 @param txt text to be converted 156 @param txt text to be converted
150 @type str 157 @type str
151 @param oldFormat current format of the text 158 @param oldFormat current format of the text
152 @type str 159 @type str
153 @param newFormat format to convert to 160 @param newFormat format to convert to
160 if oldFormat == "hex": 167 if oldFormat == "hex":
161 txt = txt.replace(":", "") # get rid of ':' address separators 168 txt = txt.replace(":", "") # get rid of ':' address separators
162 index = int(txt, 16) 169 index = int(txt, 16)
163 else: 170 else:
164 index = int(txt, 10) 171 index = int(txt, 10)
165 172
166 # step 2: convert the integer to text using the new format 173 # step 2: convert the integer to text using the new format
167 if newFormat == "hex": 174 if newFormat == "hex":
168 txt = "{0:x}".format(index) 175 txt = "{0:x}".format(index)
169 txt = Globals.strGroup(txt, ":", 4) 176 txt = Globals.strGroup(txt, ":", 4)
170 else: 177 else:
171 txt = "{0:d}".format(index) 178 txt = "{0:d}".format(index)
172 179
173 return txt 180 return txt

eric ide

mercurial