프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / DTI_PID / Shapes / EngineeringOriginItem.py @ 38657e1b

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

1
# coding: utf-8
2

    
3
import os.path
4
import copy
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 Qt, QRectF, pyqtSignal, QT_VERSION_STR
12
        from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog
13
    except ImportError:
14
        raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.")
15

    
16
from EngineeringLineItem import QEngineeringLineItem
17

    
18
class QEngineeringOriginItem(QGraphicsEllipseItem):
19
    SMALL_SIZE = 10
20
    BIG_SIZE = 16
21
    HIGHLIGHT = '#BC4438'
22

    
23
    '''
24
        @brief  
25
        @author 
26
        @date   
27
    '''
28
    def __init__(self, parent=None):
29
        QGraphicsEllipseItem.__init__(self, parent)
30
        self.parent = parent
31
        self.buildItem()
32
        self.highlight = False
33
        self.connectedItem = None
34
        self.sceneConnectPoint = None
35
        self.connectPoint = None
36
        self._hoverItem = None
37

    
38
        self.setAcceptHoverEvents(True)
39

    
40
        if type(parent) is QEngineeringLineItem:
41
            self.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
42
            self.setAcceptTouchEvents(True)
43

    
44
        if self.parent is not None: 
45
            self.transfer = Transfer()
46
            self.transfer.onPosChanged.connect(parent.onConnectorPosChaned)
47

    
48
    '''
49
        @brief  build origin item
50
        @author humkyung
51
        @date   2018.08.28
52
    '''
53
    def buildItem(self):
54
        color = Qt.red
55
        self.setBrush(color)
56
        if self.parent is not None:
57
            self.setZValue(self.parent.zValue() + 1)
58

    
59
    '''
60
        @brief      return center of origin
61
        @author     humkyung
62
        @date       2018.08.28
63
    '''
64
    def center(self):
65
        pt = self.sceneBoundingRect().center()
66
        return (pt.x(), pt.y())
67
        
68
    '''
69
        @brief  set position of origin
70
        @author humkyung
71
        @date   2018.08.28
72
    '''
73
    def setPos(self, pos):
74
        self._loc = [pos[0], pos[1]] 
75
        self.setRect(self._loc[0] - round(self.SMALL_SIZE*0.5), self._loc[1] - round(self.SMALL_SIZE*0.5), self.SMALL_SIZE, self.SMALL_SIZE)
76
        self.update()
77
    
78
    '''
79
        @brief  highlight origin
80
        @authro humkyung
81
        @date   2018.08.28
82
    '''
83
    def hoverEnterEvent(self, event):
84
        try:
85
            self.setRect(self._loc[0] - round(self.BIG_SIZE*0.5), self._loc[1] - round(self.BIG_SIZE*0.5), self.BIG_SIZE, self.BIG_SIZE)
86
            c = QColor()
87
            c.setNamedColor(QEngineeringOriginItem.HIGHLIGHT)
88
            self.setPen(Qt.red)
89
            self.setBrush(c)
90
            if self.scene().sceneRect().contains(self.sceneBoundingRect()):
91
                self.update()
92
            else:
93
                self.scene().update()
94
        finally:
95
            if self.parent is not None:
96
                self._ownerColor = self.parent.getColor()
97
                self.parent.setColor(QEngineeringOriginItem.HIGHLIGHT)
98

    
99
            QApplication.setOverrideCursor(Qt.CrossCursor)
100

    
101
    '''
102
        @brief  unhighlight origin
103
        @author humkyung
104
        @date   2018.08.28
105
    '''
106
    def hoverLeaveEvent(self, event):
107
        try:
108
            self.setRect(self._loc[0] - round(self.SMALL_SIZE*0.5), self._loc[1] - round(self.SMALL_SIZE*0.5), self.SMALL_SIZE, self.SMALL_SIZE)
109
            self.setBrush(Qt.red)
110
            self.setPen(Qt.black)
