프로젝트

일반

사용자정보

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

hytos / DTI_PID / DTI_PID / Commands / SelectAttributeCommand.py @ d6819cb8

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

1
# coding: utf-8
2
""" This is select attribute command module """
3

    
4
import os.path
5
import sys
6
import AbstractCommand
7

    
8
try:
9
    from PyQt5.QtCore import *
10
    from PyQt5.QtGui import *
11
    from PyQt5.QtWidgets import *
12
except ImportError:
13
    try:
14
        from PyQt4.QtCore import Qt, QRectF, pyqtSignal, QT_VERSION_STR, QEvent
15
        from PyQt4.QtGui import QGraphicsView, QGraphicsScene, QImge, QPixmap, QPainterPath, QFileDialog, QCursor, QMouseEvent
16
    except ImportError:
17
        raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.")
18

    
19
'''
20
    @brief      QtImageViewer Select Attribute Command
21
    @author     kyouho
22
    @date       18.04.10
23
'''
24
class SelectAttributeCommand(AbstractCommand.AbstractCommand):
25
    onSuccess = pyqtSignal()
26

    
27
    def __init__(self, item, attr, imageViewer):
28
        super(SelectAttributeCommand, self).__init__(imageViewer)
29
        self.name = 'SelectAttribute'
30
        self.imageViewer.setCursor(QCursor(Qt.ArrowCursor))
31
        self._item = item
32
        self._attr = attr
33

    
34
    '''
35
        @brief      Select Attribuew
36
        @author     kyouho
37
        @date       18.07.19
38
        @history    euising  2019.01.15  add specbreak
39
        @history    add Valve Oper Item 2019.04.15 by humkyung
40
    '''
41
    def execute(self, param):
42
        from SymbolAttr import SymbolAttr
43
        from SymbolAttr import SymbolProp
44

    
45
        event = param[1]
46
        scenePos = param[2]
47

    
48
        try:
49
            if 'mouseReleaseEvent' == param[0] and event.button() == Qt.LeftButton:
50
                from EngineeringAbstractItem import QEngineeringAbstractItem
51
                from SymbolSvgItem import SymbolSvgItem
52
                from EngineeringTextItem import QEngineeringTextItem
53
                from EngineeringLineNoTextItem import QEngineeringLineNoTextItem
54
                from EngineeringRunItem import QEngineeringRunItem
55
                from QEngineeringTagNoTextItem import QEngineeringTagNoTextItem
56
                from EngineeringLineItem import QEngineeringLineItem
57
                from EngineeringSpecBreakItem import QEngineeringSpecBreakItem
58
                from EngineeringValveOperCodeTextItem import QEngineeringValveOperCodeTextItem
59
                from EngineeringConnectorItem import QEngineeringConnectorItem
60
                from EngineeringEndBreakItem import QEngineeringEndBreakItem
61
                from EngineeringVendorItem import QEngineeringVendorItem
62
                from EngineeringReservedWordTextItem import QEngineeringReservedWordTextItem
63

    
64
                if self._attr is not None and type(self._attr) is SymbolAttr:
65
                    item = self.imageViewer.scene().itemAt(scenePos, QTransform())
66
                    if self._item is not item:
67
                        if False:#item is not None and self._attr.AttributeType == 'Line Item' and issubclass(type(item), QEngineeringLineItem):
68
                            # unused code
69
                            self._item.conns.clear()
70
                            self._item.conns.append(item)
71
                            self.onSuccess.emit()
72
                        elif item is not None and self._attr.AttributeType == 'CONN' and (issubclass(type(item), QEngineeringLineItem) or issubclass(type(item), SymbolSvgItem) or type(item) is QEngineeringVendorItem or type(item) is QEngineeringReservedWordTextItem):
73
                            self._item.connectors[self._attr.AttrAt - 1].connect(item, QEngineeringAbstractItem.CONNECTED_AT_BODY)
74
                            if type(item) is QEngineeringReservedWordTextItem:
75
                                item.owner = self._item
76
                            self.onSuccess.emit()
77
                        elif item is not None and self._attr.AttributeType == 'CONN' and (type(item) is QEngineeringConnectorItem):
78
                            self._item.connectors[self._attr.AttrAt - 1].connect(item.parent, QEngineeringAbstractItem.CONNECTED_AT_PT)
79
                            self.onSuccess.emit()
80
                        #elif item is not None and issubclass(type(self._item), QEngineeringSpecBreakItem) and self._attr.AttributeType == 'Comp Item' and type(item) is not QGraphicsPixmapItem:
81
                        #    self._item.attrs[self._attr] = str(item.uid)
82
                        #    self.onSuccess.emit()
83
                        elif item is not None and (type(self._item) is not QEngineeringEndBreakItem) and (type(item) is QEngineeringLineNoTextItem) and self._attr.AttributeType == 'OWNER':
84
                            inserted = False
85
                            if item.runs:
86
                                for run in item.runs:
87
                                    for index in range(len(run.items)):
88
                                        if run.items[index].is_connected(self._item):
89
                                            if index + 1 != len(run.items) and index != 0 and run.items[index + 1].is_connected(self._item):
90
                                                run.items.insert(index + 1, self._item)
