add regex highlight
This commit is contained in:
82
bigtext.py
82
bigtext.py
@@ -1,31 +1,43 @@
|
||||
import math
|
||||
from typing import Optional
|
||||
import re
|
||||
from typing import Optional, List
|
||||
import PyQt6.QtGui
|
||||
from PyQt6.QtCore import *
|
||||
from PyQt6.QtGui import *
|
||||
from PyQt6.QtWidgets import *
|
||||
|
||||
from highlight import Highlight
|
||||
from highlight_selection import HightlightSelection
|
||||
from highlight_regex import HighlightRegex
|
||||
from highlight_selection import HighlightSelection
|
||||
from highlighted_range import HighlightedRange
|
||||
from line import Line
|
||||
from logFileModel import LogFileModel
|
||||
import re
|
||||
|
||||
from settings import Settings
|
||||
|
||||
|
||||
class BigText(QWidget):
|
||||
_byte_offset = 0
|
||||
_left_offset = 0
|
||||
|
||||
highlights : [Highlight] = []
|
||||
highlights: [Highlight] = []
|
||||
|
||||
def __init__(self, model: LogFileModel):
|
||||
super(BigText, self).__init__()
|
||||
self.model = model
|
||||
self.font = QFont("monospace", 20)
|
||||
self.font = QFont("monospace", 12)
|
||||
self.update_font_metrics(QPainter(self))
|
||||
self.lines = []
|
||||
self.selection_highlight= HightlightSelection(0,0)
|
||||
self.highlights = [self.selection_highlight]
|
||||
self.selection_highlight = HighlightSelection(0, 0)
|
||||
self.highlights = [
|
||||
HighlightRegex(
|
||||
r"INFO",
|
||||
brush=QBrush(QColor(220, 112, 122)),
|
||||
brush_full_line=QBrush(QColor(255, 112, 122))
|
||||
),
|
||||
self.selection_highlight,
|
||||
]
|
||||
|
||||
def paintEvent(self, event: QPaintEvent) -> None:
|
||||
self.draw()
|
||||
@@ -43,7 +55,7 @@ class BigText(QWidget):
|
||||
if self.selection_highlight.end_byte != current_byte:
|
||||
self.selection_highlight.set_end_byte(current_byte)
|
||||
self.update()
|
||||
#print("-> %s,%s" %(self._selection_start_byte, self._selection_end_byte))
|
||||
# print("-> %s,%s" %(self._selection_start_byte, self._selection_end_byte))
|
||||
|
||||
def to_byte_offset(self, e: QMouseEvent) -> int:
|
||||
line_number = int(e.pos().y() / self.char_height)
|
||||
@@ -52,10 +64,10 @@ class BigText(QWidget):
|
||||
line = self.lines[line_number]
|
||||
char_in_line = round(e.pos().x() / self.char_width) + self._left_offset
|
||||
char_in_line = min(char_in_line, line.length())
|
||||
#print("%s in line %s" % (char_in_line, line_number))
|
||||
# print("%s in line %s" % (char_in_line, line_number))
|
||||
|
||||
current_byte = line.byte_offset() + char_in_line
|
||||
#print("%s + %s = %s" % (line.byte_offset(), char_in_line, current_byte))
|
||||
# print("%s + %s = %s" % (line.byte_offset(), char_in_line, current_byte))
|
||||
else:
|
||||
current_byte = self.model.byte_count()
|
||||
return current_byte
|
||||
@@ -75,12 +87,11 @@ class BigText(QWidget):
|
||||
# draws over a character
|
||||
y_line_offset = self.char_height;
|
||||
for l in self.lines:
|
||||
#if l.intersects(self._selection_start_byte, self._selection_end_byte):
|
||||
# self.draw_selection(painter, l, y_line_offset)
|
||||
for h in self.highlights:
|
||||
optional_highlight_range = h.compute_highlight(l)
|
||||
if optional_highlight_range:
|
||||
self.draw_highlight(optional_highlight_range, painter, y_line_offset)
|
||||
for highlight in optional_highlight_range:
|
||||
self.draw_highlight(highlight, painter, y_line_offset)
|
||||
y_line_offset = y_line_offset + self.char_height
|
||||
|
||||
y_line_offset = self.char_height;
|
||||
@@ -90,41 +101,26 @@ class BigText(QWidget):
|
||||
|
||||
painter.end()
|
||||
|
||||
def draw_selection(self, painter: QPainter, line: Line, y_line_offset: int):
|
||||
hightlight_color = QColor(255, 255, 0)
|
||||
def draw_highlight(self, highlight: HighlightedRange, painter: QPainter, y_line_offset: int):
|
||||
x1 = highlight.get_start() * self.char_width
|
||||
width = highlight.get_width() * self.char_width
|
||||
|
||||
if line.includes_byte(self._selection_start_byte):
|
||||
x1 = (self._selection_start_byte - line.byte_offset() - self._left_offset) * self.char_width
|
||||
else:
|
||||
x1 = 0
|
||||
|
||||
if line.includes_byte(self._selection_end_byte):
|
||||
width = (self._selection_end_byte - line.byte_offset() - self._left_offset) * self.char_width - x1
|
||||
#print("char in line: %d -> width: %d" % (self._selection_end_byte - line.byte_offset() - self._left_offset, width))
|
||||
else:
|
||||
width = len(line.line().rstrip()) * self.char_width -x1
|
||||
|
||||
y1 = y_line_offset- self.char_height + self.char_height / 7
|
||||
y1 = y_line_offset - self.char_height + self.char_height / 7
|
||||
height = self.char_height
|
||||
rect = QRect(x1,y1,width,height)
|
||||
#print(rect)
|
||||
self.hightlight(painter,rect , hightlight_color)
|
||||
|
||||
def draw_highlight(self, highlight: HighlightedRange, painter: QPainter, y_line_offset:int):
|
||||
x1 = highlight.get_start()*self.char_width
|
||||
width = highlight.get_width()*self.char_width
|
||||
if highlight.is_highlight_full_line():
|
||||
full_width = Settings.max_line_length() * self.char_width
|
||||
rect = QRect(0, y1, full_width, height)
|
||||
self.highlight_background(painter, rect, highlight.get_brush_full_line())
|
||||
|
||||
y1 = y_line_offset- self.char_height + self.char_height / 7
|
||||
height = self.char_height
|
||||
rect = QRect(x1,y1,width,height)
|
||||
#print(rect)
|
||||
self.hightlight(painter,rect , highlight.get_brush(), highlight.get_pen())
|
||||
rect = QRect(x1, y1, width, height)
|
||||
self.highlight_background(painter, rect, highlight.get_brush())
|
||||
|
||||
def hightlight(self, painter: QPainter,rect: QRect, brush: QBrush, pen:QPen):
|
||||
def highlight_background(self, painter: QPainter, rect: QRect, brush: QBrush):
|
||||
old_brush = painter.brush()
|
||||
old_pen = painter.pen()
|
||||
painter.setBrush(brush)
|
||||
painter.setPen(pen)
|
||||
painter.setPen(Qt.PenStyle.NoPen)
|
||||
painter.drawRoundedRect(rect, 3.0, 3.0)
|
||||
painter.setBrush(old_brush)
|
||||
painter.setPen(old_pen)
|
||||
@@ -132,8 +128,8 @@ class BigText(QWidget):
|
||||
def update_font_metrics(self, painter: QPainter):
|
||||
fm: QFontMetrics = painter.fontMetrics()
|
||||
self.char_height = fm.height()
|
||||
self.char_width = fm.averageCharWidth()# all chars have same with for monospace font
|
||||
self.char_width = fm.averageCharWidth() # all chars have same with for monospace font
|
||||
text = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
||||
self.char_width = fm.horizontalAdvance(text)/float(len(text))
|
||||
#print("bounding rect: %s"%(boundingRect))
|
||||
#print("font width=%s height=%s" % (self.char_width, self.char_height))
|
||||
self.char_width = fm.horizontalAdvance(text) / float(len(text))
|
||||
# print("bounding rect: %s"%(boundingRect))
|
||||
# print("font width=%s height=%s" % (self.char_width, self.char_height))
|
||||
|
||||
Reference in New Issue
Block a user