From 33fbd421b99f867e3099e01671a6a2b0a63c4fc2 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Tue, 23 May 2023 19:02:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(category):=20=E6=B7=BB=E5=8A=A0=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=9B=AE=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/budget/_category.py | 136 +++++++++++++++++++++++++++++++++------- 1 file changed, 113 insertions(+), 23 deletions(-) diff --git a/src/budget/_category.py b/src/budget/_category.py index a2ecd20..61f24ef 100644 --- a/src/budget/_category.py +++ b/src/budget/_category.py @@ -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)) \ No newline at end of file + QMessageBox.critical(self, 'Error', str(e))