wxgui.py 3.8 KB

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