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()
src = e.source()
src_i = self.indexOf(src)
# case 0: smaller than source widget
if pos.y() < src.y():
for n in range(src_i):
@ -33,46 +32,34 @@ class DragDropSplitter(QtWidgets.QSplitter):
break
# case 2: greater than source widget
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)
if pos.y() < w.y() + w.size().height():
self.setRubberBand(w.y())
self.setRubberBand(w.y() + w.size().height())
break
else:
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()
def dropEvent(self, e):
pos = e.pos()
widget = e.source()
index = self.indexOf(widget)
self.setRubberBand(-1)
for n in range(self.count()):
w = self.widget(n)
if n <= index:
k = n
else:
k = n - 1
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.insertWidget(k, widget)
break
elif n == self.count() - 1:
self.insertWidget(-1, widget)
pos = e.pos()
src = e.source()
src_i = self.indexOf(src)
# case 0: smaller than source widget
if pos.y() < src.y():
for n in range(src_i):
w = self.widget(n)
if pos.y() < w.y() + w.size().height():
self.insertWidget(n, src)
break
# case 2: greater than source widget
elif pos.y() > src.y() + src.size().height():
for n in range(src_i + 1, self.count()):
w = self.widget(n)
if pos.y() < w.y() + w.size().height():
self.insertWidget(n, src)
break
e.accept()