improve highlighting

This commit is contained in:
2021-10-24 18:49:47 +02:00
parent 80741a7249
commit eab422a6ce
7 changed files with 121 additions and 14 deletions

View File

@@ -1,9 +1,13 @@
import math
from typing import Optional
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 highlighted_range import HighlightedRange
from line import Line
from logFileModel import LogFileModel
@@ -11,9 +15,8 @@ from logFileModel import LogFileModel
class BigText(QWidget):
_byte_offset = 0
_left_offset = 0
_selection_start_byte = 0
_selection_end_byte = 0
highlights : [Highlight] = []
def __init__(self, model: LogFileModel):
super(BigText, self).__init__()
@@ -21,21 +24,24 @@ class BigText(QWidget):
self.font = QFont("monospace", 20)
self.update_font_metrics(QPainter(self))
self.lines = []
self.selection_highlight= HightlightSelection(0,0)
self.highlights = [self.selection_highlight]
def paintEvent(self, event: QPaintEvent) -> None:
self.draw()
def mousePressEvent(self, e: QMouseEvent) -> None:
if e.buttons() == Qt.MouseButton.LeftButton:
self._selection_start_byte = self.to_byte_offset(e)
self._selection_end_byte = self._selection_start_byte
offset = self.to_byte_offset(e)
self.selection_highlight.set_start(offset)
self.selection_highlight.set_end_byte(offset)
self.update()
def mouseMoveEvent(self, e: QMouseEvent):
current_byte = self.to_byte_offset(e)
if self._selection_end_byte != current_byte:
self._selection_end_byte = current_byte
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))
@@ -69,8 +75,12 @@ 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)
#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)
y_line_offset = y_line_offset + self.char_height
y_line_offset = self.char_height;
@@ -100,11 +110,21 @@ class BigText(QWidget):
#print(rect)
self.hightlight(painter,rect , hightlight_color)
def hightlight(self, painter: QPainter,rect: QRect, color: QColor):
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
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())
def hightlight(self, painter: QPainter,rect: QRect, brush: QBrush, pen:QPen):
old_brush = painter.brush()
old_pen = painter.pen()
painter.setBrush(color)
painter.setPen(Qt.PenStyle.NoPen)
painter.setBrush(brush)
painter.setPen(pen)
painter.drawRoundedRect(rect, 3.0, 3.0)
painter.setBrush(old_brush)
painter.setPen(old_pen)