1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 from PyQt4 import QtGui
29
31 """
32 A QLineEdit with additional decorations :
33
34 * a background text, visible when the line edit doesn't contain any text
35 * a validity, which will trigger the background color
36
37 Use the user_input method to get the text that was entered by the user.
38 """
39
41 QtGui.QLineEdit.__init__(self, parent)
42 self._foreground_color = self.palette().color(self.foregroundRole())
43 self._background_color = self.palette().color(self.backgroundRole())
44 self._showing_background_text = False
45 self._background_text = None
46 self._valid = True
47
49 """Set the validity of the current content of the line edit
50 :param valid: True or False
51 """
52 if valid!=self._valid:
53 self._valid = valid
54 self._update_background_color()
55
56 - def set_background_text(self, background_text):
57 """Set the text to be displayed in the background when the line
58 input does not contain any text
59 :param background_text: the text to be shown, None if no text should be shown
60 """
61 self._hide_background_text()
62 self._background_text = background_text
63 if not self.hasFocus() and background_text!=None:
64 self._show_background_text()
65
67 if not self._showing_background_text and self.text().isEmpty() and self._background_text!=None:
68 self._showing_background_text = True
69 self._update_foreground_color()
70 self.setText(unicode(self._background_text))
71
73 if self._showing_background_text and self._background_text!=None:
74 self._showing_background_text = False
75 self._update_foreground_color()
76 self.setText('')
77
86
95
97 self._hide_background_text()
98 QtGui.QLineEdit.focusInEvent(self, e)
99
101 self._show_background_text()
102 self._update_foreground_color()
103 QtGui.QLineEdit.focusOutEvent(self, e)
104
109
118