81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
"""Native number inputs and scrollbars with usable click targets."""
|
|
from PySide6 import QtCore as C, QtGui as G, QtWidgets as W
|
|
|
|
|
|
class IntegerInput(W.QLineEdit):
|
|
valueChanged = C.Signal(int)
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self._value, self._minimum, self._maximum = 0, 0, 2147483647
|
|
self.setValidator(G.QIntValidator(self._minimum, self._maximum, self))
|
|
self.setText("0")
|
|
self.setMinimumWidth(76)
|
|
self.textChanged.connect(self.accept_number)
|
|
self.editingFinished.connect(self.finish_number)
|
|
|
|
def value(self):
|
|
return self._value
|
|
|
|
def setValue(self, value):
|
|
self.setText(str(min(self._maximum, max(self._minimum, int(value)))))
|
|
|
|
def setRange(self, minimum, maximum):
|
|
self._minimum, self._maximum = int(minimum), int(maximum)
|
|
self.validator().setRange(self._minimum, self._maximum)
|
|
self.setValue(self._value)
|
|
|
|
def setMinimum(self, minimum):
|
|
self.setRange(minimum, self._maximum)
|
|
|
|
def accept_number(self, text):
|
|
if self.hasAcceptableInput():
|
|
value = int(text)
|
|
if value != self._value:
|
|
self._value = value
|
|
self.valueChanged.emit(value)
|
|
|
|
def finish_number(self):
|
|
self.setValue(self._value)
|
|
|
|
|
|
class NativeScrollStyle(W.QProxyStyle):
|
|
def pixelMetric(self, metric, option=None, widget=None):
|
|
value = super().pixelMetric(metric, option, widget)
|
|
if metric == W.QStyle.PixelMetric.PM_ScrollBarExtent:
|
|
return max(22, value)
|
|
if metric == W.QStyle.PixelMetric.PM_ScrollBarSliderMin:
|
|
return max(30, value)
|
|
return value
|
|
|
|
def styleHint(self, hint, option=None, widget=None, returnData=None):
|
|
if hint == W.QStyle.StyleHint.SH_ScrollBar_Transient:
|
|
return 0
|
|
return super().styleHint(hint, option, widget, returnData)
|
|
|
|
|
|
class ScrollbarStyler(C.QObject):
|
|
def __init__(self, app):
|
|
super().__init__(app)
|
|
self.scroll_style = NativeScrollStyle(W.QStyleFactory.create("Fusion"))
|
|
self.scroll_style.setParent(self)
|
|
|
|
def eventFilter(self, widget, event):
|
|
if event.type() == C.QEvent.Type.Polish and isinstance(widget, W.QScrollBar) and not getattr(widget, "_ranch_scrollbar", False):
|
|
widget._ranch_scrollbar = True
|
|
widget.setStyle(self.scroll_style)
|
|
if widget.orientation() == C.Qt.Orientation.Horizontal:
|
|
widget.setFixedHeight(22)
|
|
else:
|
|
widget.setFixedWidth(22)
|
|
return False
|
|
|
|
|
|
def use_native_scrollbars():
|
|
app = W.QApplication.instance()
|
|
if app is None or getattr(app, "_ranch_scrollbar_styler", None) is not None:
|
|
return
|
|
styler = ScrollbarStyler(app)
|
|
app._ranch_scrollbar_styler = styler
|
|
app.installEventFilter(styler)
|