91
                                                inserted = True
92
                                                break
93
                                            elif index + 1 == len(run.items):
94
                                                run.items.append(self._item)
95
                                                inserted = True
96
                                                break
97
                                            elif index == 0:
98
                                                run.items.insert(0, self._item)
99
                                                inserted = True
100
                                                break
101

    
102
                                    if inserted:
103
                                        break
104
                                if not inserted:
105
                                    item.runs.append(QEngineeringRunItem())
106
                                    item.runs[-1].items.append(self._item)
107
                                    inserted = True
108
                            else:
109
                                item.runs.append(QEngineeringRunItem())
110
                                item.runs[0].items.append(self._item)
111
                                inserted = True
112

    
113
                            if inserted:
114
                                if not item.conns and type(self._item) is QEngineeringLineItem:
115
                                    item.conns.append(self._item)
116
                                self._item.owner = item
117
                            self.onSuccess.emit()
118
                        elif item is not None and (type(self._item) is QEngineeringEndBreakItem or issubclass(type(self._item), QEngineeringTextItem)) and (type(item) is QEngineeringLineItem or issubclass(type(item), SymbolSvgItem)) and self._attr.AttributeType == 'OWNER':
119
                            self._item.owner = item
120
                            self.onSuccess.emit()
121
                        elif item is not None and (self._attr.AttributeType == QEngineeringAbstractItem.assoc_type(item) or \
122
                                                    (self._attr.AttributeType == 'Comp Item' and (QEngineeringAbstractItem.assoc_type(item) == 'Symbol Item' or QEngineeringAbstractItem.assoc_type(item) == 'Line Item')) or \
123
                                                    (self._attr.AttributeType == 'EQ Item' and QEngineeringAbstractItem.assoc_type(item) == 'EQ Item')):
124
                            if self._attr.Freeze:
125
                                self.onSuccess.emit()
126
                                return
127
                                
128
                            if QEngineeringAbstractItem.assoc_type(item) not in self._item._associations:
129
                                self._item._associations[QEngineeringAbstractItem.assoc_type(item)] = []
130

    
131
                            for key in self._item.attrs.keys():
132
                                if key.Attribute == self._attr.Attribute:
133
                                    if not key.Codes.values or (key.Codes.values and key.Codes.find_match_exactly(item.text())):
134
                                        key.AssocItem = item
135
                                        self._item.add_assoc_item(item, self._attr.AttrAt, force=True)
136

    
137
                                        # auto freeze when manually attribute setting
138
                                        self._item.getAttributes()  # attr rebinding so old key is not valid
139
                                        for neyKey in self._item.attrs.keys():
140
                                            if neyKey.Attribute == self._attr.Attribute:
141
                                                neyKey.Freeze = True
142
                                                break
143

    
144
                                        if issubclass(type(item), QEngineeringTextItem): item.owner = self._item
145
                                        self.onSuccess.emit()
146
                                        break
147
                elif self._attr is not None and type(self._attr) is SymbolProp:
148
                    item = self.imageViewer.scene().itemAt(scenePos, QTransform())
149
                    if self._item is not item and item is not None and self._attr.match_type(item):
150
                        self._item.set_property(self._attr.Attribute, item)
151
                        if issubclass(type(item), QEngineeringTextItem): item.owner = self._item
152
                        self.onSuccess.emit()
153
        except Exception as ex:
154
            from App import App
155
            from AppDocData import MessageType
156

    
157
            message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
158
            App.mainWnd().addMessage.emit(MessageType.Error, message)       
159

    
160
        self.isTreated = True
161
        
162
    def undo(self):
163
        pass
164

    
165
    def redo(self):
166
        pass
167

    
168
    '''
169
        @brief      Find TextItem contain Point
170
        @author     kyouho
171
        @date       18.07.19
172
    '''
173
    def findTextItemInPoint(self, point):
174
        from EngineeringTextItem import QEngineeringTextItem
175
        
176
        for item in self.imageViewer.items():
177
            if type(item) is QEngineeringTextItem:
178
                if self.isOverlapItemAndPoint(item, point):
179
                    return (True, item)
180

    
181
        return (False,)
182

    
183
    '''
184
        @brief      Find Symbol Item contain Point
185
        @author     kyouho
186
        @date       18.07.19
187
    '''
188
    def findSymbolItemInPoint(self, point):
189
        from SymbolSvgItem import SymbolSvgItem
190
        
191
        for item in self.imageViewer.items():
192
            if issubclass(type(item), SymbolSvgItem):
193
                if self.isOverlapItemAndPoint(item, point):
194
                    return (True, item)
195

    
196
        return (False,)
197

    
198
    '''
199
        @brief      Check Overlap
200
        @author     kyouho
201
        @date       18.07.17
202
    '''
203
    def isOverlapItemAndPoint(self, item, point):
204
        x = point.x()
205
        y = point.y()
206
        loc = item.loc
207
        size = item.size
208

    
209
        if loc[0] <= x and loc[0] + size[0] >= x and loc[1] <= y and loc[1] + size[1] >= y:
210
            return True
211
        else:
212
            return False
클립보드 이미지 추가 (최대 크기: 500 MB)