horizontal scrolling
This commit is contained in:
@@ -10,8 +10,8 @@ from typing import List
|
|||||||
|
|
||||||
from PySide6 import QtCore, QtGui
|
from PySide6 import QtCore, QtGui
|
||||||
from PySide6.QtGui import QPaintEvent, QPainter, QFont, QFontMetrics, QColor, QBrush, QWheelEvent
|
from PySide6.QtGui import QPaintEvent, QPainter, QFont, QFontMetrics, QColor, QBrush, QWheelEvent
|
||||||
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QStatusBar
|
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QStatusBar, QGridLayout, QSizePolicy, QScrollBar
|
||||||
from PySide6.QtCore import QTimer, QPoint, Qt, QRect, QLine
|
from PySide6.QtCore import QTimer, QPoint, Qt, QRect, QLine, Slot
|
||||||
import sys
|
import sys
|
||||||
from src.pluginregistry import PluginRegistry
|
from src.pluginregistry import PluginRegistry
|
||||||
import gettext
|
import gettext
|
||||||
@@ -19,6 +19,8 @@ import gettext
|
|||||||
__version__ = '0.2.1'
|
__version__ = '0.2.1'
|
||||||
|
|
||||||
from src.i18n import _
|
from src.i18n import _
|
||||||
|
from src.ui.bigtext.BigScrollBar import BigScrollBar
|
||||||
|
from src.ui.bigtext.bigtext import InnerBigText
|
||||||
|
|
||||||
gettext.install('krowlog', 'locale')
|
gettext.install('krowlog', 'locale')
|
||||||
|
|
||||||
@@ -73,7 +75,7 @@ class MainWindow(QMainWindow):
|
|||||||
self.setWindowTitle(_("KrowLog"))
|
self.setWindowTitle(_("KrowLog"))
|
||||||
self.setMinimumWidth(800)
|
self.setMinimumWidth(800)
|
||||||
self.setMinimumHeight(880)
|
self.setMinimumHeight(880)
|
||||||
bigger_text = BiggerText()
|
bigger_text = BiggerText(FileModel("testdata/testset.txt"))
|
||||||
self.setCentralWidget(bigger_text)
|
self.setCentralWidget(bigger_text)
|
||||||
self.status_bar = QStatusBar(self)
|
self.status_bar = QStatusBar(self)
|
||||||
self.setStatusBar(self.status_bar)
|
self.setStatusBar(self.status_bar)
|
||||||
@@ -240,17 +242,53 @@ class Selection:
|
|||||||
def max_byte(self) -> int:
|
def max_byte(self) -> int:
|
||||||
return max(self.start.pos(), self.end.pos())
|
return max(self.start.pos(), self.end.pos())
|
||||||
|
|
||||||
class BiggerText(QWidget):
|
|
||||||
|
|
||||||
def __init__(self, ):
|
class BiggerText(QWidget):
|
||||||
|
def __init__(self, model: FileModel):
|
||||||
super(BiggerText, self).__init__()
|
super(BiggerText, self).__init__()
|
||||||
|
|
||||||
|
self._model = model
|
||||||
|
self.grid = QGridLayout()
|
||||||
|
self.grid.setContentsMargins(0, 0, 0, 0)
|
||||||
|
self.grid.setHorizontalSpacing(0)
|
||||||
|
self.grid.setVerticalSpacing(0)
|
||||||
|
self.setLayout(self.grid)
|
||||||
|
|
||||||
|
self.v_scroll_bar = BigScrollBar()
|
||||||
|
|
||||||
|
self.big_text_area = BiggerTextArea(self, model, self.v_scroll_bar)
|
||||||
|
self.big_text_area.setSizePolicy(QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding))
|
||||||
|
|
||||||
|
self.h_scroll_bar = QScrollBar(Qt.Orientation.Horizontal)
|
||||||
|
self.h_scroll_bar.setMinimum(0)
|
||||||
|
self.h_scroll_bar.setMaximum(1)
|
||||||
|
self.h_scroll_bar.valueChanged.connect(self.big_text_area.h_scroll_event)
|
||||||
|
|
||||||
|
# self.v_scroll_bar.value_changed.connect(self.big_text_area.v_scroll_value_changed)
|
||||||
|
# self.v_scroll_bar.scroll_event.connect(self.big_text_area.v_scroll_event)
|
||||||
|
|
||||||
|
self.grid.addWidget(self.big_text_area, 0, 1)
|
||||||
|
self.grid.addWidget(self.h_scroll_bar, 1, 1)
|
||||||
|
self.grid.addWidget(self.v_scroll_bar, 0, 2)
|
||||||
|
|
||||||
|
|
||||||
|
class BiggerTextArea(QWidget):
|
||||||
|
|
||||||
|
def __init__(self, parent: BiggerText, model: FileModel, v_scroll_bar: BigScrollBar):
|
||||||
|
super(BiggerTextArea, self).__init__()
|
||||||
|
self.parent = parent
|
||||||
|
|
||||||
|
self._v_scroll_bar = v_scroll_bar
|
||||||
|
|
||||||
|
self._left_offset = 0
|
||||||
|
self.longest_line = 1
|
||||||
|
|
||||||
self._font_size = 20
|
self._font_size = 20
|
||||||
self.selection = Selection()
|
self.selection = Selection()
|
||||||
self.mouse_pressed = False
|
self.mouse_pressed = False
|
||||||
|
|
||||||
self._encoding = "utf8"
|
self._encoding = "utf8"
|
||||||
self.file_model: FileModel = FileModel("testdata/testset.txt")
|
self.file_model: FileModel = model
|
||||||
|
|
||||||
# font ="Andale Mono"
|
# font ="Andale Mono"
|
||||||
# font = "JetBrains Mono"
|
# font = "JetBrains Mono"
|
||||||
@@ -288,6 +326,12 @@ class BiggerText(QWidget):
|
|||||||
# print("wheel event fired :) %s" % (direction))
|
# print("wheel event fired :) %s" % (direction))
|
||||||
self.scroll_by_lines(direction * 3)
|
self.scroll_by_lines(direction * 3)
|
||||||
|
|
||||||
|
@Slot()
|
||||||
|
def h_scroll_event(self, left_offset: int):
|
||||||
|
self._left_offset = left_offset
|
||||||
|
# print("left_offset: %d" % left_offset)
|
||||||
|
self.update()
|
||||||
|
|
||||||
def to_byte_offset(self, pos: QPoint) -> SelectionPos:
|
def to_byte_offset(self, pos: QPoint) -> SelectionPos:
|
||||||
|
|
||||||
line_number = self.y_pos_to_line_number_on_screen(pos.y())
|
line_number = self.y_pos_to_line_number_on_screen(pos.y())
|
||||||
@@ -327,6 +371,17 @@ class BiggerText(QWidget):
|
|||||||
def y_pos_to_line_number_on_screen(self, y: int) -> int:
|
def y_pos_to_line_number_on_screen(self, y: int) -> int:
|
||||||
return int(y / self.char_height)
|
return int(y / self.char_height)
|
||||||
|
|
||||||
|
def update_longest_line(self, lines: [Line]):
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
width_for_full_line = self.font_metric.horizontalAdvance(line.text())
|
||||||
|
# print("width_in_chars: %d" % width_in_chars)
|
||||||
|
if self.longest_line < width_for_full_line:
|
||||||
|
self.longest_line = width_for_full_line
|
||||||
|
|
||||||
|
maximum = max(0, self.longest_line - self.width() + 1)
|
||||||
|
self.parent.h_scroll_bar.setMaximum(round(maximum))
|
||||||
|
|
||||||
def paintEvent(self, event: QPaintEvent) -> None:
|
def paintEvent(self, event: QPaintEvent) -> None:
|
||||||
start_ns = time.process_time_ns()
|
start_ns = time.process_time_ns()
|
||||||
painter = QPainter(self)
|
painter = QPainter(self)
|
||||||
@@ -341,6 +396,8 @@ class BiggerText(QWidget):
|
|||||||
|
|
||||||
self.lines_to_render: [Line] = self.file_model.read(0, lines_to_read, 200, self._encoding)
|
self.lines_to_render: [Line] = self.file_model.read(0, lines_to_read, 200, self._encoding)
|
||||||
|
|
||||||
|
self.update_longest_line(self.lines_to_render)
|
||||||
|
|
||||||
painter.setPen(QColor(0, 0, 0))
|
painter.setPen(QColor(0, 0, 0))
|
||||||
|
|
||||||
line_on_screen = 1
|
line_on_screen = 1
|
||||||
@@ -379,13 +436,14 @@ class BiggerText(QWidget):
|
|||||||
painter.setPen(QColor(0, 0, 0, 0))
|
painter.setPen(QColor(0, 0, 0, 0))
|
||||||
|
|
||||||
painter.drawRect(
|
painter.drawRect(
|
||||||
QRect(x_start, int(line_on_screen * self.char_height + int(self.char_height * 0.1)), x_end,
|
QRect(x_start - self._left_offset,
|
||||||
|
int(line_on_screen * self.char_height + int(self.char_height * 0.1)), x_end,
|
||||||
-self.char_height))
|
-self.char_height))
|
||||||
|
|
||||||
painter.setBrush(prev_brush)
|
painter.setBrush(prev_brush)
|
||||||
painter.setPen(prev_pen)
|
painter.setPen(prev_pen)
|
||||||
|
|
||||||
painter.drawText(QPoint(0, line_on_screen * self.char_height), line.text())
|
painter.drawText(QPoint(-self._left_offset, line_on_screen * self.char_height), line.text())
|
||||||
line_on_screen = line_on_screen + 1
|
line_on_screen = line_on_screen + 1
|
||||||
|
|
||||||
painter.end()
|
painter.end()
|
||||||
|
|||||||
Reference in New Issue
Block a user