프로젝트

일반

사용자정보

개정판 3216847d

ID3216847d58a4a1f08caa78e7c25e43aefc44e6e7
상위 ce8ce1ee
하위 df93db82

humkyung 이(가) 약 7년 전에 추가함

Change svg symbol color

차이점 보기:

DTI_PID/DTI_PID/Shapes/QEngineeringConnectorItem.py
30 30
        self.buildItem()
31 31
        self.highlight = False
32 32

  
33
        self.setAcceptHoverEvents(True)
34

  
33 35
    '''
34 36
        @brief  build connector item
35 37
        @author humkyung
......
47 49
    '''
48 50
    def setPos(self, pos):
49 51
        self.setRect(pos[0] - round(self.SIZE*0.5), pos[1] - round(self.SIZE*0.5), self.SIZE, self.SIZE)
52
        self.update()
53
    
54
    '''
55
        @brief  highlight connector
56
        @authro humkyung
57
        @date   2018.05.09
58
    '''
59
    def hoverEnterEvent(self, event):
60
        self.setBrush(Qt.red)
61
        self.update()
62

  
63
    '''
64
        @brief  unhighlight connector
65
        @author humkyung
66
        @date   2018.05.09
67
    '''
68
    def hoverLeaveEvent(self, event):
69
        self.setBrush(Qt.blue)
50 70
        self.update()
DTI_PID/DTI_PID/Shapes/SymbolSvgItem.py
8 8
from PyQt5.QtCore import *
9 9
from PyQt5.QtSvg import *
10 10
from PyQt5.QtWidgets import (QApplication, QGraphicsItem)
11
from PyQt5.QtXml import *
11 12

  
12 13
from QEngineeringConnectorItem import QEngineeringConnectorItem
13 14

  
......
21 22
    def __init__(self, path):
22 23
        import uuid
23 24

  
24
        QGraphicsSvgItem.__init__(self, path)
25
        QGraphicsSvgItem.__init__(self)#, path)
25 26

  
26 27
        self.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsFocusable)
27 28
                      #QGraphicsItem.ItemIsMovable)
......
40 41
        self.setAcceptHoverEvents(True)
41 42
        self.setAcceptedMouseButtons(Qt.LeftButton)
42 43
        self.setAcceptTouchEvents(True)
44
        
45
        f = QFile(path)
46
        f.open(QIODevice.ReadOnly)
47
        array = f.readAll()
48
        self._document = QDomDocument()
49
        self._document.setContent(array)
50
        self._renderer = QSvgRenderer(self._document.toByteArray())
51
        self.setSharedRenderer(self._renderer)
43 52

  
44 53
    '''
45 54
        @brief  build symbol item
......
160 169

  
161 170
        for connector in self.connectors:
162 171
            connector.setVisible(True)
172
            connector.setZValue(self.zValue() + 1)
163 173
            connector.update()
164 174

  
165 175
        for attr in self.attrs:
......
175 185
        QApplication.instance().restoreOverrideCursor()
176 186
        self.update()
177 187

  
178
        for connector in self.connectors:
179
            connector.setVisible(False)
180
            connector.update()
188
        if not self.isSelected():
189
            for connector in self.connectors:
190
                connector.setVisible(False)
191
                connector.update()
181 192

  
182 193
        for attr in self.attrs:
183 194
            attr.setDefaultTextColor(Qt.blue)
......
189 200
        @date   2018.04.28
190 201
    '''
191 202
    def hoverMoveEvent(self, event):
203
        pass
204
    '''
192 205
        scenePos = self.mapToScene(event.pos())
193 206
        connPt = self.getConnectionPointCloseTo((scenePos.x(), scenePos.y()), 5)
194 207
        if connPt is not None:
......
199 212
            QApplication.instance().changeOverrideCursor(cursor)
200 213

  
201 214
        self.update()
215
    '''
202 216

  
203 217
    '''
204 218
        @brief      Mouse Press Event
......
316 330
            item = QEngineeringInstrumentItem(path)
317 331
        else:
318 332
            item = SymbolSvgItem(path)
333
            item.setColor('#FF0000')
319 334
        
320 335
        return item
321
        
336

  
337
    '''
338
        @brief  change svg's color
339
        @author humkyung
340
        @date   2018.05.10
341
    '''
342
    def setColor(self, color):
343
        self.changeAttributes('fill', color)
344
        self.changeAttributes('stroke', color)
345
        self.renderer().load(self._document.toByteArray())
346

  
347
    '''
348
        @brief  change attributes
349
        @author humkyung
350
        @date   2018.05.10
351
    '''
352
    def changeAttributes(self, attName, attValue):
353
        root = self._document.documentElement()
354
        node = root.firstChild()
355
        while not node.isNull():
356
            if node.isElement():
357
                element = node.toElement()
358
                if element.hasAttribute(attName):
359
                    element.setAttribute(attName, attValue)
360

  
361
                if element.hasChildNodes():
362
                    recursiveChangeAttributes(element.firstChild(), attName, attValue)
363

  
364
            node = node.nextSibling()
365

  
366
    '''
367
        @brief  change attribute
368
        @author humkyung
369
        @date   2018.05.10
370
    '''
371
    def recursiveChangeAttributes(self, node, attName, attValue):
372
        while not node.isNull():
373
            if node.isElement():
374
                element = node.toElement()
375
                if element.hasAttribute(attName):
376
                    element.setAttribute(attName, attValue)
377

  
378
                if node.hasChildNodes():
379
                    recursiveChangeAttributes(node.firstChild(), attName, attValue)
380
            
381
            node = node.nextSibling()
382
       
322 383
    '''
323 384
        @brief  override paint(draw connection points)
324 385
        @author humkyung
......
354 415
        else:
355 416
            transform.translate(self.loc[0],self.loc[1])
356 417
        self.setTransform(transform)
357
        scene.addItem(self)
418
        scene.addItem(self)
419

  
420
def recursiveChangeAttributes(node, attName, attValue):
421
    while not node.isNull():
422
        if node.isElement():
423
            element = node.toElement()
424
            if element.hasAttribute(attName):
425
                element.setAttribute(attName, attValue)
426

  
427
            if node.hasChildNodes():
428
                recursiveChangeAttributes(node.firstChild(), attName, attValue)
429
        
430
        node = node.nextSibling()
431

  
432

  
433
if __name__ == '__main__':
434
    f = QFile('d:/Projects/DTIPID/DTI_PID/DTI_PID/SG_TEST/svg/ANGLE VALVE.svg')
435
    f.open(QIODevice.ReadOnly)
436
    array = f.readAll()
437
    document = QDomDocument()
438
    document.setContent(array)
439

  
440
    root = document.documentElement()
441
    node = root.firstChild()
442
    while not node.isNull():
443
        if node.isElement():
444
            element = node.toElement()
445
            if element.hasAttribute('fill'):
446
                element.setAttribute('fill', '#FFFFF')
447

  
448
            if element.hasChildNodes():
449
                recursiveChangeAttributes(element.firstChild(), 'fill', '#FFFFF')
450

  
451
        node = node.nextSibling()

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)