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. from core.utils import _
  24. import wx
  25. try:
  26. import wx.lib.agw.advancedsplash as SC
  27. except ImportError:
  28. SC = None
  29. from lmgr.frame import GMFrame
  30. class GMApp(wx.App):
  31. def __init__(self, workspace = None):
  32. """!Main GUI class.
  33. @param workspace path to the workspace file
  34. """
  35. self.workspaceFile = workspace
  36. # call parent class initializer
  37. wx.App.__init__(self, False)
  38. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  39. def OnInit(self):
  40. """!Initialize all available image handlers
  41. @return True
  42. """
  43. if not globalvar.CheckWxVersion([2, 9]):
  44. wx.InitAllImageHandlers()
  45. # create splash screen
  46. introImagePath = os.path.join(globalvar.ETCIMGDIR, "silesia_splash.png")
  47. introImage = wx.Image(introImagePath, wx.BITMAP_TYPE_PNG)
  48. introBmp = introImage.ConvertToBitmap()
  49. if SC and sys.platform != 'darwin':
  50. # AdvancedSplash is buggy on the Mac as of 2.8.12.1
  51. # and raises annoying (though seemingly harmless) errors everytime the GUI is started
  52. splash = SC.AdvancedSplash(bitmap = introBmp,
  53. timeout = 2000, parent = None, id = wx.ID_ANY)
  54. splash.SetText(_('Starting GRASS GUI...'))
  55. splash.SetTextColour(wx.Colour(45, 52, 27))
  56. splash.SetTextFont(wx.Font(pointSize = 15, family = wx.DEFAULT, style = wx.NORMAL,
  57. weight = wx.BOLD))
  58. splash.SetTextPosition((150, 430))
  59. else:
  60. wx.SplashScreen (bitmap = introBmp, splashStyle = wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  61. milliseconds = 2000, parent = None, id = wx.ID_ANY)
  62. wx.Yield()
  63. # create and show main frame
  64. mainframe = GMFrame(parent = None, id = wx.ID_ANY,
  65. workspace = self.workspaceFile)
  66. mainframe.Show()
  67. self.SetTopWindow(mainframe)
  68. return True
  69. class Usage(Exception):
  70. def __init__(self, msg):
  71. self.msg = msg
  72. def printHelp():
  73. """!Print program help"""
  74. print >> sys.stderr, "Usage:"
  75. print >> sys.stderr, " python wxgui.py [options]"
  76. print >> sys.stderr, "%sOptions:" % os.linesep
  77. print >> sys.stderr, " -w\t--workspace file\tWorkspace file to load"
  78. sys.exit(1)
  79. def process_opt(opts, args):
  80. """!Process command-line arguments"""
  81. workspaceFile = None
  82. for o, a in opts:
  83. if o in ("-h", "--help"):
  84. printHelp()
  85. if o in ("-w", "--workspace"):
  86. if a != '':
  87. workspaceFile = str(a)
  88. else:
  89. workspaceFile = args.pop(0)
  90. return (workspaceFile,)
  91. def main(argv = None):
  92. if argv is None:
  93. argv = sys.argv
  94. try:
  95. try:
  96. opts, args = getopt.getopt(argv[1:], "hw:",
  97. ["help", "workspace"])
  98. except getopt.error, msg:
  99. raise Usage(msg)
  100. except Usage, err:
  101. print >> sys.stderr, err.msg
  102. print >> sys.stderr, "for help use --help"
  103. printHelp()
  104. workspaceFile = process_opt(opts, args)[0]
  105. app = GMApp(workspaceFile)
  106. # suppress wxPython logs
  107. q = wx.LogNull()
  108. app.MainLoop()
  109. if __name__ == "__main__":
  110. sys.exit(main())