Esempio n. 1
0
def setTableItem(table,
                 row,
                 col,
                 text,
                 check=None,
                 pullDown=None,
                 jsonEnc=False,
                 editable=True,
                 alignH=QtCore.Qt.AlignLeft,
                 alignV=QtCore.Qt.AlignVCenter,
                 bgColor=QColor(255, 255, 255),
                 grayOdd=True):
    '''
        Sets the contents of a cell in a table, and allows adding check
        boxes and pull-down selection boxes in cells.  Can't use check
        box and pull-down together

        table:  the table to add or update contents
        row:  the cell row
        col:  the cell column
        text:  the text to display or currently item for pulldown
        check:  if true add a check box to the column
        pullDown:  if is a list make a pulldown box
        json:  use json encoder to write text for a cell
    '''
    if grayOdd and row % 2:
        # the first row is 0 (not gray) make rows darker
        red = max(bgColor.red() - 25, 0)
        green = max(bgColor.green() - 25, 0)
        blue = max(bgColor.blue() - 25, 0)
        bgColor = QColor(red, green, blue)
    item = None
    if type(text).__module__ == numpy.__name__: text = text.tolist()
    if pullDown == None:
        # just add text to a cell
        if jsonEnc: text = json.dumps(text)
        item = QTableWidgetItem(str(text))
        item.setBackground(bgColor)
        item.setTextAlignment(alignH | alignV)
        if check != None:
            if editable:
                item.setFlags(
                    QtCore.Qt.ItemIsUserCheckable|\
                    QtCore.Qt.ItemIsEnabled|\
                    QtCore.Qt.ItemIsEditable|\
                    QtCore.Qt.ItemIsSelectable)
            else:
                item.setFlags(
                    QtCore.Qt.ItemIsUserCheckable|\
                    QtCore.Qt.ItemIsEnabled|\
                    QtCore.Qt.ItemIsSelectable)
            if check == True:
                item.setCheckState(QtCore.Qt.Checked)
            else:
                item.setCheckState(QtCore.Qt.Unchecked)
        else:
            if editable:
                item.setFlags(
                    QtCore.Qt.ItemIsEditable|\
                    QtCore.Qt.ItemIsEnabled|\
                    QtCore.Qt.ItemIsSelectable)
            else:
                item.setFlags(
                    QtCore.Qt.ItemIsEnabled|\
                    QtCore.Qt.ItemIsSelectable)
        table.setItem(row, col, item)
    elif pullDown != None:
        #add a pulldown and try to set current to text
        if jsonEnc:
            pullDown = copy.copy(pullDown)
            for i, o in enumerate(pullDown):
                pullDown[i] = json.dumps(o)
            text = json.dumps(text)
        item = QComboBox()
        item.addItems(pullDown)
        i = item.findText(text)
        if i < 0:
            i = 0
        if not editable:
            item.setEnabled(False)
        item.setCurrentIndex(i)
        table.setCellWidget(row, col, item)
    return item