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