프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / Shapes / GraphicsBoundingBoxItem.py @ 2ffe1307

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

1
import os.path
2
import copy
3
try:
4
    from PyQt5.QtCore import *
5
    from PyQt5.QtGui import *
6
    from PyQt5.QtWidgets import *
7
except ImportError:
8
    try:
9
        from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QT_VERSION_STR
10
        from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImage, QPixmap, QPainterPath, QFileDialog
11
    except ImportError:
12
        raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.")
13

    
14
from EngineeringAbstractItem import QEngineeringAbstractItem
15

    
16
class QGraphicsBoundingBoxItem(QGraphicsRectItem, QEngineeringAbstractItem):
17
    onRemoved = pyqtSignal(QGraphicsItem)
18

    
19
    def __init__(self, x, y, width, height):
20
        from EngineeringConnectorItem import QEngineeringConnectorItem
21

    
22
        QGraphicsRectItem.__init__(self, x, y, width, height)
23
        QEngineeringAbstractItem.__init__(self)
24

    
25
        self.setAcceptHoverEvents(True)
26
        self.setAcceptTouchEvents(True)
27
        self.isEntered = False
28
        self.center = self.rect().center()
29
        self.angle = 0
30
        self.isSymbol = False
31
        self.isCreated = False
32
        self._vertices = []
33
        
34
        ## create connectors for corner
35
        connector = QEngineeringConnectorItem(self)
36
        connector.setPos((x, y))
37
        connector.setParentItem(self)
38
        connector.setZValue(self.zValue() + 1)
39
        connector.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
40
        connector.setAcceptTouchEvents(True)
41
        connector.transfer.onPosChanged.connect(self.onConnectorPosChaned)
42
        self.connectors.append(connector)
43

    
44
        connector = QEngineeringConnectorItem(self)
45
        connector.setPos((x + width, y))
46
        connector.setParentItem(self)
47
        connector.setZValue(self.zValue() + 1)
48
        connector.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
49
        connector.setAcceptTouchEvents(True)
50
        connector.transfer.onPosChanged.connect(self.onConnectorPosChaned)
51
        self.connectors.append(connector)
52
        
53
        connector = QEngineeringConnectorItem(self)
54
        connector.setPos((x + width, y + height))
55
        connector.setParentItem(self)
56
        connector.setZValue(self.zValue() + 1)
57
        connector.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
58
        connector.setAcceptTouchEvents(True)
59
        connector.transfer.onPosChanged.connect(self.onConnectorPosChaned)
60
        self.connectors.append(connector)
61

    
62
        connector = QEngineeringConnectorItem(self)
63
        connector.setPos((x, y + height))
64
        connector.setParentItem(self)
65
        connector.setZValue(self.zValue() + 1)
66
        connector.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
67
        connector.setAcceptTouchEvents(True)
68
        connector.transfer.onPosChanged.connect(self.onConnectorPosChaned)
69
        self.connectors.append(connector)
70
        ## up to here
71

    
72
        self.transfer = Transfer()
73

    
74
    '''
75
        @brief      reshape rectangle
76
        @author     humkyung
77
        @date       2018.08.29
78
    '''
79
    def onConnectorPosChaned(self, connector):
80
        found = -1
81
        for i in range(len(self.connectors)):
82
            if self.connectors[i] is connector:
83
                found = i
84
                break
85

    
86
        if found != -1:
87
            other = (found + 2) % 4
88
            minx = min(self.connectors[found].center()[0], self.connectors[other].center()[0])
89
            miny = min(self.connectors[found].center()[1], self.connectors[other].center()[1])
90
            maxx = max(self.connectors[found].center()[0], self.connectors[other].center()[0])
91
            maxy = max(self.connectors[found].center()[1], self.connectors[other].center()[1])
92
            self.setRect(minx, miny, maxx - minx, maxy - miny)
93

    
94
            self.connectors[found].setPos((minx, miny))
95
            index = (found + 1) % 4
96
            self.connectors[index].setPos((minx, maxy))
97
            self.connectors[other].setPos((maxx, maxy))
98
            index = (found + 3) % 4
99
            self.connectors[index].setPos((maxx, miny))
100

    
101
        self.transfer.onSizeChanged.emit(self)
102

    
103
    '''
104
        @brief      set graphicsboundingbox's color
105
        @author     humkyung
106
        @date       2018.08.29
107
    '''
108
    def setColor(self, color):
109
        self._color = color
110
        c = QColor()
111
        c.setNamedColor(color)
112
        _pen = self.pen()
113
        _pen.setColor(c)
114
        self.setPen(_pen)
115
        self.update()
116

    
117
    def getColor(self):
118
        return self.pen().color().name()
119

    
120
    """
121
        @brief      override setRect
122
        @author     humkyung
123
        @date       2018.09.12
124
    """
125
    def setRect(self, x, y, width, height):
126
        QGraphicsRectItem.setRect(self, x, y, width, height)
127
        
