source: subversion/applications/mobile/rana2/rana2.py

Last change on this file was 13130, checked in by ojw, 11 years ago

try using GTK for menus instead of doing it all in caro
+ display nearest postcode
+ speedo mode, with average speed/distance mode
+ compass mode

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------------
3# GUI framework, for displaying pages created by the modules/ directory
4#----------------------------------------------------------------------
5# Copyright 2007-2009, Oliver White
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#----------------------------------------------------------------------
20import pygtk
21pygtk.require('2.0')
22import gtk
23import sys
24import os
25import gobject
26
27def updateObject(obj):
28  obj.update()
29  gobject.timeout_add(1200, updateObject, obj)
30  return(False)
31
32class GuiBase:
33  """Modular GUI for the Rana mobile map program"""
34  def __init__(self, initialPage=None):
35    # Create the window
36    self.window = gtk.Window()
37    self.window.set_title('Rana')
38    self.window.connect('delete-event', gtk.main_quit)
39    self.window.resize(480,640) # to simulate openmoko
40    self.window.move(gtk.gdk.screen_width() - 500, 50)
41   
42    self.widget = None # Widget filling the screen
43    self.visible_module = None # Module responsible for self.widget
44    self.update_function = None
45   
46    self.m = {} # List of modules
47   
48     # Program data
49     # keys starting with underscores are not for saving to disk
50    self.d = {
51      '_pages':{
52        'menu':'global'},
53      '_functions':{
54        'change_page':self.change_page},
55      '_menu_grid':(3,4),
56      }
57
58    # Load all the program functionality, which is in modules
59    self.load_modules('modules')
60   
61    # Select the initial page
62    if(initialPage):
63      self.change_page(None, initialPage)
64    else:
65      self.change_page(None, 'title')
66
67    # Regular updates
68    gobject.timeout_add(1000, updateObject, self)
69   
70    # Run the GUI
71    gtk.main()
72
73  def load_modules(self, module_path):
74    """Load all modules from the specified directory"""
75    sys.path.append(module_path)
76    for f in os.listdir(module_path):
77      if(f[0:1] != '_' and f[-3:] == '.py'):
78        name = f[0:-3]
79        filename = "%s\\%s" % ( module_path, f[0:-3]) # with directory name, but without .py extension
80        try:
81          a = __import__(name)
82        except ImportError:
83          continue
84        self.m[name] = getattr(a, name)(self.m, self.d, name)
85        #print " * %s: %s" % (name, self.m[name].__doc__)
86    for k,m in self.m.items():
87      m.startup()
88 
89  def update(self):
90    if(self.update_function):
91      self.update_function()
92
93  def setWidget(self, newMainWidget):
94    if(self.widget):
95      self.window.remove(self.widget)
96    self.widget = newMainWidget
97    self.window.add(newMainWidget)
98    self.window.show_all()
99
100  def change_page(self, widget, page):
101    self.d['history:-1'] = self.d.get('page', None)
102    self.d['page'] = page
103    which_module = self.d['_pages'].get(page, None)
104    if(not which_module):
105      print "No module registered for %s" % page
106    elif(not self.m.has_key(which_module)):
107      print "Module %s not loaded" % which_module
108    else:
109      module = self.m[which_module]
110      function = getattr(module, "page_"+page)
111      widget = function()
112      self.setWidget(widget)
113      self.visible_module = module
114      try:
115        self.update_function = getattr(module, "update")
116      except AttributeError:
117        self.update_function = None
118
119if __name__ == "__main__":
120  if(len(sys.argv) > 1):
121    initialPage = sys.argv[1]
122  else:
123    initialPage = None
124   
125  program = GuiBase(initialPage)
126
127 
Note: See TracBrowser for help on using the repository browser.