GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
wxgui.py
Go to the documentation of this file.
1 """!
2 @package wxgui
3 
4 @brief Main Python application for GRASS wxPython GUI
5 
6 Classes:
7  - wxgui::GMApp
8  - wxgui::Usage
9 
10 (C) 2006-2011 by the GRASS Development Team
11 
12 This program is free software under the GNU General Public License
13 (>=v2). Read the file COPYING that comes with GRASS for details.
14 
15 @author Michael Barton (Arizona State University)
16 @author Jachym Cepicky (Mendel University of Agriculture)
17 @author Martin Landa <landa.martin gmail.com>
18 @author Vaclav Petras <wenzeslaus gmail.com> (menu customization)
19 """
20 
21 import os
22 import sys
23 import getopt
24 
25 if __name__ == "__main__":
26  sys.path.append(os.path.join(os.getenv('GISBASE'), 'etc', 'wxpython'))
27 from core import globalvar
28 import wx
29 try:
30  import wx.lib.agw.advancedsplash as SC
31 except ImportError:
32  SC = None
33 
34 from lmgr.frame import GMFrame
35 
36 class GMApp(wx.App):
37  def __init__(self, workspace = None):
38  """!Main GUI class.
39 
40  @param workspace path to the workspace file
41  """
42  self.workspaceFile = workspace
43 
44  # call parent class initializer
45  wx.App.__init__(self, False)
46 
47  self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
48 
49  def OnInit(self):
50  """!Initialize all available image handlers
51 
52  @return True
53  """
54  wx.InitAllImageHandlers()
55 
56  # create splash screen
57  introImagePath = os.path.join(globalvar.ETCIMGDIR, "silesia_splash.png")
58  introImage = wx.Image(introImagePath, wx.BITMAP_TYPE_PNG)
59  introBmp = introImage.ConvertToBitmap()
60  if SC and sys.platform != 'darwin':
61  # AdvancedSplash is buggy on the Mac as of 2.8.12.1
62  # and raises annoying (though seemingly harmless) errors everytime the GUI is started
63  splash = SC.AdvancedSplash(bitmap = introBmp,
64  timeout = 2000, parent = None, id = wx.ID_ANY)
65  splash.SetText(_('Starting GRASS GUI...'))
66  splash.SetTextColour(wx.Colour(45, 52, 27))
67  splash.SetTextFont(wx.Font(pointSize = 15, family = wx.DEFAULT, style = wx.NORMAL,
68  weight = wx.BOLD))
69  splash.SetTextPosition((150, 430))
70  else:
71  wx.SplashScreen (bitmap = introBmp, splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
72  milliseconds = 2000, parent = None, id = wx.ID_ANY)
73 
74  wx.Yield()
75 
76  # create and show main frame
77  mainframe = GMFrame(parent = None, id = wx.ID_ANY,
78  workspace = self.workspaceFile)
79 
80  mainframe.Show()
81  self.SetTopWindow(mainframe)
82 
83  return True
84 
85 class Usage(Exception):
86  def __init__(self, msg):
87  self.msg = msg
88 
89 def printHelp():
90  """!Print program help"""
91  print >> sys.stderr, "Usage:"
92  print >> sys.stderr, " python wxgui.py [options]"
93  print >> sys.stderr, "%sOptions:" % os.linesep
94  print >> sys.stderr, " -w\t--workspace file\tWorkspace file to load"
95  sys.exit(0)
96 
97 def process_opt(opts, args):
98  """!Process command-line arguments"""
99  workspaceFile = None
100  for o, a in opts:
101  if o in ("-h", "--help"):
102  printHelp()
103 
104  if o in ("-w", "--workspace"):
105  if a != '':
106  workspaceFile = str(a)
107  else:
108  workspaceFile = args.pop(0)
109 
110  return (workspaceFile,)
111 
112 def main(argv = None):
113  import gettext
114  gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
115 
116  if argv is None:
117  argv = sys.argv
118  try:
119  try:
120  opts, args = getopt.getopt(argv[1:], "hw:",
121  ["help", "workspace"])
122  except getopt.error, msg:
123  raise Usage(msg)
124 
125  except Usage, err:
126  print >> sys.stderr, err.msg
127  print >> sys.stderr, "for help use --help"
128  printHelp()
129 
130  workspaceFile = process_opt(opts, args)[0]
131 
132  app = GMApp(workspaceFile)
133  # suppress wxPython logs
134  q = wx.LogNull()
135 
136  app.MainLoop()
137 
138 if __name__ == "__main__":
139  sys.exit(main())