-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeModelGUI.py
173 lines (119 loc) · 5.55 KB
/
treeModelGUI.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from PyQt4 import QtGui
from PyQt4 import QtCore
import treeModel
from copy import deepcopy
# ---------------------------------------------------------------------
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow,self).__init__(parent)
self.resize(600,400)
self.setWindowTitle("Treeview Example")
#self.treeview = QtGui.QTreeView(self)
self.model = treeModel.TreeModel()
self.treeview = MyTreeView(self,model=self.model)
self.setCentralWidget(self.treeview)
class MyTreeView(QtGui.QTreeView):
def __init__(self, parent=None, model=None):
super(MyTreeView, self).__init__(parent)
self.setModel(model)
self.myModel = model
self.dragEnabled()
self.acceptDrops()
self.showDropIndicator()
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.connect(self.model(), QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), self.change)
#self.expandAll()
self.doubleClicked.connect(self.on_treeview_clicked)
#Context menu
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.openMenu)
#Node that has been cut:
self.cutIndex = None
self.copyIndex = None
def change(self, topLeftIndex, bottomRightIndex):
self.update(topLeftIndex)
#self.expandAll()
self.expand(topLeftIndex)
self.expanded()
def expanded(self):
for column in range(self.model().columnCount(QtCore.QModelIndex())):
self.resizeColumnToContents(column)
@QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeview_clicked(self, index):
"""This functions retrieves a treeItem that is double clicked
The @QtCore.pyqtSlot decorator seems unecessary but is keeps anyway
"""
treeItem = index.internalPointer()
displayData = treeItem.displayData
print(displayData[0])
def openMenu(self, position):
self.popup_menu = QtGui.QMenu(parent=self)
self.popup_menu.addAction("New", self.new)
self.popup_menu.addAction("Rename", self.new)
self.popup_menu.addSeparator()
self.popup_menu.addAction("Copy", self.copy)
self.popup_menu.addAction("Cut", self.cut)
self.popup_menu.addAction("Paste", self.paste)
self.popup_menu.addSeparator()
self.popup_menu.addAction("Delete", self.deleteItem)
self.popup_menu.exec_(self.viewport().mapToGlobal(position))
def new(self):
"""Unfinnished"""
currentIndex = self.currentIndex()
currentItem = currentIndex.internalPointer()
print currentItem.displayData[0]
def cut(self):
"""Cut a node"""
self.cutIndex = self.currentIndex()
self.copyIndex = None
def copy(self):
"""Copy a node"""
self.cutIndex = None
self.copyIndex = self.currentIndex()
def paste(self):
"""Paste a node
A node is pasted before the selected destination node
"""
sourceIndex = None
if self.cutIndex != None:
sourceIndex = self.cutIndex
elif self.copyIndex != None:
sourceIndex = self.copyIndex
if sourceIndex != None:
destinationIndex = self.currentIndex()
destinationItem = destinationIndex.internalPointer()
destinationParent = destinationItem.parent
destinationParentIndex = self.myModel.parent(index=destinationIndex)
sourceItem = sourceIndex.internalPointer()
sourceRow = sourceItem.row()
sourceParentIndex = self.myModel.parent(index=sourceIndex)
row = destinationItem.row()
if self.cutIndex != None:
self.myModel.moveItem(sourceParentIndex=sourceParentIndex,
sourceRow=sourceRow,
destinationParentIndex=destinationParentIndex,
destinationRow = row)
else:
self.myModel.copyItem(sourceParentIndex=sourceParentIndex,
sourceRow=sourceRow,
destinationParentIndex=destinationParentIndex,
destinationRow = row)
def deleteItem(self):
"""Deletes the current item (node)"""
currentIndex = self.currentIndex()
currentItem = currentIndex.internalPointer()
quitMessage = "Are you sure that %s should be deleted?" % currentItem.displayData
messageBox = QtGui.QMessageBox(parent=self)
reply = messageBox.question(self, 'Message',
quitMessage, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
#This item is this row at its parents child list:
row = currentItem.row()
parentIndex = self.myModel.parent(index = currentIndex)
self.myModel.removeRow(row=row,parentIndex=parentIndex)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())