hytos / DTI_PID / DTI_PID / StreamlineDialog.py @ 261421bd
이력 | 보기 | 이력해설 | 다운로드 (10.3 KB)
1 |
# coding: utf-8
|
---|---|
2 |
""" This is Streamline dialog module """
|
3 |
|
4 |
import os |
5 |
import sys |
6 |
from functools import partial |
7 |
from PyQt5.QtCore import * |
8 |
from PyQt5.QtGui import * |
9 |
from PyQt5.QtWidgets import * |
10 |
|
11 |
from AppDocData import AppDocData, MessageType |
12 |
|
13 |
from UI.Streamline_UI import Ui_StreamlineDialog |
14 |
|
15 |
|
16 |
class FromToModel(QStandardItemModel): |
17 |
def __init__(self, parent=None, *args): |
18 |
QStandardItemModel.__init__(self, parent, *args)
|
19 |
|
20 |
self._need_to_pick_backcolor = QColor(255, 0, 255) |
21 |
|
22 |
def data(self, index, role): |
23 |
if not index.isValid(): |
24 |
return None |
25 |
|
26 |
if role in [Qt.BackgroundRole]: |
27 |
if index.column() in [1, 2]: |
28 |
uid = super(FromToModel, self).item(index.row(), index.column()).data(Qt.UserRole) |
29 |
if not uid: |
30 |
return self._need_to_pick_backcolor |
31 |
|
32 |
return super(FromToModel, self).data(index, role) |
33 |
|
34 |
|
35 |
class FromToWidget(QWidget): |
36 |
def __init__(self, parent, column: int, from_to: str = ''): |
37 |
QWidget.__init__(self, parent)
|
38 |
self._column = column
|
39 |
|
40 |
layout = QHBoxLayout() |
41 |
layout.setAlignment(Qt.AlignRight) |
42 |
layout.setContentsMargins(0, 0, 0, 0) |
43 |
pick = QToolButton(self)
|
44 |
pick.setIcon(QIcon(':/newPrefix/OK.svg'))
|
45 |
pick.setMaximumHeight(32)
|
46 |
pick.setMaximumWidth(32)
|
47 |
pick.clicked.connect(partial(parent.parent().parent().parent().on_select_comp, self._column))
|
48 |
layout.addWidget(pick) |
49 |
self.setLayout(layout)
|
50 |
|
51 |
|
52 |
class QStreamlineDialog(QDialog): |
53 |
""" This Streamline dialog class """
|
54 |
|
55 |
def __init__(self, parent): |
56 |
QDialog.__init__(self, parent)
|
57 |
self._from_to_widgets = {}
|
58 |
|
59 |
self.ui = Ui_StreamlineDialog()
|
60 |
self.ui.setupUi(self) |
61 |
self.ui.splitterHorizontal.setSizes([200, 700]) |
62 |
|
63 |
self.fill_hmb_data()
|
64 |
self.ui.tableViewStreamNo.selectionModel().selectionChanged.connect(self.on_selection_changed) |
65 |
|
66 |
self.ui.toolButtonAddFromTo.clicked.connect(self.on_add_new_from_to) |
67 |
self.ui.toolButtonDelFromTo.clicked.connect(self.on_del_from_to) |
68 |
self.ui.pushButtonSaveFromTo.clicked.connect(self.on_save_from_to) |
69 |
|
70 |
#self.ui.buttonBox.button(QDialogButtonBox.Ok).setIcon(QIcon(':/newPrefix/OK.svg'))
|
71 |
self.ui.buttonBox.button(QDialogButtonBox.Close).setIcon(QIcon(':/newPrefix/Remove.svg')) |
72 |
|
73 |
def fill_hmb_data(self): |
74 |
"""load hmb data and fill table view"""
|
75 |
import uuid |
76 |
|
77 |
hmbs = AppDocData.get_hmb_data_from(None)
|
78 |
if not hmbs: |
79 |
return
|
80 |
|
81 |
model = QStandardItemModel() |
82 |
header = ['UID', 'Stream No'] |
83 |
|
84 |
for hmb in hmbs: |
85 |
UID = hmb.uid |
86 |
STREAM_NO = hmb.stream_no |
87 |
items = [QStandardItem(UID), QStandardItem(STREAM_NO)] |
88 |
|
89 |
for data in sorted(hmb.data, key=lambda param:param.index): |
90 |
if hmbs.index(hmb) == 0: |
91 |
header.append(data.name + ' (' + data.unit + ')') |
92 |
items.append(QStandardItem(data.value)) |
93 |
model.appendRow(items) |
94 |
|
95 |
model.setHorizontalHeaderLabels(header) |
96 |
|
97 |
self.ui.tableViewStreamNo.setModel(model)
|
98 |
self.ui.tableViewStreamNo.resizeColumnsToContents()
|
99 |
self.ui.tableViewStreamNo.setEditTriggers(QAbstractItemView.NoEditTriggers)
|
100 |
for col in range(self.ui.tableViewStreamNo.model().columnCount()): |
101 |
self.ui.tableViewStreamNo.setColumnHidden(col, col != 1) # hide all columns except index is 1 |
102 |
|
103 |
def on_selection_changed(self, selection): |
104 |
"""show stream line from, to related to selected hmb"""
|
105 |
|
106 |
if selection.indexes():
|
107 |
row = selection.indexes()[0].row()
|
108 |
uid = self.ui.tableViewStreamNo.model().item(row, 0).text() |
109 |
|
110 |
if not self.ui.tableViewFromTo.model(): |
111 |
model = FromToModel() |
112 |
self.ui.tableViewFromTo.setModel(model)
|
113 |
|
114 |
model = self.ui.tableViewFromTo.model()
|
115 |
model.clear() |
116 |
|
117 |
app_doc_data = AppDocData.instance() |
118 |
rows = app_doc_data.get_stream_lines(uid) |
119 |
if rows:
|
120 |
for row_ in rows: |
121 |
from_ = row_['From_Component_UID']
|
122 |
from_item = QStandardItem(from_) |
123 |
from_item.setData(row_['From_Component_UID'], Qt.UserRole)
|
124 |
|
125 |
to_ = row_['To_Component_UID']
|
126 |
to_item = QStandardItem(to_) |
127 |
to_item.setData(row_['To_Component_UID'], Qt.UserRole)
|
128 |
|
129 |
drawing = row_['Name']
|
130 |
drawing_item = QStandardItem(drawing) |
131 |
drawing_item.setData(row_['Drawing_UID'], Qt.UserRole)
|
132 |
|
133 |
items = [drawing_item, from_item, to_item] |
134 |
model.appendRow(items) |
135 |
|
136 |
model.setHorizontalHeaderLabels(['Drawing No', 'From', 'To']) |
137 |
self.ui.tableViewFromTo.setModel(model)
|
138 |
self.ui.tableViewFromTo.resizeColumnsToContents()
|
139 |
|
140 |
self._from_to_widgets.clear()
|
141 |
for row in range(self.ui.tableViewFromTo.model().rowCount()): |
142 |
i = self.ui.tableViewFromTo.model().index(row, 1) |
143 |
data = self.ui.tableViewFromTo.model().data(i, Qt.DisplayRole)
|
144 |
from_button = FromToWidget(self.ui.tableViewFromTo, 1, data) |
145 |
i = self.ui.tableViewFromTo.model().index(row, 1) |
146 |
self.ui.tableViewFromTo.setIndexWidget(i, from_button)
|
147 |
self._from_to_widgets[from_button] = (row, 1) |
148 |
|
149 |
i = self.ui.tableViewFromTo.model().index(row, 2) |
150 |
data = self.ui.tableViewFromTo.model().data(i, Qt.DisplayRole)
|
151 |
to_button = FromToWidget(self.ui.tableViewFromTo, 2, data) |
152 |
self.ui.tableViewFromTo.setIndexWidget(i, to_button)
|
153 |
self._from_to_widgets[to_button] = (row, 2) |
154 |
|
155 |
"""Properties"""
|
156 |
if not self.ui.tableViewProperties.model(): |
157 |
model_property = QStandardItemModel() |
158 |
self.ui.tableViewProperties.setModel(model_property)
|
159 |
|
160 |
model_property = self.ui.tableViewProperties.model()
|
161 |
model_property.clear() |
162 |
|
163 |
for col in range(self.ui.tableViewStreamNo.model().columnCount()): |
164 |
value = self.ui.tableViewStreamNo.model().item(row, col).text()
|
165 |
property_ = self.ui.tableViewStreamNo.model().headerData(col, Qt.Horizontal)
|
166 |
items = [QStandardItem(property_), QStandardItem(value)] |
167 |
model_property.appendRow(items) |
168 |
|
169 |
model_property.setHorizontalHeaderLabels(['Property', 'Value']) |
170 |
self.ui.tableViewProperties.setModel(model_property)
|
171 |
self.ui.tableViewFromTo.horizontalHeader().setResizeMode(QHeaderView.Stretch)
|
172 |
self.ui.tableViewProperties.resizeColumnsToContents()
|
173 |
self.ui.tableViewProperties.setEditTriggers(QAbstractItemView.NoEditTriggers)
|
174 |
self.ui.tableViewProperties.setRowHidden(0, True) # hide all columns except index is 1 |
175 |
self.ui.tableViewProperties.verticalHeader().setVisible(False) |
176 |
|
177 |
def on_add_new_from_to(self): |
178 |
"""add a new from/to record"""
|
179 |
if not self.ui.tableViewFromTo.model(): |
180 |
return
|
181 |
rows = self.ui.tableViewFromTo.model().rowCount()
|
182 |
index = self.ui.tableViewFromTo.model().index(rows, 0) |
183 |
|
184 |
app_doc_data = AppDocData.instance() |
185 |
drawing_item = QStandardItem(app_doc_data.activeDrawing.name) |
186 |
drawing_item.setData(app_doc_data.activeDrawing.UID, Qt.UserRole) |
187 |
|
188 |
self.ui.tableViewFromTo.model().insertRow(rows, [drawing_item, QStandardItem(), QStandardItem()])
|
189 |
self.ui.tableViewFromTo.selectionModel().setCurrentIndex(index, QItemSelectionModel.NoUpdate)
|
190 |
|
191 |
from_button = FromToWidget(self.ui.tableViewFromTo, 1) |
192 |
i = self.ui.tableViewFromTo.model().index(rows, 1) |
193 |
self.ui.tableViewFromTo.setIndexWidget(i, from_button)
|
194 |
self._from_to_widgets[from_button] = (i.row(), 1) |
195 |
|
196 |
to_button = FromToWidget(self.ui.tableViewFromTo, 2) |
197 |
i = self.ui.tableViewFromTo.model().index(rows, 2) |
198 |
self.ui.tableViewFromTo.setIndexWidget(i, to_button)
|
199 |
self._from_to_widgets[to_button] = (i.row(), 2) |
200 |
|
201 |
def on_del_from_to(self): |
202 |
"""delete selected from/to"""
|
203 |
current = self.ui.tableViewFromTo.selectionModel().currentIndex()
|
204 |
self.ui.tableViewFromTo.model().removeRow(current.row())
|
205 |
|
206 |
def on_select_comp(self, column: int): |
207 |
"""select from or to component"""
|
208 |
from App import App |
209 |
from Commands.SelectCompCommand import SelectCompCommand |
210 |
|
211 |
def on_success_select_comp(selected): |
212 |
current = self.ui.tableViewFromTo.selectionModel().currentIndex()
|
213 |
row = self.ui.tableViewFromTo.model().item(current.row(), 0) |
214 |
column = row.data(Qt.UserRole) |
215 |
item = self.ui.tableViewFromTo.model().item(current.row(), column)
|
216 |
item.setText(str(selected))
|
217 |
item.setData(str(selected.uid), Qt.UserRole)
|
218 |
App.mainWnd().graphicsView.useDefaultCommand() |
219 |
|
220 |
# save column index
|
221 |
if self.sender().parent() in self._from_to_widgets: |
222 |
row, column = self._from_to_widgets[self.sender().parent()] |
223 |
item = self.ui.tableViewFromTo.model().item(row, 0) |
224 |
item.setData(column, Qt.UserRole) |
225 |
self.ui.tableViewFromTo.setCurrentIndex(self.ui.tableViewFromTo.model().index(row, column)) |
226 |
# up to here
|
227 |
cmd = SelectCompCommand(App.mainWnd().graphicsView) |
228 |
cmd.onSuccess.connect(on_success_select_comp) |
229 |
App.mainWnd().graphicsView.command = cmd |
230 |
|
231 |
def on_save_from_to(self): |
232 |
"""save current from/to data"""
|
233 |
row = self.ui.tableViewStreamNo.selectionModel().currentIndex().row()
|
234 |
stream_no_uid = self.ui.tableViewStreamNo.model().item(row, 0).text() |
235 |
|
236 |
from_to_data = [] |
237 |
|
238 |
model = self.ui.tableViewFromTo.model()
|
239 |
for row in range(model.rowCount()): |
240 |
drawing_ = model.item(row, 0)
|
241 |
drawing_uid = drawing_.data(Qt.UserRole) |
242 |
from_ = model.item(row, 1)
|
243 |
from_uid = from_.data(Qt.UserRole) |
244 |
to_ = model.item(row, 2)
|
245 |
to_uid = to_.data(Qt.UserRole) |
246 |
|
247 |
from_to_data.append((drawing_uid, from_uid, to_uid, stream_no_uid)) |
248 |
|
249 |
AppDocData.save_stream_line_data(from_to_data) |
250 |
|
251 |
def accept(self): |
252 |
QDialog.accept(self)
|
253 |
|
254 |
def reject(self): |
255 |
QDialog.reject(self)
|