wxgui.py 3.9 KB

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