def _populate_table(front, tbl, col_headers, col_editable, row_list, row2_datatup): #front.printDBG('_populate_table()') hheader = tbl.horizontalHeader() def set_header_context_menu(hheader): hheader.setContextMenuPolicy(Qt.CustomContextMenu) # TODO: for chip table: delete metedata column opt2_callback = [ ('header', lambda: print('finishme')), ('cancel', lambda: print('cancel')), ] # HENDRIK / JASON TODO: # I have a small right-click context menu working # Maybe one of you can put some useful functions in these? popup_slot = guitools.popup_menu(tbl, opt2_callback) hheader.customContextMenuRequested.connect(popup_slot) def set_table_context_menu(tbl): tbl.setContextMenuPolicy(Qt.CustomContextMenu) # RCOS TODO: How do we get the clicked item on a right click? # tbl.selectedItems # tbl.selectedIndexes opt2_callback = [ ('Query', front.querySignal.emit), ] #('item', lambda: print('finishme')), #('cancel', lambda: print('cancel')), ] popup_slot = guitools.popup_menu(tbl, opt2_callback) tbl.customContextMenuRequested.connect(popup_slot) #set_header_context_menu(hheader) set_table_context_menu(tbl) sort_col = hheader.sortIndicatorSection() sort_ord = hheader.sortIndicatorOrder() tbl.sortByColumn(0, Qt.AscendingOrder) # Basic Sorting tblWasBlocked = tbl.blockSignals(True) tbl.clear() tbl.setColumnCount(len(col_headers)) tbl.setRowCount(len(row_list)) tbl.verticalHeader().hide() tbl.setHorizontalHeaderLabels(col_headers) tbl.setSelectionMode(QAbstractItemView.SingleSelection) tbl.setSelectionBehavior(QAbstractItemView.SelectRows) tbl.setSortingEnabled(False) for row in iter(row_list): data_tup = row2_datatup[row] for col, data in enumerate(data_tup): item = QtGui.QTableWidgetItem() # RCOS TODO: Pass in datatype here. #if col_headers[col] == 'AIF': #print('col=%r dat=%r, %r' % (col, data, type(data))) if tools.is_bool(data) or data == 'True' or data == 'False': bit = bool(data) #print(bit) if bit: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) #item.setData(Qt.DisplayRole, bool(data)) elif tools.is_int(data): item.setData(Qt.DisplayRole, int(data)) elif tools.is_float(data): item.setData(Qt.DisplayRole, float(data)) else: item.setText(str(data)) item.setTextAlignment(Qt.AlignHCenter) if col_editable[col]: item.setFlags(item.flags() | Qt.ItemIsEditable) #print(item.getBackground()) item.setBackground(QtGui.QColor(250, 240, 240)) else: item.setFlags(item.flags() ^ Qt.ItemIsEditable) tbl.setItem(row, col, item) tbl.setSortingEnabled(True) tbl.sortByColumn(sort_col, sort_ord) # Move back to old sorting tbl.show() tbl.blockSignals(tblWasBlocked)
def make_csv_table(column_labels=None, column_list=[], header='', column_type=None): if len(column_list) == 0: print('[ld2] No columns') return header column_len = [len(col) for col in column_list] num_data = column_len[0] if num_data == 0: print('[ld2.make_csv_table()] No data. (header=%r)' % (header,)) return header if any([num_data != clen for clen in column_len]): print('[lds] column_labels = %r ' % (column_labels,)) print('[lds] column_len = %r ' % (column_len,)) print('[ld2] inconsistent column lengths') return header if column_type is None: column_type = [type(col[0]) for col in column_list] csv_rows = [] csv_rows.append(header) csv_rows.append('# NumData %r' % num_data) column_maxlen = [] column_str_list = [] if column_labels is None: column_labels = [''] * len(column_list) def _toint(c): try: if np.isnan(c): return 'nan' except TypeError as ex: print('------') print('[ld2] TypeError %r ' % ex) print('[ld2] _toint(c) failed') print('[ld2] c = %r ' % c) print('[ld2] type(c) = %r ' % type(c)) print('------') raise return ('%d') % int(c) for col, lbl, coltype in iter(zip(column_list, column_labels, column_type)): if tools.is_list(coltype): col_str = [str(c).replace(',', ' ').replace('.', '') for c in iter(col)] elif tools.is_float(coltype): col_str = [('%.2f') % float(c) for c in iter(col)] elif tools.is_int(coltype): col_str = [_toint(c) for c in iter(col)] elif tools.is_str(coltype): col_str = [str(c) for c in iter(col)] else: col_str = [str(c) for c in iter(col)] col_lens = [len(s) for s in iter(col_str)] max_len = max(col_lens) max_len = max(len(lbl), max_len) column_maxlen.append(max_len) column_str_list.append(col_str) _fmtfn = lambda maxlen: ''.join(['%', str(maxlen + 2), 's']) fmtstr = ','.join([_fmtfn(maxlen) for maxlen in column_maxlen]) csv_rows.append('# ' + fmtstr % tuple(column_labels)) for row in zip(*column_str_list): csv_rows.append(' ' + fmtstr % row) csv_text = '\n'.join(csv_rows) return csv_text
def _populate_table(front, tbl, col_headers, col_editable, row_list, row2_datatup): # front.printDBG('_populate_table()') hheader = tbl.horizontalHeader() def set_header_context_menu(hheader): hheader.setContextMenuPolicy(Qt.CustomContextMenu) # TODO: for chip table: delete metedata column opt2_callback = [("header", lambda: print("finishme")), ("cancel", lambda: print("cancel"))] # HENDRIK / JASON TODO: # I have a small right-click context menu working # Maybe one of you can put some useful functions in these? popup_slot = guitools.popup_menu(tbl, opt2_callback) hheader.customContextMenuRequested.connect(popup_slot) def set_table_context_menu(tbl): tbl.setContextMenuPolicy(Qt.CustomContextMenu) # RCOS TODO: How do we get the clicked item on a right click? # tbl.selectedItems # tbl.selectedIndexes opt2_callback = [("Query", front.querySignal.emit)] # ('item', lambda: print('finishme')), # ('cancel', lambda: print('cancel')), ] popup_slot = guitools.popup_menu(tbl, opt2_callback) tbl.customContextMenuRequested.connect(popup_slot) # set_header_context_menu(hheader) set_table_context_menu(tbl) sort_col = hheader.sortIndicatorSection() sort_ord = hheader.sortIndicatorOrder() tbl.sortByColumn(0, Qt.AscendingOrder) # Basic Sorting tblWasBlocked = tbl.blockSignals(True) tbl.clear() tbl.setColumnCount(len(col_headers)) tbl.setRowCount(len(row_list)) tbl.verticalHeader().hide() tbl.setHorizontalHeaderLabels(col_headers) tbl.setSelectionMode(QAbstractItemView.SingleSelection) tbl.setSelectionBehavior(QAbstractItemView.SelectRows) tbl.setSortingEnabled(False) for row in iter(row_list): data_tup = row2_datatup[row] for col, data in enumerate(data_tup): item = QtGui.QTableWidgetItem() # RCOS TODO: Pass in datatype here. # if col_headers[col] == 'AIF': # print('col=%r dat=%r, %r' % (col, data, type(data))) if tools.is_bool(data) or data == "True" or data == "False": bit = bool(data) # print(bit) if bit: item.setCheckState(Qt.Checked) else: item.setCheckState(Qt.Unchecked) # item.setData(Qt.DisplayRole, bool(data)) elif tools.is_int(data): item.setData(Qt.DisplayRole, int(data)) elif tools.is_float(data): item.setData(Qt.DisplayRole, float(data)) else: item.setText(str(data)) item.setTextAlignment(Qt.AlignHCenter) if col_editable[col]: item.setFlags(item.flags() | Qt.ItemIsEditable) # print(item.getBackground()) item.setBackground(QtGui.QColor(250, 240, 240)) else: item.setFlags(item.flags() ^ Qt.ItemIsEditable) tbl.setItem(row, col, item) tbl.setSortingEnabled(True) tbl.sortByColumn(sort_col, sort_ord) # Move back to old sorting tbl.show() tbl.blockSignals(tblWasBlocked)
def make_csv_table(column_labels=None, column_list=[], header='', column_type=None): if len(column_list) == 0: print('[ld2] No columns') return header column_len = [len(col) for col in column_list] num_data = column_len[0] if num_data == 0: print('[ld2.make_csv_table()] No data. (header=%r)' % (header, )) return header if any([num_data != clen for clen in column_len]): print('[lds] column_labels = %r ' % (column_labels, )) print('[lds] column_len = %r ' % (column_len, )) print('[ld2] inconsistent column lengths') return header if column_type is None: column_type = [type(col[0]) for col in column_list] csv_rows = [] csv_rows.append(header) csv_rows.append('# NumData %r' % num_data) column_maxlen = [] column_str_list = [] if column_labels is None: column_labels = [''] * len(column_list) def _toint(c): try: if np.isnan(c): return 'nan' except TypeError as ex: print('------') print('[ld2] TypeError %r ' % ex) print('[ld2] _toint(c) failed') print('[ld2] c = %r ' % c) print('[ld2] type(c) = %r ' % type(c)) print('------') raise return ('%d') % int(c) for col, lbl, coltype in iter(zip(column_list, column_labels, column_type)): if tools.is_list(coltype): col_str = [ str(c).replace(',', ' ').replace('.', '') for c in iter(col) ] elif tools.is_float(coltype): col_str = [('%.2f') % float(c) for c in iter(col)] elif tools.is_int(coltype): col_str = [_toint(c) for c in iter(col)] elif tools.is_str(coltype): col_str = [str(c) for c in iter(col)] else: col_str = [str(c) for c in iter(col)] col_lens = [len(s) for s in iter(col_str)] max_len = max(col_lens) max_len = max(len(lbl), max_len) column_maxlen.append(max_len) column_str_list.append(col_str) _fmtfn = lambda maxlen: ''.join(['%', str(maxlen + 2), 's']) fmtstr = ','.join([_fmtfn(maxlen) for maxlen in column_maxlen]) csv_rows.append('# ' + fmtstr % tuple(column_labels)) for row in zip(*column_str_list): csv_rows.append(' ' + fmtstr % row) csv_text = '\n'.join(csv_rows) return csv_text
def _populate_table(front, tbl, col_fancyheaders, col_editable, row_list, datatup_list): # TODO: for chip table: delete metedata column # RCOS TODO: # I have a small right-click context menu working # Maybe one of you can put some useful functions in these? # RCOS TODO: How do we get the clicked item on a right click? # RCOS TODO: # The data tables should not use the item model # Instead they should use the more efficient and powerful # QAbstractItemModel / QAbstractTreeModel def set_header_context_menu(hheader): hheader.setContextMenuPolicy(Qt.CustomContextMenu) opt2_callback = [ ('header', lambda: print('finishme')), ('cancel', lambda: print('cancel')), ] popup_slot = guitools.popup_menu(tbl, opt2_callback) hheader.customContextMenuRequested.connect(popup_slot) def set_table_context_menu(tbl): tbl.setContextMenuPolicy(Qt.CustomContextMenu) opt2_callback = [ ('Query', front.querySignal.emit), ] popup_slot = guitools.popup_menu(tbl, opt2_callback) tbl.customContextMenuRequested.connect(popup_slot) hheader = tbl.horizontalHeader() #set_header_context_menu(hheader) #set_table_context_menu(tbl) sort_col = hheader.sortIndicatorSection() sort_ord = hheader.sortIndicatorOrder() tbl.sortByColumn(0, Qt.AscendingOrder) # Basic Sorting tblWasBlocked = tbl.blockSignals(True) tbl.clear() tbl.setColumnCount(len(col_fancyheaders)) tbl.setRowCount(len(row_list)) tbl.verticalHeader().hide() tbl.setHorizontalHeaderLabels(col_fancyheaders) tbl.setSelectionMode(QAbstractItemView.SingleSelection) tbl.setSelectionBehavior(QAbstractItemView.SelectRows) tbl.setSortingEnabled(False) #dbg_col2_dtype = {} #def DEBUG_COL_DTYPE(col, dtype): #if not dtype in dbg_col2_dtype: #dbg_col2_dtype[dtype] = [col] #else: #if not col in dbg_col2_dtype[dtype]: #dbg_col2_dtype[dtype].append(col) # Add items for each row and column for row in iter(row_list): data_tup = datatup_list[row] for col, data in enumerate(data_tup): item = QtGui.QTableWidgetItem() # RCOS TODO: Pass in datatype here. # BOOLEAN DATA if tools.is_bool(data) or data == 'True' or data == 'False': check_state = Qt.Checked if bool(data) else Qt.Unchecked item.setCheckState(check_state) #DEBUG_COL_DTYPE(col, 'bool') #item.setData(Qt.DisplayRole, bool(data)) # INTEGER DATA elif tools.is_int(data): item.setData(Qt.DisplayRole, int(data)) #DEBUG_COL_DTYPE(col, 'int') # FLOAT DATA elif tools.is_float(data): item.setData(Qt.DisplayRole, float(data)) #DEBUG_COL_DTYPE(col, 'float') # STRING DATA else: item.setText(str(data)) #DEBUG_COL_DTYPE(col, 'string') # Mark as editable or not if col_editable[col]: item.setFlags(item.flags() | Qt.ItemIsEditable) item.setBackground(QtGui.QColor(250, 240, 240)) else: item.setFlags(item.flags() ^ Qt.ItemIsEditable) item.setTextAlignment(Qt.AlignHCenter) tbl.setItem(row, col, item) #print(dbg_col2_dtype) tbl.setSortingEnabled(True) tbl.sortByColumn(sort_col, sort_ord) # Move back to old sorting tbl.show() tbl.blockSignals(tblWasBlocked)