2012-03-02 02:55:32 +00:00
|
|
|
from pyqtgraph.Qt import QtGui, QtCore
|
2012-05-11 22:05:41 +00:00
|
|
|
from .GraphicsObject import GraphicsObject
|
2012-03-02 02:55:32 +00:00
|
|
|
|
|
|
|
__all__ = ['ButtonItem']
|
|
|
|
class ButtonItem(GraphicsObject):
|
|
|
|
"""Button graphicsItem displaying an image."""
|
|
|
|
|
|
|
|
clicked = QtCore.Signal(object)
|
|
|
|
|
2012-10-11 15:14:35 +00:00
|
|
|
def __init__(self, imageFile=None, width=None, parentItem=None, pixmap=None):
|
2012-03-02 02:55:32 +00:00
|
|
|
self.enabled = True
|
|
|
|
GraphicsObject.__init__(self)
|
2012-10-11 15:14:35 +00:00
|
|
|
if imageFile is not None:
|
|
|
|
self.setImageFile(imageFile)
|
|
|
|
elif pixmap is not None:
|
|
|
|
self.setPixmap(pixmap)
|
|
|
|
|
2012-03-02 02:55:32 +00:00
|
|
|
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):
|
2012-10-11 15:14:35 +00:00
|
|
|
self.setPixmap(QtGui.QPixmap(imageFile))
|
|
|
|
|
|
|
|
def setPixmap(self, pixmap):
|
|
|
|
self.pixmap = pixmap
|
2012-03-02 02:55:32 +00:00
|
|
|
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())
|
|
|
|
|