wxgui.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if SC and sys.platform != 'darwin':
  52. # AdvancedSplash is buggy on the Mac as of 2.8.12.1
  53. # and raises annoying (though seemingly harmless) errors everytime
  54. # the GUI is started
  55. splash = SC.AdvancedSplash(bitmap=introBmp,
  56. timeout=2000, parent=None, id=wx.ID_ANY)
  57. splash.SetText(_('Starting GRASS GUI...'))
  58. splash.SetTextColour(wx.Colour(45, 52, 27))
  59. splash.SetTextFont(
  60. wx.Font(
  61. pointSize=15,
  62. family=wx.DEFAULT,
  63. style=wx.NORMAL,
  64. weight=wx.BOLD))
  65. splash.SetTextPosition((150, 430))
  66. else:
  67. if globalvar.wxPythonPhoenix:
  68. import wx.adv as wxadv
  69. wxadv.SplashScreen(
  70. bitmap=introBmp,
  71. splashStyle=wxadv.SPLASH_CENTRE_ON_SCREEN | wxadv.SPLASH_TIMEOUT,
  72. milliseconds=2000,
  73. parent=None,
  74. id=wx.ID_ANY)
  75. else:
  76. wx.SplashScreen(
  77. bitmap=introBmp,
  78. splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  79. milliseconds=2000,
  80. parent=None,
  81. id=wx.ID_ANY)
  82. wx.GetApp().Yield()
  83. # create and show main frame
  84. from lmgr.frame import GMFrame
  85. mainframe = GMFrame(parent=None, id=wx.ID_ANY,
  86. workspace=self.workspaceFile)
  87. mainframe.Show()
  88. self.SetTopWindow(mainframe)
  89. return True
  90. def OnExit(self):
  91. """Clean up on exit"""
  92. unregisterPid(os.getpid())
  93. return super().OnExit()
  94. def printHelp():
  95. """ Print program help"""
  96. print("Usage:", file=sys.stderr)
  97. print(" python wxgui.py [options]", file=sys.stderr)
  98. print("%sOptions:" % os.linesep, file=sys.stderr)
  99. print(" -w\t--workspace file\tWorkspace file to load", file=sys.stderr)
  100. sys.exit(1)
  101. def process_opt(opts, args):
  102. """ Process command-line arguments"""
  103. workspaceFile = None
  104. for o, a in opts:
  105. if o in ("-h", "--help"):
  106. printHelp()
  107. elif o in ("-w", "--workspace"):
  108. if a != '':
  109. workspaceFile = str(a)
  110. else:
  111. workspaceFile = args.pop(0)
  112. return workspaceFile
  113. def main(argv=None):
  114. if argv is None:
  115. argv = sys.argv
  116. try:
  117. try:
  118. opts, args = getopt.getopt(argv[1:], "hw:",
  119. ["help", "workspace"])
  120. except getopt.error as msg:
  121. raise Usage(msg)
  122. except Usage as err:
  123. print(err.msg, file=sys.stderr)
  124. print(sys.stderr, "for help use --help", file=sys.stderr)
  125. printHelp()
  126. workspaceFile = process_opt(opts, args)
  127. app = GMApp(workspaceFile)
  128. # suppress wxPython logs
  129. q = wx.LogNull()
  130. set_raise_on_error(True)
  131. # register GUI PID
  132. registerPid(os.getpid())
  133. app.MainLoop()
  134. if __name__ == "__main__":
  135. sys.exit(main())