wxgui.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if globalvar.wxPythonPhoenix:
  63. import wx.adv as wxadv
  64. wxadv.SplashScreen(
  65. bitmap=introBmp,
  66. splashStyle=wxadv.SPLASH_CENTRE_ON_SCREEN | wxadv.SPLASH_TIMEOUT,
  67. milliseconds=2000,
  68. parent=None,
  69. id=wx.ID_ANY)
  70. else:
  71. wx.SplashScreen(
  72. bitmap=introBmp,
  73. splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  74. milliseconds=2000,
  75. parent=None,
  76. id=wx.ID_ANY)
  77. wx.Yield()
  78. # create and show main frame
  79. from lmgr.frame import GMFrame
  80. mainframe = GMFrame(parent=None, id=wx.ID_ANY,
  81. workspace=self.workspaceFile)
  82. mainframe.Show()
  83. self.SetTopWindow(mainframe)
  84. return True
  85. def printHelp():
  86. """ Print program help"""
  87. print >> sys.stderr, "Usage:"
  88. print >> sys.stderr, " python wxgui.py [options]"
  89. print >> sys.stderr, "%sOptions:" % os.linesep
  90. print >> sys.stderr, " -w\t--workspace file\tWorkspace file to load"
  91. sys.exit(1)
  92. def process_opt(opts, args):
  93. """ Process command-line arguments"""
  94. workspaceFile = None
  95. for o, a in opts:
  96. if o in ("-h", "--help"):
  97. printHelp()
  98. elif o in ("-w", "--workspace"):
  99. if a != '':
  100. workspaceFile = str(a)
  101. else:
  102. workspaceFile = args.pop(0)
  103. return workspaceFile
  104. def cleanup():
  105. unregisterPid(os.getpid())
  106. def main(argv=None):
  107. if argv is None:
  108. argv = sys.argv
  109. try:
  110. try:
  111. opts, args = getopt.getopt(argv[1:], "hw:",
  112. ["help", "workspace"])
  113. except getopt.error as msg:
  114. raise Usage(msg)
  115. except Usage as err:
  116. print >> sys.stderr, err.msg
  117. print >> sys.stderr, "for help use --help"
  118. printHelp()
  119. workspaceFile = process_opt(opts, args)
  120. app = GMApp(workspaceFile)
  121. # suppress wxPython logs
  122. q = wx.LogNull()
  123. set_raise_on_error(True)
  124. # register GUI PID
  125. registerPid(os.getpid())
  126. app.MainLoop()
  127. if __name__ == "__main__":
  128. atexit.register(cleanup)
  129. sys.exit(main())