HexEdit/HexEditGotoWidget.py

Sun, 17 Jan 2016 11:23:17 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 17 Jan 2016 11:23:17 +0100
changeset 4670
d401ba329d24
child 4687
f1d921533cc5
permissions
-rw-r--r--

Added 'Goto' functionality to the hex editor.

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

eric ide

mercurial