forked from kuoted/Quotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fuct_FuctUI.py
160 lines (131 loc) · 5.3 KB
/
Fuct_FuctUI.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
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from collections import OrderedDict
import csv
from Fuct_Global import *
"""
"""
class BasicMonitor(QtGui.QTableWidget):
"""
基础表格属性
"""
# signal = QtCore.pyqtSignal(type(Event()))
# ----------------------------------------------------------------------
def __init__(self, parent=None):
"""Constructor"""
super(BasicMonitor, self).__init__(parent)
# 保存表头标签用
self.headerList = OrderedDict() # 表格头
self.dataList = [] # 表格数据
# 初始化右键菜单
self.initMenu()
# ----------------------------------------------------------------------
def initTable(self):
"""初始化表格"""
# 设置表格的列数
col = len(self.headerList)
self.setColumnCount(col)
# 设置列表头
self.setHorizontalHeaderLabels(self.headerList)
# 关闭左边的垂直表头
self.verticalHeader().setVisible(False)
# 设为不可编辑
self.setEditTriggers(self.NoEditTriggers)
# 设为行交替颜色
self.setAlternatingRowColors(True)
# ----------------------------------------------------------------------
def updateEvent(self, event):
"""收到事件更新"""
data = event.dict_['data']
self.updateData(data)
# ----------------------------------------------------------------------
def updateData(self, Data_List):
"""将数据更新到表格中"""
# 如果允许了排序功能,则插入数据前必须关闭,否则插入新的数据会变乱
self.clearContents()
for i in range(len(Data_List)):
for j in range(len(Data_List[i])):
Data = self.Data_List[i]
Info = Data[j]
if j in [4,5,10,11]:
Info = unicode(Info)+u"%"
newItem = QTableWidgetItem(unicode(Info))
# 添加提示气泡
newItem.setToolTip(unicode(Info))
if j == 4 and Data[4] > 0:
newItem.setTextColor(QColor(255, 0, 0))
elif j == 4 and Data[4] < 0:
newItem.setTextColor(QColor(0, 255, 0))
if j == 11 and Data[11] >= 30 and 11 > Data[4] > 9.9:
newItem.setTextColor(QColor(255, 0, 0))
elif j == 11 and Data[11] < 30:
newItem.setTextColor(QColor(0, 255, 0))
self.MyTable.setItem(i, j, newItem)
def initMenu(self):
"""初始化右键菜单"""
self.menu = QtGui.QMenu(self)
detailAction = QtGui.QAction(u'详情', self)
detailAction.triggered.connect(self.detail)
copyAction = QtGui.QAction(u'复制代码', self)
copyAction.triggered.connect(self.copy)
copyAllAction = QtGui.QAction(u'复制全部', self)
copyAllAction.triggered.connect(self.copyAll)
saveAction = QtGui.QAction(u'保存内容', self)
saveAction.triggered.connect(self.saveToCsv)
self.menu.addAction(detailAction)
self.menu.addAction(copyAction)
self.menu.addAction(copyAllAction)
self.menu.addAction(saveAction)
def detail(self):
"""详情展示"""
self.show_message(u"功能添加中")
def copy(self):
"""详情展示"""
self.show_message(u"功能添加中")
def copyAll(self):
"""详情展示"""
# 复制到剪切板
data = [i[1] for i in self.Data_List]
clipboard = QtGui.QApplication.clipboard()
clipboard.setText(",".join(data))
self.show_message(u"复制成功")
def show_message(self, log):
"""消息提示框"""
QtGui.QMessageBox.information(self, u"提示", log)
def saveToCsv(self):
"""保存表格内容到CSV文件"""
# 先隐藏右键菜单
self.menu.close()
# 获取想要保存的文件名
path = QtGui.QFileDialog.getSaveFileName(self, u'保存数据', '', 'CSV(*.csv)')
try:
if not path.isEmpty():
with open(unicode(path), 'wb') as f:
writer = csv.writer(f)
# 保存标签
headers = [header.encode('gbk') for header in self.headerList]
writer.writerow(headers)
# 保存每行内容
for row in range(self.MyTable.rowCount()):
rowdata = []
for column in range(self.MyTable.columnCount()):
item = self.MyTable.item(row, column)
if item is not None:
rowdata.append(
unicode(item.text()).encode('gbk'))
else:
rowdata.append('')
writer.writerow(rowdata)
except IOError:
pass
def contextMenuEvent(self, event):
"""右键点击事件"""
self.menu.popup(QtGui.QCursor.pos())
def resultSort(self, index):
# 重写排序
self.Data_List.sort(key=lambda x:x[index])
self.addReportData(self.Data_List)
if __name__ == '__main__':
pass