forked from rjj510/uiKLine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuiBasicIO.py
199 lines (186 loc) · 7.29 KB
/
uiBasicIO.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
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
# PyQt
from qtpy.QtGui import *
from qtpy.QtWidgets import *
from qtpy.QtCore import *
from qtpy import QtGui,QtCore
# Others
import os
import imp
import sys
import json
import glob
from functools import partial
from collections import OrderedDict
# 导入按钮函数
#---------------------------------------------------------------------------------------
ALL_FUNC_BUTTON = []
funcBtnPath = os.getcwd() + '/func-button/'
allPath = glob.glob(funcBtnPath+r'*.py')
for path in allPath:
fileName = path.split("\\")[-1]
modelName = fileName.split(".")[0]
ALL_FUNC_BUTTON.append(modelName)
imp.load_source('ctaFuncButttons',path)
BUTTON_FUNC = {}
from ctaFuncButttons import *
for func_bt in ALL_FUNC_BUTTON:
fn_obj = getattr(sys.modules['ctaFuncButttons'], func_bt)
BUTTON_FUNC[func_bt] = fn_obj
# 字符串转换
#---------------------------------------------------------------------------------------
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
########################################################################
class uiBasicIO(QWidget):
"""通过json文件,自动生成输入框和按钮的元类"""
#----------------------------------------------------------------------
def __init__(self,parent=None,inpFile='',btnFile=''):
"""初始化函数"""
super(uiBasicIO,self).__init__(parent)
# 输入框数据
self.classDict = OrderedDict()
self.labelDict = {}
self.widthDict = {}
self.typeDict = {}
self.evalDict = {}
self.editDict = {}
# 按钮数据
self.bClassDict = OrderedDict()
self.bWidthDict = {}
self.bFunDict = {}
self.buttonDict = {}
# 输入框和按钮
self.groupInput = None
self.groupProcess = None
# 输入框和按钮的配置文件
self.inpFile = inpFile
self.btnFile = btnFile
self.loadInputSetting()
self.loadButtonSetting()
self.initBasicUi()
#----------------------------------------------------------------------
def getInputParamByName(self,name):
"""获得输入框参数值"""
typeName = self.typeDict[name]
editCell = self.editDict[name]
val = str(editCell.currentText()) if typeName == 'List' else str(editCell.text())
try:
return (eval(val) if self.evalDict[name] else val)
except:
return val
#----------------------------------------------------------------------
def loadInputSetting(self):
"""载入输入框界面配置"""
settingFile = self.inpFile
with open(settingFile) as f:
for setting in json.load(f):
name = setting['name']
label = setting['label']
typeName = setting['type']
evalType = setting['eval']
width = setting['width']
className = setting['class']
default = setting['default']
# 标签
self.labelDict[name] = QLabel(label)
self.labelDict[name].setAlignment(QtCore.Qt.AlignCenter)
# 宽度
self.widthDict[name] = width
# 输入框类型
self.typeDict[name] = typeName
self.evalDict[name] = evalType
# 分类
if className in self.classDict:
self.classDict[className].append(name)
else:
self.classDict[className] = [name]
# 输入框
if typeName == 'Edit':
self.editDict[name] = QLineEdit()
self.editDict[name].setText(default)
elif typeName == 'List':
self.editDict[name] = QComboBox()
self.editDict[name].addItems(eval(setting['ListVar']))
#----------------------------------------------------------------------
def loadButtonSetting(self):
"""载入按钮界面配置"""
settingFile = self.btnFile
with open(settingFile) as f:
for setting in json.load(f):
label = setting['label']
func = setting['func']
width = setting['width']
className = setting['class']
style = setting['style']
# 按钮
self.buttonDict[func] = QPushButton(label)
self.buttonDict[func].setObjectName(_fromUtf8(style))
self.buttonDict[func].clicked.connect(partial(BUTTON_FUNC[func],self))
# 宽度
self.bWidthDict[func] = width
# 分类
if className in self.bClassDict:
self.bClassDict[className].append(func)
else:
self.bClassDict[className] = [func]
#----------------------------------------------------------------------
def initBasicUi(self):
"""初始化界面"""
# 根据配置文件生成输入框界面
self.groupInput = QGroupBox()
self.groupInput.setTitle(u'')
gridup = QGridLayout()
i = 0
for className in self.classDict:
classIndex = i
# 标题和输入框
for name in self.classDict[className]:
width = self.widthDict[name]
qLabel = self.labelDict[name]
qEdit = self.editDict[name]
gridup.addWidget(qLabel, 1, i)
gridup.addWidget(qEdit, 2, i)
gridup.setColumnStretch(i, width)
i+=1
# 分类标题
qcLabel = QLabel(className)
qcLabel.setAlignment(QtCore.Qt.AlignCenter)
qcLabel.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
gridup.addWidget(qcLabel, 0, classIndex,1,i-classIndex)
# 分隔符
for j in xrange(0,3):
qcSplit = QLabel(u'|')
qcSplit.setAlignment(QtCore.Qt.AlignCenter)
gridup.addWidget(qcSplit, j, i)
i+=1
self.groupInput.setLayout(gridup)
# 根据配置文件生成按钮界面
self.groupProcess = QGroupBox()
self.groupProcess.setTitle(u'')
griddown = QGridLayout()
i = 0
for className in self.bClassDict:
classIndex = i
# 标题和输入框
for name in self.bClassDict[className]:
width = self.bWidthDict[name]
qButton = self.buttonDict[name]
griddown.addWidget(qButton, 1, i)
griddown.setColumnStretch(i, width)
i+=1
# 分类标题
qcLabel = QLabel(className)
qcLabel.setAlignment(QtCore.Qt.AlignCenter)
qcLabel.setFont(QFont("Roman times",10,QtGui.QFont.Bold))
griddown.addWidget(qcLabel, 0, classIndex,1,i-classIndex)
# 分隔符
for j in xrange(0,2):
qcSplit = QLabel(u'|')
qcSplit.setAlignment(QtCore.Qt.AlignCenter)
griddown.addWidget(qcSplit, j, i)
i+=1
self.groupProcess.setLayout(griddown)