source: subversion/applications/routing/pyroute/dataStore.py @ 5721

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

Move the map tile code into its own module

Add headers and usage instructions to a bunch of modules

File size: 4.3 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------
3# Global data table
4#
5# Usage:
6#   (library code for pyroute GUI, not for direct use)
7#
8# Types of data stored:
9#   * Options and settings
10#   * Data (e.g. current position)
11#   * Event-related data (e.g. position of last click)
12#
13# Not stored here:
14#   * Routes (internal within the routing module)
15#   * POIs and map data
16#
17# TODO:
18#   * Event handling code needs to be moved into a module
19#-----------------------------------------------------------------------------
20# Copyright 2007, Oliver White
21#
22# This program is free software: you can redistribute it and/or modify
23# it under the terms of the GNU General Public License as published by
24# the Free Software Foundation, either version 3 of the License, or
25# (at your option) any later version.
26#
27# This program is distributed in the hope that it will be useful,
28# but WITHOUT ANY WARRANTY; without even the implied warranty of
29# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30# GNU General Public License for more details.
31#
32# You should have received a copy of the GNU General Public License
33# along with this program.  If not, see <http://www.gnu.org/licenses/>.
34#-----------------------------------------------------------------------------
35from base import pyrouteModule
36
37class DataStore(pyrouteModule):
38    def __init__(self, globals, modules):
39        pyrouteModule.__init__(self,modules)
40        self.options = {}
41        self.globals = globals # TODO: remove
42       
43    def handleEvent(self,event):
44        action,params = event.split(':',1)
45        print "Handling event %s" % event
46       
47        if(action == 'menu'):
48            self.set('menu', params)
49        elif(action == 'mode'):
50            self.set('mode', params)
51            self.closeAllMenus()
52           
53        elif(action == 'option'):
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(action == 'route'):
61          ownpos = self.get('ownpos')
62         
63          if(not ownpos['valid']):
64            print "Can't route, don't know start position"
65            return
66          if(params == 'clicked'):
67            lat,lon = self.get('clicked')
68          else:
69            lat, lon = [float(ll) for ll in params.split(':',1)]
70           
71          transport = self.get('mode')
72         
73          router = self.m['route']
74          router.setStartLL(ownpos['lat'], ownpos['lon'], transport)
75          router.setEndLL(lat,lon,transport)
76          router.setMode('route')
77          router.update()
78          self.globals.forceRedraw()
79          self.closeAllMenus()
80        elif(action == 'ownpos'):
81          lat,lon = self.get('clicked')
82          self.set('ownpos', {'lat':lat,'lon':lon,'valid':True})
83          print "Set ownpos to %f,%f" % (lat,lon)
84          self.closeAllMenus()
85          self.globals.handleUpdatedPosition()
86        elif(action == 'direct'):
87          start = self.get('ownpos')
88          transport = self.get('mode')
89          self.globals.modules['route'].setStartLL(start['lat'], start['lon'], transport)
90          if(params == 'clicked'):
91            lat,lon = self.get('clicked')
92          else:
93            lat, lon = [float(ll) for ll in params.split(':',1)]
94          self.globals.modules['route'].setEndLL(lat,lon,transport)
95          self.globals.modules['route'].setMode('direct')
96          self.globals.modules['route'].update()
97          self.globals.forceRedraw()
98          self.closeAllMenus()
99        elif(action == 'download'):
100          centre = self.get('ownpos')
101          if(not centre['valid']):
102            print "Need to set your own position before downloading"
103            return
104          sizeToDownload = 0.1
105          self.globals.modules['osmdata'].download( \
106            centre['lat'],
107            centre['lon'],
108            sizeToDownload)
109   
110    def closeAllMenus(self):
111        self.set('menu',None)
112    def getData(self,name,default=None):
113        return(self.options.get(name,default))
114    def setData(self,name,value):
115        self.options[name] = value
Note: See TracBrowser for help on using the repository browser.