프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / TextItemFactory.py @ d1566ccd

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

1
from SingletonInstance import SingletonInstane
2
import re
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 *
10
        from PyQt4.QtGui import *
11
    except ImportError:
12
        raise ImportError("ImageViewerQt: Requires PyQt5 or PyQt4.")
13
from AppDocData import AppDocData
14
import sys, os
15
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\Shapes')
16
from QEngineeringTextItem import QEngineeringTextItem
17
from QEngineeringLineNoTextItem import QEngineeringLineNoTextItem
18
from QEngineeringNoteItem import QEngineeringNoteItem
19
from QEngineeringSizeTextItem import QEngineeringSizeTextItem
20
from QEngineeringTagNoTextItem import QEngineeringTagNoTextItem
21

    
22
class TextItemFactory(SingletonInstane):
23
    def __init__(self):
24
        self.delimiter = '"'
25

    
26
    '''
27
        @history    2018.04.27  Jeongwoo    Add condition on elif-statement (delimiter != '' and lineNoConfig != '')
28
                    humkyung 2018.05.02 create engineering size text item if condition matches
29
                    humkyung 2018.06.19 set line no's color
30
                    kyouho 2018.07.04 edit for isLineNo method (add parameter)
31
    '''             
32
    def createTextItem(self, text, delimiter = '', lineNoConfig = '', linePropertyData = None, tagSeqNoPattern = None):
33
        import random
34

    
35
        item = None
36

    
37
        try:
38
            docData = AppDocData.instance()
39
            configs = docData.getConfigs('Size', 'Delimiter')
40
            sizeDelimiter = configs[0].value.upper() if 1 == len(configs) else None
41

    
42
            result = (False,)
43
            if (delimiter != '' and lineNoConfig != ''):
44
                result = self.isLineNo(text, delimiter, lineNoConfig, linePropertyData, tagSeqNoPattern)
45

    
46
            if self.isNoteNoText(text):
47
                item = QEngineeringNoteItem()
48
                item.setToolTip(text)
49
            elif (delimiter != '' and lineNoConfig != '') and result[0]:
50
                item = QEngineeringLineNoTextItem(text)
51
                item.setToolTip(text)
52

    
53
                # get color option
54
                configs = docData.getConfigs('Line Color', 'Visible Option')
55
                colorProperty = ''
56
                if configs:
57
                    visibleOption = configs[0].value
58
                else:
59
                    visibleOption = 'Random'
60

    
61
                configs = docData.getConfigs('Color Property', 'State')
62
                if configs:
63
                    colorProperty = configs[0].value
64

    
65
                # set color
66
                if visibleOption == 'Random':
67
                    colorCount = len(docData.colors)
68
                    if colorCount:
69
                        colorIdx = random.randrange(0, colorCount)
70
                        rgb = docData.colors[colorIdx]
71
                        item.setColor(QColor(rgb.red, rgb.green, rgb.blue).name())
72

    
73
                else:
74
                    configs = lineNoConfig[0].value.split(self.delimiter)
75
                    values = result[1]
76
                    index = configs.index(colorProperty)
77
                    if index >= 0:
78
                        value = values[index]
79
                        if colorProperty == 'Nominal Diameter':
80
                            configs = docData.getConfigs('Line No', 'Size Unit')
81
                            if configs and configs[0].value == "Inch":
82
                                value = docData.convertInchToMetric(value)
83

    
84
                        configs = docData.getConfigs(colorProperty, value)
85
                        if configs:
86
                            data = configs[0].value
87
                            rgb = data.split(',')
88
                            item.setColor(QColor(int(rgb[0]), int(rgb[1]), int(rgb[2])).name())
89
                        
90

    
91
                    # up to here
92
            elif self.isSizeText(text, sizeDelimiter):
93
                item = QEngineeringSizeTextItem()
94
                item.setToolTip(text)
95
            elif self.isTagNoText(text):
96
                item = QEngineeringTagNoTextItem()
97
                item.setToolTip(text)
98
            else:
99
                item = QEngineeringTextItem()
100
                item.setToolTip(text)
101
        except Exception as ex:
102
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
103

    
104
        return item
105
    
106
    '''
107
        @brief  check if given text is possible to be line no
108
        @author humkyung
109
        @date   2018.04.20
110
        @history 2018.04.26 Jeongwoo Moved here from QEngineeringTextItem
111
                 2018.04.30 Jeongwoo lineNoConfig None Check
112
                 kyouho 2018.07.04 edit logic
113
    '''
114
    def isLineNo(self, text, delimiter, lineNoConfig, linePropertyData, tagSeqNoPattern):
115
        try:
116
            res = []
117

    
118
            if lineNoConfig is None or len(lineNoConfig) == 0:
119
                return (False,)
120
        
121
            tokens = text.split(delimiter)
122
            patternText = lineNoConfig[0].value.replace(self.delimiter, '').split(self.delimiter)
