E5Gui/E5LineEditButton.py

Fri, 31 Dec 2010 15:49:50 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 31 Dec 2010 15:49:50 +0100
changeset 791
9ec2ac20e54e
parent 643
719db11dd4df
child 945
8cd4d08fa9f6
child 1510
e75ecf2bd9dd
permissions
-rw-r--r--

Updated copyright notice.

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

eric ide

mercurial