source: subversion/applications/routing/pyroute/events.py

Last change on this file was 6001, checked in by ojw, 12 years ago

move OSM data into module

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