123
            #Line Number에 Delimiter가 있을 수 있음
124
            if lineNoConfig[0].value != 'None' and len(tokens) >= len(patternText):
125
                configs = lineNoConfig[0].value.split(self.delimiter)
126
                
127
                loopText = text.replace(' ','')
128
                
129
                for propertyName in configs:
130
                    isTable = True
131

    
132
                    loopList = []
133

    
134
                    if propertyName == 'Tag Seq No':
135
                        isTable = False
136
                    elif propertyName == delimiter:
137
                        loopList.append(delimiter)
138
                    else:
139
                        loopList = linePropertyData[propertyName.replace(' ','').replace('&','n')]
140

    
141

    
142
                    if isTable:
143
                        find = False
144
                        for loop in loopList:
145
                            result = self.isStartWithWord(loopText, loop)
146
                            if result[0]:
147
                                loopText = result[1]
148
                                res.append(result[2])
149
                                find = True
150
                                break
151
                        if not find:
152
                            return (False,)
153
                    else:
154
                        result = self.isTagSeqNo(loopText, tagSeqNoPattern)
155
                        if result[0]:
156
                            loopText = result[1]
157
                            res.append(result[2])
158
                        else:
159
                            return (False,)
160

    
161
                if loopText == '':
162
                    return (True, res)
163
                else:
164
                    return (False,)
165

    
166
            return (False,)
167
        except Exception as ex:
168
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
169

    
170
    '''
171
        @brief      Check Text startwith word
172
        @author     kyouho
173
        @date       2018.07.04
174
    '''
175
    def isStartWithWord(self, text, word):
176
        sliceText = text[0:len(word)]
177
        if sliceText == word:
178
            return (True, text[len(sliceText):len(text)], word)
179
        else:
180
            return (False,)
181

    
182

    
183
    '''
184
        @brief      Check Text startwith number
185
        @author     kyouho
186
        @date       2018.07.05
187
    '''
188
    def isStartWithNumber(self, text):
189
        sliceText = text[0:1]
190
        num = ''
191
        if self.isNumber(sliceText):
192
            text = text[1:len(text)]
193
            num += text 
194
            while len(text) > 0:
195
                sliceText = text[0:1]
196
                
197
                if self.isNumber(sliceText):
198
                    text = text[1:len(text)]
199
                    num += text
200
                else:
201
                    break
202
            
203
            return (True, text, num)
204
        else:
205
            return (False,)
206

    
207

    
208
    '''
209
        @brief      Check Number
210
        @author     kyouho
211
        @date       2018.07.05
212
        @history    kyouho 2018.07.09   add Refular Expression
213
    '''
214
    def isNumber(self, num):
215
        p = re.compile('(^[0-9]+$)')
216
        result = p.match(num)
217

    
218
        if result:
219
            return True
220
        else:
221
            return False
222

    
223
    '''
224
        @brief      Check Number
225
        @author     kyouho
226
        @date       2018.07.05
227
    '''
228
    def isTagSeqNo(self, text, pattern):
229
        try:
230
            if pattern is not None:
231
                attr = text
232
                result = eval(pattern)
233
                if self.isNumber(result):
234
                    sliceText = text[0:len(result)]
235
                    return (True, text[len(sliceText):len(text)], sliceText)
236
                else:
237
                    return (False,)
238
            else:
239
                return (False,)
240
        except Exception as ex:
241
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))        
242

    
243
    '''
244
        @brief      Check if given text is Note No Text (ex : NOTE 1, NOTE 2, ...)
245
        @author     Jeongwoo
246
        @date       2018.04.26
247
        @history    2018.05.16  Jeongwoo    Modify Validator flag
248
    '''
249
    def isNoteNoText(self, text):
250
        if QRegExpValidator(QRegExp("NOTE\s\d+")).validate(text, 0)[0] == QValidator.Acceptable :
251
            return True
252
        else:
253
            return False
254

    
255
    '''
256
        @brief  check if given text is size text
257
        @author humkyung
258
        @date   2018.05.02
259
    '''
260
    def isSizeText(self, text, delimiter=None):
261
        docData = AppDocData.instance()
262
        sizeDataList = docData.getNomialPipeSizeData()
263

    
264
        tokens = text.upper().split(delimiter) if delimiter is not None else [text]
265
        for token in tokens:
266
            matches = [sizeData for sizeData in sizeDataList if sizeData.sizeValue() == token]
267
            if not matches: return False
268
        
269
        return True
270
    
271
    '''
272
        @brief  check if given text is tag no
273
        @author humkyung
274
        @date   2018.05.03
275
    '''
276
    def isTagNoText(self, text):
277
        docData = AppDocData.instance()
278
        dataList = docData.getEquipmentDataList()
279
        matches = [data for data in dataList if data.tagNo == text]
280
        return (len(matches) > 0)
281