src/eric7/EricWidgets/EricSqueezeLabels.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
16 16
17 class EricSqueezeLabel(QLabel): 17 class EricSqueezeLabel(QLabel):
18 """ 18 """
19 Class implementing a label that squeezes its contents to fit its size. 19 Class implementing a label that squeezes its contents to fit its size.
20 """ 20 """
21
21 def __init__(self, parent=None): 22 def __init__(self, parent=None):
22 """ 23 """
23 Constructor 24 Constructor
24 25
25 @param parent reference to the parent Widget (QWidget) 26 @param parent reference to the parent Widget (QWidget)
26 """ 27 """
27 super().__init__(parent) 28 super().__init__(parent)
28 29
29 self.__text = '' 30 self.__text = ""
30 self.__elided = '' 31 self.__elided = ""
31 32
32 def paintEvent(self, event): 33 def paintEvent(self, event):
33 """ 34 """
34 Protected method called when some painting is required. 35 Protected method called when some painting is required.
35 36
36 @param event reference to the paint event (QPaintEvent) 37 @param event reference to the paint event (QPaintEvent)
37 """ 38 """
38 fm = self.fontMetrics() 39 fm = self.fontMetrics()
39 try: 40 try:
40 pixelLength = fm.horizontalAdvance(self.__text) 41 pixelLength = fm.horizontalAdvance(self.__text)
41 except AttributeError: 42 except AttributeError:
42 pixelLength = fm.width(self.__text) 43 pixelLength = fm.width(self.__text)
43 if pixelLength > self.contentsRect().width(): 44 if pixelLength > self.contentsRect().width():
44 self.__elided = fm.elidedText( 45 self.__elided = fm.elidedText(
45 self.text(), Qt.TextElideMode.ElideMiddle, self.width()) 46 self.text(), Qt.TextElideMode.ElideMiddle, self.width()
47 )
46 super().setText(self.__elided) 48 super().setText(self.__elided)
47 else: 49 else:
48 super().setText(self.__text) 50 super().setText(self.__text)
49 super().paintEvent(event) 51 super().paintEvent(event)
50 52
51 def setText(self, txt): 53 def setText(self, txt):
52 """ 54 """
53 Public method to set the label's text. 55 Public method to set the label's text.
54 56
55 @param txt the text to be shown (string) 57 @param txt the text to be shown (string)
56 """ 58 """
57 self.__text = txt 59 self.__text = txt
58 super().setText(self.__text) 60 super().setText(self.__text)
59 61
60 62
61 class EricSqueezeLabelPath(QLabel): 63 class EricSqueezeLabelPath(QLabel):
62 """ 64 """
63 Class implementing a label showing a file path compacted to fit its size. 65 Class implementing a label showing a file path compacted to fit its size.
64 """ 66 """
67
65 def __init__(self, parent=None): 68 def __init__(self, parent=None):
66 """ 69 """
67 Constructor 70 Constructor
68 71
69 @param parent reference to the parent Widget (QWidget) 72 @param parent reference to the parent Widget (QWidget)
70 """ 73 """
71 super().__init__(parent) 74 super().__init__(parent)
72 75
73 self.__path = '' 76 self.__path = ""
74 self.__surrounding = "{0}" 77 self.__surrounding = "{0}"
75 78
76 def setSurrounding(self, surrounding): 79 def setSurrounding(self, surrounding):
77 """ 80 """
78 Public method to set the surrounding of the path string. 81 Public method to set the surrounding of the path string.
79 82
80 @param surrounding the a string containg placeholders for the path 83 @param surrounding the a string containg placeholders for the path
81 (string) 84 (string)
82 """ 85 """
83 self.__surrounding = surrounding 86 self.__surrounding = surrounding
84 super().setText( 87 super().setText(self.__surrounding.format(self.__path))
85 self.__surrounding.format(self.__path)) 88
86
87 def setPath(self, path): 89 def setPath(self, path):
88 """ 90 """
89 Public method to set the path of the label. 91 Public method to set the path of the label.
90 92
91 @param path path to be shown (string) 93 @param path path to be shown (string)
92 """ 94 """
93 self.__path = path 95 self.__path = path
94 super().setText( 96 super().setText(self.__surrounding.format(self.__path))
95 self.__surrounding.format(self.__path)) 97
96
97 def setTextPath(self, surrounding, path): 98 def setTextPath(self, surrounding, path):
98 """ 99 """
99 Public method to set the surrounding and the path of the label. 100 Public method to set the surrounding and the path of the label.
100 101
101 @param surrounding the a string containg placeholders for the path 102 @param surrounding the a string containg placeholders for the path
102 (string) 103 (string)
103 @param path path to be shown (string) 104 @param path path to be shown (string)
104 """ 105 """
105 self.__surrounding = surrounding 106 self.__surrounding = surrounding
106 self.__path = path 107 self.__path = path
107 super().setText( 108 super().setText(self.__surrounding.format(self.__path))
108 self.__surrounding.format(self.__path)) 109
109
110 def paintEvent(self, event): 110 def paintEvent(self, event):
111 """ 111 """
112 Protected method called when some painting is required. 112 Protected method called when some painting is required.
113 113
114 @param event reference to the paint event (QPaintEvent) 114 @param event reference to the paint event (QPaintEvent)
115 """ 115 """
116 if self.length(self.__path) > self.contentsRect().width(): 116 if self.length(self.__path) > self.contentsRect().width():
117 super().setText( 117 super().setText(
118 self.__surrounding.format(compactPath(self.__path, 118 self.__surrounding.format(
119 self.contentsRect().width(), 119 compactPath(self.__path, self.contentsRect().width(), self.length)
120 self.length)) 120 )
121 ) 121 )
122 else: 122 else:
123 super().setText( 123 super().setText(self.__surrounding.format(self.__path))
124 self.__surrounding.format(self.__path))
125 super().paintEvent(event) 124 super().paintEvent(event)
126 125
127 def length(self, txt): 126 def length(self, txt):
128 """ 127 """
129 Public method to return the length of a text in pixels. 128 Public method to return the length of a text in pixels.
130 129
131 @param txt text to calculate the length for after wrapped (string) 130 @param txt text to calculate the length for after wrapped (string)
132 @return length of the wrapped text in pixels (integer) 131 @return length of the wrapped text in pixels (integer)
133 """ 132 """
134 fm = self.fontMetrics() 133 fm = self.fontMetrics()
135 try: 134 try:

eric ide

mercurial