vertical scroll by keys

This commit is contained in:
2021-10-25 13:43:17 +02:00
parent 424f4bb11d
commit 3662b47651
2 changed files with 23 additions and 5 deletions

View File

@@ -63,6 +63,8 @@ class InnerBigText(QWidget):
super(InnerBigText, self).__init__() super(InnerBigText, self).__init__()
self.model = model self.model = model
self.parent = parent self.parent = parent
self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
self.font = QFont("monospace", 12) self.font = QFont("monospace", 12)
self.update_font_metrics(QPainter(self)) self.update_font_metrics(QPainter(self))
self.lines = [] self.lines = []
@@ -79,10 +81,24 @@ class InnerBigText(QWidget):
def paintEvent(self, event: QPaintEvent) -> None: def paintEvent(self, event: QPaintEvent) -> None:
self.draw() self.draw()
def keyPressEvent(self, e: QKeyEvent) -> None:
lines_to_scroll = math.floor(self.lines_shown()) -1
if e.key() == Qt.Key.Key_PageUp:
self.scroll_by_lines(lines_to_scroll)
if e.key() == Qt.Key.Key_PageDown:
self.scroll_by_lines(-lines_to_scroll)
if e.key() == 16777235:
self.scroll_by_lines(3)
if e.key() == 16777237:
self.scroll_by_lines(-3)
def wheelEvent(self, event: QWheelEvent): def wheelEvent(self, event: QWheelEvent):
direction = 1 if event.angleDelta().y() > 0 else -1 direction = 1 if event.angleDelta().y() > 0 else -1
#print("wheel event fired :) %s" % (direction)) #print("wheel event fired :) %s" % (direction))
self.scroll_lines = direction * 3 self.scroll_by_lines(direction * 3)
def scroll_by_lines(self, scroll_lines: int):
self.scroll_lines = scroll_lines
self.update() self.update()
self.parent.v_scroll_bar.setValue(self._byte_offset) self.parent.v_scroll_bar.setValue(self._byte_offset)
@@ -133,14 +149,16 @@ class InnerBigText(QWidget):
current_byte = self.model.byte_count() current_byte = self.model.byte_count()
return current_byte return current_byte
def lines_shown(self):
return self.height() / float(self.char_height)
def draw(self): def draw(self):
painter = QPainter() painter = QPainter(self)
painter.begin(self)
painter.setFont(self.font) painter.setFont(self.font)
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.height() / float(self.char_height) lines_to_show = 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)

View File

@@ -17,7 +17,7 @@ class LogFileModel:
lines_before_offset: List[Line] = [] lines_before_offset: List[Line] = []
lines_after_offset: List[Line] = [] lines_after_offset: List[Line] = []
result: List[Line] = [] result: List[Line] = []
lines_to_find = lines # + -1 * scroll_lines lines_to_find = lines + abs(scroll_lines)
with self._lock: with self._lock:
# TODO handle lines longer than 4096 bytes # TODO handle lines longer than 4096 bytes