E5Gui/E5SqueezeLabels.py

branch
Py2 comp.
changeset 2525
8b507a9a2d40
parent 2302
f29e9405c851
child 2791
a9577f248f04
equal deleted inserted replaced
2523:139f182b72f6 2525:8b507a9a2d40
4 # 4 #
5 5
6 """ 6 """
7 Module implementing labels that squeeze their contents to fit the size of the label. 7 Module implementing labels that squeeze their contents to fit the size of the label.
8 """ 8 """
9
10 from __future__ import unicode_literals # __IGNORE_WARNING__
9 11
10 from PyQt4.QtCore import Qt 12 from PyQt4.QtCore import Qt
11 from PyQt4.QtGui import QLabel 13 from PyQt4.QtGui import QLabel
12 14
13 from Utilities import compactPath 15 from Utilities import compactPath
21 """ 23 """
22 Constructor 24 Constructor
23 25
24 @param parent reference to the parent Widget (QWidget) 26 @param parent reference to the parent Widget (QWidget)
25 """ 27 """
26 super().__init__(parent) 28 super(E5SqueezeLabel, self).__init__(parent)
27 29
28 self.__text = '' 30 self.__text = ''
29 self.__elided = '' 31 self.__elided = ''
30 32
31 def paintEvent(self, event): 33 def paintEvent(self, event):
35 @param event reference to the paint event (QPaintEvent) 37 @param event reference to the paint event (QPaintEvent)
36 """ 38 """
37 fm = self.fontMetrics() 39 fm = self.fontMetrics()
38 if fm.width(self.__text) > self.contentsRect().width(): 40 if fm.width(self.__text) > self.contentsRect().width():
39 self.__elided = fm.elidedText(self.text(), Qt.ElideMiddle, self.width()) 41 self.__elided = fm.elidedText(self.text(), Qt.ElideMiddle, self.width())
40 super().setText(self.__elided) 42 super(E5SqueezeLabel, self).setText(self.__elided)
41 else: 43 else:
42 super().setText(self.__text) 44 super(E5SqueezeLabel, self).setText(self.__text)
43 super().paintEvent(event) 45 super(E5SqueezeLabel, self).paintEvent(event)
44 46
45 def setText(self, txt): 47 def setText(self, txt):
46 """ 48 """
47 Public method to set the label's text. 49 Public method to set the label's text.
48 50
49 @param txt the text to be shown (string) 51 @param txt the text to be shown (string)
50 """ 52 """
51 self.__text = txt 53 self.__text = txt
52 super().setText(self.__text) 54 super(E5SqueezeLabel, self).setText(self.__text)
53 55
54 56
55 class E5SqueezeLabelPath(QLabel): 57 class E5SqueezeLabelPath(QLabel):
56 """ 58 """
57 Class implementing a label showing a file path compacted to fit it's size. 59 Class implementing a label showing a file path compacted to fit it's size.
60 """ 62 """
61 Constructor 63 Constructor
62 64
63 @param parent reference to the parent Widget (QWidget) 65 @param parent reference to the parent Widget (QWidget)
64 """ 66 """
65 super().__init__(parent) 67 super(E5SqueezeLabelPath, self).__init__(parent)
66 68
67 self.__path = '' 69 self.__path = ''
68 self.__surrounding = "{0}" 70 self.__surrounding = "{0}"
69 71
70 def setSurrounding(self, surrounding): 72 def setSurrounding(self, surrounding):
72 Public method to set the surrounding of the path string. 74 Public method to set the surrounding of the path string.
73 75
74 @param surrounding the a string containg placeholders for the path (string) 76 @param surrounding the a string containg placeholders for the path (string)
75 """ 77 """
76 self.__surrounding = surrounding 78 self.__surrounding = surrounding
77 super().setText(self.__surrounding.format(self.__path)) 79 super(E5SqueezeLabelPath, self).setText(self.__surrounding.format(self.__path))
78 80
79 def setPath(self, path): 81 def setPath(self, path):
80 """ 82 """
81 Public method to set the path of the label. 83 Public method to set the path of the label.
82 84
83 @param path path to be shown (string) 85 @param path path to be shown (string)
84 """ 86 """
85 self.__path = path 87 self.__path = path
86 super().setText(self.__surrounding.format(self.__path)) 88 super(E5SqueezeLabelPath, self).setText(self.__surrounding.format(self.__path))
87 89
88 def setTextPath(self, surrounding, path): 90 def setTextPath(self, surrounding, path):
89 """ 91 """
90 Public method to set the surrounding and the path of the label. 92 Public method to set the surrounding and the path of the label.
91 93
92 @param surrounding the a string containg placeholders for the path (string) 94 @param surrounding the a string containg placeholders for the path (string)
93 @param path path to be shown (string) 95 @param path path to be shown (string)
94 """ 96 """
95 self.__surrounding = surrounding 97 self.__surrounding = surrounding
96 self.__path = path 98 self.__path = path
97 super().setText(self.__surrounding.format(self.__path)) 99 super(E5SqueezeLabelPath, self).setText(self.__surrounding.format(self.__path))
98 100
99 def paintEvent(self, event): 101 def paintEvent(self, event):
100 """ 102 """
101 Protected method called when some painting is required. 103 Protected method called when some painting is required.
102 104
103 @param event reference to the paint event (QPaintEvent) 105 @param event reference to the paint event (QPaintEvent)
104 """ 106 """
105 fm = self.fontMetrics() 107 fm = self.fontMetrics()
106 if fm.width(self.__surrounding.format(self.__path)) > self.contentsRect().width(): 108 if fm.width(self.__surrounding.format(self.__path)) > self.contentsRect().width():
107 super().setText( 109 super(E5SqueezeLabelPath, self).setText(
108 self.__surrounding.format(compactPath(self.__path, 110 self.__surrounding.format(compactPath(self.__path,
109 self.contentsRect().width(), 111 self.contentsRect().width(),
110 self.length)) 112 self.length))
111 ) 113 )
112 else: 114 else:
113 super().setText(self.__surrounding.format(self.__path)) 115 super(E5SqueezeLabelPath, self).setText(self.__surrounding.format(self.__path))
114 super().paintEvent(event) 116 super(E5SqueezeLabelPath, self).paintEvent(event)
115 117
116 def length(self, txt): 118 def length(self, txt):
117 """ 119 """
118 Public method to return the length of a text in pixels. 120 Public method to return the length of a text in pixels.
119 121

eric ide

mercurial