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

Last change on this file since 5996 was 5850, checked in by ojw, 12 years ago

convert to an "integer zoom level" (so zooming isn't continuous, but must double or halve the detail in each step) -- this is to make the map images the same size as their original, so that cairo doesn't have to do any scaling

File size: 5.1 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    centre = self.get('ownpos')
120    if(not centre['valid']):
121      print "Need to set your own position before downloading"
122    else:
123      sizeToDownload = 0.1
124      self.m['osmdata'].download( \
125        centre['lat'],
126        centre['lon'],
127        sizeToDownload)
128
129  def handleEvent(self,event):
130    closeMenuAfterwards = False
131   
132    # Events prefixed with a + means "...and then close the menu"
133    if(event[0] == '+'):
134      event = event[1:]
135      closeMenuAfterwards = True
136   
137    # format is "eventname:parameters" (where parameters may be further split)
138    action,params = event.split(':',1)
139   
140    # Look for an event-handler function, and call it
141    print "Handling event %s" % event
142    functionName = "handle_%s" % action
143    try:
144      function = getattr(self, functionName)
145    except AttributeError:
146      print "Error: %s not defined" % functionName
147      self.set('menu',None)
148    else:
149      function(params)
150   
151    # Optionally return to the map
152    if(closeMenuAfterwards):
153      self.set('menu',None)
Note: See TracBrowser for help on using the repository browser.