wxgui.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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(
  59. bitmap=introBmp, timeout=2000, parent=None, id=wx.ID_ANY
  60. )
  61. splash.SetText(_("Starting GRASS GUI..."))
  62. splash.SetTextColour(wx.Colour(45, 52, 27))
  63. splash.SetTextFont(
  64. wx.Font(
  65. pointSize=15, family=wx.DEFAULT, style=wx.NORMAL, weight=wx.BOLD
  66. )
  67. )
  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. )
  79. else:
  80. wx.SplashScreen(
  81. bitmap=introBmp,
  82. splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  83. milliseconds=2000,
  84. parent=None,
  85. id=wx.ID_ANY,
  86. )
  87. wx.GetApp().Yield()
  88. # create and show main frame
  89. from lmgr.frame import GMFrame
  90. mainframe = GMFrame(parent=None, id=wx.ID_ANY, workspace=self.workspaceFile)
  91. mainframe.Show()
  92. self.SetTopWindow(mainframe)
  93. return True
  94. def OnExit(self):
  95. """Clean up on exit"""
  96. unregisterPid(os.getpid())
  97. return super().OnExit()
  98. def printHelp():
  99. """ Print program help"""
  100. print("Usage:", file=sys.stderr)
  101. print(" python wxgui.py [options]", file=sys.stderr)
  102. print("%sOptions:" % os.linesep, file=sys.stderr)
  103. print(" -w\t--workspace file\tWorkspace file to load", file=sys.stderr)
  104. sys.exit(1)
  105. def process_opt(opts, args):
  106. """ Process command-line arguments"""
  107. workspaceFile = None
  108. for o, a in opts:
  109. if o in ("-h", "--help"):
  110. printHelp()
  111. elif o in ("-w", "--workspace"):
  112. if a != "":
  113. workspaceFile = str(a)
  114. else:
  115. workspaceFile = args.pop(0)
  116. return workspaceFile
  117. def main(argv=None):
  118. if argv is None:
  119. argv = sys.argv
  120. try:
  121. try:
  122. opts, args = getopt.getopt(argv[1:], "hw:", ["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())