wxgui.py 3.9 KB

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