wxgui.py 4.2 KB

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