프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / Shapes / SymbolSvgItem.py @ 6fb1a52e

이력 | 보기 | 이력해설 | 다운로드 (5.15 KB)

1
#!/usr/bin/env/python3
2
# coding: utf-8
3

    
4
import sys
5
import os
6
import math
7
from PyQt5.QtGui import *
8
from PyQt5.QtCore import *
9
from PyQt5.QtSvg import *
10
from PyQt5.QtWidgets import (QApplication, QGraphicsItem)
11

    
12
class SymbolSvgItem(QGraphicsSvgItem):
13
    clicked = pyqtSignal(QGraphicsSvgItem)
14
    removed = pyqtSignal(QGraphicsSvgItem)
15

    
16
    '''
17
        @history    18.04.11    Jeongwoo    Add Variable (Name, Type)
18
    '''
19
    def __init__(self, path):
20
        import uuid
21

    
22
        super(SymbolSvgItem, self).__init__(path)
23

    
24
        self.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
25
                      #QGraphicsItem.ItemIsMovable)
26
        
27
        self.uid = uuid.uuid4() # generate UUID
28
        self.name = ''
29
        self.type = ''
30
        self.angle = 0
31
        self.loc = None
32
        self.size = None
33
        self.conns = []
34

    
35
        self.setAcceptHoverEvents(True)
36
        self.setAcceptedMouseButtons(Qt.LeftButton)
37
        self.setAcceptTouchEvents(True)
38

    
39
    '''
40
        @brief  return bounding box of symbol
41
        @author humkyung
42
        @date   2018.04.08
43
    '''
44
    def rect(self):
45
        return self.sceneBoundingRect()
46
        
47
    '''
48
        @brief  return true if line is able to connect symbol
49
        @author humkyung
50
        @date   2018.04.13
51
    '''
52
    def isConnectable(self, line, offset,  toler=10):
53
        for pt in self.connPts:
54
            dx = pt[0] - (line[0][0] + offset[0])
55
            dy = pt[1] - (line[0][1] + offset[1])
56
            if (math.sqrt(dx*dx + dy*dy) < toler): return True
57
            dx = pt[0] - (line[-1][0] + offset[0])
58
            dy = pt[1] - (line[-1][1] + offset[1])
59
            if (math.sqrt(dx*dx + dy*dy) < toler): return True
60

    
61
        return False
62
        
63
    '''
64
        @brief  connect line and symbol is able to be connected and return line
65
        @author humkyung
66
        @date   2018.04.16
67
    '''
68
    def connectIfPossible(self, line, toler=10):
69
        from shapely.geometry import Point
70
        res = []
71

    
72
        startPt = line.startPoint()
73
        endPt = line.endPoint()
74
        for i in range(len(self.connPts)):
75
            if len(self.conns) <= i: self.conns.append(None)
76

    
77
            if (self.conns[i] is None) and (Point(startPt[0], startPt[1]).distance(Point(self.connPts[i][0], self.connPts[i][1])) < toler):
78
                self.conns[i] = line
79
                res.append(line)
80
            if (self.conns[i] is None) and (Point(endPt[0], endPt[1]).distance(Point(self.connPts[i][0], self.connPts[i][1])) < toler):
81
                self.conns[i] = line
82
                res.append(line)
83

    
84
        return res
85

    
86
    '''
87
        @brief  return center of symbol
88
        @author humkyung
89
        @date   2018.04.08
90
    '''
91
    def center(self):
92
        return self.sceneBoundingRect().center()
93
        
94
    def hoverEnterEvent(self, event):
95
        cursor = QCursor( Qt.OpenHandCursor )
96
        QApplication.instance().setOverrideCursor( cursor )
97
        self.update()
98

    
99
    def hoverLeaveEvent(self, event):
100
        QApplication.instance().restoreOverrideCursor()
101
        self.update()
102

    
103
    def hoverMoveEvent(self, event):
104
        pass
105

    
106
    '''
107
        @brief      Mouse Press Event
108
        @author     Jeongwoo
109
        @date       18.04.11
110
        @history    .
111
    '''
112
    def mousePressEvent(self, event):
113
        self.clicked.emit(self)
114

    
115
    '''
116
        @brief  remove item when user press delete key
117
        @author humkyung
118
        @date   2018.04.23
119
    '''
120
    def keyPressEvent(self, event): 
121
        if event.key() == Qt.Key_Delete:
122
            for conn in self.conns:
123
                conn.removeSymbol(self)
124
                break
125

    
126
            self.removed.emit(self)
127
            self.scene().removeItem(self)
128

    
129
    '''
130
        @brief  generate xml code
131
        @author humkyung
132
        @date   2018.04.23
133
    '''
134
    def toXml(self):
135
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
136

    
137
        try:
138
            node = Element('SYMBOL')
139
            uidNode = Element('UID')
140
            uidNode.text = str(self.uid)
141

    
142
            nameNode = Element('NAME')
143
            nameNode.text = self.name
144

    
145
            rect = self.sceneBoundingRect()
146
            locNode = Element('LOCATION')
147
            locNode.text = '{},{}'.format(rect.left(), rect.top())
148

    
149
            sizeNode = Element('SIZE')
150
            sizeNode.text = '{},{}'.format(rect.width(), rect.height())
151

    
152
            node.append(uidNode)
153
            node.append(nameNode)
154
            node.append(locNode)
155
            node.append(sizeNode)
156
        except Exception as ex:
157
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
158

    
159
        return node 
160

    
161
    '''
162
        @brief  override paint(draw connection points)
163
        @author humkyung
164
        @date   2018.04.21
165
    '''
166
    def paint(self, painter, options=None, widget=None):
167
        pen = QPen(Qt.red, 10, Qt.SolidLine)
168
        painter.setPen(pen)
169
        painter.drawEllipse(self.center(), 5, 5)
170

    
171
        QGraphicsSvgItem.paint(self, painter, options, widget)
172

    
173
        if self.isUnderMouse() == True:
174
            pen = QPen(Qt.red, 10, Qt.SolidLine)
175
            painter.setPen(pen)
176
            painter.drawEllipse(self.center(), 5, 5)
클립보드 이미지 추가 (최대 크기: 500 MB)