Let's play it safe when restoring state in select_node.
[straw.git] / straw / ValueMonitor.py
blob1992eeeed7c25e089b9a903e4202e0b2d668c6c0
1 """ ValueMonitor.py
3 """
4 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
5 __license__ = """
6 Straw is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 2 of the License, or (at your option) any later
9 version.
11 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 Place - Suite 330, Boston, MA 02111-1307, USA. """
20 import pygtk
21 pygtk.require('2.0')
22 import gobject
23 import error
25 class ValueMonitor(object):
26 def __init__(self, current, timeout, notify, data):
27 self._previous_value = current
28 self._value = None
29 self._timeout = timeout
30 self._notify = notify
31 self._data = data
32 self._connected = True
33 self._value_changed = False
35 def get_value(self):
36 return self._value
38 def set_value(self, value):
39 if self._connected:
40 if value != self._value:
41 self._value_changed = True
42 gobject.timeout_add(self._timeout, self._check_value)
43 self._value = value
45 value = property(get_value, set_value)
47 def connect(self):
48 self._connected = True
50 def disconnect(self):
51 self.flush()
52 self._connected = False
54 def flush(self):
55 self._check_value()
57 def _check_value(self):
58 if (self._connected and self._value_changed and self.value != self._previous_value):
59 self._notify(self.value, self._data)
60 self._previous_value = self.value
62 def create_for_gtkentry(widget, timeout, notify):
63 vm = ValueMonitor(widget.get_text(), timeout, notify, widget)
64 def changed(object, *data):
65 vm.value = object.get_text()
66 widget.connect("changed", changed)
67 return vm
69 def create_for_gtkspin(widget, timeout, notify):
70 vm = ValueMonitor(widget.get_value_as_int, timeout, notify, widget)
71 def changed(object, *data):
72 vm.value = object.get_value_as_int()
73 widget.connect("value-changed", changed)
74 return vm