wxgui.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. # i18n is taken care of in the grass library code.
  19. # So we need to import it before any of the GUI code.
  20. from grass.exceptions import Usage
  21. from grass.script.core import set_raise_on_error
  22. from core import globalvar
  23. from core.utils import registerPid, unregisterPid
  24. import wx
  25. # import adv and html before wx.App is created, otherwise
  26. # we get annoying "Debug: Adding duplicate image handler for 'Windows bitmap file'"
  27. # during start up, remove when not needed
  28. import wx.adv
  29. import wx.html
  30. try:
  31. import wx.lib.agw.advancedsplash as SC
  32. except ImportError:
  33. SC = None
  34. class GMApp(wx.App):
  35. def __init__(self, workspace=None):
  36. """ Main GUI class.
  37. :param workspace: path to the workspace file
  38. """
  39. self.workspaceFile = workspace
  40. # call parent class initializer
  41. wx.App.__init__(self, False)
  42. self.locale = wx.Locale(language=wx.LANGUAGE_DEFAULT)
  43. def OnInit(self):
  44. """ Initialize all available image handlers
  45. :return: True
  46. """
  47. # Internal and display name of the app (if supported by/on platform)
  48. self.SetAppName("GRASS GIS")
  49. self.SetVendorName("The GRASS Development Team")
  50. # create splash screen
  51. introImagePath = os.path.join(globalvar.IMGDIR, "splash_screen.png")
  52. introImage = wx.Image(introImagePath, wx.BITMAP_TYPE_PNG)
  53. introBmp = introImage.ConvertToBitmap()
  54. if SC and sys.platform != 'darwin':
  55. # AdvancedSplash is buggy on the Mac as of 2.8.12.1
  56. # and raises annoying (though seemingly harmless) errors everytime
  57. # the GUI is started
  58. splash = SC.AdvancedSplash(bitmap=introBmp,
  59. timeout=2000, parent=None, id=wx.ID_ANY)
  60. splash.SetText(_('Starting GRASS GUI...'))
  61. splash.SetTextColour(wx.Colour(45, 52, 27))
  62. splash.SetTextFont(
  63. wx.Font(
  64. pointSize=15,
  65. family=wx.DEFAULT,
  66. style=wx.NORMAL,
  67. weight=wx.BOLD))
  68. splash.SetTextPosition((150, 430))
  69. else:
  70. if globalvar.wxPythonPhoenix:
  71. import wx.adv as wxadv
  72. wxadv.SplashScreen(
  73. bitmap=introBmp,
  74. splashStyle=wxadv.SPLASH_CENTRE_ON_SCREEN | wxadv.SPLASH_TIMEOUT,
  75. milliseconds=2000,
  76. parent=None,
  77. id=wx.ID_ANY)
  78. else:
  79. wx.SplashScreen(
  80. bitmap=introBmp,
  81. splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  82. milliseconds=2000,
  83. parent=None,
  84. id=wx.ID_ANY)
  85. wx.GetApp().Yield()
  86. # create and show main frame
  87. from lmgr.frame import GMFrame
  88. mainframe = GMFrame(parent=None, id=wx.ID_ANY,
  89. workspace=self.workspaceFile)
  90. mainframe.Show()
  91. self.SetTopWindow(mainframe)
  92. return True
  93. def OnExit(self):
  94. """Clean up on exit"""
  95. unregisterPid(os.getpid())
  96. return super().OnExit()
  97. def printHelp():
  98. """ Print program help"""
  99. print("Usage:", file=sys.stderr)
  100. print(" python wxgui.py [options]", file=sys.stderr)
  101. print("%sOptions:" % os.linesep, file=sys.stderr)
  102. print(" -w\t--workspace file\tWorkspace file to load", file=sys.stderr)
  103. sys.exit(1)
  104. def process_opt(opts, args):
  105. """ Process command-line arguments"""
  106. workspaceFile = None
  107. for o, a in opts:
  108. if o in ("-h", "--help"):
  109. printHelp()
  110. elif o in ("-w", "--workspace"):
  111. if a != '':
  112. workspaceFile = str(a)
  113. else:
  114. workspaceFile = args.pop(0)
  115. return workspaceFile
  116. def main(argv=None):
  117. if argv is None:
  118. argv = sys.argv
  119. try:
  120. try:
  121. opts, args = getopt.getopt(argv[1:], "hw:",
  122. ["help", "workspace"])
  123. except getopt.error as msg:
  124. raise Usage(msg)
  125. except Usage as err:
  126. print(err.msg, file=sys.stderr)
  127. print(sys.stderr, "for help use --help", file=sys.stderr)
  128. printHelp()
  129. workspaceFile = process_opt(opts, args)
  130. app = GMApp(workspaceFile)
  131. # suppress wxPython logs
  132. q = wx.LogNull()
  133. set_raise_on_error(True)
  134. # register GUI PID
  135. registerPid(os.getpid())
  136. app.MainLoop()
  137. if __name__ == "__main__":
  138. sys.exit(main())