hytos / DTI_PID / DTI_PID / Shapes / EngineeringUnknownItem.py @ 3ebd61eb
이력 | 보기 | 이력해설 | 다운로드 (10.7 KB)
1 |
# coding: utf-8
|
---|---|
2 |
"""
|
3 |
This is unknown item module
|
4 |
"""
|
5 | |
6 |
import os.path |
7 |
import copy |
8 |
import sys |
9 |
try:
|
10 |
from PyQt5.QtCore import * |
11 |
from PyQt5.QtGui import * |
12 |
from PyQt5.QtWidgets import * |
13 |
except ImportError: |
14 |
try:
|
15 |
from PyQt4.QtCore import * |
16 |
from PyQt4.QtGui import * |
17 |
except ImportError: |
18 |
raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.") |
19 | |
20 |
from EngineeringPolylineItem import QEngineeringPolylineItem |
21 |
from GraphicsBoundingBoxItem import QGraphicsBoundingBoxItem |
22 |
from AppDocData import * |
23 |
from EngineeringAbstractItem import QEngineeringAbstractItem |
24 | |
25 |
class QEngineeringUnknownItem(QEngineeringPolylineItem, QEngineeringAbstractItem): |
26 |
"""
|
27 |
This is unknown item class
|
28 |
"""
|
29 | |
30 |
HIGHLIGHT = '#BC4438'
|
31 |
ZVALUE = 60
|
32 | |
33 |
'''
|
34 |
@history 2018.06.18 Jeongwoo Add variable [transfer] for pyqtSignal
|
35 |
'''
|
36 |
def __init__(self, pts, lineIndicator, isVH=None, otherLine=None, parent=None): |
37 |
import uuid |
38 | |
39 |
QEngineeringPolylineItem.__init__(self, parent)
|
40 |
QEngineeringAbstractItem.__init__(self)
|
41 | |
42 |
self.uid = uuid.uuid4() # generate UUID |
43 |
self.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
|
44 |
self.setAcceptHoverEvents(True) |
45 |
self.setAcceptTouchEvents(True) |
46 | |
47 |
for pt in pts: |
48 |
self._pol.append(QPointF(pt[0], pt[1])) |
49 |
self._pol.append(QPointF(pts[0][0], pts[0][1])) # close path |
50 |
self.lineIndicator = lineIndicator
|
51 |
self.isVH = isVH
|
52 |
self.otherLine = otherLine
|
53 |
self.buildItem()
|
54 |
|
55 |
'''
|
56 |
@build build path
|
57 |
@author humkyung
|
58 |
@date 2018.07.06
|
59 |
'''
|
60 |
def buildItem(self): |
61 |
super(QEngineeringUnknownItem, self).buildItem() |
62 |
self.setZValue(QEngineeringUnknownItem.ZVALUE)
|
63 | |
64 |
if self.lineIndicator == 'Match': |
65 |
self.setPen(QPen(Qt.blue, 1, Qt.SolidLine)) |
66 |
color = QColor(0, 0, 255, 255) |
67 |
self.setBrush(QBrush(color, Qt.SolidPattern))
|
68 |
self._color = color.name()
|
69 |
else:
|
70 |
self.setPen(QPen(Qt.red, 1, Qt.DashDotLine)) |
71 |
color = QColor(255, 255, 0, 127) |
72 |
self.setBrush(QBrush(color))
|
73 |
self._color = color.name()
|
74 | |
75 |
def hoverEnterEvent(self, event): |
76 |
self.hover = True |
77 |
self.update()
|
78 | |
79 |
def hoverLeaveEvent(self, event): |
80 |
self.hover = False |
81 |
self.update()
|
82 | |
83 |
def hoverMoveEvent(self, event): |
84 |
pass
|
85 | |
86 |
'''
|
87 |
@brief remove item when user press delete key
|
88 |
@author humkyung
|
89 |
@date 2018.06.11
|
90 |
@history 2018.06.18 Jeongwoo Call deleteUnknownItemFromScene method
|
91 |
'''
|
92 |
def keyPressEvent(self, event): |
93 |
if event.key() == Qt.Key_Delete:
|
94 |
self.deleteUnknownItemFromScene()
|
95 | |
96 |
'''
|
97 |
@brief show context menu
|
98 |
@author humkyung
|
99 |
@date 2018.08.08
|
100 |
'''
|
101 |
def contextMenuEvent(self, event): |
102 |
if self.isSelected(): |
103 |
menu = QMenu() |
104 |
registerSymbolAction = QAction("심볼 등록")
|
105 |
registerSymbolAction.triggered.connect(lambda: self.onRegisterSymbolEvent()) |
106 |
menu.addAction(registerSymbolAction) |
107 |
recognizeTextAction = QAction("텍스트 인식")
|
108 |
recognizeTextAction.triggered.connect(lambda: self.onRecognizeTextEvent()) |
109 |
menu.addAction(recognizeTextAction) |
110 | |
111 |
menu.exec_(event.screenPos()) |
112 | |
113 |
'''
|
114 |
@brief change unknown item's color
|
115 |
@author humkyung
|
116 |
@date 2018.08.13
|
117 |
'''
|
118 |
def setColor(self, color): |
119 |
c = QColor() |
120 |
c.setNamedColor(color) |
121 |
if self.lineIndicator == 'Match': |
122 |
c.setAlpha(255)
|
123 |
else:
|
124 |
c.setAlpha(177)
|
125 |
self.setBrush(QBrush(c))
|
126 |
self.update()
|
127 | |
128 |
'''
|
129 |
@brief override paint(draw connection points)
|
130 |
@author humkyung
|
131 |
@date 2018.08.08
|
132 |
'''
|
133 |
def paint(self, painter, options=None, widget=None): |
134 |
self.setColor(self.getColor()) |
135 | |
136 |
painter.setClipRect(options.exposedRect) |
137 |
QEngineeringPolylineItem.paint(self, painter, options, widget)
|
138 |
|
139 |
if self.isSelected(): |
140 |
painter.fillPath(self.path(), QBrush(QColor(255, 0, 0, 127))) |
141 | |
142 |
'''
|
143 |
@brief show symbol editor
|
144 |
@author humkyung
|
145 |
@date 2018.08.08
|
146 |
'''
|
147 |
def onRegisterSymbolEvent(self): |
148 |
from App import App |
149 | |
150 |
try:
|
151 |
selected = [item for item in self.scene().items() if item.isSelected() and type(item) is QEngineeringUnknownItem] |
152 |
rect = QRectF() |
153 |
for item in selected: |
154 |
rect = rect.united(item.sceneBoundingRect()) |
155 | |
156 |
App.mainWnd().onAreaSelected(round(rect.left()), round(rect.top()), round(rect.width()), round(rect.height())) |
157 |
except Exception as ex: |
158 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
159 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
160 | |
161 |
'''
|
162 |
@brief show text recognition dialog
|
163 |
@author humkyung
|
164 |
@date 2018.08.08
|
165 |
'''
|
166 |
def onRecognizeTextEvent(self): |
167 |
from App import App |
168 | |
169 |
try:
|
170 |
selected = [item for item in self.scene().items() if item.isSelected() and type(item) is QEngineeringUnknownItem] |
171 |
rect = QRectF() |
172 |
for item in selected: |
173 |
rect = rect.united(item.sceneBoundingRect()) |
174 | |
175 |
App.mainWnd().onRecognizeText(round(rect.left()), round(rect.top()), round(rect.width()), round(rect.height())) |
176 |
except Exception as ex: |
177 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
178 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
179 | |
180 |
'''
|
181 |
@brief parse xml for item
|
182 |
@author humkyung
|
183 |
@date 2018.06.20
|
184 |
'''
|
185 |
@staticmethod
|
186 |
def fromXml(node): |
187 |
from AppDocData import AppDocData |
188 | |
189 |
item = None
|
190 |
try:
|
191 |
location = [float(x) for x in node.find('LOCATION').text.split(',')] |
192 |
size = [float(x) for x in node.find('SIZE').text.split(',')] |
193 |
points = [[int(float(coord)) for coord in pt.split(',')] for pt in node.find('POINTS').text.split('\n')] |
194 | |
195 |
line_indicator_node = node.find('LINEINDICATOR')
|
196 |
if line_indicator_node is not None: |
197 |
lineIndicator = line_indicator_node.text.split('#')
|
198 |
if lineIndicator[0] == 'True': |
199 |
item = QEngineeringUnknownItem(points, 'True', lineIndicator[1], [round(float(lineIndicator[2])), round(float(lineIndicator[3])), round(float(lineIndicator[4])), round(float(lineIndicator[5]))]) |
200 |
elif lineIndicator[0] == 'Match': |
201 |
item = QEngineeringUnknownItem(points, 'Match', lineIndicator[1], [round(float(lineIndicator[2])), round(float(lineIndicator[3])), round(float(lineIndicator[4])), round(float(lineIndicator[5]))]) |
202 |
else:
|
203 |
item = QEngineeringUnknownItem(points, 'False')
|
204 |
else:
|
205 |
item = QEngineeringUnknownItem(points, 'False')
|
206 |
|
207 |
item.setVisible(False)
|
208 |
if node.find('AREA') is None: |
209 |
appDocData = AppDocData.instance() |
210 | |
211 |
minx = min([coord[0] for coord in points]) |
212 |
maxx = max([coord[0] for coord in points]) |
213 |
miny = min([coord[1] for coord in points]) |
214 |
maxy = max([coord[1] for coord in points]) |
215 |
for area in appDocData.getAreaList(): |
216 |
if area.contains([minx, miny, maxx, maxy]):
|
217 |
item.area = area.name |
218 |
break
|
219 |
else:
|
220 |
item.area = node.find('AREA').text
|
221 |
except Exception as ex: |
222 |
from App import App |
223 |
message = 'error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno) |
224 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
225 | |
226 |
return item
|
227 | |
228 |
'''
|
229 |
@brief generate xml code
|
230 |
@author humkyung
|
231 |
@date 2018.06.11
|
232 |
@history 2018.06.14 Jeongwoo Add LOCATION, SIZE info
|
233 |
'''
|
234 |
def toXml(self, owner = None, name='UNKNOWN'): |
235 |
from xml.etree.ElementTree import Element, SubElement, dump, ElementTree |
236 |
from EngineeringLineItem import QEngineeringLineItem |
237 |
from SymbolSvgItem import SymbolSvgItem |
238 | |
239 |
try:
|
240 |
node = Element('UNKNOWN')
|
241 |
|
242 |
rect = self.sceneBoundingRect()
|
243 |
locNode = Element('LOCATION')
|
244 |
locNode.text = '{},{}'.format(rect.left(), rect.top())
|
245 |
node.append(locNode) |
246 | |
247 |
sizeNode = Element('SIZE')
|
248 |
sizeNode.text = '{},{}'.format(rect.width(), rect.height())
|
249 |
node.append(sizeNode) |
250 | |
251 |
areaNode = Element('AREA')
|
252 |
areaNode.text = self.area
|
253 |
node.append(areaNode) |
254 | |
255 |
ptNode = Element('POINTS')
|
256 |
points = None
|
257 |
for pt in self._pol: |
258 |
if points is None: |
259 |
points = '{},{}'.format(int(pt.x()), int(pt.y())) |
260 |
else:
|
261 |
points += '\n{},{}'.format(int(pt.x()), int(pt.y())) |
262 |
ptNode.text = points |
263 |
node.append(ptNode) |
264 | |
265 |
lINode = Element('LINEINDICATOR')
|
266 |
if self.lineIndicator == 'True': |
267 |
lINode.text = 'True' + '#' + self.isVH + '#' + str(self.otherLine[0]) + '#' + str(self.otherLine[1]) + '#' + str(self.otherLine[2]) + '#' + str(self.otherLine[3]) |
268 |
elif self.lineIndicator == 'Match': |
269 |
lINode.text = 'Match' + '#' + self.isVH + '#' + str(self.otherLine[0]) + '#' + str(self.otherLine[1]) + '#' + str(self.otherLine[2]) + '#' + str(self.otherLine[3]) |
270 |
else:
|
271 |
lINode.text = 'False' + '#' + 'None' + '#' + 'None' |
272 |
node.append(lINode) |
273 |
except Exception as ex: |
274 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
275 |
return str(self.uid) |
276 | |
277 |
return node
|
278 | |
279 |
'''
|
280 |
@brief Add to Scene
|
281 |
@author Jeongwoo
|
282 |
@date 2018.06.14
|
283 |
'''
|
284 |
def addUnknownItemToScene(self, scene): |
285 |
self.setZValue(1.0) |
286 |
scene.addItem(self)
|
287 | |
288 |
'''
|
289 |
@brief Delete from Scene
|
290 |
@author Jeongwoo
|
291 |
@date 2018.06.14
|
292 |
@history 2018.06.18 Jeongwoo Add calling removed signal
|
293 |
'''
|
294 |
def deleteUnknownItemFromScene(self): |
295 |
self.transfer.onRemoved.emit(self) |
296 |
self.scene().removeItem(self) |
297 | |
298 |
def boundingRectOnScene(self): |
299 |
rect = self.sceneBoundingRect()
|
300 |
return rect
|