feat(account): 初步添加账户删除功能

This commit is contained in:
iridiumR 2023-05-23 19:44:44 +08:00
parent a02155d539
commit 71a18fc398
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8

View file

@ -15,23 +15,144 @@ class AccountTab(TabPage):
# 创建连接信息控件
self.accountComboBox = QComboBox()
self.accountAddButton = QPushButton('Add New account')
self.accountAddButton.clicked.connect(self.onaccountAddClicked)
self.accountModifyButton = QPushButton('Modify')
self.accountAddButton = QPushButton('Add')
self.accountModifyButton.clicked.connect(self.onAccountModifyClicked)
self.accountAddButton.clicked.connect(self.onAccountAddClicked)
# 创建控件布局
accountLayout = QHBoxLayout()
accountLayout.addWidget(self.accountComboBox)
accountLayout.addWidget(self.accountModifyButton)
accountLayout.addWidget(self.accountAddButton)
accountWidget = QWidget()
self.setLayout(accountLayout)
def onaccountAddClicked(self):
def onAccountDeleteClicked(self):
# 新建对话框,询问是否删除
dialog = QDialog(self)
dialog.setWindowTitle('Delete Account')
dialog.resize(300, 200)
label1 = QLabel("Are you sure to delete this account?")
label2 = QLabel("This action cannot be undone.")
buttonBox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
buttonBox.accepted.connect(dialog.accept)
buttonBox.rejected.connect(dialog.reject)
layout = QVBoxLayout()
layout.addWidget(label1)
layout.addWidget(label2)
layout.addWidget(buttonBox)
dialog.setLayout(layout)
# 若确认删除,则执行删除操作
if dialog.exec() == QDialog.DialogCode.Accepted:
index = self.accountComboBox.currentIndex()
if (index == -1):
QMessageBox.critical(self, 'Error', 'No account selected')
return
a_id = self.rows[index][0]
self.pg.execute("DELETE FROM account WHERE a_id=%s", (a_id,))
self.selected()
QMessageBox.information(self, 'Success', 'Account deleted')
# 关闭原来的对话框
self.aDialog.close()
def onAccountModifyClicked(self):
# 创建添加数据对话框
addDialog = QDialog(self)
addDialog.setWindowTitle('Add New Account')
addDialog.resize(400, 300)
self.aDialog = QDialog(self)
self.aDialog.setWindowTitle('Modify')
self.aDialog.resize(400, 300)
index = self.accountComboBox.currentIndex()
# 若为空,抛出错误
if (index == -1):
QMessageBox.critical(self, 'Error', 'No account selected')
return
# 构造查询语句
a_id = self.rows[index][0]
print(a_id)
self.pg.execute(
"SELECT meta->>'name', meta->>'balance', meta->'description' FROM account WHERE a_id=%s", (a_id,))
data = self.pg.fetchall()
print(data)
# 绘制界面并填充数据
label1 = QLabel("Account Name:")
line1 = QLineEdit()
line1.setText(data[0][0])
label2 = QLabel("Account Balance:")
line2 = QLineEdit()
line2.setText(f'{float(data[0][1]):.2f}')
# 只允许输入两位小数
line2.setValidator(QDoubleValidator(0.00, 999999999.99, 2))
label3 = QLabel("Description:")
line3 = QLineEdit()
line3.setText(data[0][2])
button1 = QPushButton("Confirm")
button3 = QPushButton("Delete")
button2 = QPushButton("Abort")
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(button1)
buttonLayout.addWidget(button3)
buttonLayout.addWidget(button2)
button1.clicked.connect(self.aDialog.accept)
button3.clicked.connect(self.onAccountDeleteClicked)
button2.clicked.connect(self.aDialog.reject)
layout = QFormLayout()
layout.addRow(label1,line1)
layout.addRow(label2,line2)
layout.addRow(label3,line3)
layout.addRow(buttonLayout)
self.aDialog.setLayout(layout)
# 显示添加数据对话框
if self.aDialog.exec() == QDialog.DialogCode.Accepted:
try:
# 获取输入数据
name = line1.text()
balance = float(line2.text())
description = line3.text()
if (name == ''):
raise Exception('Account name cannot be empty')
# JSONB插入数据
data = {"name": line1.text(), "balance": balance, "description": description}
data = json.dumps(data)
print("insert data:",data)
self.pg.execute("UPDATE account SET meta=%s WHERE a_id=%s", (data, a_id))
# 刷新表格
self.selected()
except Exception as e:
# 处理插入数据错误
print(e)
QMessageBox.critical(self, 'Error', str(e))
def onAccountAddClicked(self):
# 创建添加数据对话框
self.aDialog = QDialog(self)
self.aDialog.setWindowTitle('Add New Account')
self.aDialog.resize(400, 300)
label1 = QLabel("Account Name:")
line1 = QLineEdit()
@ -49,8 +170,8 @@ class AccountTab(TabPage):
buttonLayout.addWidget(button1)
buttonLayout.addWidget(button2)
button1.clicked.connect(addDialog.accept)
button2.clicked.connect(addDialog.reject)
button1.clicked.connect(self.aDialog.accept)
button2.clicked.connect(self.aDialog.reject)
layout = QFormLayout()
layout.addRow(label1,line1)
@ -58,10 +179,10 @@ class AccountTab(TabPage):
layout.addRow(label3,line3)
layout.addRow(buttonLayout)
addDialog.setLayout(layout)
self.aDialog.setLayout(layout)
# 显示添加数据对话框
if addDialog.exec() == QDialog.DialogCode.Accepted:
if self.aDialog.exec() == QDialog.DialogCode.Accepted:
try:
# 获取输入数据
name = line1.text()