replace ScaledScrollBar with BigScrollBar

step 2 - connect the line up/down, page up/down events
This commit is contained in:
2024-04-14 09:06:17 +02:00
parent 3d6cf84cd7
commit 9b9399f120
2 changed files with 51 additions and 7 deletions

View File

@@ -1,4 +1,6 @@
from PySide6.QtWidgets import QWidget, QStylePainter, QStyle, QStyleOptionSlider, QSlider import enum
from PySide6.QtWidgets import QWidget, QStylePainter, QStyle, QStyleOptionSlider, QSlider, QAbstractSlider
from PySide6.QtCore import Qt, QSize, QEvent, QRect, QPoint, Signal from PySide6.QtCore import Qt, QSize, QEvent, QRect, QPoint, Signal
@@ -11,6 +13,14 @@ class BigScrollBar(QWidget):
code involved. We work around this by converting the python int code involved. We work around this by converting the python int
into a string.""" into a string."""
class ScrollEvent(enum.IntEnum):
PageUp = 1
PageDown = 2
LinesUp = 3
LinesDown = 4
scroll_event = Signal(ScrollEvent)
pressedControl = QStyle.SubControl.SC_None pressedControl = QStyle.SubControl.SC_None
click_offset = 0 click_offset = 0
@@ -104,13 +114,13 @@ class BigScrollBar(QWidget):
self.pressedControl = self.style().hitTestComplexControl(QStyle.ComplexControl.CC_ScrollBar, style_options, self.pressedControl = self.style().hitTestComplexControl(QStyle.ComplexControl.CC_ScrollBar, style_options,
event.position().toPoint(), self) event.position().toPoint(), self)
# print(f"pressedControl {self.pressedControl}") print(f"pressedControl {self.pressedControl}")
sr: QRect = self.style().subControlRect(QStyle.ComplexControl.CC_ScrollBar, style_options, sr: QRect = self.style().subControlRect(QStyle.ComplexControl.CC_ScrollBar, style_options,
QStyle.SubControl.SC_ScrollBarSlider, self) QStyle.SubControl.SC_ScrollBarSlider, self)
click: QPoint = event.position().toPoint() click: QPoint = event.position().toPoint()
pressYValue = click.y() - sr.center().y() + sr.topLeft().y() press_y_value = click.y() - sr.center().y() + sr.topLeft().y()
pressYValue = self.pixelPosToRangeValue(pressYValue) press_y_value = self.pixelPosToRangeValue(press_y_value)
# print(f"pressYValue {pressYValue}") # print(f"pressYValue {pressYValue}")
if self.pressedControl == QStyle.SubControl.SC_ScrollBarSlider: if self.pressedControl == QStyle.SubControl.SC_ScrollBarSlider:
@@ -126,6 +136,27 @@ class BigScrollBar(QWidget):
self.click_offset = slider_length / 2 self.click_offset = slider_length / 2
pass pass
self.activateControl(self.pressedControl)
# self.repaint(self.style().subControlRect(QStyle.ComplexControl.CC_ScrollBar, style_options, self.pressedControl))
def activateControl(self, control):
action = QAbstractSlider.SliderAction.SliderNoAction
match control:
case QStyle.SubControl.SC_ScrollBarAddPage:
self.scroll_event.emit(self.ScrollEvent.PageDown)
case QStyle.SubControl.SC_ScrollBarSubPage:
self.scroll_event.emit(self.ScrollEvent.PageUp)
case QStyle.SubControl.SC_ScrollBarAddLine:
self.scroll_event.emit(self.ScrollEvent.LinesDown)
case QStyle.SubControl.SC_ScrollBarSubLine:
self.scroll_event.emit(self.ScrollEvent.LinesUp)
case QStyle.SubControl.SC_ScrollBarFirst:
self.set_value(self.minimun)
case QStyle.SubControl.SC_ScrollBarLast:
self.set_value(self.maximum)
case _:
pass
def mouseReleaseEvent(self, event): def mouseReleaseEvent(self, event):
self.pressedControl = QStyle.SubControl.SC_None self.pressedControl = QStyle.SubControl.SC_None
@@ -169,5 +200,5 @@ class BigScrollBar(QWidget):
def set_value(self, value: int): def set_value(self, value: int):
self.value = value self.value = value
self.value_changed.emit(str(value)) self.value_changed.emit(str(self.value))
self.repaint() self.repaint()

View File

@@ -98,7 +98,8 @@ class BigText(QWidget):
self.h_scroll_bar.valueChanged.connect(self.big_text.h_scroll_event) self.h_scroll_bar.valueChanged.connect(self.big_text.h_scroll_event)
# self.v_scroll_bar.setPageStep(1) # self.v_scroll_bar.setPageStep(1)
self.v_scroll_bar.value_changed.connect(self.big_text.v_scroll_event) self.v_scroll_bar.value_changed.connect(self.big_text.v_scroll_value_changed)
self.v_scroll_bar.scroll_event.connect(self.big_text.v_scroll_event)
if show_range_slider: if show_range_slider:
self.range_limit = RangeSlider() self.range_limit = RangeSlider()
@@ -399,10 +400,22 @@ class InnerBigText(QWidget):
self.update() self.update()
@Slot() @Slot()
def v_scroll_event(self, byte_offset: str): def v_scroll_value_changed(self, byte_offset: str):
self._byte_offset = int(byte_offset) self._byte_offset = int(byte_offset)
self.update() self.update()
@Slot()
def v_scroll_event(self, event: BigScrollBar.ScrollEvent):
match event:
case BigScrollBar.ScrollEvent.LinesUp:
self.scroll_by_lines(-3)
case BigScrollBar.ScrollEvent.LinesDown:
self.scroll_by_lines(3)
case BigScrollBar.ScrollEvent.PageUp:
self.scroll_by_lines(-(int(self.lines_shown()) - 1))
case BigScrollBar.ScrollEvent.PageDown:
self.scroll_by_lines(int(self.lines_shown()) - 1)
def update_longest_line(self, length: int): def update_longest_line(self, length: int):
width_in_chars = self.width() / self.char_width width_in_chars = self.width() / self.char_width
# print("width_in_chars: %d" % width_in_chars) # print("width_in_chars: %d" % width_in_chars)