fix computation of text width

This commit is contained in:
2021-10-24 16:35:27 +02:00
parent 3390ccf261
commit 80741a7249
9 changed files with 176 additions and 11 deletions

View File

@@ -1,3 +1,5 @@
import math
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
@@ -16,7 +18,7 @@ class BigText(QWidget):
def __init__(self, model: LogFileModel):
super(BigText, self).__init__()
self.model = model
self.font = QFont("monospace", 12)
self.font = QFont("monospace", 20)
self.update_font_metrics(QPainter(self))
self.lines = []
@@ -35,14 +37,14 @@ class BigText(QWidget):
if self._selection_end_byte != current_byte:
self._selection_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)
if line_number < len(self.lines):
line = self.lines[line_number]
char_in_line = int(e.pos().x() / self.char_width) + self._left_offset
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))
@@ -57,17 +59,22 @@ class BigText(QWidget):
painter.begin(self)
painter.setFont(self.font)
painter.setPen(QColor(0, 0, 0))
self.update_font_metrics( painter)
self.update_font_metrics(painter)
lines_to_show = self.height() / (self.fontMetrics().height())
self.lines = self.model.data(self._byte_offset, lines_to_show)
# draw hightlights first - some characters may overlap to the next line
# by drawing the background hightlights first we prevent that the hightlight
# 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)
y_line_offset = y_line_offset + self.char_height
y_line_offset = self.char_height;
for l in self.lines:
painter.drawText(0, y_line_offset, l.line())
y_line_offset = y_line_offset + self.char_height
@@ -75,18 +82,19 @@ class BigText(QWidget):
def draw_selection(self, painter: QPainter, line: Line, y_line_offset: int):
hightlight_color = QColor(255, 255, 0)
if line.includes_byte(self._selection_start_byte):
#print("%s bytes in line: " % (self._selection_start_byte - line.byte_offset()))
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
y1 = y_line_offset- self.char_height + self.char_height / 7
height = self.char_height
rect = QRect(x1,y1,width,height)
#print(rect)
@@ -96,9 +104,8 @@ class BigText(QWidget):
old_brush = painter.brush()
old_pen = painter.pen()
painter.setBrush(color)
painter.setPen(color)
painter.setPen(Qt.PenStyle.NoPen)
painter.drawRoundedRect(rect, 3.0, 3.0)
#painter.drawRect(rect)
painter.setBrush(old_brush)
painter.setPen(old_pen)
@@ -106,4 +113,7 @@ class BigText(QWidget):
fm: QFontMetrics = painter.fontMetrics()
self.char_height = fm.height()
self.char_width = fm.averageCharWidth()# all chars have same with for monospace font
print("font width=%s height=%s" % (self.char_width, self.char_height))
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))