111
            if self.scene().sceneRect().contains(self.sceneBoundingRect()):
112
                self.update()
113
            else:
114
                self.scene().update()
115
        finally:
116
            if self.parent is not None:
117
                self.parent.setColor(self._ownerColor)
118

    
119
            QApplication.restoreOverrideCursor()
120

    
121
    '''
122
        @brief      
123
        @author     humkyung
124
        @date       2018.08.28
125
    '''
126
    def mousePressEvent(self, event):
127
        if type(self.parent) is QEngineeringLineItem:
128
            if event.buttons() == Qt.LeftButton:
129
                self._savedPos = self._loc
130
                self._savedConnectedItem = self.connectedItem
131
                self.grabKeyboard()
132
                
133
        QGraphicsEllipseItem.mousePressEvent(self, event)
134

    
135
    '''
136
        @brief      
137
        @author     humkyung
138
        @date       2018.08.28
139
    '''
140
    def mouseReleaseEvent(self, event):
141
        import shapely
142

    
143
        try:
144
            if type(self.parent) is QEngineeringLineItem and self._savedPos is not None:
145
                items = [item for item in self.scene().items(event.scenePos()) if item is not self and item is not self.parent and type(item) is not QGraphicsPixmapItem]
146

    
147
                if items and type(items[0]) is QEngineeringLineItem:
148
                    length = items[0].length()*0.5
149
                    dir = items[0].perpendicular()
150
                    if self.parent.isHorizontal():
151
                        start = [event.scenePos().x() - dir[0]*length, self.parent.start_point()[1] - dir[1]*length]
152
                        end = [event.scenePos().x() + dir[0]*length, self.parent.start_point()[1] + dir[1]*length]
153
                    else:
154
                        start = [self.parent.start_point()[0] - dir[0]*length, event.scenePos().y() - dir[1]*length]
155
                        end = [self.parent.start_point()[0] + dir[0]*length, event.scenePos().y() + dir[1]*length]
156

    
157
                    pt = items[0].intersection([start, end])
158
                    if (pt is not None) and (type(pt) == shapely.geometry.point.Point):
159
                        self.setPos((pt.x, pt.y))
160
                        self.connectedItem = items[0]
161
                elif items and type(items[0]) is QEngineeringConnectorItem:
162
                    self.setPos(items[0].center())
163
                    
164
                    if self.connectedItem is not None:
165
                        for connect in self.connectedItem.connectors:
166
                            if connect.connectedItem == self.parent:
167
                                connect.connectedItem = None
168

    
169
                    self.connectedItem = items[0].parent
170
                    items[0].connectedItem = self.parent
171
                else:
172
                    other = [connector for connector in self.parent.connectors if connector is not self]
173
                    if other:
174
                        pt = [event.scenePos().x(), event.scenePos().y()]
175
                        dx = abs(pt[0] - other[0].center()[0])
176
                        dy = abs(pt[1] - other[0].center()[1])
177
                        if dx < dy:
178
                            pt[0] = other[0].center()[0]
179
                        else:
180
                            pt[1] = other[0].center()[1]
181

    
182
                    self.setPos(pt)
183
                    if self.connectedItem is not None:
184
                        for connect in self.connectedItem.connectors:
185
                            if connect.connectedItem == self.parent:
186
                                connect.connectedItem = None
187

    
188
                    self.connectedItem = None
189

    
190
                ## unhighlight underitem
191
                if hasattr(self, '_underItem') and self._underItem is not None:
192
                    self._underItem.hoverLeaveEvent(event)
193
                ## up to here
194

    
195
            if type(self.parent) is QEngineeringLineItem:
196
                self._savedPos = None 
197
                self._savedConnectedItem = None
198
                self.transfer.onPosChanged.emit(self)
199
        finally:
200
            self.ungrabKeyboard()
201

    
202
        QGraphicsEllipseItem.mouseReleaseEvent(self, event)
