-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtDataframe.py
174 lines (167 loc) · 7.19 KB
/
qtDataframe.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
174
from PyQt5.QtWidgets import QTableWidgetItem
class IQDataFrameWidget():
def __init__(self,QTable,columnH=True, RowH=False, flags=None):
self.flags=flags
self.ColumnH=columnH
self.RowH=RowH
self.QTable=QTable
self.df=None
self.lastR=None
self.lastC=None
#self.QTable.itemSelectionChanged.connect(lambda:self.checkAddRC())
self.setCaddCallback(print)
self.setRaddCallback(print)
self.setAddRCLabel("Add Track","Add Info")
#self.QTable.itemSelectionChanged.connect(lambda:self.checkAddRC(False,True))
self.QTable.itemChanged.connect(lambda:self.checkAddRC(True,False))
self.QTable.itemClicked.connect(self.clk)
#self.checkAddRC(row=True, column=False,init=True)
#self.QTable.itemEntered.connect(lambda:self.checkAddRC(True,False))
#self.QTable.itemDoubleClicked
self.rowClickedCallback=None
self.Rrename_callback=None
self.RemoveRowCallback=None
self.lastTrack=None
self.QTable.show()
self.prog = 0
self.progCallBack = None
def setprogCallBack(self, cb):
self.progCallBack = cb
def setProg(self, prog, mode="abs"):
if mode == "abs":
self.prog=prog
elif mode == "add":
self.prog+=prog
if self.progCallBack:
self.progCallBack(int(self.prog))
def setRowRenameCallback(self,cb):
self.Rrename_callback=cb
def setRowClickedCallback(self,cb):
self.rowClickedCallback=cb
def setRemoveRowCallback(self,cb):
self.RemoveRowCallback=cb
def clk(self,event=None):
if event==None:
self.lastR=self.QTable.currentRow()
self.lastC=self.QTable.currentColumn()
self.lastT=self.QTable.item(self.lastR,self.lastC).text()
self.lastTrack=self.QTable.item(self.lastR,0).text()
else:
self.lastR=event.row()
self.lastC=event.column()
self.lastT=event.text()
self.lastTrack=self.QTable.item(self.lastR,0).text()
if self.rowClickedCallback:
self.rowClickedCallback(self.QTable.item(self.lastR,0).text())
def setCaddCallback(self,func):
#print("Qdataframe setCaddCallback ",func)
self.Cadd_callback=func
def setRaddCallback(self,func):
#print("Qdataframe setRaddCallback",func)
self.Radd_callback=func
def getDataFrame(self):
#print("Qdataframe getDataFrame")
for x,h in enumerate(self.df.columns):
for y,v in enumerate(self.df.index):
self.df[h][v]=self.QTable.item(x,y).text()
return self.df
def setAddRCLabel(self,Rlab,Clab):
#print("Qdataframe setAddRCLabel ",Rlab,Clab)
self.Raddlab=Rlab
self.Caddlab=Clab
def rmCurrentRow(self):
if self.Raddlab and (self.lastTrack==self.Raddlab):
return False
print("Qdataframe rmCurrentRow from "+str(self.lastR)+" "+str(self.lastC)+" "+self.lastTrack)
self.df=self.df[self.df["Track name"]!=self.lastTrack]
print(self.df)
self.QTable.removeRow(self.lastR)
self.QTable.setCurrentCell(self.lastR,self.lastC)
self.RemoveRowCallback()
self.clk()
print("Qdataframe rmCurrentRow to "+str(self.lastR)+" "+str(self.lastC)+" "+self.lastTrack)
return True
def checkAddRC(self,row=True, column=False,Rheader=None,Cheader=None,init=False):
""" header = None or True / False """
if self.flags:
if self.flags["checkAddRC"]:
return True
self.flags["checkAddRC"]=True
if column and (((self.QTable.columnCount()-1)==self.QTable.currentColumn()) or init):
txt=self.QTable.item(self.QTable.currentColumn(),0)
if txt==None:
txt=""
else:
txt=txt.text()
if txt!=self.Caddlab or init:
self.Cadd_callback(txt)
self.QTable.insertColumn(self.QTable.columnCount())
if Cheader==(None or True):
self.QTable.setItem(self.QTable.columnCount(),0,QTableWidgetItem(self.Caddlab))
if row==True and (((self.QTable.rowCount()-1)==self.QTable.currentRow()) or init):
txt=self.QTable.item(self.QTable.currentRow(),0)
if txt==None:
self.QTable.setRowCount(self.QTable.rowCount()+1)
#self.QTable.insertRow(self.QTable.rowCount()-1)
self.QTable.setItem(self.QTable.rowCount()-1,0,QTableWidgetItem(self.Raddlab))
else:
txt=txt.text()
condDF=self.df[self.df["Track name"]==txt].shape[0]==0
if (txt!=self.Raddlab) and condDF:
self.QTable.setRowCount(self.QTable.rowCount()+1)
#self.QTable.insertRow(self.QTable.rowCount()-1)
self.Radd_callback(txt)
self.df.loc[self.df.shape[0]]=""
self.df["Track name"][self.df.shape[0]-1]=txt
self.QTable.setItem(self.QTable.rowCount()-1,0,QTableWidgetItem(self.Raddlab))
self.clk()
elif not condDF and (self.lastTrack==self.Raddlab):
self.QTable.setItem(self.lastR,0,QTableWidgetItem(self.Raddlab))
elif row:
txt=self.QTable.item(self.QTable.currentRow(),0)
if txt!=None:
txt=txt.text()
if self.lastTrack and txt!=self.lastTrack:
self.df["Track name"][self.lastR]=txt
if self.Rrename_callback:
self.Rrename_callback(txt)
self.clk()
if self.flags:
self.flags["checkAddRC"]=False
def setDataFrame(self,df,update=1):
self.setProg(0)
if update:
dif=self.df!=df
else:
dif=df==df
self.df=df
if self.ColumnH:
self.QTable.horizontalHeader().setVisible(1)
else:
self.QTable.horizontalHeader().setVisible(0)
if self.RowH:
self.QTable.verticalHeader().setVisible(1)
else:
self.QTable.verticalHeader().setVisible(0)
self.QTable.setRowCount(df.shape[0])
self.QTable.setColumnCount(df.shape[1])
rowHOK=0
if df.shape[0]>0:
progStep = 100/(df.shape[0]*df.shape[1])
else:
progStep = 100
for x,h in enumerate(df.columns):
if self.ColumnH:
self.QTable.setHorizontalHeaderItem(x,QTableWidgetItem(str(h)))
for y,v in enumerate(df.index):
if self.RowH and not rowHOK:
self.QTable.setVerticalHeaderItem(y,QTableWidgetItem(str(v)))
if (type(dif[h][v])==bool or type(dif[h][v])==int) and dif[h][v]==1:
self.QTable.setItem(y,x,QTableWidgetItem(str(df[h][v])))
elif type(dif[h][v])!=bool and type(dif[h][v])!=int:
self.QTable.setItem(y,x,QTableWidgetItem(str(df[h][v])))
self.setProg(progStep, "add")
rowHOK=1
self.setProg(progStep, "add")
self.df=df
self.QTable.show()