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