123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- import itertools
- import sqlite3
-
- from PySide6.QtGui import QStandardItemModel, QStandardItem
- from PySide6.QtCore import QItemSelectionModel
-
-
- def process_utf_en(utf_en):
- # 确保 utf_en 是字节字符串
- if not isinstance(utf_en, bytes):
- utf_en = utf_en.encode('utf-8')
-
- # 将字节字符串解码为普通字符串
- file_name = utf_en.decode('utf-8')
-
- # 检查文件后缀名并进行相应处理
- if file_name.endswith('.xls'):
- # 去掉 .xls 后缀并添加 .xlsx 后缀
- file_name = file_name[:-4] + '.xlsx'
-
- # 将处理后的字符串重新编码为字节字符串
- utf_en = file_name.encode('utf-8')
-
- return utf_en
-
-
- def main_function(ui, db_path, utf_en):
- # 处理 utf_en
- utf_en = process_utf_en(utf_en)
-
- # 设置 QTabWidget 的可见性和标签文本
- tabs_to_show = [(0, '水准测段高差计算成果表')]
- for index in range(ui.tabWidget.count()):
- visible = index in [t[0] for t in tabs_to_show]
- ui.tabWidget.setTabVisible(index, visible)
- for index, text in tabs_to_show:
- ui.tabWidget.setTabText(index, text)
-
- # 连接到数据库并执行查询
- db1 = sqlite3.connect(db_path)
- db1.text_factory = lambda x: str(x, 'utf-8')
- cursor1 = db1.cursor()
-
- # 查询 GC_Output_Point 表中的数据
- query = """
- SELECT New_ID, New_ResultName, New_SPName, New_EPName, New_HDiff, New_RLen, Correct_Factor, Period_Diff, Dis_Ass
- FROM GC_Output_Point
- WHERE TableName = ?
- """
- cursor1.execute(query, (utf_en,))
- output_result = cursor1.fetchall()
-
- # 查询 GC_Input_Param 表中的 Correct_Factor 字段
- param_query_correct_factor = """
- SELECT Correct_Factor
- FROM GC_Input_Param
- WHERE TableName = ?
- """
- cursor1.execute(param_query_correct_factor, (utf_en,))
- correct_factor_result = cursor1.fetchall()
-
- cursor1.close()
- db1.close()
-
- # 创建 QStandardItemModel 实例
- model = QStandardItemModel()
- model.setColumnCount(len(output_result[0]) + 1) # 增加一列
-
- # 设置表头
- headers = ['序号', '结果期数', '起点', '终点', '高差', '路线长', '修正数', '期间差异', '变形判定', '修正系数']
- model.setHorizontalHeaderLabels(headers)
-
- # 使用 itertools.zip_longest 处理长度不一致的情况
- for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result,
- fillvalue=(None, None)):
- items = []
- for i, item in enumerate(row_data):
- if item is None:
- item = "" # 如果是 None,显示空字符串
- else:
- if i == 4 or i == 5: # 索引4(高差)和索引5(路线长)
- item = f"{item:.6f}" # 格式化为6位小数
- elif i == 6: # 索引 6(修正数)
- item = f"{item:.2f}"
- elif i == 7: # 假设 Period_Diff 在索引 7
- if item is not None and isinstance(item, (int, float)):
- item = f"{item:.2f}" # 格式化为2位小数
- else:
- item = "" # 如果是 None 或非数字类型,显示空字符串
- elif i == 8: # 假设 Dis_Ass 在索引 8
- if item is not None and isinstance(item, str):
- item = item # 显示字符串内容
- else:
- item = "" # 如果是 None,显示空字符串
- items.append(QStandardItem(str(item)))
-
- # 添加修正系数列数据
- if correct_factor_data is not None and correct_factor_data[0] is not None:
- correct_factor = f"{correct_factor_data[0]:.10f}" # 格式化为10位小数
- else:
- correct_factor = ""
-
- items.append(QStandardItem(str(correct_factor)))
-
- model.appendRow(items)
-
- # 设置并展示表格
- ui.resultTableView.setModel(model)
- ui.resultTableView.show()
-
- # 隐藏 QLabel
- ui.default_remind.hide()
-
-
- def search_show_function(ui, db_path, utf_en):
- # 确保 utf_en 是字节字符串
- if not isinstance(utf_en, bytes):
- utf_en = utf_en.encode('utf-8')
-
- tabs_to_show = [(0, '输入数据'), (1, '水准测段高差计算成果表')]
- for index in range(ui.View.count()):
- visible = index in [t[0] for t in tabs_to_show]
- ui.View.setTabVisible(index, visible)
- for index, text in tabs_to_show:
- ui.View.setTabText(index, text)
-
- # 连接到数据库并执行查询
- db1 = sqlite3.connect(db_path)
- db1.text_factory = lambda x: str(x, 'utf-8')
- cursor1 = db1.cursor()
-
- # 查询 GC_Input_Point 表中的数据
- input_query = """
- SELECT New_ID, Last_ResultName, Last_SPName, Last_EPName, Last_HDiff, Last_RLen, New_ResultName, New_SPName, New_EPName, New_HDiff, New_RLen
- FROM GC_Input_Point
- WHERE TableName = ?
- """
- cursor1.execute(input_query, (utf_en,))
- input_result = cursor1.fetchall()
-
- # 查询 GC_Input_Param 表中的数据
- param_query = """
- SELECT ObservationLevel, Ms_Station, Last_StationCount, Last_SumHDiff, Last_SumRLen, New_StationCount, New_SumHDiff, New_SumRLen, Correct_Factor
- FROM GC_Input_Param
- WHERE TableName = ?
- """
- cursor1.execute(param_query, (utf_en,))
- param_result = cursor1.fetchall()
-
- # 创建 QStandardItemModel 实例
- input_model = QStandardItemModel()
- input_model.setColumnCount(len(input_result[0]) + 8) # 增加八列(2 + 6)
-
- # 设置表头
- input_headers = [
- '序号', '上期成果名', '上期测段起点', '上期测段终点', '上期高差', '上期路线长', '本期成果名',
- '本期测段起点', '本期测段终点',
- '本期高差', '本期路线长', '观测等级', '测站全中误差',
- '上次总测段数', '上期高差合计', '上期路线长合计', '本期总测段数', '本期高差合计', '本期路线长合计'
- ]
- input_model.setHorizontalHeaderLabels(input_headers)
-
- # 使用 itertools.zip_longest 处理长度不一致的情况
- for row_data, param_data in itertools.zip_longest(input_result, param_result,
- fillvalue=(None, None, None, None, None, None, None, None, None)):
- items = []
- for i, item in enumerate(row_data):
- if item is None:
- item = "" # 如果是 None,显示空字符串
- else:
- if i in [4, 5, 10, 11]: # 索引4,5,10,11(高差和路线长)
- item = f"{item:.6f}" # 格式化为6位小数
- items.append(QStandardItem(str(item)))
-
- # 添加新的列数据
- if param_data is not None:
- observation_level, ms_station, last_station_count, last_sum_hdiff, last_sum_rlen, new_station_count, new_sum_hdiff, new_sum_rlen, correct_factor = param_data
- else:
- observation_level, ms_station, last_station_count, last_sum_hdiff, last_sum_rlen, new_station_count, new_sum_hdiff, new_sum_rlen, correct_factor = "", "", "", "", "", "", "", "", ""
-
- # 确保 observation_level 和 ms_station 是字符串
- if observation_level is None:
- observation_level = ""
- if ms_station is None:
- ms_station = ""
-
- # 确保其他字段是字符串或格式化后的字符串
- if last_station_count is None:
- last_station_count = ""
- if last_sum_hdiff is None:
- last_sum_hdiff = ""
- else:
- last_sum_hdiff = f"{last_sum_hdiff:.6f}"
- if last_sum_rlen is None:
- last_sum_rlen = ""
- else:
- last_sum_rlen = f"{last_sum_rlen:.6f}"
- if new_station_count is None:
- new_station_count = ""
- if new_sum_hdiff is None:
- new_sum_hdiff = ""
- else:
- new_sum_hdiff = f"{new_sum_hdiff:.6f}"
- if new_sum_rlen is None:
- new_sum_rlen = ""
- else:
- new_sum_rlen = f"{new_sum_rlen:.6f}"
- if correct_factor is None:
- correct_factor = ""
-
- items.append(QStandardItem(str(observation_level)))
- items.append(QStandardItem(f"{ms_station:.2f}" if isinstance(ms_station, (int, float)) else ""))
- items.append(QStandardItem(str(last_station_count)))
- items.append(QStandardItem(str(last_sum_hdiff)))
- items.append(QStandardItem(str(last_sum_rlen)))
- items.append(QStandardItem(str(new_station_count)))
- items.append(QStandardItem(str(new_sum_hdiff)))
- items.append(QStandardItem(str(new_sum_rlen)))
-
- input_model.appendRow(items)
-
- # 设置并展示输入数据表格
- ui.View.setTabText(0, '输入数据')
- ui.resultTableView1.setModel(input_model)
- ui.resultTableView1.show()
- # 隐藏 QLabel
- ui.default_remind1.hide()
-
- # 查询 GC_Output_Point 表中的数据
- output_query = """
- SELECT New_ID, New_ResultName, New_SPName, New_EPName, New_HDiff, New_RLen, Correct_Factor, Period_Diff, Dis_Ass
- FROM GC_Output_Point
- WHERE TableName = ?
- """
- cursor1.execute(output_query, (utf_en,))
- output_result = cursor1.fetchall()
-
- # 查询 GC_Input_Param 表中的 Correct_Factor 字段
- param_query_correct_factor = """
- SELECT Correct_Factor
- FROM GC_Input_Param
- WHERE TableName = ?
- """
- cursor1.execute(param_query_correct_factor, (utf_en,))
- correct_factor_result = cursor1.fetchall()
-
- # 创建 QStandardItemModel 实例
- output_model = QStandardItemModel()
- output_model.setColumnCount(len(output_result[0]) + 1) # 增加一列
-
- # 设置表头
- output_headers = [
- '序号', '结果期数', '起点', '终点', '高差', '路线长', '修正数', '期间差异', '变形判定', '修正系数'
- ]
- output_model.setHorizontalHeaderLabels(output_headers)
-
- # 使用 itertools.zip_longest 处理长度不一致的情况
- for row_data, correct_factor_data in itertools.zip_longest(output_result, correct_factor_result,
- fillvalue=(None, None)):
- items = []
- for i, item in enumerate(row_data):
- if item is None:
- item = "" # 如果是 None,显示空字符串
- else:
- if i == 4 or i == 5: # 索引4(高差)和索引5(路线长)
- item = f"{item:.6f}" # 格式化为6位小数
- elif i == 6: # 索引 6(修正数)
- item = f"{item:.2f}"
- elif i == 7: # 假设 Period_Diff 在索引 7
- if item is not None and isinstance(item, (int, float)):
- item = f"{item:.2f}" # 格式化为2位小数
- else:
- item = "" # 如果是 None 或非数字类型,显示空字符串
- elif i == 8: # 假设 Dis_Ass 在索引 8
- if item is not None and isinstance(item, str):
- item = item # 显示字符串内容
- else:
- item = "" # 如果是 None,显示空字符串
- items.append(QStandardItem(str(item)))
-
- # 添加修正系数列数据
- if correct_factor_data is not None and correct_factor_data[0] is not None:
- correct_factor = f"{correct_factor_data[0]:.10f}" # 格式化为6位小数
- else:
- correct_factor = ""
-
- items.append(QStandardItem(str(correct_factor)))
-
- output_model.appendRow(items)
-
- # 设置并展示输出数据表格
- ui.View.setTabText(1, '水准测段高差计算成果表')
- ui.reconTableView1.setModel(output_model)
- ui.reconTableView1.show()
-
- # 返回一个选择模型
- selectionModel = QItemSelectionModel(input_model) # Item选择模型
- ui.resultTableView1.setSelectionModel(selectionModel)
- return selectionModel
-
- # 关闭数据库连接
- cursor1.close()
- db1.close()
-
- # 隐藏 QLabel
- ui.default_remind1.hide()
|