浏览代码

20250217 添加父节点删除功能

rmy 4 个月前
父节点
当前提交
1586a47abd
共有 1 个文件被更改,包括 113 次插入20 次删除
  1. 113
    20
      Front/main.py

+ 113
- 20
Front/main.py 查看文件

206
                 elif item.parent().parent() is None:
206
                 elif item.parent().parent() is None:
207
                     # 父节点菜单
207
                     # 父节点菜单
208
                     action1 = QAction("删除", self)
208
                     action1 = QAction("删除", self)
209
-                    action1.triggered.connect(lambda: self.on_parent_action(item))
209
+                    action1.triggered.connect(self.delete_js)
210
                     context_menu.addAction(action1)
210
                     context_menu.addAction(action1)
211
                 else:
211
                 else:
212
                     # 子节点菜单
212
                     # 子节点菜单
232
         except:
232
         except:
233
             pass
233
             pass
234
 
234
 
235
+    def delete_js(self):
236
+        #选中当前计算
237
+        selected_items = widgets.allTreeWidget.selectedItems()
238
+        item = selected_items[0]
239
+        parent_name = item.text(0)
240
+        # 获取父节点
241
+        db_item = item.parent()
242
+        if not db_item:
243
+            QMessageBox.warning(self, '警告', '所选项目没有父节点')
244
+            return
245
+        dbname = db_item.text(0)
246
+        dbpath = os.path.join(os.path.abspath('./SQL'), f"{dbname}.db")
247
+        # 确定要清空的表
248
+        tables_to_delete = []
249
+        if parent_name == '水准测段高差稳定计算':
250
+            tables_to_delete = ['GC_Input_Param', 'GC_Input_Point', 'GC_Output_Point']
251
+        elif parent_name == '控制网复测平面基准计算':
252
+            tables_to_delete = ['GS_Input_Param', 'GS_Input_Point', 'GS_Result_Point', 'GS_Trans_Param',
253
+                                'GS_Trans_Point']
254
+        elif parent_name == '平面控制网稳定性计算':
255
+            tables_to_delete = ['WD_Input_Param', 'WD_Input_Point', 'WD_Result_Param', 'WD_Result_Point']
256
+        else:
257
+            QMessageBox.warning(self, '警告', '未知的父节点')
258
+            return
259
+        # 确认删除操作
260
+        reply = QMessageBox.question(self, '确认删除', f'确定要删除所有 {parent_name} 的计算数据吗?',
261
+                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
262
+        if reply == QMessageBox.No:
263
+            return
264
+            # 清空表
265
+        try:
266
+            conn = sqlite3.connect(dbpath)
267
+            cursor = conn.cursor()
268
+            for table in tables_to_delete:
269
+                cursor.execute(f"DELETE FROM {table}")
270
+            conn.commit()
271
+            conn.close()
272
+            QMessageBox.information(self, '成功', f'计算 {parent_name} 中的数据已清空')
273
+            # 刷新树状图
274
+            self.refresh_tree()
275
+        except Exception as e:
276
+            QMessageBox.critical(self, '错误', f'删除数据时出错: {str(e)}')
277
+
235
     def delete_db(self):
278
     def delete_db(self):
236
         # 获取当前选中的项目
279
         # 获取当前选中的项目
237
         selected_items = widgets.allTreeWidget.selectedItems()
280
         selected_items = widgets.allTreeWidget.selectedItems()
238
         item = selected_items[0]
281
         item = selected_items[0]
239
         dbname = item.text(0)
282
         dbname = item.text(0)
240
-        dbpath = os.path.join(os.path.abspath('../SQL'), f"{dbname}.db")
283
+        dbpath = os.path.join(os.path.abspath('./SQL'), f"{dbname}.db")
241
         # 确认删除操作
284
         # 确认删除操作
242
         reply = QMessageBox.question(self, '确认删除', f'确定要删除项目 {dbname} 吗?',
285
         reply = QMessageBox.question(self, '确认删除', f'确定要删除项目 {dbname} 吗?',
243
                                      QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
286
                                      QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
256
             QMessageBox.information(self, '成功', f'项目 {dbname} 中的数据已删除')
299
             QMessageBox.information(self, '成功', f'项目 {dbname} 中的数据已删除')
257
             # 刷新树状图
300
             # 刷新树状图
258
             self.refresh_tree()
301
             self.refresh_tree()
259
-            # 模拟点击 Enter 键  实现程序自动刷新的效果
260
-            QTest.keyClick(widgets.allTreeWidget, Qt.Key_Return)
302
+            # # 模拟点击 Enter 键  实现程序自动刷新的效果
303
+            # QTest.keyClick(widgets.allTreeWidget, Qt.Key_Return)
261
         except Exception as e:
304
         except Exception as e:
262
             QMessageBox.critical(self, '错误', f'删除数据时出错: {str(e)}')
305
             QMessageBox.critical(self, '错误', f'删除数据时出错: {str(e)}')
263
 
306
 
435
                 elif item.parent().parent() is None:
478
                 elif item.parent().parent() is None:
436
                     # 父节点菜单
479
                     # 父节点菜单
437
                     action1 = QAction("删除", self)
480
                     action1 = QAction("删除", self)
438
-                    action1.triggered.connect(lambda: self.on_parent_action(item))
481
+                    action1.triggered.connect(self.delete_js)
439
                     context_menu.addAction(action1)
482
                     context_menu.addAction(action1)
440
                 else:
483
                 else:
441
                     # 子节点菜单
484
                     # 子节点菜单
449
         except:
492
         except:
450
             pass
493
             pass
451
 
494
 
495
+    def delete_js(self):
496
+        #选中当前计算
497
+        selected_items = widgets.qureyTreeWidget.selectedItems()
498
+        item = selected_items[0]
499
+        parent_name = item.text(0)
500
+        # 获取父节点
501
+        db_item = item.parent()
502
+        if not db_item:
503
+            QMessageBox.warning(self, '警告', '所选项目没有父节点')
504
+            return
505
+        dbname = db_item.text(0)
506
+        dbpath = os.path.join(os.path.abspath('./SQL'), f"{dbname}.db")
507
+        # 确定要清空的表
508
+        tables_to_delete = []
509
+        if parent_name == '水准测段高差稳定计算':
510
+            tables_to_delete = ['GC_Input_Param', 'GC_Input_Point', 'GC_Output_Point']
511
+        elif parent_name == '控制网复测平面基准计算':
512
+            tables_to_delete = ['GS_Input_Param', 'GS_Input_Point', 'GS_Result_Point', 'GS_Trans_Param',
513
+                                'GS_Trans_Point']
514
+        elif parent_name == '平面控制网稳定性计算':
515
+            tables_to_delete = ['WD_Input_Param', 'WD_Input_Point', 'WD_Result_Param', 'WD_Result_Point']
516
+        else:
517
+            QMessageBox.warning(self, '警告', '未知的父节点')
518
+            return
519
+        # 确认删除操作
520
+        reply = QMessageBox.question(self, '确认删除', f'确定要删除所有 {parent_name} 的计算数据吗?',
521
+                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
522
+        if reply == QMessageBox.No:
523
+            return
524
+            # 清空表
525
+        try:
526
+            conn = sqlite3.connect(dbpath)
527
+            cursor = conn.cursor()
528
+            for table in tables_to_delete:
529
+                cursor.execute(f"DELETE FROM {table}")
530
+            conn.commit()
531
+            conn.close()
532
+            QMessageBox.information(self, '成功', f'计算 {parent_name} 中的数据已清空')
533
+            # 清空树状图
534
+            widgets.qureyTreeWidget.clear()
535
+            # 刷新树状图
536
+            self.refresh_tree()
537
+            # 模拟点击 Enter 键  实现程序自动刷新的效果
538
+            QTest.keyClick(widgets.qureyTreeWidget, Qt.Key_Return)
539
+        except Exception as e:
540
+            QMessageBox.critical(self, '错误', f'删除数据时出错: {str(e)}')
541
+
452
     def delete_db(self):
542
     def delete_db(self):
453
         # 获取当前选中的项目
543
         # 获取当前选中的项目
454
         selected_items = widgets.qureyTreeWidget.selectedItems()
544
         selected_items = widgets.qureyTreeWidget.selectedItems()
455
         item = selected_items[0]
545
         item = selected_items[0]
456
         dbname = item.text(0)
546
         dbname = item.text(0)
457
-        dbpath = os.path.join(os.path.abspath('../SQL'), f"{dbname}.db")
547
+        dbpath = os.path.join(os.path.abspath('./SQL'), f"{dbname}.db")
458
         # 确认删除操作
548
         # 确认删除操作
459
         reply = QMessageBox.question(self, '确认删除', f'确定要删除项目 {dbname} 吗?',
549
         reply = QMessageBox.question(self, '确认删除', f'确定要删除项目 {dbname} 吗?',
460
                                      QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
550
                                      QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
475
             # 清空树状图
565
             # 清空树状图
476
             widgets.qureyTreeWidget.clear()
566
             widgets.qureyTreeWidget.clear()
477
             self.refresh_tree()
567
             self.refresh_tree()
478
-            # 模拟点击 Enter 键  实现程序自动刷新的效果
479
-            QTest.keyClick(widgets.qureyTreeWidget, Qt.Key_Return)
568
+            # # 模拟点击 Enter 键  实现程序自动刷新的效果
569
+            # QTest.keyClick(widgets.qureyTreeWidget, Qt.Key_Return)
480
         except Exception as e:
570
         except Exception as e:
481
             QMessageBox.critical(self, '错误', f'删除数据时出错: {str(e)}')
571
             QMessageBox.critical(self, '错误', f'删除数据时出错: {str(e)}')
482
 
572
 
927
                 pid = id
1017
                 pid = id
928
                 id = id + 1
1018
                 id = id + 1
929
                 # 三种方法
1019
                 # 三种方法
930
-                sqlitem.append(TreeWidgetItem(id, pid, '水准测段高差稳定计算', icon=QIcon(
931
-                    os.path.join(self.absPath, "images/icons/cil-description.png"))))
932
-                gcid = id
933
-                id = id + 1
934
-                sqlitem.append(TreeWidgetItem(id, pid, '控制网复测平面基准计算', icon=QIcon(
935
-                    os.path.join(self.absPath, "images/icons/cil-description.png"))))
936
-                gsid = id
937
-                id = id + 1
938
-                sqlitem.append(TreeWidgetItem(id, pid, '平面控制网稳定性计算', icon=QIcon(
939
-                    os.path.join(self.absPath, "images/icons/cil-description.png"))))
940
-                wdid = id
941
-                id = id + 1
942
                 # 读取所有的表名(三种方式往下)
1020
                 # 读取所有的表名(三种方式往下)
943
                 db1 = sqlite3.connect(dbpath)
1021
                 db1 = sqlite3.connect(dbpath)
944
                 # 获取游标
1022
                 # 获取游标
946
                 sqlstr1 = 'SELECT TableName FROM GC_Input_Param;'
1024
                 sqlstr1 = 'SELECT TableName FROM GC_Input_Param;'
947
                 cursor1.execute(sqlstr1)
1025
                 cursor1.execute(sqlstr1)
948
                 result1 = cursor1.fetchall()
1026
                 result1 = cursor1.fetchall()
1027
+                if len(result1) != 0:
1028
+                    sqlitem.append(TreeWidgetItem(id, pid, '水准测段高差稳定计算', icon=QIcon(
1029
+                        os.path.join(self.absPath, "images/icons/cil-description.png"))))
1030
+                    gcid = id
1031
+                    id = id + 1
949
                 for re1 in result1:
1032
                 for re1 in result1:
950
                     str1 = re1[0].decode('utf-8')
1033
                     str1 = re1[0].decode('utf-8')
951
                     list1 = []
1034
                     list1 = []
959
                 sqlstr2 = 'SELECT TableName FROM GS_Input_Param;'
1042
                 sqlstr2 = 'SELECT TableName FROM GS_Input_Param;'
960
                 cursor1.execute(sqlstr2)
1043
                 cursor1.execute(sqlstr2)
961
                 result2 = cursor1.fetchall()
1044
                 result2 = cursor1.fetchall()
1045
+                if len(result2) != 0:
1046
+                    sqlitem.append(TreeWidgetItem(id, pid, '控制网复测平面基准计算', icon=QIcon(
1047
+                        os.path.join(self.absPath, "images/icons/cil-description.png"))))
1048
+                    gsid = id
1049
+                    id = id + 1
962
                 for re2 in result2:
1050
                 for re2 in result2:
963
                     str2 = re2[0].decode('utf-8')
1051
                     str2 = re2[0].decode('utf-8')
964
                     list2 = []
1052
                     list2 = []
973
                 sqlstr3 = 'SELECT TableName FROM WD_Input_Param;'
1061
                 sqlstr3 = 'SELECT TableName FROM WD_Input_Param;'
974
                 cursor1.execute(sqlstr3)
1062
                 cursor1.execute(sqlstr3)
975
                 result3 = cursor1.fetchall()
1063
                 result3 = cursor1.fetchall()
1064
+                if len(result3) != 0:
1065
+                    sqlitem.append(TreeWidgetItem(id, pid, '平面控制网稳定性计算', icon=QIcon(
1066
+                        os.path.join(self.absPath, "images/icons/cil-description.png"))))
1067
+                    wdid = id
1068
+                    id = id + 1
976
                 for re3 in result3:
1069
                 for re3 in result3:
977
                     str3 = re3[0].decode('utf-8')
1070
                     str3 = re3[0].decode('utf-8')
978
                     list3 = []
1071
                     list3 = []

正在加载...
取消
保存