dndwidgets: improvements

This commit is contained in:
Simon Renblad 2024-01-16 10:29:58 +08:00
parent 4c9b0f1159
commit 21c391865f

View File

@ -59,7 +59,6 @@ class DragDropSplitter(QtWidgets.QSplitter):
class VDragScrollArea(QtWidgets.QScrollArea): class VDragScrollArea(QtWidgets.QScrollArea):
def __init__(self, parent): def __init__(self, parent):
QtWidgets.QScrollArea.__init__(self, parent) QtWidgets.QScrollArea.__init__(self, parent)
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.installEventFilter(self) self.installEventFilter(self)
self._margin = 40 self._margin = 40
self._timer = QtCore.QTimer(self) self._timer = QtCore.QTimer(self)
@ -68,31 +67,32 @@ class VDragScrollArea(QtWidgets.QScrollArea):
self._direction = 0 self._direction = 0
self._speed = 10 self._speed = 10
def setAutoScrollMargin(self, margin):
self._margin = margin
def setAutoScrollSpeed(self, speed):
self._speed = speed
def eventFilter(self, obj, e): def eventFilter(self, obj, e):
if e.type() == QtCore.QEvent.DragMove: if e.type() == QtCore.QEvent.DragMove:
val = self.verticalScrollBar().value() val = self.verticalScrollBar().value()
vp_height = self.viewport().height() height = self.viewport().height()
pos = e.pos() y = e.pos().y()
if pos.y() < val + self._margin:
self._direction = -1
elif pos.y() > vp_height + val - self._margin:
self._direction = 1
else:
self._direction = 0 self._direction = 0
if y < val + self._margin:
self._direction = -1
elif y > height + val - self._margin:
self._direction = 1
if not self._timer.isActive(): if not self._timer.isActive():
self._timer.start() self._timer.start()
elif e.type() in (QtCore.QEvent.Drop, QtCore.QEvent.DragLeave): elif e.type() in (QtCore.QEvent.Drop, QtCore.QEvent.DragLeave):
self._timer.stop() self._timer.stop()
return False return False
def setAutoScrollMargin(self, margin):
self._margin = margin
def _on_auto_scroll(self): def _on_auto_scroll(self):
val = self.verticalScrollBar().value() val = self.verticalScrollBar().value()
mini = self.verticalScrollBar().minimum() mn = self.verticalScrollBar().minimum()
maxi = self.verticalScrollBar().maximum() mx = self.verticalScrollBar().maximum()
dx = self._direction * self._speed dy = self._direction * self._speed
new_val = min(maxi, max(mini, val + dx)) new_val = min(mx, max(mn, val + dy))
self.verticalScrollBar().setValue(new_val) self.verticalScrollBar().setValue(new_val)