E5Gui/E5LineEditButton.py

changeset 643
719db11dd4df
child 791
9ec2ac20e54e
equal deleted inserted replaced
641:b06d4df23797 643:719db11dd4df
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a button class to be used with E5LineEdit.
8 """
9
10 from PyQt4.QtCore import Qt, QPoint, QPointF
11 from PyQt4.QtGui import QAbstractButton, QPainter, QPainterPath
12
13 class E5LineEditButton(QAbstractButton):
14 """
15 Class implementing a button to be used with E5LineEdit.
16 """
17 def __init__(self, parent = None):
18 """
19 Constructor
20
21 @param parent reference to the parent widget (QWidget)
22 """
23 QAbstractButton.__init__(self, parent)
24
25 self.__menu = None
26 self.__image = None
27
28 self.setFocusPolicy(Qt.NoFocus)
29 self.setCursor(Qt.ArrowCursor)
30 self.setMinimumSize(16, 16)
31
32 self.clicked[()].connect(self.__clicked)
33
34 def setMenu(self, menu):
35 """
36 Public method to set the button menu.
37
38 @param menu reference to the menu (QMenu)
39 """
40 self.__menu = menu
41 self.update()
42
43 def menu(self):
44 """
45 Public method to get a reference to the menu.
46
47 @return reference to the associated menu (QMenu)
48 """
49 return self.__menu
50
51 def setIcon(self, icon):
52 """
53 Public method to set the button icon.
54
55 @param icon icon to be set (QIcon)
56 """
57 if icon.isNull():
58 self.__image = None
59 else:
60 self.__image = icon.pixmap(16, 16).toImage()
61 QAbstractButton.setIcon(self, icon)
62
63 def __clicked(self):
64 """
65 Private slot to handle a button click.
66 """
67 if self.__menu:
68 pos = self.mapToGlobal(QPoint(0, self.height()))
69 self.__menu.exec_(pos)
70
71 def paintEvent(self, evt):
72 """
73 Protected method handling a paint event.
74
75 @param evt reference to the paint event (QPaintEvent)
76 """
77 painter = QPainter(self)
78
79 if self.__image is not None and not self.__image.isNull():
80 x = (self.width() - self.__image.width()) // 2 - 1
81 y = (self.height() - self.__image.height()) // 2 - 1
82 painter.drawImage(x, y, self.__image)
83
84 if self.__menu is not None:
85 triagPath = QPainterPath()
86 startPos = QPointF(self.width() - 5, self.height() - 3)
87 triagPath.moveTo(startPos)
88 triagPath.lineTo(startPos.x() + 4, startPos.y())
89 triagPath.lineTo(startPos.x() + 2, startPos.y() + 2)
90 triagPath.closeSubpath()
91 painter.setPen(Qt.black)
92 painter.setBrush(Qt.black)
93 painter.setRenderHint(QPainter.Antialiasing, False)
94 painter.drawPath(triagPath)

eric ide

mercurial