From 6040b1633dc07a42417e726723afa8ac92ec55e0 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Fri, 26 Aug 2022 14:31:51 +0200 Subject: [PATCH] cleanup --- src/ui/bigtext/bigtext.py | 44 ++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/ui/bigtext/bigtext.py b/src/ui/bigtext/bigtext.py index 8f990cf..072283f 100644 --- a/src/ui/bigtext/bigtext.py +++ b/src/ui/bigtext/bigtext.py @@ -1,5 +1,3 @@ -import sys - import math import os import time @@ -97,7 +95,6 @@ class BigText(QWidget): def add_line_click_listener(self, listener: Callable[[int], None]): """ - :param listener: a callable, the parameter is the byte offset of the clicked line :return: """ @@ -111,6 +108,7 @@ class BigText(QWidget): pass +# noinspection PyArgumentList,PyTypeChecker class InnerBigText(QWidget): _byte_offset = 0 _left_offset = 0 @@ -119,6 +117,8 @@ class InnerBigText(QWidget): def __init__(self, parent: BigText, model: LogFileModel): super(InnerBigText, self).__init__() + self.char_height = None + self.char_width = None self.model = model self.parent = parent self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) @@ -203,6 +203,7 @@ class InnerBigText(QWidget): self.update() self.parent.v_scroll_bar.setValue(self._byte_offset) + # noinspection PyTypeChecker def mousePressEvent(self, e: QtGui.QMouseEvent) -> None: if e.buttons() == Qt.MouseButton.LeftButton and e.modifiers() == Qt.KeyboardModifier.ShiftModifier: 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 = 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) - # 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) current_byte = line.byte_offset() + byte_in_line # print("%s + %s = %s" % (line.byte_offset(), char_in_line, current_byte)) @@ -332,6 +333,7 @@ class InnerBigText(QWidget): _("data selection"), _( "You have selected {0} of data.").format(bytes_human_readable)) + # noinspection PyTypeChecker you_sure.setStandardButtons(QMessageBox.Cancel) copy_btn = you_sure.addButton(_("Copy {0} to Clipboard").format(bytes_human_readable), QMessageBox.ActionRole) @@ -357,6 +359,7 @@ class InnerBigText(QWidget): end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte) dialog = QFileDialog(self) (selected_file, _filter) = dialog.getSaveFileName( + parent=self, caption=_("Save File"), dir=os.path.dirname(self.model.get_file()) ) @@ -372,16 +375,13 @@ class InnerBigText(QWidget): self.update() def paintEvent(self, event: QPaintEvent) -> None: - # print("paintEvent %s" % (self.model.get_file())) painter = QPainter(self) - # painter.setFont(self.model.settings.font()) - # 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" + # 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.setPen(QColor(0, 0, 0)) 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)) 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 self.parent.v_scroll_bar.setMaximum(self.model.byte_count() - 1) - for l in self.lines: - self.update_longest_line(len(l.line())) + for line in self.lines: + self.update_longest_line(len(line.line())) highlighters = self.model.highlighters() if self.model.get_query_highlight(): highlighters = highlighters + [self.model.get_query_highlight()] highlighters = highlighters + [self.selection_highlight] # selection highlight should be last - # draw hightlights first - some characters may overlap to the next line - # by drawing the background hightlights first we prevent that the hightlight + # draw highlights first - some characters may overlap to the next line + # by drawing the background highlights first we prevent that the highlight # draws over a character - start = time.time() - y_line_offset = self.char_height; - for l in self.lines: + y_line_offset = self.char_height + for line in self.lines: highlight_ranges = [] for h in highlighters: - optional_highlight_range = h.compute_highlight(l) + optional_highlight_range = h.compute_highlight(line) if optional_highlight_range: highlight_ranges = highlight_ranges + optional_highlight_range self.draw_highlights(highlight_ranges, painter, y_line_offset) 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) - y_line_offset = self.char_height; - for l in self.lines: - text = l.line_prepared_for_display() + y_line_offset = self.char_height + for line in self.lines: + text = line.line_prepared_for_display() painter.drawText(left_offset, y_line_offset, text) y_line_offset = y_line_offset + self.char_height @@ -461,6 +457,6 @@ class InnerBigText(QWidget): fm: QFontMetrics = painter.fontMetrics() self.char_height = fm.height() 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)) # print("font width=%s height=%s" % (self.char_width, self.char_height))