203

    
204
    '''
205
        @brief      move origin's position while mouse drag
206
        @author     humkyung
207
        @date       2018.08.28
208
    '''
209
    def mouseMoveEvent(self, event):
210
        import shapely
211

    
212
        if type(self.parent) is QEngineeringLineItem and self._savedPos is not None:
213
            if event.buttons() == Qt.LeftButton:
214
                items = [item for item in self.scene().items(event.scenePos()) if item is not self and item is not self.parent and type(item) is not QGraphicsPixmapItem]
215
                ## highlight underitem
216
                if len(items) > 0:
217
                    if not hasattr(self, '_underItem') or self._underItem is not items[0]:
218
                        if hasattr(self, '_underItem') and self._underItem is not None:
219
                            self._underItem.hoverLeaveEvent(event)
220

    
221
                        self._underItem = items[0]
222
                        self._underItem.hoverEnterEvent(event)
223
                elif hasattr(self, '_underItem') and self._underItem is not None:
224
                    self._underItem.hoverLeaveEvent(event)
225
                    self._underItem = None
226
                ## up to here
227

    
228
                if items and self.parent not in items and type(items[0]) is QEngineeringLineItem:
229
                    length = items[0].length()*0.5
230
                    dir = items[0].perpendicular()
231
                    if self.parent.isHorizontal():
232
                        start = [event.scenePos().x() - dir[0]*length, self.parent.start_point()[1] - dir[1]*length]
233
                        end = [event.scenePos().x() + dir[0]*length, self.parent.start_point()[1] + dir[1]*length]
234
                    else:
235
                        start = [self.parent.start_point()[0] - dir[0]*length, event.scenePos().y() - dir[1]*length]
236
                        end = [self.parent.start_point()[0] + dir[0]*length, event.scenePos().y() + dir[1]*length]
237

    
238
                    pt = items[0].intersection([start, end])
239
                    if (pt is not None) and (type(pt) == shapely.geometry.point.Point):
240
                        self.setPos((pt.x, pt.y))
241
                elif items and type(items[0]) is QEngineeringConnectorItem:
242
                    self.setPos(items[0].center())
243
                else:
244
                    other = [connector for connector in self.parent.connectors if connector is not self]
245
                    if other:
246
                        pt = [event.scenePos().x(), event.scenePos().y()]
247
                        dx = abs(pt[0] - other[0].center()[0])
248
                        dy = abs(pt[1] - other[0].center()[1])
249
                        if dx < dy:
250
                            pt[0] = other[0].center()[0]
251
                        else:
252
                            pt[1] = other[0].center()[1]
253

    
254
                    self.setPos(pt)
255

    
256
                self.update()
257

    
258
        QGraphicsEllipseItem.mouseMoveEvent(self, event)
259

    
260
    '''
261
        @brief      reject if user press Escape key
262
        @author     humkyung
263
        @date       2018.08.28
264
    '''
265
    def keyPressEvent(self, event):
266
        if event.key() == Qt.Key_Escape:
267
            if hasattr(self, '_savedPos') and self._savedPos is not None:
268
                self.ungrabKeyboard()
269
                self.setPos(self._savedPos)
270
                self.connectedItem = self._savedConnectedItem
271
                self._savedPos = None
272
                self.update()
273

    
274
        QGraphicsEllipseItem.keyPressEvent(self, event)
275

    
276
'''
277
    @brief      The class transfer pyqtSignal Event. Cause Subclass of QGraphicsRectItem can't use pyqtSignal
278
    @author     Jeongwoo
279
    @date       2018.06.18
280
'''
281
class Transfer(QObject):
282
    onPosChanged = pyqtSignal(QGraphicsItem)
283
    onRemoved = pyqtSignal(QGraphicsItem)
284

    
285
    def __init__(self, parent = None):
286
        QObject.__init__(self, parent)
클립보드 이미지 추가 (최대 크기: 500 MB)