dndwidgets: improved drag drop splitter

This commit is contained in:
Simon Renblad 2024-01-15 16:55:02 +08:00
parent 9e6b54a7df
commit 19bcc2357b

View File

@ -23,7 +23,6 @@ class DragDropSplitter(QtWidgets.QSplitter):
pos = e.pos() pos = e.pos()
src = e.source() src = e.source()
src_i = self.indexOf(src) src_i = self.indexOf(src)
# case 0: smaller than source widget # case 0: smaller than source widget
if pos.y() < src.y(): if pos.y() < src.y():
for n in range(src_i): for n in range(src_i):
@ -33,46 +32,34 @@ class DragDropSplitter(QtWidgets.QSplitter):
break break
# case 2: greater than source widget # case 2: greater than source widget
elif pos.y() > src.y() + src.size().height(): elif pos.y() > src.y() + src.size().height():
for n in range(src_i): for n in range(src_i + 1, self.count()):
w = self.widget(n) w = self.widget(n)
if pos.y() < w.y() + w.size().height(): if pos.y() < w.y() + w.size().height():
self.setRubberBand(w.y()) self.setRubberBand(w.y() + w.size().height())
break break
else: else:
self.setRubberBand(-1) self.setRubberBand(-1)
for n in range(self.count()):
w = self.widget(n)
if self.orientation() == QtCore.Qt.Vertical:
pos_p, w_p, w_s = pos.y(), w.y(), w.size().height()
else:
pos_p, w_p, w_s = pos.x(), w.x(), w.size().width()
if pos_p < w_p + w_s // 2:
self.setRubberBand(w_p)
break
elif n == self.count() - 1:
self.setRubberBand(w_p + w_s)
e.accept() e.accept()
def dropEvent(self, e): def dropEvent(self, e):
pos = e.pos()
widget = e.source()
index = self.indexOf(widget)
self.setRubberBand(-1) self.setRubberBand(-1)
for n in range(self.count()): pos = e.pos()
w = self.widget(n) src = e.source()
if n <= index: src_i = self.indexOf(src)
k = n # case 0: smaller than source widget
else: if pos.y() < src.y():
k = n - 1 for n in range(src_i):
if self.orientation() == QtCore.Qt.Vertical: w = self.widget(n)
pos_p, w_p, w_s = pos.y(), w.y(), w.size().height() if pos.y() < w.y() + w.size().height():
else: self.insertWidget(n, src)
pos_p, w_p, w_s = pos.x(), w.x(), w.size().width() break
if pos_p < w_p + w_s // 2: # case 2: greater than source widget
self.insertWidget(k, widget) elif pos.y() > src.y() + src.size().height():
break for n in range(src_i + 1, self.count()):
elif n == self.count() - 1: w = self.widget(n)
self.insertWidget(-1, widget) if pos.y() < w.y() + w.size().height():
self.insertWidget(n, src)
break
e.accept() e.accept()