-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecycleBinDialog.py
189 lines (144 loc) · 6.1 KB
/
RecycleBinDialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 使用UTF-8标准编码避免中文乱码
# -*- coding: UTF-8 -*-
import sys
import datetime
from PyQt5.QtWidgets import QDialog, QApplication, QAbstractItemView, QTableView
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal
#模块
from ShowDataDialog import ShowDataDialog
from Util.Common import get_sql_connection, get_logger, show_error_message
#导入ui
from UI.Ui_RecycleBinDialog import Ui_RecycleBinDialog
class RecycleBinDialog(QDialog):
data_signal = pyqtSignal(tuple)
def __init__(self, parent=None):
#继承所有dialog的方法
super(RecycleBinDialog, self).__init__(parent)
#设置ui
self.__UI = Ui_RecycleBinDialog()
self.__UI.setupUi(self)
##自定义的方法##
# 设置model_view
self.set_tableview(self.__UI.tableView, horsize=90, versize=80)
# 设置数据模型
self.data_model = self.get_model()
#在这里写查询吧
self.logger = None
self.connection = None
self.cursor = None
self.set_connection_cursor()
self.set_logger()
#获取sql的内容
self.sql = self.get_search_sql()
if self.sql is not None:
try:
self.cursor.execute(self.sql) # 执行sql语句
self.show_search_data()
except Exception as e:
self.record_debug(e)
show_error_message(self, '查询失败')
# 接收查询结果
@pyqtSlot(tuple)
def do_receive_data(self, data_tuple):
#将数据模型更新为添加数据之后的
self.data_model = self.add_model_data(self.data_model, list(data_tuple))
#将数据模型应用到view上
self.set_model()
# 初始化table_view函数
def set_tableview(self, widget: QTableView, horsize: int, versize: int, is_altercolor=True) -> None:
widget.setAlternatingRowColors(is_altercolor)
widget.setSelectionBehavior(QAbstractItemView.SelectItems)
widget.setSelectionMode(QAbstractItemView.SingleSelection)
widget.horizontalHeader().setDefaultSectionSize(horsize)
widget.verticalHeader().setDefaultSectionSize(versize)
widget.setEditTriggers(QAbstractItemView.NoEditTriggers)
## ============================== 功能函数区 ==============================#
# 设置cursor和connection
def set_connection_cursor(self) -> None:
self.connection = get_sql_connection()
self.cursor = self.connection.cursor()
def set_logger(self) -> None:
self.logger = get_logger("my_logger")
# 获得查询语句
def get_search_sql(self) -> str:
sql = None
#今日日期
today_date = datetime.datetime.now().strftime("%Y-%m-%d")
sql = """select * from recycle_sample"""
return sql
def show_search_data(self):
if self.is_search_valid():
data_tuple = self.cursor.fetchall()
self.create_show_dialog()
#发出信号,参数是发射的内容
self.data_signal.emit(data_tuple)
else:
show_error_message(self, "没有查找到任何结果")
def create_show_dialog(self):
self.data_signal.connect(self.do_receive_data)
# 检查查询是否有效
def is_search_valid(self):
return True if self.cursor.rowcount != 0 else False
# 记录Debug信息
def record_debug(self, debug_message: str) -> None:
self.logger.debug("语句错误,错误原因为{}".format(debug_message))
# 获取(原始)数据模型
def get_model(self):
#传入标签的名称和数量
raw_model = self.get_raw_model(labels=['样本编号', '病人ID', '样本类型',
'样本量', '添加日期', '更新时间', '状态', '归属','位置'], colCount=9)
return raw_model
#获取无数据的数据模型
def get_raw_model(self, labels: list, colCount: int =2) -> QStandardItemModel:
'''
获取无数据的数据模型
:param colCount:要设置的列数
:return:无数据的数据模型
'''
raw_model = QStandardItemModel(0, colCount)
raw_model.setHorizontalHeaderLabels(labels)
return raw_model
# 向模型添加数据
def add_model_data(self, model: QStandardItemModel, data_list: list) -> QStandardItemModel:
fun = self.choose_add_function(data_list)
if fun is None:
show_error_message(self, "数据类型错误,请检查")
return
final_model = fun(model, data_list)
return final_model
# 生产者函数:选择添加模式
def choose_add_function(self, data_list: list):
method = None
if isinstance(data_list[0], tuple):
method = self.add_multiple_data
else:
method = self.add_single_data
return method
# 添加单条记录
def add_single_data(self, model: QStandardItemModel, record: list) -> QStandardItemModel:
return model.appendRow([QStandardItem(self.process_data(item)) for item in record])
# 添加多条信息
def add_multiple_data(self, model: QStandardItemModel, data_list: list) -> QStandardItemModel:
for record in data_list:
row = []
for item in record:
item = self.process_data(item)
row.append(QStandardItem(item))
model.appendRow(row)
return model
# 处理数据
def process_data(self, data: any) -> any:
if isinstance(data, str):
pass
#处理日期格式的
elif isinstance(data, datetime.datetime):
data = data.strftime("%Y-%m-%d %H:%M:%S")
else:
data = str(data)
return data
# 设置模型
def set_model(self):
if self.data_model is None:
return
self.__UI.tableView.setModel(self.data_model)