src/eric7/HexEdit/HexEditGotoWidget.py

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

eric ide

mercurial