HexEdit/HexEditWidget.py

changeset 4658
d760763dcc4a
parent 4656
ec546bd4ec56
child 4659
2863d05e83c6
equal deleted inserted replaced
4656:ec546bd4ec56 4658:d760763dcc4a
19 from .HexEditUndoStack import HexEditUndoStack 19 from .HexEditUndoStack import HexEditUndoStack
20 20
21 import Globals 21 import Globals
22 22
23 23
24 # TODO: implement cursor in ASCII area
25 # TODO: implement editing in ASCII area 24 # TODO: implement editing in ASCII area
26 25
27 26
28 class HexEditWidget(QAbstractScrollArea): 27 class HexEditWidget(QAbstractScrollArea):
29 """ 28 """
69 self.__embedded = embedded 68 self.__embedded = embedded
70 69
71 # Properties 70 # Properties
72 self.__addressArea = True 71 self.__addressArea = True
73 # switch the address area on/off 72 # switch the address area on/off
74 self.__addressAreaColor = QColor() 73 self.__addressAreaBrush = QBrush()
75 # color of the address area 74 self.__addressAreaPen = QPen()
75 # background and pen of the address area
76 self.__addressOffset = 0 76 self.__addressOffset = 0
77 # offset into the shown address range 77 # offset into the shown address range
78 self.__addressWidth = 4 78 self.__addressWidth = 4
79 # address area width in characters 79 # address area width in characters
80 self.__asciiArea = True 80 self.__asciiArea = True
135 if Globals.isWindowsPlatform(): 135 if Globals.isWindowsPlatform():
136 self.setFont(QFont("Courier", 10)) 136 self.setFont(QFont("Courier", 10))
137 else: 137 else:
138 self.setFont(QFont("Monospace", 10)) 138 self.setFont(QFont("Monospace", 10))
139 139
140 self.setAddressAreaColor(self.palette().alternateBase().color()) 140 self.setAddressAreaColors(
141 self.setHighlightColor(QColor(0xff, 0xff, 0x99, 0xff)) 141 self.palette().color(QPalette.WindowText),
142 self.setSelectionColor(self.palette().highlight().color()) 142 self.palette().alternateBase().color())
143 self.setHighlightColors(
144 self.palette().color(QPalette.WindowText),
145 QColor(0xff, 0xff, 0x99, 0xff))
146 self.setSelectionColors(
147 self.palette().highlightedText().color(),
148 self.palette().highlight().color())
143 149
144 self.__cursorTimer = QTimer() 150 self.__cursorTimer = QTimer()
145 self.__cursorTimer.timeout.connect(self.__updateCursor) 151 self.__cursorTimer.timeout.connect(self.__updateCursor)
146 152
147 self.verticalScrollBar().valueChanged.connect(self.__adjust) 153 self.verticalScrollBar().valueChanged.connect(self.__adjust)
209 self.__addressArea = on 215 self.__addressArea = on
210 self.__adjust() 216 self.__adjust()
211 self.setCursorPosition(self.__cursorPosition) 217 self.setCursorPosition(self.__cursorPosition)
212 self.viewport().update() 218 self.viewport().update()
213 219
214 def addressAreaColor(self): 220 def addressAreaColors(self):
215 """ 221 """
216 Public method to get the address area color. 222 Public method to get the address area colors.
217 223
218 @return address area color 224 @return address area foreground and background colors
219 @rtype QColor 225 @rtype tuple of 2 QColor
220 """ 226 """
221 return QColor(self.__addressAreaColor) 227 return self.__addressAreaPen.color(), self.__addressAreaBrush.color()
222 228
223 def setAddressAreaColor(self, color): 229 def setAddressAreaColors(self, foreground, background):
224 """ 230 """
225 Public method to set the address area color. 231 Public method to set the address area colors.
226 232
227 @param color address area color 233 @param foreground address area foreground color
228 @type QColor 234 @type QColor
229 """ 235 @param background address area background color
230 self.__addressAreaColor = QColor(color) 236 @type QColor
237 """
238 self.__addressAreaPen = QPen(foreground)
239 self.__addressAreaBrush = QBrush(background)
231 self.viewport().update() 240 self.viewport().update()
232 241
233 def addressOffset(self): 242 def addressOffset(self):
234 """ 243 """
235 Public method to get the address offset. 244 Public method to get the address offset.
434 @type bool 443 @type bool
435 """ 444 """
436 self.__highlighting = on 445 self.__highlighting = on
437 self.viewport().update() 446 self.viewport().update()
438 447
439 def highlightingColor(self): 448 def highlightColors(self):
440 """ 449 """
441 Public method to get the highlighting color. 450 Public method to get the highlight colors.
442 451
443 @return highlighting color 452 @return highlight foreground and background colors
444 @rtype QColor 453 @rtype tuple of 2 QColor
445 """ 454 """
446 return self.__highlightingBrush.color() 455 return self.__highlightingPen.color(), self.__highlightingBrush.color()
447 456
448 def setHighlightColor(self, color): 457 def setHighlightColors(self, foreground, background):
449 """ 458 """
450 Public method to set the highlight color. 459 Public method to set the highlight colors.
451 460
452 @param color new highlight color 461 @param foreground highlight foreground color
453 @type QColor 462 @type QColor
454 """ 463 @param background highlight background color
455 self.__highlightingBrush = QBrush(color) 464 @type QColor
456 self.__highlightingPen = QPen( 465 """
457 self.viewport().palette().color(QPalette.WindowText)) 466 self.__highlightingPen = QPen(foreground)
467 self.__highlightingBrush = QBrush(background)
458 self.viewport().update() 468 self.viewport().update()
459 469
460 def overwriteMode(self): 470 def overwriteMode(self):
461 """ 471 """
462 Public method to get the overwrite mode. 472 Public method to get the overwrite mode.
474 @type bool 484 @type bool
475 """ 485 """
476 self.__overwriteMode = on 486 self.__overwriteMode = on
477 self.overwriteModeChanged.emit(self.__overwriteMode) 487 self.overwriteModeChanged.emit(self.__overwriteMode)
478 488
479 def selectionColor(self): 489 def selectionColors(self):
480 """ 490 """
481 Public method to get the selection color. 491 Public method to get the selection colors.
482 492
483 @return selection color 493 @return selection foreground and background colors
484 @rtype QColor 494 @rtype tuple of 2 QColor
485 """ 495 """
486 return self.__selectionBrush.color() 496 return self.__selectionPen.color(), self.__selectionBrush.color()
487 497
488 def setSelectionColor(self, color): 498 def setSelectionColors(self, foreground, background):
489 """ 499 """
490 Public method to set the selection color. 500 Public method to set the selection colors.
491 501
492 @param color new selection color 502 @param foreground selection foreground color
493 @type QColor 503 @type QColor
494 """ 504 @param background selection background color
495 self.__selectionBrush = QBrush(color) 505 @type QColor
496 self.__selectionPen = QPen(Qt.white) 506 """
507 self.__selectionPen = QPen(foreground)
508 self.__selectionBrush = QBrush(background)
497 self.viewport().update() 509 self.viewport().update()
498 510
499 def isReadOnly(self): 511 def isReadOnly(self):
500 """ 512 """
501 Public method to test the read only state. 513 Public method to test the read only state.
537 self.__pxGapAdr = self.__pxCharWidth // 2 549 self.__pxGapAdr = self.__pxCharWidth // 2
538 self.__pxGapAdrHex = self.__pxCharWidth 550 self.__pxGapAdrHex = self.__pxCharWidth
539 self.__pxGapHexAscii = 2 * self.__pxCharWidth 551 self.__pxGapHexAscii = 2 * self.__pxCharWidth
540 self.__pxCursorWidth = self.__pxCharHeight // 7 552 self.__pxCursorWidth = self.__pxCharHeight // 7
541 self.__pxSelectionSub = self.fontMetrics().descent() 553 self.__pxSelectionSub = self.fontMetrics().descent()
554 self.__adjust()
542 self.viewport().update() 555 self.viewport().update()
543 556
544 def dataAt(self, pos, count=-1): 557 def dataAt(self, pos, count=-1):
545 """ 558 """
546 Public method to get data from a given position. 559 Public method to get data from a given position.
1315 if self.__addressArea: 1328 if self.__addressArea:
1316 painter.fillRect( 1329 painter.fillRect(
1317 QRect(-pxOfsX, evt.rect().top(), 1330 QRect(-pxOfsX, evt.rect().top(),
1318 self.__pxPosHexX - self.__pxGapAdrHex // 2 - pxOfsX, 1331 self.__pxPosHexX - self.__pxGapAdrHex // 2 - pxOfsX,
1319 self.height()), 1332 self.height()),
1320 self.__addressAreaColor) 1333 self.__addressAreaBrush)
1321 if self.__asciiArea: 1334 if self.__asciiArea:
1322 linePos = self.__pxPosAsciiX - (self.__pxGapHexAscii // 2) 1335 linePos = self.__pxPosAsciiX - (self.__pxGapHexAscii // 2)
1323 painter.setPen(Qt.gray) 1336 painter.setPen(Qt.gray)
1324 painter.drawLine(linePos - pxOfsX, evt.rect().top(), 1337 painter.drawLine(linePos - pxOfsX, evt.rect().top(),
1325 linePos - pxOfsX, self.height()) 1338 linePos - pxOfsX, self.height())
1327 painter.setPen( 1340 painter.setPen(
1328 self.viewport().palette().color(QPalette.WindowText)) 1341 self.viewport().palette().color(QPalette.WindowText))
1329 1342
1330 # paint the address area 1343 # paint the address area
1331 if self.__addressArea: 1344 if self.__addressArea:
1345 painter.setPen(self.__addressAreaPen)
1332 address = "" 1346 address = ""
1333 row = 0 1347 row = 0
1334 pxPosY = self.__pxCharHeight 1348 pxPosY = self.__pxCharHeight
1335 while row <= len(self.__dataShown) // self.BYTES_PER_LINE: 1349 while row <= len(self.__dataShown) // self.BYTES_PER_LINE:
1336 address = "{0:0{1}x}".format( 1350 address = "{0:0{1}x}".format(
1415 colIdx += 1 1429 colIdx += 1
1416 1430
1417 # increment loop variables 1431 # increment loop variables
1418 row += 1 1432 row += 1
1419 pxPosY += self.__pxCharHeight 1433 pxPosY += self.__pxCharHeight
1420 1434
1421 painter.setBackgroundMode(Qt.TransparentMode) 1435 painter.setBackgroundMode(Qt.TransparentMode)
1422 painter.setPen( 1436 painter.setPen(
1423 self.viewport().palette().color(QPalette.WindowText)) 1437 self.viewport().palette().color(QPalette.WindowText))
1424 1438
1425 # paint cursor 1439 # paint cursor
1426 if self.__blink and not self.__readOnly and self.hasFocus(): 1440 if self.__blink and not self.__readOnly and self.hasFocus():
1427 painter.fillRect( 1441 painter.fillRect(
1428 self.__cursorRect, self.palette().color(QPalette.WindowText)) 1442 self.__cursorRect, self.palette().color(QPalette.WindowText))

eric ide

mercurial