wxgui.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. from core.settings import UserSettings
  25. import wx
  26. # import adv and html before wx.App is created, otherwise
  27. # we get annoying "Debug: Adding duplicate image handler for 'Windows bitmap file'"
  28. # during start up, remove when not needed
  29. import wx.adv
  30. import wx.html
  31. try:
  32. import wx.lib.agw.advancedsplash as SC
  33. except ImportError:
  34. SC = None
  35. class GMApp(wx.App):
  36. def __init__(self, workspace=None):
  37. """Main GUI class.
  38. :param workspace: path to the workspace file
  39. """
  40. self.workspaceFile = workspace
  41. # call parent class initializer
  42. wx.App.__init__(self, False)
  43. self.locale = wx.Locale(language=wx.LANGUAGE_DEFAULT)
  44. def OnInit(self):
  45. """Initialize all available image handlers
  46. :return: True
  47. """
  48. # Internal and display name of the app (if supported by/on platform)
  49. self.SetAppName("GRASS GIS")
  50. self.SetVendorName("The GRASS Development Team")
  51. # create splash screen
  52. introImagePath = os.path.join(globalvar.IMGDIR, "splash_screen.png")
  53. introImage = wx.Image(introImagePath, wx.BITMAP_TYPE_PNG)
  54. introBmp = introImage.ConvertToBitmap()
  55. wx.adv.SplashScreen(
  56. bitmap=introBmp,
  57. splashStyle=wx.adv.SPLASH_CENTRE_ON_SCREEN | wx.adv.SPLASH_TIMEOUT,
  58. milliseconds=3000,
  59. parent=None,
  60. id=wx.ID_ANY,
  61. )
  62. wx.GetApp().Yield()
  63. def show_main_gui():
  64. # create and show main frame
  65. single = UserSettings.Get(
  66. group="general", key="singleWindow", subkey="enabled"
  67. )
  68. if single:
  69. from main_window.frame import GMFrame
  70. else:
  71. from lmgr.frame import GMFrame
  72. mainframe = GMFrame(parent=None, id=wx.ID_ANY, workspace=self.workspaceFile)
  73. mainframe.Show()
  74. self.SetTopWindow(mainframe)
  75. wx.CallAfter(show_main_gui)
  76. return True
  77. def OnExit(self):
  78. """Clean up on exit"""
  79. unregisterPid(os.getpid())
  80. return super().OnExit()
  81. def printHelp():
  82. """Print program help"""
  83. print("Usage:", file=sys.stderr)
  84. print(" python wxgui.py [options]", file=sys.stderr)
  85. print("%sOptions:" % os.linesep, file=sys.stderr)
  86. print(" -w\t--workspace file\tWorkspace file to load", file=sys.stderr)
  87. sys.exit(1)
  88. def process_opt(opts, args):
  89. """Process command-line arguments"""
  90. workspaceFile = None
  91. for o, a in opts:
  92. if o in ("-h", "--help"):
  93. printHelp()
  94. elif o in ("-w", "--workspace"):
  95. if a != "":
  96. workspaceFile = str(a)
  97. else:
  98. workspaceFile = args.pop(0)
  99. return workspaceFile
  100. def main(argv=None):
  101. if argv is None:
  102. argv = sys.argv
  103. try:
  104. try:
  105. opts, args = getopt.getopt(argv[1:], "hw:", ["help", "workspace"])
  106. except getopt.error as msg:
  107. raise Usage(msg)
  108. except Usage as err:
  109. print(err.msg, file=sys.stderr)
  110. print(sys.stderr, "for help use --help", file=sys.stderr)
  111. printHelp()
  112. workspaceFile = process_opt(opts, args)
  113. app = GMApp(workspaceFile)
  114. # suppress wxPython logs
  115. q = wx.LogNull()
  116. set_raise_on_error(True)
  117. # register GUI PID
  118. registerPid(os.getpid())
  119. app.MainLoop()
  120. if __name__ == "__main__":
  121. sys.exit(main())