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