annotate python/ide/hexedit.py @ 354:5477e499b039

Added some sort of string functionality
author Windel Bouwman
date Thu, 13 Mar 2014 18:59:06 +0100
parents 11c5a8a70c02
children
rev   line source
142
982ddb5f786d Added shbang string
Windel Bouwman
parents: 140
diff changeset
1 #!/usr/bin/python
982ddb5f786d Added shbang string
Windel Bouwman
parents: 140
diff changeset
2
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
3 import sys
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
4 from qtwrapper import QtGui, QtCore, QtWidgets, Qt, abspath, uic
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
5
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
6
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 145
diff changeset
7 BYTES_PER_LINE, GAP = 8, 12
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
8
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
9 def clamp(minimum, x, maximum):
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
10 return max(minimum, min(x, maximum))
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
11
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
12 def asciiChar(v):
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
13 if v < 0x20 or v > 0x7e:
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
14 return '.'
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
15 else:
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
16 return chr(v)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
17
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
18 class BinViewer(QtWidgets.QWidget):
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
19 """ The view has an address, hex byte and ascii column """
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
20 def __init__(self, scrollArea):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
21 super().__init__(scrollArea)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
22 self.scrollArea = scrollArea
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
23 self.setFont(QtGui.QFont('Courier', 16))
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
24 self.setFocusPolicy(Qt.StrongFocus)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
25 self.blinkcursor = False
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
26 self.cursorX = self.cursorY = 0
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
27 self.scrollArea = scrollArea
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
28 self.Data = bytearray()
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
29 self.Offset = 0
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
30 t = QtCore.QTimer(self)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
31 t.timeout.connect(self.updateCursor)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
32 t.setInterval(500)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
33 t.start()
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
34
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
35 def updateCursor(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
36 self.blinkcursor = not self.blinkcursor
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
37 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
38
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
39 def setCursorPosition(self, position):
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
40 position = clamp(0, int(position), len(self.Data) * 2 - 1)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
41 self.cursorPosition = position
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
42 x = position % (2 * BYTES_PER_LINE)
137
0a540ce31cd5 Added debug toolbar and spaced hexedit
Windel Bouwman
parents: 136
diff changeset
43 x = x + int(x / 2) # Create a gap between hex values
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
44 self.cursorX = self.xposHex + x * self.charWidth
137
0a540ce31cd5 Added debug toolbar and spaced hexedit
Windel Bouwman
parents: 136
diff changeset
45 y = int(position / (2 * BYTES_PER_LINE))
0a540ce31cd5 Added debug toolbar and spaced hexedit
Windel Bouwman
parents: 136
diff changeset
46 self.cursorY = y * self.charHeight + 2
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
47 self.blinkcursor = True
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
48 self.update()
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
49
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
50 def getCursorPosition(self):
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
51 return self.cursorPosition
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
52 CursorPosition = property(getCursorPosition, setCursorPosition)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
53 def setOffset(self, off):
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
54 self.offset = off
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
55 self.update()
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
56
139
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
57 Offset = property(lambda self: self.offset, setOffset)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
58 def paintEvent(self, event):
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
59 # Helper variables:
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
60 er = event.rect()
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
61 chw, chh = self.charWidth, self.charHeight
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
62 painter = QtGui.QPainter(self)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
63 # Background:
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
64 painter.fillRect(er, self.palette().color(QtGui.QPalette.Base))
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
65 painter.fillRect(QtCore.QRect(self.xposAddr, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
66 painter.setPen(Qt.gray)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
67 x = self.xposAscii - (GAP / 2)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
68 painter.drawLine(x, er.top(), x, er.bottom())
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
69 x = self.xposEnd - (GAP / 2)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
70 painter.drawLine(x, er.top(), x, er.bottom())
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
71 # first and last index
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
72 firstIndex = max((int(er.top() / chh) - chh) * BYTES_PER_LINE, 0)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
73 lastIndex = max((int(er.bottom() / chh) + chh) * BYTES_PER_LINE, 0)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
74 yposStart = int(firstIndex / BYTES_PER_LINE) * chh + chh
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
75 # Draw contents:
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
76 painter.setPen(Qt.black)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
77 ypos = yposStart
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
78 for index in range(firstIndex, lastIndex, BYTES_PER_LINE):
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
79 painter.setPen(Qt.black)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
80 painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index + self.Offset))
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
81 xpos = self.xposHex
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
82 xposAscii = self.xposAscii
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
83 for colIndex in range(BYTES_PER_LINE):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
84 if index + colIndex < len(self.Data):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
85 b = self.Data[index + colIndex]
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
86 bo = self.originalData[index + colIndex]
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
87 if b == bo:
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
88 painter.setPen(Qt.black)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
89 else:
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
90 painter.setPen(Qt.red)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
91 painter.drawText(xpos, ypos, '{0:02X}'.format(b))
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
92 painter.drawText(xposAscii, ypos, asciiChar(b))
137
0a540ce31cd5 Added debug toolbar and spaced hexedit
Windel Bouwman
parents: 136
diff changeset
93 xpos += 3 * chw
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
94 xposAscii += chw
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
95 ypos += chh
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
96 # cursor
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
97 if self.blinkcursor:
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
98 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black)
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
99
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
100 def keyPressEvent(self, event):
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
101 if event.matches(QtGui.QKeySequence.MoveToNextChar):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
102 self.CursorPosition += 1
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
103 if event.matches(QtGui.QKeySequence.MoveToPreviousChar):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
104 self.CursorPosition -= 1
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
105 if event.matches(QtGui.QKeySequence.MoveToNextLine):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
106 self.CursorPosition += 2 * BYTES_PER_LINE
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
107 if event.matches(QtGui.QKeySequence.MoveToPreviousLine):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
108 self.CursorPosition -= 2 * BYTES_PER_LINE
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
109 if event.matches(QtGui.QKeySequence.MoveToNextPage):
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
110 rows = int(self.scrollArea.viewport().height() / self.charHeight)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
111 self.CursorPosition += (rows - 1) * 2 * BYTES_PER_LINE
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
112 if event.matches(QtGui.QKeySequence.MoveToPreviousPage):
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
113 rows = int(self.scrollArea.viewport().height() / self.charHeight)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
114 self.CursorPosition -= (rows - 1) * 2 * BYTES_PER_LINE
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
115 char = event.text().lower()
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
116 if char and char in '0123456789abcdef':
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
117 i = int(self.CursorPosition / 2)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
118 hb = self.CursorPosition % 2
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
119 v = int(char, 16)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
120 if hb == 0:
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
121 # high half byte
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
122 self.data[i] = (self.data[i] & 0xF) | (v << 4)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
123 else:
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
124 self.data[i] = (self.data[i] & 0xF0) | v
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
125 self.CursorPosition += 1
137
0a540ce31cd5 Added debug toolbar and spaced hexedit
Windel Bouwman
parents: 136
diff changeset
126 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4)
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
127 self.update()
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
128
139
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
129 def setCursorPositionAt(self, pos):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
130 """ Calculate cursor position at a certain point """
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
131 if pos.x() > self.xposHex and pos.x() < self.xposAscii:
139
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
132 x = round((2 * (pos.x() - self.xposHex)) / (self.charWidth * 3))
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
133 y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE
139
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
134 self.setCursorPosition(x + y)
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
135
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
136 def mousePressEvent(self, event):
139
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
137 self.setCursorPositionAt(event.pos())
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
138
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
139 def adjust(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
140 self.charHeight = self.fontMetrics().height()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
141 self.charWidth = self.fontMetrics().width('x')
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
142 self.xposAddr = GAP
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
143 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
137
0a540ce31cd5 Added debug toolbar and spaced hexedit
Windel Bouwman
parents: 136
diff changeset
144 self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
145 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
146 self.setMinimumWidth(self.xposEnd)
145
c101826ffe2b Fixed scrollbar width
Windel Bouwman
parents: 142
diff changeset
147 if self.isVisible():
c101826ffe2b Fixed scrollbar width
Windel Bouwman
parents: 142
diff changeset
148 sbw = self.scrollArea.verticalScrollBar().width()
c101826ffe2b Fixed scrollbar width
Windel Bouwman
parents: 142
diff changeset
149 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5)
139
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
150 r = len(self.Data) % BYTES_PER_LINE
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
151 r = 1 if r > 0 else 0
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
152 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + r) * self.charHeight + 4)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
153 self.scrollArea.setMinimumHeight(self.charHeight * 8)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
154 self.update()
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
155
145
c101826ffe2b Fixed scrollbar width
Windel Bouwman
parents: 142
diff changeset
156 def showEvent(self, e):
c101826ffe2b Fixed scrollbar width
Windel Bouwman
parents: 142
diff changeset
157 self.adjust()
c101826ffe2b Fixed scrollbar width
Windel Bouwman
parents: 142
diff changeset
158 super().showEvent(e)
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
159
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
160 def setData(self, d):
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
161 self.data = bytearray(d)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
162 self.originalData = bytearray(d)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
163 self.adjust()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
164 self.setCursorPosition(0)
139
2ec4d4332b7a Improve hexedit
Windel Bouwman
parents: 137
diff changeset
165 Data = property(lambda self: self.data, setData)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
166
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
167
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
168 class HexEdit(QtWidgets.QScrollArea):
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
169 def __init__(self):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
170 super().__init__()
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
171 self.bv = BinViewer(self)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
172 self.setWidget(self.bv)
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
173 self.setWidgetResizable(True)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
174 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
175 self.setFocusPolicy(Qt.NoFocus)
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
176
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
177
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
178 class HexEditor(QtWidgets.QMainWindow):
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
179 def __init__(self):
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
180 super().__init__()
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
181 uic.loadUi(abspath('hexeditor.ui'), baseinstance=self)
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
182 self.he = HexEdit()
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
183 self.setCentralWidget(self.he)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
184 self.actionOpen.triggered.connect(self.doOpen)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
185 self.actionSave.triggered.connect(self.doSave)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
186 self.actionSaveAs.triggered.connect(self.doSaveAs)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
187 self.fileName = None
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
188 self.updateControls()
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
189
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
190 def updateControls(self):
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
191 s = True if self.fileName else False
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
192 self.actionSave.setEnabled(s)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
193 self.actionSaveAs.setEnabled(s)
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
194
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
195 def doOpen(self):
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
196 filename = QtWidgets.QFileDialog.getOpenFileName(self)
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
197 if filename:
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
198 with open(filename, 'rb') as f:
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
199 self.he.bv.Data = f.read()
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
200 self.fileName = filename
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
201 self.updateControls()
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
202
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
203 def doSave(self):
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
204 self.updateControls()
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
205
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
206 def doSaveAs(self):
140
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
207 filename = QFileDialog.getSaveFileName(self)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
208 if filename:
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
209 with open(filename, 'wb') as f:
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
210 f.write(self.he.bv.Data)
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
211 self.fileName = filename
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
212 self.updateControls()
104037b292cc Added ui file for hexeditor
Windel Bouwman
parents: 139
diff changeset
213
333
dcae6574c974 Increment to qt5
Windel Bouwman
parents: 290
diff changeset
214
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
215 if __name__ == '__main__':
343
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
216 app = QtWidgets.QApplication(sys.argv)
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
217 he = HexEditor()
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
218 he.show()
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
219 #he.bv.Data = bytearray(range(100)) * 8 + b'x'
11c5a8a70c02 Fix ide
Windel Bouwman
parents: 333
diff changeset
220 app.exec()