source: subversion/applications/routing/pyroute-dev/events.py @ 34690

Last change on this file since 34690 was 18455, checked in by buerste, 10 years ago

-remove BOM
-fix version strings

  • Property svn:keywords set to Rev
File size: 4.6 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""Event handler.  Messages (as text) get sent here, telling pyroute
5to do various things (e.g. save a waypoint, open a menu, route to
6some point)
7
8Usage:
9        (library code)
10"""
11
12__version__ = "$Rev: 18455 $"[1:-2]
13__license__ = """This program is free software: you can redistribute it and/or modify
14it under the terms of the GNU General Public License as published by
15the Free Software Foundation, either version 3 of the License, or
16(at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program. If not, see <http://www.gnu.org/licenses/>."""
25_debug = 0
26
27
28from base import pyrouteModule
29
30class pyrouteEvents(pyrouteModule):
31        def __init__(self, modules):
32                pyrouteModule.__init__(self,modules)
33
34        def handle_zoom(self,params):
35                """zoom:[in|out|zoomlevel] - zooms to a quadtile level (17=max detail, <6=whole world)"""
36                if(params == 'in'):
37                        self.m['projection'].setZoom(1, True)
38                elif(params == 'out'):
39                        self.m['projection'].setZoom(-1, True)
40                else:
41                        self.m['projection'].setZoom(int(params))
42                self.set("needRedraw", True)
43
44        def handle_menu(self,params):
45                """menu:(name) - displays a menu. Use blank name to hide the menu"""
46                self.set('menu', params)
47
48        def handle_mode(self,params):
49                """mode:(name) - sets the transport type in use (e.g. car, bike)"""
50                self.set('mode', params)
51
52        def handle_save(self,params):
53                whichModule = params
54                self.m[whichModule].save()
55
56        def handle_option(self,params):
57                """option:add|toggle:(name):(value)"""
58                method, name = params.split(':',1)
59                if(method == 'toggle'):
60                        self.set(name, not self.get(name))
61                elif(method == 'add'):
62                        name,add = name.split(':')
63                        self.set(name, self.get(name,0) + float(add))
64                elif(method == 'set'):
65                        name,value = name.split(':')
66                        self.set(name, value)
67
68        def handle_set_colour(self,params):
69                use,r,g,b = params.split(':')
70                print "Setting %s to %s,%s,%s" % (use,r,g,b)
71                self.set('%s_r' % (use), r)
72                self.set('%s_g' % (use), g)
73                self.set('%s_b' % (use), b)
74
75        def handle_add_waypoint(self,params):
76                if(params == 'clicked'):
77                        lat,lon = self.get('clicked')
78                        self.m['poi']['waypoints'].storeNumberedWaypoint({'lat':lat,'lon':lon})
79                        self.m['poi']['waypoints'].save()
80                else:
81                        print "Not handled: dropping waypoint somewhere other than last click"
82
83        def handle_route(self,params):
84                ownpos = self.get('ownpos')
85
86                if(not ownpos['valid']):
87                        print "Can't route, don't know start position"
88                else:
89                        if(params == 'clicked'):
90                                lat,lon = self.get('clicked')
91                        else:
92                                lat, lon = [float(ll) for ll in params.split(':',1)]
93
94                        transport = self.get('mode')
95
96                        router = self.m['route']
97                        router.setStartLL(ownpos['lat'], ownpos['lon'], transport)
98                        router.setEndLL(lat,lon,transport)
99                        router.setMode('route')
100                        router.update()
101                        self.set('needRedraw', True)
102
103        def handle_ownpos(self,params):
104                lat,lon = self.get('clicked')
105                self.set('ownpos', {'lat':lat,'lon':lon,'valid':True})
106                print "Set ownpos to %f,%f" % (lat,lon)
107                self.set('positionUpdated', True)
108
109        def handle_direct(self,params):
110                start = self.get('ownpos')
111                transport = self.get('mode')
112                self.m['route'].setStartLL(start['lat'], start['lon'], transport)
113                if(params == 'clicked'):
114                        lat,lon = self.get('clicked')
115                else:
116                        lat, lon = [float(ll) for ll in params.split(':',1)]
117                self.m['route'].setEndLL(lat,lon,transport)
118                self.m['route'].setMode('direct')
119                self.m['route'].update()
120                self.set('needRedraw', True)
121
122        def handle_download(self,params):
123                self.m['osmdata'].download(params)
124                self.set('needRedraw', True)
125
126        def handleEvent(self,event):
127                closeMenuAfterwards = False
128
129                # Events prefixed with a + means "...and then close the menu"
130                if(event[0] == '+'):
131                        event = event[1:]
132                        closeMenuAfterwards = True
133
134                # format is "eventname:parameters" (where parameters may be further split)
135                action,params = event.split(':',1)
136
137                # Look for an event-handler function, and call it
138                print "Handling event %s" % event
139                functionName = "handle_%s" % action
140                try:
141                        function = getattr(self, functionName)
142                except AttributeError:
143                        print "Error: %s not defined" % functionName
144                        self.set('menu',None)
145                else:
146                        function(params)
147
148                # Optionally return to the map
149                if(closeMenuAfterwards):
150                        self.set('menu',None)
Note: See TracBrowser for help on using the repository browser.