feat(category): 添加修改目录功能
This commit is contained in:
parent
836f042122
commit
33fbd421b9
1 changed files with 113 additions and 23 deletions
|
@ -3,6 +3,7 @@ from ._tab import TabPage
|
|||
from ._pg import PostgresTab
|
||||
import json
|
||||
|
||||
|
||||
class CategoryTab(TabPage):
|
||||
def __init__(self, parent):
|
||||
super().__init__()
|
||||
|
@ -13,36 +14,59 @@ class CategoryTab(TabPage):
|
|||
def initUI(self):
|
||||
# 创建连接信息控件
|
||||
self.categoryComboBox = QComboBox()
|
||||
|
||||
self.categoryAddButton = QPushButton('Add New Category')
|
||||
|
||||
self.categoryModifyButton = QPushButton('Modify')
|
||||
self.categoryModifyButton.clicked.connect(self.onCategoryModifyClicked)
|
||||
self.categoryAddButton = QPushButton('Add')
|
||||
self.categoryAddButton.clicked.connect(self.onCategoryAddClicked)
|
||||
|
||||
# 创建控件布局
|
||||
categoryLayout = QHBoxLayout()
|
||||
categoryLayout.addWidget(self.categoryComboBox)
|
||||
categoryLayout.addWidget(self.categoryModifyButton)
|
||||
categoryLayout.addWidget(self.categoryAddButton)
|
||||
|
||||
categoryWidget = QWidget()
|
||||
self.setLayout(categoryLayout)
|
||||
|
||||
|
||||
def onCategoryAddClicked(self):
|
||||
|
||||
|
||||
def onCategoryModifyClicked(self):
|
||||
# 创建添加数据对话框
|
||||
addDialog = QDialog(self)
|
||||
addDialog.setWindowTitle('Add New Category')
|
||||
addDialog.setWindowTitle('Modify Category')
|
||||
addDialog.resize(400, 300)
|
||||
|
||||
index = self.categoryComboBox.currentIndex()
|
||||
# 若为空,抛出错误
|
||||
if (index == -1):
|
||||
QMessageBox.critical(self, 'Error', 'No category selected')
|
||||
return
|
||||
|
||||
# 构造查询语句
|
||||
c_id = self.rows[index][0]
|
||||
print(c_id)
|
||||
self.pg.execute(
|
||||
"SELECT meta->>'name', meta->>'type', meta->'description' FROM category WHERE c_id=%s", (c_id,))
|
||||
data = self.pg.fetchall()
|
||||
print(data)
|
||||
|
||||
# 绘制界面并填充数据
|
||||
label1 = QLabel("Category Name:")
|
||||
line1 = QLineEdit()
|
||||
line1.setText(data[0][0])
|
||||
|
||||
label2 = QLabel("Category Type:")
|
||||
line2 =QComboBox()
|
||||
line2 = QComboBox()
|
||||
line2.addItem("Expense")
|
||||
line2.addItem("Income")
|
||||
if (data[0][1] == "in"):
|
||||
line2.setCurrentIndex(0)
|
||||
else:
|
||||
line2.setCurrentIndex(1)
|
||||
|
||||
label3 = QLabel("Description:")
|
||||
line3 = QLineEdit()
|
||||
line2.addItem("Income")
|
||||
line2.addItem("Expense")
|
||||
|
||||
line3.setText(data[0][2])
|
||||
|
||||
button1 = QPushButton("Confirm")
|
||||
button2 = QPushButton("Abort")
|
||||
buttonLayout = QHBoxLayout()
|
||||
|
@ -53,9 +77,9 @@ class CategoryTab(TabPage):
|
|||
button2.clicked.connect(addDialog.reject)
|
||||
|
||||
layout = QFormLayout()
|
||||
layout.addRow(label1,line1)
|
||||
layout.addRow(label2,line2)
|
||||
layout.addRow(label3,line3)
|
||||
layout.addRow(label1, line1)
|
||||
layout.addRow(label2, line2)
|
||||
layout.addRow(label3, line3)
|
||||
layout.addRow(buttonLayout)
|
||||
|
||||
addDialog.setLayout(layout)
|
||||
|
@ -68,19 +92,20 @@ class CategoryTab(TabPage):
|
|||
type = line2.currentText()
|
||||
if (name == ''):
|
||||
raise Exception('Category name cannot be empty')
|
||||
if(type == "Income"):
|
||||
type = True
|
||||
if (type == "Income"):
|
||||
type = "in"
|
||||
else:
|
||||
type = False
|
||||
type = "out"
|
||||
|
||||
description = line3.text()
|
||||
|
||||
# JSONB插入数据
|
||||
data = {"name": line1.text(), "income_type": type, "description": description}
|
||||
data = {"name": line1.text(), "type": type,
|
||||
"description": description}
|
||||
data = json.dumps(data)
|
||||
print("insert data:",data)
|
||||
|
||||
self.pg.execute("INSERT INTO category (meta) VALUES (%s)", (data,))
|
||||
print("insert data:", data)
|
||||
|
||||
self.pg.execute("UPDATE category SET meta=%s WHERE c_id=%s",(data,c_id,))
|
||||
|
||||
# 刷新表格
|
||||
self.selected()
|
||||
|
@ -90,13 +115,78 @@ class CategoryTab(TabPage):
|
|||
print(e)
|
||||
QMessageBox.critical(self, 'Error', str(e))
|
||||
|
||||
def onCategoryAddClicked(self):
|
||||
# 创建添加数据对话框
|
||||
addDialog = QDialog(self)
|
||||
addDialog.setWindowTitle('Add New Category')
|
||||
addDialog.resize(400, 300)
|
||||
|
||||
label1 = QLabel("Category Name:")
|
||||
line1 = QLineEdit()
|
||||
label2 = QLabel("Category Type:")
|
||||
line2 = QComboBox()
|
||||
label3 = QLabel("Description:")
|
||||
line3 = QLineEdit()
|
||||
line2.addItem("Expense")
|
||||
line2.addItem("Income")
|
||||
|
||||
|
||||
button1 = QPushButton("Confirm")
|
||||
button2 = QPushButton("Abort")
|
||||
buttonLayout = QHBoxLayout()
|
||||
buttonLayout.addWidget(button1)
|
||||
buttonLayout.addWidget(button2)
|
||||
|
||||
button1.clicked.connect(addDialog.accept)
|
||||
button2.clicked.connect(addDialog.reject)
|
||||
|
||||
layout = QFormLayout()
|
||||
layout.addRow(label1, line1)
|
||||
layout.addRow(label2, line2)
|
||||
layout.addRow(label3, line3)
|
||||
layout.addRow(buttonLayout)
|
||||
|
||||
addDialog.setLayout(layout)
|
||||
|
||||
# 显示添加数据对话框
|
||||
if addDialog.exec() == QDialog.DialogCode.Accepted:
|
||||
try:
|
||||
# 获取输入数据
|
||||
name = line1.text()
|
||||
type = line2.currentText()
|
||||
if (name == ''):
|
||||
raise Exception('Category name cannot be empty')
|
||||
if (type == "Income"):
|
||||
type = "in"
|
||||
else:
|
||||
type = "out"
|
||||
|
||||
description = line3.text()
|
||||
|
||||
# JSONB插入数据
|
||||
data = {"name": line1.text(), "type": type,
|
||||
"description": description}
|
||||
data = json.dumps(data)
|
||||
print("insert data:", data)
|
||||
|
||||
self.pg.execute(
|
||||
"INSERT INTO category (meta) VALUES (%s)", (data,))
|
||||
|
||||
# 刷新表格
|
||||
self.selected()
|
||||
|
||||
except Exception as e:
|
||||
# 处理插入数据错误
|
||||
print(e)
|
||||
QMessageBox.critical(self, 'Error', str(e))
|
||||
|
||||
def selected(self):
|
||||
# 清空表格
|
||||
self.categoryComboBox.clear()
|
||||
# 获取表格列名
|
||||
try:
|
||||
self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
|
||||
self.pg.execute(
|
||||
"SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
|
||||
self.rows = self.pg.cur.fetchall()
|
||||
print(self.rows)
|
||||
for row in self.rows:
|
||||
|
@ -104,4 +194,4 @@ class CategoryTab(TabPage):
|
|||
self.categoryComboBox.addItem(data)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
QMessageBox.critical(self, 'Error', str(e))
|
||||
QMessageBox.critical(self, 'Error', str(e))
|
||||
|
|
Loading…
Reference in a new issue