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
|
# Copyright 2007 Benjamin M. Schwartz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import gtk
import gobject
import arange
import locale
from gettext import gettext as _
def _label_factory(label, toolbar):
''' Factory for adding a label to a toolbar '''
my_label = gtk.Label(label)
my_label.set_line_wrap(True)
my_label.show()
_toolitem = gtk.ToolItem()
_toolitem.add(my_label)
toolbar.insert(_toolitem, -1)
_toolitem.show()
return my_label
def _entry_factory(length, toolbar, callback):
''' Factory for adding a text enrty to a toolbar '''
my_entry = gtk.Entry()
my_entry.set_max_length(length)
my_entry.set_width_chars(length)
my_entry.connect('changed', callback)
my_entry.show()
_toolitem = gtk.ToolItem()
_toolitem.add(my_entry)
toolbar.insert(_toolitem, -1)
_toolitem.show()
return my_entry
def _separator_factory(toolbar, expand=False, visible=False):
""" add a separator to a toolbar """
_separator = gtk.SeparatorToolItem()
_separator.props.draw = visible
_separator.set_expand(expand)
toolbar.insert(_separator, -1)
_separator.show()
class TempToolbar(gtk.Toolbar):
_speed = 0
def __init__(self):
gtk.Toolbar.__init__(self)
temp_label = _label_factory(_("Temperature (C): "), self)
self._temp_field = _entry_factory(6, self, self._update_cb)
_separator_factory(self)
humid_label = _label_factory(_("Relative Humidity (%): "), self)
self._humid_field = _entry_factory(5, self, self._update_cb)
_separator_factory(self)
results_label = _label_factory(_("Speed of Sound (m/s): "), self)
self._result = _label_factory('', self)
self.set_temp(25)
self.set_humid(60)
self.update_speed()
def get_temp(self):
try:
t = locale.atof(self._temp_field.get_text())
except:
return None
if t > 70:
return None
if t < -20:
return None
return t
def set_temp(self, t):
try:
self._temp_field.set_text(locale.str(max(-20, min(70, t))))
return True
except:
return False
def get_humid(self):
try:
t = locale.atof(self._humid_field.get_text())
except:
return None
if t > 100:
return None
if t < 0:
return None
return t
def set_humid(self, h):
try:
self._humid_field.set_text(locale.str(max(0, min(100, h))))
return True
except:
return False
def get_speed(self):
return self._speed
def _set_speed(self, s):
self._speed = s
try:
self._result.set_text(locale.format('%.2f', s))
return True
except:
return False
def _update_cb(self, widget=None):
gobject.idle_add(self.update_speed)
def update_speed(self):
t = self.get_temp()
h = self.get_humid()
if (t is not None) and (h is not None):
s = arange.speed_of_sound(t, h / 100)
self._set_speed(s)
else:
self._result.set_text('')
return False
|