wxgui.py 4.1 KB

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