HexEdit/HexEditGotoWidget.py

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

eric ide

mercurial