wxgui.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. import atexit
  19. from core import globalvar
  20. from core.utils import _, registerPid, unregisterPid
  21. from grass.exceptions import Usage
  22. from grass.script.core import set_raise_on_error
  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.IMGDIR, "splash_screen.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. def printHelp():
  69. """ Print program help"""
  70. print >> sys.stderr, "Usage:"
  71. print >> sys.stderr, " python wxgui.py [options]"
  72. print >> sys.stderr, "%sOptions:" % os.linesep
  73. print >> sys.stderr, " -w\t--workspace file\tWorkspace file to load"
  74. sys.exit(1)
  75. def process_opt(opts, args):
  76. """ Process command-line arguments"""
  77. workspaceFile = None
  78. for o, a in opts:
  79. if o in ("-h", "--help"):
  80. printHelp()
  81. elif o in ("-w", "--workspace"):
  82. if a != '':
  83. workspaceFile = str(a)
  84. else:
  85. workspaceFile = args.pop(0)
  86. return workspaceFile
  87. def cleanup():
  88. unregisterPid(os.getpid())
  89. def main(argv = None):
  90. if argv is None:
  91. argv = sys.argv
  92. try:
  93. try:
  94. opts, args = getopt.getopt(argv[1:], "hw:",
  95. ["help", "workspace"])
  96. except getopt.error as msg:
  97. raise Usage(msg)
  98. except Usage as err:
  99. print >> sys.stderr, err.msg
  100. print >> sys.stderr, "for help use --help"
  101. printHelp()
  102. workspaceFile = process_opt(opts, args)
  103. app = GMApp(workspaceFile)
  104. # suppress wxPython logs
  105. q = wx.LogNull()
  106. set_raise_on_error(True)
  107. # register GUI PID
  108. registerPid(os.getpid())
  109. app.MainLoop()
  110. if __name__ == "__main__":
  111. atexit.register(cleanup)
  112. sys.exit(main())