wxgui.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. from __future__ import print_function
  15. import os
  16. import sys
  17. import getopt
  18. import atexit
  19. # i18n is taken care of in the grass library code.
  20. # So we need to import it before any of the GUI code.
  21. from grass.exceptions import Usage
  22. from grass.script.core import set_raise_on_error
  23. from core import globalvar
  24. from core.utils import registerPid, unregisterPid
  25. import wx
  26. try:
  27. import wx.lib.agw.advancedsplash as SC
  28. except ImportError:
  29. SC = None
  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.IMGDIR, "splash_screen.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
  52. # the GUI is started
  53. splash = SC.AdvancedSplash(bitmap=introBmp,
  54. timeout=2000, parent=None, id=wx.ID_ANY)
  55. splash.SetText(_('Starting GRASS GUI...'))
  56. splash.SetTextColour(wx.Colour(45, 52, 27))
  57. splash.SetTextFont(
  58. wx.Font(
  59. pointSize=15,
  60. family=wx.DEFAULT,
  61. style=wx.NORMAL,
  62. weight=wx.BOLD))
  63. splash.SetTextPosition((150, 430))
  64. else:
  65. if globalvar.wxPythonPhoenix:
  66. import wx.adv as wxadv
  67. wxadv.SplashScreen(
  68. bitmap=introBmp,
  69. splashStyle=wxadv.SPLASH_CENTRE_ON_SCREEN | wxadv.SPLASH_TIMEOUT,
  70. milliseconds=2000,
  71. parent=None,
  72. id=wx.ID_ANY)
  73. else:
  74. wx.SplashScreen(
  75. bitmap=introBmp,
  76. splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  77. milliseconds=2000,
  78. parent=None,
  79. id=wx.ID_ANY)
  80. wx.Yield()
  81. # create and show main frame
  82. from lmgr.frame import GMFrame
  83. mainframe = GMFrame(parent=None, id=wx.ID_ANY,
  84. workspace=self.workspaceFile)
  85. mainframe.Show()
  86. self.SetTopWindow(mainframe)
  87. return True
  88. def printHelp():
  89. """ Print program help"""
  90. print("Usage:", file=sys.stderr)
  91. print(" python wxgui.py [options]", file=sys.stderr)
  92. print("%sOptions:" % os.linesep, file=sys.stderr)
  93. print(" -w\t--workspace file\tWorkspace file to load", file=sys.stderr)
  94. sys.exit(1)
  95. def process_opt(opts, args):
  96. """ Process command-line arguments"""
  97. workspaceFile = None
  98. for o, a in opts:
  99. if o in ("-h", "--help"):
  100. printHelp()
  101. elif o in ("-w", "--workspace"):
  102. if a != '':
  103. workspaceFile = str(a)
  104. else:
  105. workspaceFile = args.pop(0)
  106. return workspaceFile
  107. def cleanup():
  108. unregisterPid(os.getpid())
  109. def main(argv=None):
  110. if argv is None:
  111. argv = sys.argv
  112. try:
  113. try:
  114. opts, args = getopt.getopt(argv[1:], "hw:",
  115. ["help", "workspace"])
  116. except getopt.error as msg:
  117. raise Usage(msg)
  118. except Usage as err:
  119. print(err.msg, file=sys.stderr)
  120. print(sys.stderr, "for help use --help", file=sys.stderr)
  121. printHelp()
  122. workspaceFile = process_opt(opts, args)
  123. app = GMApp(workspaceFile)
  124. # suppress wxPython logs
  125. q = wx.LogNull()
  126. set_raise_on_error(True)
  127. # register GUI PID
  128. registerPid(os.getpid())
  129. app.MainLoop()
  130. if __name__ == "__main__":
  131. atexit.register(cleanup)
  132. sys.exit(main())