-
Notifications
You must be signed in to change notification settings - Fork 0
/
qanytreeview.py
59 lines (42 loc) · 1.74 KB
/
qanytreeview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from PyQt5.QtWidgets import QTreeView, QAbstractItemView
from PyQt5.QtCore import Qt
class QAnyTreeView(QTreeView):
def __init__(self, parent=None):
super().__init__(parent)
self.draggedItem = None
self.setDragDropMode(QAbstractItemView.InternalMove)
def setModel(self, model):
super().setModel(model)
# Connect model's rowsMoved signal
try:
model.rowsMoved.connect(self.rowsMoved, Qt.UniqueConnection)
except TypeError:
pass
def rowsMoved(self, parent, start, end, destination, row):
if parent == destination and start < row:
row -= 1
index = self.model().index(row, 0, parent)
self.setCurrentIndex(index)
def dragEnterEvent(self, e):
indexes = self.selectedIndexes()
if indexes:
self.draggedItem = self.itemFromIndex(indexes[0])
super().dragEnterEvent(e)
def dragMoveEvent(self, e):
if self.draggedItem:
droppedIndex = self.indexAt(e.pos())
droppedItem = self.itemFromIndex(droppedIndex)
# If drop location is invalid or does not share the dragged item's parent
if not droppedIndex.isValid() or droppedItem.parent != self.draggedItem.parent:
e.ignore()
return
e.accept()
def dropEvent(self, e):
if self.draggedItem:
droppedIndex = self.indexAt(e.pos())
parentIndex = droppedIndex.parent()
oldPosition = self.draggedItem.childNumber()
newPosition = droppedIndex.row()
self.model().moveRow(parentIndex, oldPosition, parentIndex, newPosition)
def itemFromIndex(self, index):
return self.model().getItem(index)