colName = {
'Experiments': ['Name','Researcher','Date','Solvents','Spheres'],
'Solvents': ['Name','dD','dH','dP','MVol','Score'],
'Spheres': ['Name','dD','dH','dP','R']
}
def __init__(self,parent=None):
self.Experiments = DM.LoadExperiments()
def colIndex(self,level,colName):
return self.colName[level].index(colName)
def headerData(self,section,orientation,role = Qt.DisplayRole):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return self.colName['Experiments'][section]
if not parent.isValid(): # Top level experiment
return self.createIndex(row,col,self.Experiments[row])
Experiment = parent.internalPointer()
if parent.column() == self.colIndex('Experiments','Solvents'):
return self.createIndex(row,col,Experiment['Solvents'][row])
elif parent.column() == self.colIndex('Experiments','Sphers'):
return self.createIndex(row,col,Experiment['Spheres'][row])
def parent(self,index):
if not parent.isValid():
return len(self.Experiments)
elif parent.column() == self.colIndex('Experiments','Solvents'):
return len(self.Experiments[parent.row()]['Solvents'])
elif parent.column() == self.colIndex('Experiments','Spheres'):
return len(self.Experiments[parent.row()]['Spheres'])
return 0
if not parent.isValid():
return len(self.colName['Experiments'])
elif parent.column() == self.colIndex('Experiments','Solvents'):
return len(self.colName['Solvents'])
elif parent.column() == self.colIndex('Experiments','Sphers'):
return len(self.colName['Spheres'])
return 0
def data(self,index,role=Qt.DisplayRole):
if role == Qt.DisplayRole:
row, col = index.row(), index.column()
if not index.parent().isValid(): # Then it is an experiment
colName = self.colName['Experiments'][col]
return self.Experiments[row][colName]
else:
pCol, pRow = index.parent().column(), index.parent().row()
# If we are looking at a solvent
if pCol == self.colIndex('Experiments','Solvents'):
colName = self.colName['Solvents'][col]
return self.Experiments[pRow]['Solvents'][row][colName]
# If we are looking at a sphere
elif pCol == self.colIndex('Experiments','Spheres'):
colName = self.colName['Spheres'][col]
return self.Experiments[pRow]['Spheres'][row][colName]
class ExperimentsModelTest(QAbstractItemModel):
colName = {
'Experiments': ['Name','Researcher','Date','Solvents','Spheres'],
'Solvents': ['Name','dD','dH','dP','MVol','Score'],
'Spheres': ['Name','dD','dH','dP','R']
}
def __init__(self,parent=None):
QAbstractItemModel.__init__(self,parent=None)
self.Experiments = DM.LoadExperiments()
def colIndex(self,level,colName):
return self.colName[level].index(colName)
def headerData(self,section,orientation,role = Qt.DisplayRole):
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
return self.colName['Experiments'][section]
return QtCore.QVariant()
def index(self,row,col,parent=QModelIndex()):
if not parent.isValid(): # Top level experiment
return self.createIndex(row,col,self.Experiments[row])
Experiment = parent.internalPointer()
if parent.column() == self.colIndex('Experiments','Solvents'):
return self.createIndex(row,col,Experiment['Solvents'][row])
elif parent.column() == self.colIndex('Experiments','Sphers'):
return self.createIndex(row,col,Experiment['Spheres'][row])
def parent(self,index):
return QModelIndex()
def rowCount(self,parent = QModelIndex()):
if not parent.isValid():
return len(self.Experiments)
elif parent.column() == self.colIndex('Experiments','Solvents'):
return len(self.Experiments[parent.row()]['Solvents'])
elif parent.column() == self.colIndex('Experiments','Spheres'):
return len(self.Experiments[parent.row()]['Spheres'])
return 0
def columnCount(self,parent = QModelIndex()):
if not parent.isValid():
return len(self.colName['Experiments'])
elif parent.column() == self.colIndex('Experiments','Solvents'):
return len(self.colName['Solvents'])
elif parent.column() == self.colIndex('Experiments','Sphers'):
return len(self.colName['Spheres'])
return 0
def data(self,index,role=Qt.DisplayRole):
if role == Qt.DisplayRole:
row, col = index.row(), index.column()
if not index.parent().isValid(): # Then it is an experiment
colName = self.colName['Experiments'][col]
return self.Experiments[row][colName]
else:
pCol, pRow = index.parent().column(), index.parent().row()
# If we are looking at a solvent
if pCol == self.colIndex('Experiments','Solvents'):
colName = self.colName['Solvents'][col]
return self.Experiments[pRow]['Solvents'][row][colName]
# If we are looking at a sphere
elif pCol == self.colIndex('Experiments','Spheres'):
colName = self.colName['Spheres'][col]
return self.Experiments[pRow]['Spheres'][row][colName]
return QtCore.QVariant()
To copy to clipboard, switch view to plain text mode
Bookmarks