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