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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# !/usr/bin/python
# -*- coding: utf-8 -*-
# Glitter Toolkit
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "Label widget"
import gobject
import pango
import clutter
from container import Container
class Label(Container):
"""
A label widget represents a text label which can be drawn in any pango
supported font, colored, ellipsized and line wrapped.
"""
def __init__(self, text='Label', ellipsize=True, line_wrap=False):
""" Initialize label """
super(Label, self).__init__()
self._text = text
self._ellipsize = ellipsize
self._line_wrap = line_wrap
self._init_elements()
self._update_style(self.style)
def _init_elements(self):
""" Initializes visual elements """
# The clutter label
self._label = clutter.Label()
self._label.set_text(self.text)
if self.ellipsize:
self._label.set_ellipsize(pango.ELLIPSIZE_END)
self._label.set_line_wrap(self.line_wrap)
self.add(self._label)
self._label.show()
def _update_layout(self):
""" Updates layout """
super(Label, self)._update_layout()
# Ajust text font size to height and calculate minimum width
height = self.get_height()
self._label.set_font_name(self.font + ' ' + str(int(height / 1.229)) + 'px')
self.minimum_width = self._label.get_widthu()
# Update the label to account for the new minimum width
super(Label, self)._update_layout()
# Clip the text
width = self.get_width()
self.set_clip(0, 0, width, height)
def _update_style(self, props=None):
""" Updates widget style """
super(Label, self)._update_style(props)
if not props:
return
for key, value in props:
if key == 'font-name':
self.font = value
elif key == 'color':
self.color = value
# Properties
def get_text(self):
""" Retrieve label text
return (str) -- label text
"""
return self._text
def set_text(self, text):
""" Sets label text
text (str) -- label text
"""
self._text = text
text = property(get_text, set_text)
def get_font(self):
""" Retrieve font
return (str) -- font
"""
return self._font
def set_font(self, font):
""" Sets font
font (str) -- font name
"""
self._font = font
height = self.get_height()
self._label.set_font_name(self._font + ' ' + str(height) + 'px')
font = property(get_font, set_font)
def get_color(self):
""" Retrieve color
return (int, int, int, int) -- color in rgba format
"""
return self._color
def set_color(self, color):
""" Sets color
color (int, int, int, int) -- color in rgba format
"""
self._color = color
self._label.set_color(self._color)
color = property(get_color, set_color)
def get_ellipsize(self):
""" Retrieve whether text is to be ellipsized
return (bool) -- ellipsize
"""
return self._ellipsize
def set_ellipsize(self, ellipsize):
""" Sets whether text is to be ellipsized
ellipsize (bool) -- ellipsize
"""
self._ellipsize = ellipsize
if ellipsize:
self._label.set_ellipsize(pango.ELLIPSIZE_END)
ellipsize = property(get_ellipsize, set_ellipsize)
def get_line_wrap(self):
""" Retrieve whether text is to be line wrapped
return (bool) -- line wrap
"""
return self._line_wrap
def set_line_wrap(self, line_wrap):
""" Sets whether text is to be line wrapped
line_wrap (bool) -- line wrap
"""
self._line_wrap = line_wrap
self._label.set_line_wrap(line_wrap)
line_wrap = property(get_line_wrap, set_line_wrap)
|