|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing labels that squeeze their contents to fit the size of the |
|
8 label. |
|
9 """ |
|
10 |
|
11 from PyQt6.QtCore import Qt |
|
12 from PyQt6.QtWidgets import QLabel |
|
13 |
|
14 from Utilities import compactPath |
|
15 |
|
16 |
|
17 class EricSqueezeLabel(QLabel): |
|
18 """ |
|
19 Class implementing a label that squeezes its contents to fit its size. |
|
20 """ |
|
21 def __init__(self, parent=None): |
|
22 """ |
|
23 Constructor |
|
24 |
|
25 @param parent reference to the parent Widget (QWidget) |
|
26 """ |
|
27 super().__init__(parent) |
|
28 |
|
29 self.__text = '' |
|
30 self.__elided = '' |
|
31 |
|
32 def paintEvent(self, event): |
|
33 """ |
|
34 Protected method called when some painting is required. |
|
35 |
|
36 @param event reference to the paint event (QPaintEvent) |
|
37 """ |
|
38 fm = self.fontMetrics() |
|
39 try: |
|
40 pixelLength = fm.horizontalAdvance(self.__text) |
|
41 except AttributeError: |
|
42 pixelLength = fm.width(self.__text) |
|
43 if pixelLength > self.contentsRect().width(): |
|
44 self.__elided = fm.elidedText( |
|
45 self.text(), Qt.TextElideMode.ElideMiddle, self.width()) |
|
46 super().setText(self.__elided) |
|
47 else: |
|
48 super().setText(self.__text) |
|
49 super().paintEvent(event) |
|
50 |
|
51 def setText(self, txt): |
|
52 """ |
|
53 Public method to set the label's text. |
|
54 |
|
55 @param txt the text to be shown (string) |
|
56 """ |
|
57 self.__text = txt |
|
58 super().setText(self.__text) |
|
59 |
|
60 |
|
61 class EricSqueezeLabelPath(QLabel): |
|
62 """ |
|
63 Class implementing a label showing a file path compacted to fit its size. |
|
64 """ |
|
65 def __init__(self, parent=None): |
|
66 """ |
|
67 Constructor |
|
68 |
|
69 @param parent reference to the parent Widget (QWidget) |
|
70 """ |
|
71 super().__init__(parent) |
|
72 |
|
73 self.__path = '' |
|
74 self.__surrounding = "{0}" |
|
75 |
|
76 def setSurrounding(self, surrounding): |
|
77 """ |
|
78 Public method to set the surrounding of the path string. |
|
79 |
|
80 @param surrounding the a string containg placeholders for the path |
|
81 (string) |
|
82 """ |
|
83 self.__surrounding = surrounding |
|
84 super().setText( |
|
85 self.__surrounding.format(self.__path)) |
|
86 |
|
87 def setPath(self, path): |
|
88 """ |
|
89 Public method to set the path of the label. |
|
90 |
|
91 @param path path to be shown (string) |
|
92 """ |
|
93 self.__path = path |
|
94 super().setText( |
|
95 self.__surrounding.format(self.__path)) |
|
96 |
|
97 def setTextPath(self, surrounding, path): |
|
98 """ |
|
99 Public method to set the surrounding and the path of the label. |
|
100 |
|
101 @param surrounding the a string containg placeholders for the path |
|
102 (string) |
|
103 @param path path to be shown (string) |
|
104 """ |
|
105 self.__surrounding = surrounding |
|
106 self.__path = path |
|
107 super().setText( |
|
108 self.__surrounding.format(self.__path)) |
|
109 |
|
110 def paintEvent(self, event): |
|
111 """ |
|
112 Protected method called when some painting is required. |
|
113 |
|
114 @param event reference to the paint event (QPaintEvent) |
|
115 """ |
|
116 if self.length(self.__path) > self.contentsRect().width(): |
|
117 super().setText( |
|
118 self.__surrounding.format(compactPath(self.__path, |
|
119 self.contentsRect().width(), |
|
120 self.length)) |
|
121 ) |
|
122 else: |
|
123 super().setText( |
|
124 self.__surrounding.format(self.__path)) |
|
125 super().paintEvent(event) |
|
126 |
|
127 def length(self, txt): |
|
128 """ |
|
129 Public method to return the length of a text in pixels. |
|
130 |
|
131 @param txt text to calculate the length for after wrapped (string) |
|
132 @return length of the wrapped text in pixels (integer) |
|
133 """ |
|
134 fm = self.fontMetrics() |
|
135 try: |
|
136 return fm.horizontalAdvance(self.__surrounding.format(txt)) |
|
137 except AttributeError: |
|
138 return fm.width(self.__surrounding.format(txt)) |