class EditDialog(QDialog): def __init__(self, transaction, mainwindow): super().__init__() self.ui = Ui_edit_transaction_dialog() self.ui.setupUi(self) self.mainwindow = mainwindow self.transaction = transaction self.mainwindow.setEnabled(False) self.transaction_manager = Transactions() self.categories = Categories().category_list[self.transaction['type']] self.laod_transaction() self.show() self.ui.back_pushButton.clicked.connect(self.close) self.ui.savebutton.clicked.connect(self.edit) print(transaction) def laod_transaction(self): self.ui.type_label.setText(self.transaction['type']) self.ui.amountlineEdit.setText(self.transaction['amount']) date_ = [int(part) for part in self.transaction['date'].split('-')] self.ui.dateEdit.setDate(QDate(date_[0], date_[1], date_[2])) self.ui.notelineEdit.setText(self.transaction['note']) self.ui.categories_combobox.clear() self.ui.categories_combobox.addItems(self.categories) self.ui.categories_combobox.setCurrentIndex( self.categories.index(self.transaction['category'])) def change(self): ui_date = self.ui.dateEdit.date().toPyDate() entry_date = [ int(part) for part in self.transaction['date'].split('-') ] entry_date = date(entry_date[0], entry_date[1], entry_date[2]) ui_amount = float(self.ui.amountlineEdit.text()) entry_amount = float(self.transaction['amount']) ui_category = self.ui.categories_combobox.currentText() entry_category = self.transaction['category'] ui_note = self.ui.notelineEdit.text().strip() entry_note = self.transaction['note'] print(ui_note, entry_note) return not (ui_amount == entry_amount and ui_category == entry_category and ui_note == entry_note and ui_date == entry_date) def edit(self): if self.change(): row_id = self.transaction['row_id'] category = self.ui.categories_combobox.currentText() amount = float(self.ui.amountlineEdit.text()) note = self.ui.notelineEdit.text().strip() date_ = str(self.ui.dateEdit.date().toPyDate()) self.transaction_manager.edit(row_id, category, amount, date_, note) self.transaction_manager.refresh() self.mainwindow.load_table() self.close() QMessageBox.information(self, 'Transaction Updated', 'Transaction is updated successfully', QMessageBox.Ok) self.transaction['category'] = category self.transaction['amount'] = amount self.transaction['date'] = date_ self.transaction['note'] = note else: QMessageBox.warning( self, 'Can\'t update', 'Nothing is edited to update\nPlease edit the transaction entry first before clicking edit', QMessageBox.Ok) def closeEvent(self, event: QtGui.QCloseEvent): self.mainwindow.setEnabled(True) event.accept()
class EditWindow(QDialog): def __init__(self, entry: dict, mainwindow): super().__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) self.show() self.ui.back_pushButton.clicked.connect(self.close) self.ui.back_pushButton_2.clicked.connect(self.edit) self.entry = entry self.mainwindow = mainwindow self.categories = Categories().category_list[self.entry['type']] self.transactions = Transactions() self.load_entry() def load_entry(self): self.ui.type_label.setText(self.entry['type']) self.ui.amountlineEdit.setText(self.entry['amount']) date_ = [int(part) for part in self.entry['date'].split('-')] self.ui.dateEdit.setDate(QDate(date_[0], date_[1], date_[2])) self.ui.noteLineEdit.setText(self.entry['note']) self.ui.categories_combobox.clear() self.ui.categories_combobox.addItems(self.categories) self.ui.categories_combobox.setCurrentIndex( self.categories.index(self.entry['category'])) def change(self): ui_date = [int(part) for part in self.ui.dateEdit.text().split('/')] ui_date = date(ui_date[2], ui_date[1], ui_date[0]) entry_date = [int(part) for part in self.entry['date'].split('-')] entry_date = date(entry_date[0], entry_date[1], entry_date[2]) ui_amount = float(self.ui.amountlineEdit.text()) entry_amount = float(self.entry['amount']) ui_category = self.ui.categories_combobox.currentText() entry_category = self.entry['category'] ui_note = self.ui.noteLineEdit.text().strip() entry_note = self.entry['note'].strip() return not (ui_amount == entry_amount and ui_category == entry_category and ui_note == entry_note and\ ui_date == entry_date) def edit(self): if self.change(): row_id = self.entry['row_id'] category = self.ui.categories_combobox.currentText() amount = float(self.ui.amountlineEdit.text().strip()) note = self.ui.noteLineEdit.text().strip() ui_date = [ int(part) for part in self.ui.dateEdit.text().split('/') ] ui_date = date(ui_date[2], ui_date[1], ui_date[0]) self.transactions.edit(row_id, category, amount, ui_date, note) print('updated') self.transactions.refresh() self.mainwindow.load_table() self.entry['category'] = category self.entry['amount'] = amount self.entry['date'] = str(ui_date) self.entry['note'] = note else: print('nothing is edited to update')