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