def loadInfo(self): brokers = DATABASE.getBrokers() self.viewTreasure.clear() self.viewTreasure.setHeaderLabels(COLUMNSTREASURE) self.viewStocks.clear() self.viewStocks.setHeaderLabels(COLUMNSSTOCK) for broker in brokers: treasureItem = TreeItem(self.viewTreasure, broker) treasureItem.setFont(0, BOLD) treasureItem.setText( 1, "R$ %.2f" % DATABASE.getBalanceFromBroker(broker, TREASURE)) treasureItem.setTextAlignment(1, Qt.AlignRight) stockItem = TreeItem(self.viewStocks, broker) stockItem.setFont(0, BOLD) stockItem.setText( 3, "R$ %.2f" % DATABASE.getBalanceFromBroker(broker, STOCK)) stockItem.setTextAlignment(3, Qt.AlignRight) investiments = DATABASE.getInvestiments(broker) for investiment in investiments: if investiment[2] == TREASURE: item = TreeItem(treasureItem, investiment[0]) item.setCheckState(0, Qt.Unchecked) item.setText(1, "R$ %.2f" % investiment[3]) item.setTextAlignment(1, Qt.AlignRight) elif investiment[2] == STOCK: item = TreeItem(stockItem, investiment[0]) item.setCheckState(0, Qt.Unchecked) item.setText(1, "%d" % investiment[1]) item.setTextAlignment(1, Qt.AlignCenter) item.setText(2, "R$ %.2f" % investiment[3]) item.setTextAlignment(2, Qt.AlignRight) item.setText(3, "R$ %.2f" % (investiment[1] * investiment[3])) item.setTextAlignment(3, Qt.AlignRight) self.viewTreasure.expandAll() self.viewTreasure.resizeColumnToContents(0) self.viewTreasure.sortItems(0, Qt.AscendingOrder) self.viewStocks.expandAll() self.viewStocks.resizeColumnToContents(0) self.viewStocks.sortItems(0, Qt.AscendingOrder)
def createLayout(self, item): layout = QFormLayout() types = QGroupBox("Tipo", self) self.treasure = QRadioButton("Tesouro Direto") self.treasure.setChecked(True) self.stock = QRadioButton("Ações") hbox = QHBoxLayout() hbox.addWidget(self.treasure) hbox.addWidget(self.stock) types.setLayout(hbox) layout.addRow(types) self.name = QLineEdit() self.name.textChanged.connect(self.enableButton) self.name.setMaxLength(30) layout.addRow("Nome", self.name) self.qtd = QSpinBox() self.qtd.setSuffix(" papéis") self.qtd.setRange(1, 100000000) self.qtd.setEnabled(False) self.treasure.toggled.connect( lambda: self.qtd.setEnabled(not self.treasure.isChecked())) layout.addRow("Quantidade", self.qtd) self.unit = QDoubleSpinBox() self.unit.setPrefix("R$ ") self.unit.setDecimals(2) self.unit.setSingleStep(25) self.unit.setRange(0, 100000000) layout.addRow("Valor Unitário", self.unit) self.broker = QComboBox() brokers = DATABASE.getBrokers() self.broker.addItems(brokers) layout.addRow("Corretora", self.broker) sublayout = QHBoxLayout() self.ok = QPushButton("Adicionar") self.ok.clicked.connect(self.accept) self.ok.setEnabled(False) cancel = QPushButton("Cancelar") cancel.clicked.connect(self.reject) sublayout.addWidget(self.ok) sublayout.addWidget(cancel) layout.addRow(sublayout) if item: info = DATABASE.getInvestiments(name=item.text(0)) if info[2] == TREASURE: self.treasure.setChecked(True) else: self.stock.setChecked(True) self.name.setText(info[0]) self.qtd.setValue(info[1]) self.unit.setValue(info[3]) idx = self.broker.findText(info[4], Qt.MatchExactly) self.broker.setCurrentIndex(idx) self.ok.setText("Editar") self.setLayout(layout)