|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing specialized line edits. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import Qt |
|
11 from PyQt4.QtGui import QLineEdit, QStyleOptionFrameV2, QStyle, QPainter, QPalette |
|
12 |
|
13 class E5LineEdit(QLineEdit): |
|
14 """ |
|
15 Class implementing a line edit widget showing some inactive text. |
|
16 """ |
|
17 def __init__(self, parent = None, inactiveText = ""): |
|
18 """ |
|
19 Constructor |
|
20 |
|
21 @param parent reference to the parent widget (QWidget) |
|
22 @param inactiveText text to be shown on inactivity (string) |
|
23 """ |
|
24 QLineEdit.__init__(self, parent) |
|
25 |
|
26 self.__inactiveText = inactiveText |
|
27 |
|
28 def inactiveText(self): |
|
29 """ |
|
30 Public method to get the inactive text. |
|
31 |
|
32 return inactive text (string) |
|
33 """ |
|
34 return self.__inactiveText |
|
35 |
|
36 def setInactiveText(self, inactiveText): |
|
37 """ |
|
38 Public method to set the inactive text. |
|
39 |
|
40 @param inactiveText text to be shown on inactivity (string) |
|
41 """ |
|
42 self.__inactiveText = inactiveText |
|
43 self.update() |
|
44 |
|
45 def paintEvent(self, evt): |
|
46 """ |
|
47 Protected method handling a paint event. |
|
48 |
|
49 @param evt reference to the paint event (QPaintEvent) |
|
50 """ |
|
51 QLineEdit.paintEvent(self, evt) |
|
52 if not self.text() and \ |
|
53 self.__inactiveText and \ |
|
54 not self.hasFocus(): |
|
55 panel = QStyleOptionFrameV2() |
|
56 self.initStyleOption(panel) |
|
57 textRect = \ |
|
58 self.style().subElementRect(QStyle.SE_LineEditContents, panel, self) |
|
59 textRect.adjust(2, 0, 0, 0) |
|
60 painter = QPainter(self) |
|
61 painter.setPen(self.palette().brush(QPalette.Disabled, QPalette.Text).color()) |
|
62 painter.drawText(textRect, Qt.AlignLeft | Qt.AlignVCenter, self.__inactiveText) |