wxgui.py 4.8 KB

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