def highlight(self, addr): fmt = QTextCharFormat() fmt.setBackground(Qt.cyan) fmt.setFont('Courier New') cur = self.box.textCursor() text = self.box.toPlainText() count = 0 for line in text.split('\n'): if len(line) > 0: line_addr = line.split()[0] n = (len(line_addr[2:-1]) * 4) mask = (2**n) - 1 if int(line_addr[:-1], 16) == (addr & mask): break count += 1 block = self.box.document().findBlockByLineNumber(count) cur.setPosition(block.position()) cur.select(QTextCursor.LineUnderCursor) cur.setCharFormat(fmt) self.box.setTextCursor(cur)
def clear_highlight(self): fmt = QTextCharFormat() fmt.setFont('Courier New') cur = self.box.textCursor() cur.select(QTextCursor.Document) cur.setCharFormat(fmt) self.box.setTextCursor(cur)
def highlight(self, addr, group): self.clear_highlight() # adding new highlights fmt = QTextCharFormat() fmt.setBackground(Qt.cyan) fmt.setFont('Courier New') addr_block = self.addresses.document().findBlockByLineNumber((addr - self.baseAddress) // 16) # gets linenos mem_block = self.mem_display.document().findBlockByLineNumber((addr - self.baseAddress) // 16) chr_block = self.chr_display.document().findBlockByLineNumber((addr - self.baseAddress) // 16) addr_cur = self.addresses.textCursor() # getting cursors mem_cur = self.mem_display.textCursor() chr_cur = self.chr_display.textCursor() char_offset = 0 mem_offset = 0 self.endian_sem.acquire() if self.endian == Endian.big: mem_offset = ((addr % 16) // group) * (2 * group + 1) char_offset = (addr % 16) * 3 elif self.endian == Endian.little: mem_offset = (((16 / group) - 1 )* (2 * group + 1)) - ((addr % 16) // group) * (2 * group + 1) char_offset = (15*3) - ((addr % 16) * 3) self.endian_sem.release() addr_cur.setPosition(addr_block.position()) # getting positions mem_cur.setPosition(mem_block.position() + mem_offset) # gives character offset within 16 byte line chr_cur.setPosition(chr_block.position() + char_offset) # sets position of char chr_text = self.chr_display.toPlainText() if chr_text[chr_cur.position()] == '\\' and chr_cur.position() + 1 < len(chr_text) and chr_text[chr_cur.position() + 1] in ['0', 'n', 't']: chr_cur.setPosition(chr_cur.position() + 2, mode=QTextCursor.KeepAnchor) else: chr_cur.setPosition(chr_cur.position() + 1, mode=QTextCursor.KeepAnchor) addr_cur.select(QTextCursor.LineUnderCursor) # selects whole line mem_cur.select(QTextCursor.WordUnderCursor) # selects just one word addr_cur.setCharFormat(fmt) # setting format mem_cur.setCharFormat(fmt) chr_cur.setCharFormat(fmt) self.addresses.setTextCursor(addr_cur) self.mem_display.setTextCursor(mem_cur) self.chr_display.setTextCursor(chr_cur) self.highlight_sem.acquire() self.is_highlighted = True self.highlight_addr = addr self.highlight_sem.release()
def __init__(self, *args): super().__init__(*args) if FORMATS['keyword'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.darkBlue) f.setFontWeight(QFont.Bold) FORMATS['keyword'] = f if FORMATS['quotation'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.darkGreen) FORMATS['quotation'] = f if FORMATS['function'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.blue) f.setFontWeight(QFont.Bold) FORMATS['function'] = f if FORMATS['comment'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.darkGreen) f.setFontWeight(QFont.Bold) FORMATS['comment'] = f
def __init__(self, parent, color_scheme=None): # TODO: Use the color scheme. it's not used right now super().__init__(parent, color_scheme=color_scheme) self.doc = parent # type: QCodeDocument if FORMATS['keyword'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.darkBlue) f.setFontWeight(QFont.Bold) FORMATS['keyword'] = f if FORMATS['quotation'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.darkGreen) FORMATS['quotation'] = f if FORMATS['function'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.blue) f.setFontWeight(QFont.Bold) FORMATS['function'] = f if FORMATS['comment'] is None: f = QTextCharFormat() f.setFont(Conf.code_font) f.setForeground(Qt.darkGreen) f.setFontWeight(QFont.Bold) FORMATS['comment'] = f
def clear_highlight(self): fmt = QTextCharFormat() fmt.setFont('Courier New') # clearing past highlights addr_cur = self.addresses.textCursor() # getting cursors mem_cur = self.mem_display.textCursor() chr_cur = self.chr_display.textCursor() addr_cur.select(QTextCursor.Document) # selecting entire document mem_cur.select(QTextCursor.Document) chr_cur.select(QTextCursor.Document) addr_cur.setCharFormat(fmt) # adding format mem_cur.setCharFormat(fmt) chr_cur.setCharFormat(fmt)