annotate python/hexviewer.py @ 133:a816c683c1c0

Fixes in hexedit
author Windel Bouwman
date Sun, 20 Jan 2013 21:48:41 +0100
parents 205578c96a79
children
rev   line source
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
1 #!/usr/bin/python
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
2
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
3 from PyQt4.QtCore import *
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
4 from PyQt4.QtGui import *
109
ad14c7c52589 Created qt property viewer
Windel Bouwman
parents: 105
diff changeset
5 from qtpropertyviewer import QtPropertyViewer
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
6 import sys
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
7 import hexfile
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
8
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
9
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
10 class HexFileModel(QAbstractTableModel):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
11 def __init__(self):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
12 super().__init__()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
13 self.hexFile = None
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
14 def setHexFile(self, hf):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
15 self.hexFile = hf
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
16 self.modelReset.emit()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
17 def getHexFile(self):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
18 return self.hexFile
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
19 HexFile = property(getHexFile, setHexFile)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
20 def rowCount(self, parent):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
21 if self.hexFile:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
22 region = self.hexFile.regions[-1]
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
23 r = len(region.data)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
24 s = r >> 4
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
25 if r % 16 != 0:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
26 s += 1
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
27 return s
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
28 return 0
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
29 def columnCount(self, parent):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
30 return 16 + 1
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
31 def headerData(self, section, orientation, role):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
32 if role == Qt.DisplayRole:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
33 if orientation == Qt.Horizontal:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
34 if section in range(16):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
35 return '{0:X}'.format(section)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
36 elif section == 16:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
37 return 'Ascii'
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
38 elif orientation == Qt.Vertical:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
39 region = self.hexFile.regions[-1]
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
40 addr = region.address + 16 * section
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
41 return '0x{0:X}'.format(addr)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
42 def data(self, index, role):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
43 if index.isValid():
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
44 row = index.row()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
45 col = index.column()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
46 region = self.hexFile.regions[-1]
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
47 chunk = region.data[row * 16: row * 16 + 16]
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
48
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
49 if role == Qt.DisplayRole:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
50 if col in range(16):
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
51 return '{0:02X}'.format(chunk[col])
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
52 else:
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
53 s = chunk.decode(encoding='ascii', errors='replace')
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
54 return s
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
55
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
56 class BinViewMain(QMainWindow):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
57 def __init__(self):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
58 super().__init__()
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
59 self.bv = BinViewer()
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
60 #self.setCentralWidget(self.bv)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
61 tableView = QTableView()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
62 self.setCentralWidget(tableView)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
63 self.hfm = HexFileModel()
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
64 self.hfm.modelReset.connect(tableView.resizeColumnsToContents)
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
65 tableView.setModel(self.hfm)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
66 mb = self.menuBar()
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
67 fileMenu = mb.addMenu("File")
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
68
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
69 def addMenuEntry(name, menu, callback, shortcut=None):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
70 a = QAction(name, self)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
71 menu.addAction(a)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
72 a.triggered.connect(callback)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
73 if shortcut: a.setShortcut(shortcut)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
74 addMenuEntry("Open", fileMenu, self.openFile, QKeySequence(QKeySequence.Open))
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
75 def openFile(self):
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
76 filename = QFileDialog.getOpenFileName(self, "Open hex file...", "*.hex", "Intel hexfiles (*.hex)")
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
77 if filename:
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
78 h = hexfile.HexFile(filename)
109
ad14c7c52589 Created qt property viewer
Windel Bouwman
parents: 105
diff changeset
79 @pyqtProperty(str)
ad14c7c52589 Created qt property viewer
Windel Bouwman
parents: 105
diff changeset
80 def leetValue(self):
ad14c7c52589 Created qt property viewer
Windel Bouwman
parents: 105
diff changeset
81 return '1337'
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
82
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
83 if __name__ == '__main__':
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
84 app = QApplication(sys.argv)
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
85 bv = BinViewMain()
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents: 124
diff changeset
86 #bv.show()
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
87 hf = hexfile.HexFile('audio.hex')
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
88 #bv.bv.setHexFile(
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
89 bv.hfm.HexFile = hf
109
ad14c7c52589 Created qt property viewer
Windel Bouwman
parents: 105
diff changeset
90 qpv = QtPropertyViewer()
ad14c7c52589 Created qt property viewer
Windel Bouwman
parents: 105
diff changeset
91 qpv.propertyModel.InspectedWidget = bv
124
d38729d35c4d New implementation of hexfile viewer
Windel Bouwman
parents: 109
diff changeset
92 #qpv.show()
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents: 124
diff changeset
93 bv = BinViewer()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents: 124
diff changeset
94 bv.show()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents: 124
diff changeset
95 bv.setHexFile(hf)
104
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
96 app.exec_()
ed230e947dc6 Added hexviewer
windel
parents:
diff changeset
97