def computeFormula(self, formula, widget): if formula is None: return None # check if the string is actually a formula or not slist = formula.split(' ') if not slist or not widget: # it is a normal string return formula op = slist[0].lower() firstRow = -1 firstCol = -1 secondRow = -1 secondCol = -1 if len(slist) > 1: firstRow, firstCol = decode_pos(slist[1]) if len(slist) > 2: secondRow, secondCol = decode_pos(slist[2]) start = widget.item(firstRow, firstCol) end = widget.item(secondRow, secondCol) firstVal = 0 try: firstVal = start and int(start.text()) or 0 except ValueError: pass secondVal = 0 try: secondVal = end and int(end.text()) or 0 except ValueError: pass result = None if op == "sum": sum_ = 0 for r in range(firstRow, secondRow + 1): for c in range(firstCol, secondCol + 1): tableItem = widget.item(r, c) if tableItem and tableItem != self: try: sum_ += int(tableItem.text()) except ValueError: pass result = sum_ elif op == "+": result = (firstVal + secondVal) elif op == "-": result = (firstVal - secondVal) elif op == "*": result = (firstVal * secondVal) elif op == "/": if secondVal == 0: result = "nan" else: result = (firstVal / secondVal) elif op == "=": if start: result = start.text() else: result = formula return result
def actionSum(self): row_first = 0 row_last = 0 row_cur = 0 col_first = 0 col_last = 0 col_cur = 0 selected = self.table.selectedItems() if selected: first = selected[0] last = selected[-1] row_first = self.table.row(first) row_last = self.table.row(last) col_first = self.table.column(first) col_last = self.table.column(last) current = self.table.currentItem() if current: row_cur = self.table.row(current) col_cur = self.table.column(current) cell1 = encode_pos(row_first, col_first) cell2 = encode_pos(row_last, col_last) out = encode_pos(row_cur, col_cur) ok, cell1, cell2, out = self.runInputDialog( self.tr("Sum cells"), self.tr("First cell:"), self.tr("Last cell:"), unichr(0x03a3), self.tr("Output to:"), cell1, cell2, out) if ok: row, col = decode_pos(out) self.table.item(row, col).setText(self.tr("sum %s %s" % (cell1, cell2)))
def actionSum(self): row_first = 0 row_last = 0 row_cur = 0 col_first = 0 col_last = 0 col_cur = 0 selected = self.table.selectedItems() if selected: first = selected[0] last = selected[-1] row_first = self.table.row(first) row_last = self.table.row(last) col_first = self.table.column(first) col_last = self.table.column(last) current = self.table.currentItem() if current: row_cur = self.table.row(current) col_cur = self.table.column(current) cell1 = encode_pos(row_first, col_first) cell2 = encode_pos(row_last, col_last) out = encode_pos(row_cur, col_cur) ok, cell1, cell2, out = self.runInputDialog( "Sum cells", "First cell:", "Last cell:", u"\N{GREEK CAPITAL LETTER SIGMA}", "Output to:", cell1, cell2, out) if ok: row, col = decode_pos(out) self.table.item(row, col).setText("sum %s %s" % (cell1, cell2))
def actionSum(self): row_first = 0 row_last = 0 row_cur = 0 col_first = 0 col_last = 0 col_cur = 0 selected = self.table.selectedItems() if selected: first = selected[0] last = selected[-1] row_first = self.table.row(first) row_last = self.table.row(last) col_first = self.table.column(first) col_last = self.table.column(last) current = self.table.currentItem() if current: row_cur = self.table.row(current) col_cur = self.table.column(current) cell1 = encode_pos(row_first, col_first) cell2 = encode_pos(row_last, col_last) out = encode_pos(row_cur, col_cur) ok, cell1, cell2, out = self.runInputDialog("Sum cells", "First cell:", "Last cell:", u"\N{GREEK CAPITAL LETTER SIGMA}", "Output to:", cell1, cell2, out) if ok: row, col = decode_pos(out) self.table.item(row, col).setText("sum %s %s" % (cell1, cell2))
def actionMath_helper(self, title, op): cell1 = "C1" cell2 = "C2" out = "C3" current = self.table.currentItem() if current: out = encode_pos(self.table.currentRow(), self.table.currentColumn()) ok, cell1, cell2, out = self.runInputDialog(title, "Cell 1", "Cell 2", op, "Output to:", cell1, cell2, out) if ok: row, col = decode_pos(out) self.table.item(row, col).setText("%s %s %s" % (op, cell1, cell2))
def runInputDialog(self, title, c1Text, c2Text, opText, outText, cell1, cell2, outCell): rows = [] cols = [] for r in range(self.table.rowCount()): rows.append(str(r + 1)) for c in range(self.table.columnCount()): cols.append(chr(ord('A') + c)) addDialog = QtGui.QDialog(self) addDialog.setWindowTitle(title) group = QtGui.QGroupBox(title, addDialog) group.setMinimumSize(250, 100) cell1Label = QtGui.QLabel(c1Text, group) cell1RowInput = QtGui.QComboBox(group) c1Row, c1Col = decode_pos(cell1) cell1RowInput.addItems(rows) cell1RowInput.setCurrentIndex(c1Row) cell1ColInput = QtGui.QComboBox(group) cell1ColInput.addItems(cols) cell1ColInput.setCurrentIndex(c1Col) operatorLabel = QtGui.QLabel(opText, group) operatorLabel.setAlignment(QtCore.Qt.AlignHCenter) cell2Label = QtGui.QLabel(c2Text, group) cell2RowInput = QtGui.QComboBox(group) c2Row, c2Col = decode_pos(cell2) cell2RowInput.addItems(rows) cell2RowInput.setCurrentIndex(c2Row) cell2ColInput = QtGui.QComboBox(group) cell2ColInput.addItems(cols) cell2ColInput.setCurrentIndex(c2Col) equalsLabel = QtGui.QLabel("=", group) equalsLabel.setAlignment(QtCore.Qt.AlignHCenter) outLabel = QtGui.QLabel(outText, group) outRowInput = QtGui.QComboBox(group) outRow, outCol = decode_pos(outCell) outRowInput.addItems(rows) outRowInput.setCurrentIndex(outRow) outColInput = QtGui.QComboBox(group) outColInput.addItems(cols) outColInput.setCurrentIndex(outCol) cancelButton = QtGui.QPushButton(self.tr("Cancel"), addDialog) cancelButton.clicked.connect(addDialog.reject) okButton = QtGui.QPushButton(self.tr("OK"), addDialog) okButton.setDefault(True) okButton.clicked.connect(addDialog.accept) buttonsLayout = QtGui.QHBoxLayout() buttonsLayout.addStretch(1) buttonsLayout.addWidget(okButton) buttonsLayout.addSpacing(10) buttonsLayout.addWidget(cancelButton) dialogLayout = QtGui.QVBoxLayout(addDialog) dialogLayout.addWidget(group) dialogLayout.addStretch(1) dialogLayout.addItem(buttonsLayout) cell1Layout = QtGui.QHBoxLayout() cell1Layout.addWidget(cell1Label) cell1Layout.addSpacing(10) cell1Layout.addWidget(cell1ColInput) cell1Layout.addSpacing(10) cell1Layout.addWidget(cell1RowInput) cell2Layout = QtGui.QHBoxLayout() cell2Layout.addWidget(cell2Label) cell2Layout.addSpacing(10) cell2Layout.addWidget(cell2ColInput) cell2Layout.addSpacing(10) cell2Layout.addWidget(cell2RowInput) outLayout = QtGui.QHBoxLayout() outLayout.addWidget(outLabel) outLayout.addSpacing(10) outLayout.addWidget(outColInput) outLayout.addSpacing(10) outLayout.addWidget(outRowInput) vLayout = QtGui.QVBoxLayout(group) vLayout.addItem(cell1Layout) vLayout.addWidget(operatorLabel) vLayout.addItem(cell2Layout) vLayout.addWidget(equalsLabel) vLayout.addStretch(1) vLayout.addItem(outLayout) if addDialog.exec_(): cell1 = cell1ColInput.currentText() + cell1RowInput.currentText() cell2 = cell2ColInput.currentText() + cell2RowInput.currentText() outCell = outColInput.currentText() + outRowInput.currentText() return True, cell1, cell2, outCell return False, None, None, None