128
        minx, miny, maxx, maxy = x, y, x + width, y + height
129
        self.connectors[0].setPos((minx, miny))
130
        self.connectors[1].setPos((minx, maxy))
131
        self.connectors[2].setPos((maxx, maxy))
132
        self.connectors[3].setPos((maxx, miny))
133

    
134
    '''
135
        @brief  construct a bounding box item
136
    '''
137
    def process(self, param):
138
        if ('mouseMoveEvent' == param[0]):
139
            if len(self._vertices) == 1:
140
                self._vertices.append(param[2]) 
141
            elif len(self._vertices) > 1:
142
                self._vertices[1] = param[2]
143

    
144
            if len(self._vertices) == 2:
145
                x = min(self._vertices[0].x(), self._vertices[1].x())
146
                y = min(self._vertices[0].y(), self._vertices[1].y())
147
                dx = abs(self._vertices[0].x() - self._vertices[1].x())
148
                dy = abs(self._vertices[0].y() - self._vertices[1].y())
149
                self.setRect(x, y, dx, dy)
150
        elif ('mouseReleaseEvent' == param[0]) and (param[1].button() == Qt.LeftButton):
151
            if len(self._vertices) == 0:
152
                self._vertices.append(param[2])
153
            elif len(self._vertices) == 1:
154
                self._vertices.append(param[2])
155
            elif len(self._vertices) == 2:
156
                x = min(self._vertices[0].x(), self._vertices[1].x())
157
                y = min(self._vertices[0].y(), self._vertices[1].y())
158
                dx = abs(self._vertices[0].x() - self._vertices[1].x())
159
                dy = abs(self._vertices[0].y() - self._vertices[1].y())
160
                self.setRect(x, y, dx, dy)
161
                self.isCreated = True
162
    '''
163
        @brief      reshape while mouse drag
164
        @author     humkyung
165
        @date       2018.08.18
166
    '''
167
    def mouseMoveEvent(self, event):
168
        if event.button() == Qt.LeftButton:
169
            scenePos = event.scenePos()
170

    
171
            if len(self._vertices) == 1:
172
                self._vertices.append(scenePos) 
173
            elif len(self._vertices) > 1:
174
                self._vertices[1] = scenePos
175

    
176
            if len(self._vertices) == 2:
177
                x = min(self._vertices[0].x(), self._vertices[1].x())
178
                y = min(self._vertices[0].y(), self._vertices[1].y())
179
                dx = abs(self._vertices[0].x() - self._vertices[1].x())
180
                dy = abs(self._vertices[0].y() - self._vertices[1].y())
181
                self.setRect(x, y, dx, dy)
182

    
183
        QGraphicsRectItem.mouseMoveEvent(self, event)
184

    
185
    '''
186
        @brief      
187
        @author     humkyung
188
        @date       2018.08.18
189
    '''
190
    def mouseReleaseEvent(self, event):
191
        if event.button() == Qt.LeftButton:
192
            scenePos = event.scenePos()
193

    
194
            if len(self._vertices) == 0:
195
                self._vertices.append(scenePos)
196
            elif len(self._vertices) == 1:
197
                self._vertices.append(scenePos)
198
            elif len(self._vertices) == 2:
199
                x = min(self._vertices[0].x(), self._vertices[1].x())
200
                y = min(self._vertices[0].y(), self._vertices[1].y())
201
                dx = abs(self._vertices[0].x() - self._vertices[1].x())
202
                dy = abs(self._vertices[0].y() - self._vertices[1].y())
203
                self.setRect(x, y, dx, dy)
204
                self.isCreated = True
205
        
206
        QGraphicsRectItem.mouseReleaseEvent(self, event)
207

    
208
    '''
209
        @brief  clone an object
210
    '''
211
    def clone(self):
212
        clone = QGraphicsBoundingBoxItem(self.rect().x(), self.rect().y(), self.rect().width(), self.rect().height())
213
        clone.isCreated = True
214
        clone.center = self.center
215
        return clone
216

    
217
    '''
218
        @brief  initialize
219
    '''
220
    def init(self):
221
        self._vertices = []
222
        self.isCreated = False
223

    
224
    def hoverEnterEvent(self, event):
225
        self.isEntered = True
226
        self.update()
227

    
228
    def hoverLeaveEvent(self, event):
229
        self.isEntered = False
230
        self.update()
231

    
232
    '''
233
        @brief  override paint
234
    '''
235
    def paint(self, painter, options=None, widget=None):
236
        QGraphicsRectItem.paint(self, painter, options, widget)
237

    
238
        if self.isEntered == True:
239
            painter.setPen(self.pen())
240
            painter.drawEllipse(self.center, 5, 5)
241
            
242
'''
243
    @brief      The class transfer pyqtSignal Event
244
    @author     humkyung
245
    @date       2018.08.30
246
'''
247
class Transfer(QObject):
248
    onSizeChanged = pyqtSignal(QGraphicsItem)
249
    onRemoved = pyqtSignal(QGraphicsItem)
250

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