ButtonItem.py 1.6 KB
Newer Older
1
from ..Qt import QtGui, QtCore
2
from .GraphicsObject import GraphicsObject
3
4
5
6
7
8
9

__all__ = ['ButtonItem']
class ButtonItem(GraphicsObject):
    """Button graphicsItem displaying an image."""
    
    clicked = QtCore.Signal(object)
    
10
    def __init__(self, imageFile=None, width=None, parentItem=None, pixmap=None):
11
12
        self.enabled = True
        GraphicsObject.__init__(self)
13
14
15
16
17
        if imageFile is not None:
            self.setImageFile(imageFile)
        elif pixmap is not None:
            self.setPixmap(pixmap)
            
18
19
20
21
22
23
24
25
        if width is not None:
            s = float(width) / self.pixmap.width()
            self.scale(s, s)
        if parentItem is not None:
            self.setParentItem(parentItem)
        self.setOpacity(0.7)
        
    def setImageFile(self, imageFile):        
26
27
28
29
        self.setPixmap(QtGui.QPixmap(imageFile))
        
    def setPixmap(self, pixmap):
        self.pixmap = pixmap
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        self.update()
        
    def mouseClickEvent(self, ev):
        if self.enabled:
            self.clicked.emit(self)
        
    def mouseHoverEvent(self, ev):
        if not self.enabled:
            return
        if ev.isEnter():
            self.setOpacity(1.0)
        else:
            self.setOpacity(0.7)

    def disable(self):
        self.enabled = False
        self.setOpacity(0.4)
        
    def enable(self):
        self.enabled = True
        self.setOpacity(0.7)
        
    def paint(self, p, *args):
        p.setRenderHint(p.Antialiasing)
        p.drawPixmap(0, 0, self.pixmap)
        
    def boundingRect(self):
        return QtCore.QRectF(self.pixmap.rect())