eric7/E5Gui/E5LineEditButton.py

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

eric ide

mercurial