d85f41de52708c79a5fbf54531231f6039cf4c9c
[gregoa/zavai.git] / plugins / 55_gpx_trace.py
1 # gpx_trace - zavai GPX trace functions
2 #
3 # Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19 from gettext import gettext as _
20 import gtk
21 import zavai
22
23 class GPXTracer(gtk.ToggleAction):
24     states = [_("Start GPX trace"), _("Stop GPX trace")]
25
26     def __init__(self, registry, **kw):
27         self.state = 0
28         super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
29
30         self.registry = registry
31         self.set_active(False)
32
33         self.connect("toggled", self.on_toggle)
34
35     def on_toggle(self, *args):
36         self.state = (self.state + 1) % len(self.states)
37         self.set_property("label", self.states[self.state])
38         if self.get_active():
39             self.start()
40         else:
41             self.stop()
42
43     def start(self):
44         zavai.info("GPX trace started")
45         self.registry.resource("gpx").connect("gpx", self)
46
47     def stop(self):
48         zavai.info("GPX trace ended")
49         self.registry.resource("gpx").disconnect("gpx", self)
50
51 class GPXWaypoint(gtk.Action):
52     def __init__(self, registry, **kw):
53         super(GPXWaypoint, self).__init__("menu.main.gps.waypoint", _("Take waypoint"), None, None)
54         self.gpx = registry.resource("gpx")
55         self.gpx.add_activity_monitor(self.on_gpx_activity_changed)
56         self.connect("activate", self.on_activate)
57
58     def on_gpx_activity_changed(self, gpx, state):
59         self.set_sensitive(state)
60
61     def on_activate(self, *args):
62         self.gpx.waypoint()
63
64 class GPXAudioTracer(gtk.ToggleAction):
65     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
66
67     def __init__(self, registry, **kw):
68         self.state = 0
69         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
70
71         self.registry = registry
72         self.recorder = zavai.Recorder(registry)
73         self.set_active(False)
74
75         self.connect("toggled", self.on_toggle)
76
77     def shutdown(self):
78         self.recorder.stop()
79         super(GPXAudioTracer, self).shutdown()
80
81     def on_toggle(self, *args):
82         self.state = (self.state + 1) % len(self.states)
83         self.set_property("label", self.states[self.state])
84         if self.get_active():
85             self.start()
86         else:
87             self.stop()
88
89     def start(self):
90         zavai.info("GPX trace started")
91         self.registry.resource("gpx").connect("gpx", self)
92         self.gpx.add_activity_monitor(self.on_gpx_activity_changed)
93
94     def stop(self):
95         zavai.info("GPX trace ended")
96         self.registry.resource("gpx").disconnect("gpx", self)
97         self.recorder.stop()
98         self.gpx.del_activity_monitor(self.on_gpx_activity_changed)
99
100     def on_gpx_activity_changed(self, gpx, state):
101         if state:
102             self.recorder.start(gpx.basename + ".wav")
103         else:
104             self.recorder.stop()
105
106 def init(conf = None, registry = None, **kw):
107     registry.register(GPXTracer(registry))
108     registry.register(GPXAudioTracer(registry))
109     registry.register(GPXWaypoint(registry))