comparison python/ide/hexedit.py @ 343:11c5a8a70c02 devel

Fix ide
author Windel Bouwman
date Sat, 01 Mar 2014 16:27:52 +0100
parents dcae6574c974
children
comparison
equal deleted inserted replaced
342:86b02c98a717 343:11c5a8a70c02
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import sys 3 import sys
4 import os 4 from qtwrapper import QtGui, QtCore, QtWidgets, Qt, abspath, uic
5 from qtwrapper import QtGui, QtCore, QtWidgets, Qt
6 5
7 6
8 BYTES_PER_LINE, GAP = 8, 12 7 BYTES_PER_LINE, GAP = 8, 12
9 8
10 def clamp(minimum, x, maximum): 9 def clamp(minimum, x, maximum):
11 return max(minimum, min(x, maximum)) 10 return max(minimum, min(x, maximum))
12 11
13 def asciiChar(v): 12 def asciiChar(v):
14 if v < 0x20 or v > 0x7e: 13 if v < 0x20 or v > 0x7e:
15 return '.' 14 return '.'
16 else: 15 else:
17 return chr(v) 16 return chr(v)
18 17
19 class BinViewer(QtWidgets.QWidget): 18 class BinViewer(QtWidgets.QWidget):
20 """ The view has an address, hex byte and ascii column """ 19 """ The view has an address, hex byte and ascii column """
21 def __init__(self, scrollArea): 20 def __init__(self, scrollArea):
22 super().__init__(scrollArea) 21 super().__init__(scrollArea)
97 # cursor 96 # cursor
98 if self.blinkcursor: 97 if self.blinkcursor:
99 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black) 98 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black)
100 99
101 def keyPressEvent(self, event): 100 def keyPressEvent(self, event):
102 if event.matches(QKeySequence.MoveToNextChar): 101 if event.matches(QtGui.QKeySequence.MoveToNextChar):
103 self.CursorPosition += 1 102 self.CursorPosition += 1
104 if event.matches(QKeySequence.MoveToPreviousChar): 103 if event.matches(QtGui.QKeySequence.MoveToPreviousChar):
105 self.CursorPosition -= 1 104 self.CursorPosition -= 1
106 if event.matches(QKeySequence.MoveToNextLine): 105 if event.matches(QtGui.QKeySequence.MoveToNextLine):
107 self.CursorPosition += 2 * BYTES_PER_LINE 106 self.CursorPosition += 2 * BYTES_PER_LINE
108 if event.matches(QKeySequence.MoveToPreviousLine): 107 if event.matches(QtGui.QKeySequence.MoveToPreviousLine):
109 self.CursorPosition -= 2 * BYTES_PER_LINE 108 self.CursorPosition -= 2 * BYTES_PER_LINE
110 if event.matches(QKeySequence.MoveToNextPage): 109 if event.matches(QtGui.QKeySequence.MoveToNextPage):
111 rows = int(self.scrollArea.viewport().height() / self.charHeight) 110 rows = int(self.scrollArea.viewport().height() / self.charHeight)
112 self.CursorPosition += (rows - 1) * 2 * BYTES_PER_LINE 111 self.CursorPosition += (rows - 1) * 2 * BYTES_PER_LINE
113 if event.matches(QKeySequence.MoveToPreviousPage): 112 if event.matches(QtGui.QKeySequence.MoveToPreviousPage):
114 rows = int(self.scrollArea.viewport().height() / self.charHeight) 113 rows = int(self.scrollArea.viewport().height() / self.charHeight)
115 self.CursorPosition -= (rows - 1) * 2 * BYTES_PER_LINE 114 self.CursorPosition -= (rows - 1) * 2 * BYTES_PER_LINE
116 char = event.text().lower() 115 char = event.text().lower()
117 if char and char in '0123456789abcdef': 116 if char and char in '0123456789abcdef':
118 i = int(self.CursorPosition / 2) 117 i = int(self.CursorPosition / 2)
175 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) 174 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
176 self.setFocusPolicy(Qt.NoFocus) 175 self.setFocusPolicy(Qt.NoFocus)
177 176
178 177
179 class HexEditor(QtWidgets.QMainWindow): 178 class HexEditor(QtWidgets.QMainWindow):
180 def __init__(self): 179 def __init__(self):
181 super().__init__() 180 super().__init__()
182 basedir = os.path.dirname(__file__) 181 uic.loadUi(abspath('hexeditor.ui'), baseinstance=self)
183 uic.loadUi(os.path.join(basedir, 'hexeditor.ui'), baseinstance=self)
184 self.he = HexEdit() 182 self.he = HexEdit()
185 self.setCentralWidget(self.he) 183 self.setCentralWidget(self.he)
186 self.actionOpen.triggered.connect(self.doOpen) 184 self.actionOpen.triggered.connect(self.doOpen)
187 self.actionSave.triggered.connect(self.doSave) 185 self.actionSave.triggered.connect(self.doSave)
188 self.actionSaveAs.triggered.connect(self.doSaveAs) 186 self.actionSaveAs.triggered.connect(self.doSaveAs)
189 self.fileName = None 187 self.fileName = None
190 self.updateControls() 188 self.updateControls()
191 def updateControls(self): 189
190 def updateControls(self):
192 s = True if self.fileName else False 191 s = True if self.fileName else False
193 self.actionSave.setEnabled(s) 192 self.actionSave.setEnabled(s)
194 self.actionSaveAs.setEnabled(s) 193 self.actionSaveAs.setEnabled(s)
195 def doOpen(self): 194
196 filename = QFileDialog.getOpenFileName(self) 195 def doOpen(self):
196 filename = QtWidgets.QFileDialog.getOpenFileName(self)
197 if filename: 197 if filename:
198 with open(filename, 'rb') as f: 198 with open(filename, 'rb') as f:
199 self.he.bv.Data = f.read() 199 self.he.bv.Data = f.read()
200 self.fileName = filename 200 self.fileName = filename
201 self.updateControls() 201 self.updateControls()
202 def doSave(self): 202
203 self.updateControls() 203 def doSave(self):
204 def doSaveAs(self): 204 self.updateControls()
205
206 def doSaveAs(self):
205 filename = QFileDialog.getSaveFileName(self) 207 filename = QFileDialog.getSaveFileName(self)
206 if filename: 208 if filename:
207 with open(filename, 'wb') as f: 209 with open(filename, 'wb') as f:
208 f.write(self.he.bv.Data) 210 f.write(self.he.bv.Data)
209 self.fileName = filename 211 self.fileName = filename
210 self.updateControls() 212 self.updateControls()
211 213
212 214
213 if __name__ == '__main__': 215 if __name__ == '__main__':
214 app = QApplication(sys.argv) 216 app = QtWidgets.QApplication(sys.argv)
215 he = HexEditor() 217 he = HexEditor()
216 he.show() 218 he.show()
217 #he.bv.Data = bytearray(range(100)) * 8 + b'x' 219 #he.bv.Data = bytearray(range(100)) * 8 + b'x'
218 app.exec() 220 app.exec()