This commit is contained in:
2022-08-26 14:31:51 +02:00
parent 1ddd589cc2
commit 6040b1633d

View File

@@ -1,5 +1,3 @@
import sys
import math import math
import os import os
import time import time
@@ -97,7 +95,6 @@ class BigText(QWidget):
def add_line_click_listener(self, listener: Callable[[int], None]): def add_line_click_listener(self, listener: Callable[[int], None]):
""" """
:param listener: a callable, the parameter is the byte offset of the clicked line :param listener: a callable, the parameter is the byte offset of the clicked line
:return: :return:
""" """
@@ -111,6 +108,7 @@ class BigText(QWidget):
pass pass
# noinspection PyArgumentList,PyTypeChecker
class InnerBigText(QWidget): class InnerBigText(QWidget):
_byte_offset = 0 _byte_offset = 0
_left_offset = 0 _left_offset = 0
@@ -119,6 +117,8 @@ class InnerBigText(QWidget):
def __init__(self, parent: BigText, model: LogFileModel): def __init__(self, parent: BigText, model: LogFileModel):
super(InnerBigText, self).__init__() super(InnerBigText, self).__init__()
self.char_height = None
self.char_width = None
self.model = model self.model = model
self.parent = parent self.parent = parent
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
@@ -203,6 +203,7 @@ class InnerBigText(QWidget):
self.update() self.update()
self.parent.v_scroll_bar.setValue(self._byte_offset) self.parent.v_scroll_bar.setValue(self._byte_offset)
# noinspection PyTypeChecker
def mousePressEvent(self, e: QtGui.QMouseEvent) -> None: def mousePressEvent(self, e: QtGui.QMouseEvent) -> None:
if e.buttons() == Qt.MouseButton.LeftButton and e.modifiers() == Qt.KeyboardModifier.ShiftModifier: if e.buttons() == Qt.MouseButton.LeftButton and e.modifiers() == Qt.KeyboardModifier.ShiftModifier:
offset = self.to_byte_offset(e) offset = self.to_byte_offset(e)
@@ -310,7 +311,7 @@ class InnerBigText(QWidget):
column_in_line = self.x_pos_to_column(e.pos().x()) + self._left_offset column_in_line = self.x_pos_to_column(e.pos().x()) + self._left_offset
column_in_line = min(column_in_line, line.length_in_columns()) # x was behind the last column of this line column_in_line = min(column_in_line, line.length_in_columns()) # x was behind the last column of this line
char_in_line = line.column_to_char(column_in_line) char_in_line = line.column_to_char(column_in_line)
# print("%s in line %s lcolumn_in_line=%s" % (char_in_line, line_number, column_in_line)) # print("%s in line %s column_in_line=%s" % (char_in_line, line_number, column_in_line))
byte_in_line = line.char_index_to_byte(char_in_line) byte_in_line = line.char_index_to_byte(char_in_line)
current_byte = line.byte_offset() + byte_in_line current_byte = line.byte_offset() + byte_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))
@@ -332,6 +333,7 @@ class InnerBigText(QWidget):
_("data selection"), _("data selection"),
_( _(
"You have selected <b>{0}</b> of data.").format(bytes_human_readable)) "You have selected <b>{0}</b> of data.").format(bytes_human_readable))
# noinspection PyTypeChecker
you_sure.setStandardButtons(QMessageBox.Cancel) you_sure.setStandardButtons(QMessageBox.Cancel)
copy_btn = you_sure.addButton(_("Copy {0} to Clipboard").format(bytes_human_readable), copy_btn = you_sure.addButton(_("Copy {0} to Clipboard").format(bytes_human_readable),
QMessageBox.ActionRole) QMessageBox.ActionRole)
@@ -357,6 +359,7 @@ class InnerBigText(QWidget):
end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte) end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte)
dialog = QFileDialog(self) dialog = QFileDialog(self)
(selected_file, _filter) = dialog.getSaveFileName( (selected_file, _filter) = dialog.getSaveFileName(
parent=self,
caption=_("Save File"), caption=_("Save File"),
dir=os.path.dirname(self.model.get_file()) dir=os.path.dirname(self.model.get_file())
) )
@@ -372,16 +375,13 @@ class InnerBigText(QWidget):
self.update() self.update()
def paintEvent(self, event: QPaintEvent) -> None: def paintEvent(self, event: QPaintEvent) -> None:
# print("paintEvent %s" % (self.model.get_file()))
painter = QPainter(self) painter = QPainter(self)
# painter.setFont(self.model.settings.font()) # font = "Courier New" if sys.platform == 'win32' or sys.platform == 'cygwin' else "Monospace"
# Courier New, DejaVu Sans Mono, Monospace, Liberation Mono, Noto Mono, Nimbus Mono L, Tlwg Mono, Ubuntu Mono, FreeMono, Mitra Mono
font = "Courier New" if sys.platform == 'win32' or sys.platform == 'cygwin' else "Monospace"
painter.setFont(QFont("Courier New", self.model.settings.getint_session('general', "font_size"))) painter.setFont(QFont("Courier New", self.model.settings.getint_session('general', "font_size")))
painter.setPen(QColor(0, 0, 0)) painter.setPen(QColor(0, 0, 0))
self.update_font_metrics(painter) self.update_font_metrics(painter)
lines_to_show = self.lines_shown() lines_to_show = math.ceil(self.lines_shown())
# print("%s / %s = %s" %(self.height(), float(self.char_height), lines_to_show)) # print("%s / %s = %s" %(self.height(), float(self.char_height), lines_to_show))
self.lines = self.model.data(self._byte_offset, self.scroll_lines, lines_to_show) self.lines = self.model.data(self._byte_offset, self.scroll_lines, lines_to_show)
@@ -392,36 +392,32 @@ class InnerBigText(QWidget):
# document length == maximum + pageStep + aFewBytesSoThatTheLastLineIsShown # document length == maximum + pageStep + aFewBytesSoThatTheLastLineIsShown
self.parent.v_scroll_bar.setMaximum(self.model.byte_count() - 1) self.parent.v_scroll_bar.setMaximum(self.model.byte_count() - 1)
for l in self.lines: for line in self.lines:
self.update_longest_line(len(l.line())) self.update_longest_line(len(line.line()))
highlighters = self.model.highlighters() highlighters = self.model.highlighters()
if self.model.get_query_highlight(): if self.model.get_query_highlight():
highlighters = highlighters + [self.model.get_query_highlight()] highlighters = highlighters + [self.model.get_query_highlight()]
highlighters = highlighters + [self.selection_highlight] # selection highlight should be last highlighters = highlighters + [self.selection_highlight] # selection highlight should be last
# draw hightlights first - some characters may overlap to the next line # draw highlights first - some characters may overlap to the next line
# by drawing the background hightlights first we prevent that the hightlight # by drawing the background highlights first we prevent that the highlight
# draws over a character # draws over a character
start = time.time() y_line_offset = self.char_height
y_line_offset = self.char_height; for line in self.lines:
for l in self.lines:
highlight_ranges = [] highlight_ranges = []
for h in highlighters: for h in highlighters:
optional_highlight_range = h.compute_highlight(l) optional_highlight_range = h.compute_highlight(line)
if optional_highlight_range: if optional_highlight_range:
highlight_ranges = highlight_ranges + optional_highlight_range highlight_ranges = highlight_ranges + optional_highlight_range
self.draw_highlights(highlight_ranges, painter, y_line_offset) self.draw_highlights(highlight_ranges, painter, y_line_offset)
y_line_offset = y_line_offset + self.char_height y_line_offset = y_line_offset + self.char_height
end = time.time()
# print("highlight duration: %.3f" %((end-start)*1000))
left_offset = int(-1 * self._left_offset * self.char_width) left_offset = int(-1 * self._left_offset * self.char_width)
y_line_offset = self.char_height; y_line_offset = self.char_height
for l in self.lines: for line in self.lines:
text = l.line_prepared_for_display() text = line.line_prepared_for_display()
painter.drawText(left_offset, y_line_offset, text) painter.drawText(left_offset, y_line_offset, text)
y_line_offset = y_line_offset + self.char_height y_line_offset = y_line_offset + self.char_height
@@ -461,6 +457,6 @@ class InnerBigText(QWidget):
fm: QFontMetrics = painter.fontMetrics() fm: QFontMetrics = painter.fontMetrics()
self.char_height = fm.height() self.char_height = fm.height()
self.char_width = fm.averageCharWidth() # all chars have same width for monospace font self.char_width = fm.averageCharWidth() # all chars have same width for monospace font
text = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" text = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012"
self.char_width = fm.horizontalAdvance(text) / float(len(text)) self.char_width = fm.horizontalAdvance(text) / float(len(text))
# print("font width=%s height=%s" % (self.char_width, self.char_height)) # print("font width=%s height=%s" % (self.char_width, self.char_height))