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
|
from ._pg import PostgresTab
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class CategoryTab(TabPage):
|
class CategoryTab(TabPage):
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -14,34 +15,57 @@ class CategoryTab(TabPage):
|
||||||
# 创建连接信息控件
|
# 创建连接信息控件
|
||||||
self.categoryComboBox = QComboBox()
|
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)
|
self.categoryAddButton.clicked.connect(self.onCategoryAddClicked)
|
||||||
|
|
||||||
# 创建控件布局
|
# 创建控件布局
|
||||||
categoryLayout = QHBoxLayout()
|
categoryLayout = QHBoxLayout()
|
||||||
categoryLayout.addWidget(self.categoryComboBox)
|
categoryLayout.addWidget(self.categoryComboBox)
|
||||||
|
categoryLayout.addWidget(self.categoryModifyButton)
|
||||||
categoryLayout.addWidget(self.categoryAddButton)
|
categoryLayout.addWidget(self.categoryAddButton)
|
||||||
|
|
||||||
categoryWidget = QWidget()
|
categoryWidget = QWidget()
|
||||||
self.setLayout(categoryLayout)
|
self.setLayout(categoryLayout)
|
||||||
|
|
||||||
|
def onCategoryModifyClicked(self):
|
||||||
def onCategoryAddClicked(self):
|
|
||||||
|
|
||||||
|
|
||||||
# 创建添加数据对话框
|
# 创建添加数据对话框
|
||||||
addDialog = QDialog(self)
|
addDialog = QDialog(self)
|
||||||
addDialog.setWindowTitle('Add New Category')
|
addDialog.setWindowTitle('Modify Category')
|
||||||
addDialog.resize(400, 300)
|
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:")
|
label1 = QLabel("Category Name:")
|
||||||
line1 = QLineEdit()
|
line1 = QLineEdit()
|
||||||
|
line1.setText(data[0][0])
|
||||||
|
|
||||||
label2 = QLabel("Category Type:")
|
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:")
|
label3 = QLabel("Description:")
|
||||||
line3 = QLineEdit()
|
line3 = QLineEdit()
|
||||||
line2.addItem("Income")
|
line3.setText(data[0][2])
|
||||||
line2.addItem("Expense")
|
|
||||||
|
|
||||||
button1 = QPushButton("Confirm")
|
button1 = QPushButton("Confirm")
|
||||||
button2 = QPushButton("Abort")
|
button2 = QPushButton("Abort")
|
||||||
|
@ -69,18 +93,19 @@ class CategoryTab(TabPage):
|
||||||
if (name == ''):
|
if (name == ''):
|
||||||
raise Exception('Category name cannot be empty')
|
raise Exception('Category name cannot be empty')
|
||||||
if (type == "Income"):
|
if (type == "Income"):
|
||||||
type = True
|
type = "in"
|
||||||
else:
|
else:
|
||||||
type = False
|
type = "out"
|
||||||
|
|
||||||
description = line3.text()
|
description = line3.text()
|
||||||
|
|
||||||
# JSONB插入数据
|
# JSONB插入数据
|
||||||
data = {"name": line1.text(), "income_type": type, "description": description}
|
data = {"name": line1.text(), "type": type,
|
||||||
|
"description": description}
|
||||||
data = json.dumps(data)
|
data = json.dumps(data)
|
||||||
print("insert data:", data)
|
print("insert data:", data)
|
||||||
|
|
||||||
self.pg.execute("INSERT INTO category (meta) VALUES (%s)", (data,))
|
self.pg.execute("UPDATE category SET meta=%s WHERE c_id=%s",(data,c_id,))
|
||||||
|
|
||||||
# 刷新表格
|
# 刷新表格
|
||||||
self.selected()
|
self.selected()
|
||||||
|
@ -90,13 +115,78 @@ class CategoryTab(TabPage):
|
||||||
print(e)
|
print(e)
|
||||||
QMessageBox.critical(self, 'Error', str(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):
|
def selected(self):
|
||||||
# 清空表格
|
# 清空表格
|
||||||
self.categoryComboBox.clear()
|
self.categoryComboBox.clear()
|
||||||
# 获取表格列名
|
# 获取表格列名
|
||||||
try:
|
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()
|
self.rows = self.pg.cur.fetchall()
|
||||||
print(self.rows)
|
print(self.rows)
|
||||||
for row in self.rows:
|
for row in self.rows:
|
||||||
|
|
Loading…
Reference in a new issue