hytos / DTI_PID / DTI_PID / BMSettingDialog.py @ d938b798
이력 | 보기 | 이력해설 | 다운로드 (10.2 KB)
1 |
# coding: utf-8
|
---|---|
2 |
"""
|
3 |
This is area configuration module
|
4 |
"""
|
5 |
import os |
6 |
import sys |
7 |
from PyQt5.QtCore import * |
8 |
from PyQt5.QtGui import * |
9 |
from PyQt5.QtWidgets import * |
10 |
from functools import reduce, partial |
11 |
from AppDocData import AppDocData |
12 |
|
13 |
import BMSetting_UI |
14 |
|
15 |
class AttributeListModel(QStandardItemModel): |
16 |
def __init__(self, columListAll, parent=None, *args): |
17 |
QStandardItemModel.__init__(self, parent, *args)
|
18 |
|
19 |
self.columListAll = columListAll
|
20 |
|
21 |
for symbol in self.columListAll: |
22 |
items = [QStandardItem(symbol)] |
23 |
items[0].setEditable(False) |
24 |
self.appendRow(items)
|
25 |
|
26 |
headers = [QStandardItem("Name"), QStandardItem("State")] |
27 |
for idx, header in enumerate(headers): |
28 |
header.setTextAlignment(Qt.AlignCenter) |
29 |
self.setHorizontalHeaderItem(idx, header)
|
30 |
|
31 |
class SymbolListModel(QStandardItemModel): |
32 |
def __init__(self, parent=None, *args): |
33 |
QStandardItemModel.__init__(self, parent, *args)
|
34 |
|
35 |
self.SymbolList = [ "DRAIN CUP", "EOL", "FLANGE", "FLANGE BLIND", "ORIFICE", "SCREW CAP", \ |
36 |
"3WAY", "4WAY", "BALL", "BUTTERFLY", "CHECK", "GATE", "GLOBE", "NEEDLE", \ |
37 |
"CONTROL(BALL)", "CONTROL(BUTTERFLY)", "CONTROL(CHECK)", "CONTROL(GATE)", "CONTROL(GLOBE)", \ |
38 |
"MOV(BALL)", "MOV(BUTTERFLY)", "MOV(CHECK)", "MOV(GATE)", "MOV(GLOBE)", "MAGNETIC FLOW METER", "MASS FLOW METER", "ULTRASONIC FLOW METER", "VORTEX FLOW METER", "OPC", \ |
39 |
"GOOSE NECK VENT", "IP", "PG", "PP", "PSV", "RE", "SAMPLE CONNECTION", "SB", "SPACER", "STRAINER", "TG", "HOSE CONNECTION", "OD" ] |
40 |
|
41 |
for symbol in self.SymbolList: |
42 |
items = [QStandardItem(symbol)] |
43 |
items[0].setEditable(False) |
44 |
self.appendRow(items)
|
45 |
|
46 |
headers = [QStandardItem("Name")]
|
47 |
for idx, header in enumerate(headers): |
48 |
header.setTextAlignment(Qt.AlignCenter) |
49 |
self.setHorizontalHeaderItem(idx, header)
|
50 |
|
51 |
class SymbolMappingModel(QStandardItemModel): |
52 |
"""This is SymbolMapping Model class"""
|
53 |
def __init__(self): |
54 |
"""constructor"""
|
55 |
QStandardItemModel.__init__(self)
|
56 |
|
57 |
app_doc_data = AppDocData.instance() |
58 |
project = app_doc_data.getCurrentProject() |
59 |
if project is not None: |
60 |
self.clear()
|
61 |
self.load_symbol_info()
|
62 |
|
63 |
headers = [QStandardItem("Name"), QStandardItem("State")] |
64 |
for idx, header in enumerate(headers): |
65 |
header.setTextAlignment(Qt.AlignCenter) |
66 |
self.setHorizontalHeaderItem(idx, header)
|
67 |
|
68 |
def on_symbol_state_changed(self): |
69 |
pass
|
70 |
|
71 |
def load_symbol_info(self): |
72 |
"""load symbol information and display it on tree view"""
|
73 |
try:
|
74 |
app_doc_data = AppDocData.instance() |
75 |
|
76 |
symbolTypeList = app_doc_data.getSymbolTypeList() |
77 |
for row, symbolType in enumerate(symbolTypeList): |
78 |
items = [QStandardItem(symbolType[2]), QStandardItem('')] |
79 |
items[0].setData(symbolType, Qt.UserRole)
|
80 |
items[0].setEditable(False) |
81 |
items[0].setSelectable(False) |
82 |
items[1].setEditable(False) |
83 |
items[1].setSelectable(False) |
84 |
|
85 |
symbolList = app_doc_data.getSymbolListByType('UID', symbolType[0]) |
86 |
for symbol in symbolList: |
87 |
childs = [QStandardItem(symbol.getName()), QStandardItem('')]
|
88 |
childs[0].setData(symbol.getUid(), Qt.UserRole)
|
89 |
childs[0].setEditable(False) |
90 |
|
91 |
_, svg = app_doc_data.read_symbol_shape(symbol.sName) |
92 |
if svg:
|
93 |
symbol.pixmap = QPixmap() |
94 |
symbol.pixmap.loadFromData(svg if isinstance(svg, bytes) else svg.encode()) |
95 |
icon = QIcon(symbol.pixmap) |
96 |
childs[0].setIcon(icon)
|
97 |
childs[0].svgFilePath = None # save svg file path |
98 |
else:
|
99 |
svgPath = symbol.getSvgFileFullPath() |
100 |
symbol.pixmap = QPixmap(svgPath) |
101 |
icon = QIcon(symbol.pixmap) |
102 |
childs[0].setIcon(icon)
|
103 |
childs[0].svgFilePath = svgPath # save svg file path |
104 |
|
105 |
items[0].appendRow(childs)
|
106 |
|
107 |
items[0].sortChildren(0, Qt.AscendingOrder) |
108 |
self.appendRow(items)
|
109 |
except Exception as ex: |
110 |
from App import App |
111 |
from AppDocData import MessageType |
112 |
|
113 |
message = f'error occurred({ex}) in {sys.exc_info()[-1].tb_frame.f_code.co_filename}:' \
|
114 |
f'{sys.exc_info()[-1].tb_lineno}'
|
115 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
116 |
|
117 |
|
118 |
class BMSettingDialog(QDialog): |
119 |
def __init__(self, parent, columListAll, columnOrder): |
120 |
QDialog.__init__(self, parent)
|
121 |
|
122 |
self.ui = BMSetting_UI.Ui_BMSettingDialog()
|
123 |
self.ui.setupUi(self) |
124 |
|
125 |
self.isAccepted = False |
126 |
self.symbol_mapping = {}
|
127 |
self.columListAll = columListAll
|
128 |
self.columnOrder = columnOrder
|
129 |
|
130 |
self.symbolListModel = SymbolListModel()
|
131 |
self.ui.tableViewSymbolList.setModel(self.symbolListModel) |
132 |
|
133 |
self.symbolMappingModel = SymbolMappingModel()
|
134 |
self.symbolMappingModel.invisibleRootItem()
|
135 |
self.ui.treeViewSymbolMapping.setModel(self.symbolMappingModel) |
136 |
self.ui.treeViewSymbolMapping.expandAll()
|
137 |
self.add_check_box()
|
138 |
self.load_symbol_mapping()
|
139 |
|
140 |
self.attributeListModel = AttributeListModel(self.columListAll) |
141 |
self.ui.tableViewAttributeList.setModel(self.attributeListModel) |
142 |
|
143 |
self.ui.tableViewSymbolList.horizontalHeader().setStretchLastSection(True) |
144 |
self.ui.treeViewSymbolMapping.header().setStretchLastSection(True) |
145 |
self.ui.tableViewAttributeList.horizontalHeader().setStretchLastSection(True) |
146 |
self.ui.tableViewSymbolList.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
|
147 |
self.ui.treeViewSymbolMapping.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
|
148 |
self.ui.tableViewAttributeList.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
|
149 |
self.ui.treeViewSymbolMapping.resizeColumnToContents(0) |
150 |
self.ui.treeViewSymbolMapping.setColumnWidth(1, 50) |
151 |
self.ui.tableViewSymbolList.resizeColumnsToContents()
|
152 |
self.ui.tableViewAttributeList.resizeColumnsToContents()
|
153 |
self.ui.tableViewAttributeList.setColumnWidth(1, 50) |
154 |
|
155 |
self.ui.tableViewSymbolList.clicked.connect(self.on_symbol_changed) |
156 |
self.ui.buttonBox.accepted.connect(self.accept) |
157 |
self.ui.buttonBox.rejected.connect(self.reject) |
158 |
|
159 |
self.on_symbol_changed(self.symbolListModel.index(0, 0)) |
160 |
|
161 |
def load_symbol_mapping(self): |
162 |
app_doc_data = AppDocData.instance() |
163 |
|
164 |
for symbol in self.symbolListModel.SymbolList: |
165 |
self.symbol_mapping[symbol] = []
|
166 |
|
167 |
configs = app_doc_data.getConfigs(section='BM Symbol Mapping')
|
168 |
|
169 |
for config in configs: |
170 |
self.symbol_mapping[config.key] = config.value.split(',') |
171 |
|
172 |
def add_check_box(self): |
173 |
for row in range(self.symbolMappingModel.rowCount()): |
174 |
parent_index = self.symbolMappingModel.index(row, 0) |
175 |
child_count = self.symbolMappingModel.rowCount(parent_index)
|
176 |
for child_row in range(child_count): |
177 |
checkBox = QCheckBox() |
178 |
index = self.symbolMappingModel.index(child_row, 1, parent_index) |
179 |
symbol_index = self.symbolMappingModel.index(child_row, 0, parent_index) |
180 |
symbol_id = self.symbolMappingModel.itemFromIndex(symbol_index).data(Qt.UserRole)
|
181 |
checkBox.stateChanged.connect(partial(self.on_checkBox_changed, str(symbol_id))) |
182 |
self.ui.treeViewSymbolMapping.setIndexWidget(index, checkBox)
|
183 |
|
184 |
def on_checkBox_changed(self, symbol_id, checkState): |
185 |
index = self.ui.tableViewSymbolList.currentIndex()
|
186 |
selected_symbol = self.symbolListModel.itemFromIndex(index)
|
187 |
if checkState is int(Qt.Checked) and not symbol_id in self.symbol_mapping[selected_symbol.text()]: |
188 |
self.symbol_mapping[selected_symbol.text()].append(symbol_id)
|
189 |
elif checkState is int(Qt.Unchecked) and symbol_id in self.symbol_mapping[selected_symbol.text()]: |
190 |
self.symbol_mapping[selected_symbol.text()].remove(symbol_id)
|
191 |
|
192 |
def on_symbol_changed(self, index): |
193 |
selected_symbol = self.symbolListModel.itemFromIndex(index)
|
194 |
|
195 |
for row in range(self.symbolMappingModel.rowCount()): |
196 |
category_index = self.symbolMappingModel.index(row, 0) |
197 |
|
198 |
child_count = self.symbolMappingModel.rowCount(category_index)
|
199 |
for child_row in range(child_count): |
200 |
child_index = self.symbolMappingModel.index(child_row, 0, category_index) |
201 |
id2_symbol_item = self.symbolMappingModel.itemFromIndex(child_index)
|
202 |
id2_symbol_uid = str(id2_symbol_item.data(Qt.UserRole))
|
203 |
checkBox_index = self.symbolMappingModel.index(child_row, 1, category_index) |
204 |
checkBox = self.ui.treeViewSymbolMapping.indexWidget(checkBox_index)
|
205 |
checkBox.setEnabled(True)
|
206 |
if id2_symbol_uid in self.symbol_mapping[selected_symbol.text()]: |
207 |
checkBox.setCheckState(Qt.Checked) |
208 |
else:
|
209 |
checkBox.setCheckState(Qt.Unchecked) |
210 |
|
211 |
matches = [key for key, value in self.symbol_mapping.items() if key != id2_symbol_item.text() and id2_symbol_uid in value] |
212 |
if matches:
|
213 |
checkBox.setEnabled(False)
|
214 |
|
215 |
def reject(self): |
216 |
QDialog.reject(self)
|
217 |
|
218 |
def accept(self): |
219 |
try:
|
220 |
self.isAccepted = False |
221 |
QDialog.accept(self)
|
222 |
|
223 |
except Exception as ex: |
224 |
from App import App |
225 |
from AppDocData import MessageType |
226 |
|
227 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
228 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
229 |
|
230 |
def showDialog(self): |
231 |
self.setWindowFlags(self.windowFlags() & ~Qt.WindowCloseButtonHint & ~Qt.WindowContextHelpButtonHint) |
232 |
self.exec_()
|
233 |
return self.isAccepted |