Mercurial > lcfOS
comparison python/hexedit.py @ 139:2ec4d4332b7a
Improve hexedit
author | Windel Bouwman |
---|---|
date | Sat, 26 Jan 2013 19:44:36 +0100 |
parents | 0a540ce31cd5 |
children | 104037b292cc |
comparison
equal
deleted
inserted
replaced
138:14e739ed03ab | 139:2ec4d4332b7a |
---|---|
1 import sys | 1 import sys |
2 from PyQt4.QtCore import * | 2 from PyQt4.QtCore import * |
3 from PyQt4.QtGui import * | 3 from PyQt4.QtGui import * |
4 | 4 |
5 BYTES_PER_LINE = 8 | 5 BYTES_PER_LINE, GAP = 8, 12 |
6 GAP = 12 | |
7 | 6 |
8 def clamp(minimum, x, maximum): | 7 def clamp(minimum, x, maximum): |
9 return max(minimum, min(x, maximum)) | 8 return max(minimum, min(x, maximum)) |
10 | 9 |
11 def asciiChar(v): | 10 def asciiChar(v): |
44 self.blinkcursor = True | 43 self.blinkcursor = True |
45 self.update() | 44 self.update() |
46 def getCursorPosition(self): | 45 def getCursorPosition(self): |
47 return self.cursorPosition | 46 return self.cursorPosition |
48 CursorPosition = property(getCursorPosition, setCursorPosition) | 47 CursorPosition = property(getCursorPosition, setCursorPosition) |
49 def getOffset(self): | |
50 return self.offset | |
51 def setOffset(self, off): | 48 def setOffset(self, off): |
52 self.offset = off | 49 self.offset = off |
53 self.update() | 50 self.update() |
54 Offset = property(getOffset, setOffset) | 51 Offset = property(lambda self: self.offset, setOffset) |
55 def paintEvent(self, event): | 52 def paintEvent(self, event): |
56 # Helper variables: | 53 # Helper variables: |
57 er = event.rect() | 54 er = event.rect() |
58 chw, chh = self.charWidth, self.charHeight | 55 chw, chh = self.charWidth, self.charHeight |
59 painter = QPainter(self) | 56 painter = QPainter(self) |
113 else: | 110 else: |
114 self.data[i] = (self.data[i] & 0xF0) | v | 111 self.data[i] = (self.data[i] & 0xF0) | v |
115 self.CursorPosition += 1 | 112 self.CursorPosition += 1 |
116 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4) | 113 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4) |
117 self.update() | 114 self.update() |
118 def cursorPositionAt(self, pos): | 115 def setCursorPositionAt(self, pos): |
119 """ Calculate cursor position at a certain point """ | 116 """ Calculate cursor position at a certain point """ |
120 if pos.x() > self.xposHex and pos.x() < self.xposAscii: | 117 if pos.x() > self.xposHex and pos.x() < self.xposAscii: |
121 x = (pos.x() - self.xposHex) / self.charWidth | 118 x = round((2 * (pos.x() - self.xposHex)) / (self.charWidth * 3)) |
122 y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE | 119 y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE |
123 return x + y | 120 self.setCursorPosition(x + y) |
124 return 0 | |
125 def mousePressEvent(self, event): | 121 def mousePressEvent(self, event): |
126 cpos = self.cursorPositionAt(event.pos()) | 122 self.setCursorPositionAt(event.pos()) |
127 self.setCursorPosition(cpos) | |
128 def adjust(self): | 123 def adjust(self): |
129 self.charHeight = self.fontMetrics().height() | 124 self.charHeight = self.fontMetrics().height() |
130 self.charWidth = self.fontMetrics().width('x') | 125 self.charWidth = self.fontMetrics().width('x') |
131 self.xposAddr = GAP | 126 self.xposAddr = GAP |
132 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP | 127 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP |
133 self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP | 128 self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP |
134 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP | 129 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP |
135 self.setMinimumWidth(self.xposEnd) | 130 self.setMinimumWidth(self.xposEnd) |
136 sbw = self.scrollArea.verticalScrollBar().width() | 131 sbw = self.scrollArea.verticalScrollBar().width() |
137 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5) | 132 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5) |
138 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight) | 133 r = len(self.Data) % BYTES_PER_LINE |
134 r = 1 if r > 0 else 0 | |
135 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + r) * self.charHeight + 4) | |
139 self.scrollArea.setMinimumHeight(self.charHeight * 8) | 136 self.scrollArea.setMinimumHeight(self.charHeight * 8) |
140 self.update() | 137 self.update() |
141 def getData(self): | |
142 return self.data | |
143 def setData(self, d): | 138 def setData(self, d): |
144 self.data = d | 139 self.data = d |
145 self.adjust() | 140 self.adjust() |
146 self.setCursorPosition(0) | 141 self.setCursorPosition(0) |
147 Data = property(getData, setData) | 142 Data = property(lambda self: self.data, setData) |
148 | 143 |
149 class HexEdit(QScrollArea): | 144 class HexEdit(QScrollArea): |
150 def __init__(self): | 145 def __init__(self): |
151 super().__init__() | 146 super().__init__() |
152 self.bv = BinViewer(self) | 147 self.bv = BinViewer(self) |
157 | 152 |
158 if __name__ == '__main__': | 153 if __name__ == '__main__': |
159 app = QApplication(sys.argv) | 154 app = QApplication(sys.argv) |
160 he = HexEdit() | 155 he = HexEdit() |
161 he.show() | 156 he.show() |
162 he.bv.Data = bytearray(range(100)) * 8 + b'hjkfd' | 157 he.bv.Data = bytearray(range(100)) * 8 + b'x' |
163 app.exec() | 158 app.exec() |
164 | 159 |