source: subversion/applications/routing/pyroute-dev/pyrouteMenu.py @ 18444

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

-switching to windows eol-markers
-converting all files to UTF8 with bom

File size: 4.6 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------
3# Loads touchscreen menus from textfiles on disk
4#
5# Usage:
6#   (library code for pyroute GUI, not for direct use)
7#
8# Format:
9#   * One menu per textfile  (menuname.txt)
10#   * One line per 'column'
11#   * Line containing "-----" starts a new row
12#      (the first line should also contain one of these)
13#   * 4 rows of 3 columns. all must be defined, even if blank
14#   * Item is "name" or "name|event"
15#     * name should be unique amongst all menus
16#     * name is currently displayed as-is (TODO: translation table)
17#     * "Up" is a special name that closes the menu
18#        * Typically the top/left item is always 'Up'
19#     * event is a message to send when the menu item is pressed
20#     * lines can be blank to represent no menu item in that position
21#-----------------------------------------------------------------------------
22# Copyright 2007, Oliver White
23#
24# This program is free software: you can redistribute it and/or modify
25# it under the terms of the GNU General Public License as published by
26# the Free Software Foundation, either version 3 of the License, or
27# (at your option) any later version.
28#
29# This program is distributed in the hope that it will be useful,
30# but WITHOUT ANY WARRANTY; without even the implied warranty of
31# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32# GNU General Public License for more details.
33#
34# You should have received a copy of the GNU General Public License
35# along with this program.  If not, see <http://www.gnu.org/licenses/>.
36#-----------------------------------------------------------------------------
37import os
38import re
39
40def loadMenu(filename):
41  menu = {}
42  try:
43    file = open(filename, "r")
44    x = 0
45    y = -1
46    for line in file:
47      line = line.rstrip()
48      if(line == '-----'):
49        x = 0
50        y = y + 1
51        continue
52      try:
53        name,action = line.split('|', 2)
54      except ValueError:
55        name,action = (line,None)
56      #print "%d,%d: %s" % (x,y,action)
57      icon = name.lower()
58      icon = re.sub('\s+', '_', icon)
59      menu["%d,%d"%(x,y)] = {'name':name,'action':action,'icon':icon}
60      x = x + 1
61     
62    file.close()
63  except IOError:
64    print "Can't open file %s" % filename
65  return menu
66
67def loadMenus(directory):
68  menus = {}
69  for file in os.listdir(directory):
70    result = re.search('(\w+)\.txt', file)
71    if(result):
72      menuName = result.group(1)
73      menus[menuName] = loadMenu("%s/%s" % (directory,file))
74  return(menus)
75
76def printMenu(menu):
77  print "-" * 63
78  for y in range(4):
79    for x in range(3):
80      item = menu["%d,%d"%(x,y)]
81      print expand(item['name'], 20),
82      print "|",
83    print ""
84    for x in range(3):
85      item = menu["%d,%d"%(x,y)]
86      print expand(item['action'], 20),
87      print "|",
88    print ""
89    for x in range(3):
90      item = menu["%d,%d"%(x,y)]
91      print expand(item['icon'], 20),
92      print "|",
93    print "\n" + "-" * 63
94
95def printStrings(menus):
96  print "\n\nMenu item names:"
97  names = {}
98  # Look for a list of menu names
99  for menuName, menu in menus.items():
100    for xy,item in menu.items():
101      name = item['name']
102      if name:
103        names[name] = 1
104  # Print sorted list of unique names
105  names = names.keys()
106  names.sort()
107  for name in names:
108    print name
109   
110def checkIcons(menus, directory):
111  print "\n\nMissing icons:"
112  count = 0
113  for menuName, menu in menus.items():
114    for xy,item in menu.items():
115      if(item['icon']):
116        icon = "%s/%s.png" % (directory, item['icon'])
117        if(not os.path.exists(icon)):
118          print icon
119          count = count + 1
120  print "%d missing icons" % count
121
122def expand(text,size):
123  if(text == None):
124    return(" " * size)
125  need = size - len(text)
126  n1 = int(need/2)
127  n2 = need-n1
128  return(" " * n1 + text + " " * n2)
129
130
131if(__name__ == "__main__"):
132  menus = loadMenus('.')
133 
134  import sys
135  try:
136    if(sys.argv[1] == 'list'):
137      for name, menu in menus.items():
138        print "Menu for %s:" % name
139        printMenu(menu)
140    elif(sys.argv[1] == 'strings'):
141      printStrings(menus)
142    elif(sys.argv[1] == 'icons'):
143      checkIcons(menus,"../icons/bitmap")
144    else:
145      print "Loaded %d menus" % len(menus)
146  except IndexError:
147    print "Usage: %s [mode]" % sys.argv[0]
148    print "  * 'list' - lists the contents of all menus"
149    print "  * 'strings' - exports names, ready for translating"
150    print "  * 'icons' - checks that all the icons exist"
151   
Note: See TracBrowser for help on using the repository browser.