wxgui.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. try:
  27. import wx.lib.agw.advancedsplash as SC
  28. except ImportError:
  29. SC = None
  30. class GMApp(wx.App):
  31. def __init__(self, workspace=None):
  32. """ Main GUI class.
  33. :param workspace: path to the workspace file
  34. """
  35. self.workspaceFile = workspace
  36. # call parent class initializer
  37. wx.App.__init__(self, False)
  38. self.locale = wx.Locale(language=wx.LANGUAGE_DEFAULT)
  39. def OnInit(self):
  40. """ Initialize all available image handlers
  41. :return: True
  42. """
  43. # create splash screen
  44. introImagePath = os.path.join(globalvar.IMGDIR, "splash_screen.png")
  45. introImage = wx.Image(introImagePath, wx.BITMAP_TYPE_PNG)
  46. introBmp = introImage.ConvertToBitmap()
  47. if SC and sys.platform != 'darwin':
  48. # AdvancedSplash is buggy on the Mac as of 2.8.12.1
  49. # and raises annoying (though seemingly harmless) errors everytime
  50. # the GUI is started
  51. splash = SC.AdvancedSplash(bitmap=introBmp,
  52. timeout=2000, parent=None, id=wx.ID_ANY)
  53. splash.SetText(_('Starting GRASS GUI...'))
  54. splash.SetTextColour(wx.Colour(45, 52, 27))
  55. splash.SetTextFont(
  56. wx.Font(
  57. pointSize=15,
  58. family=wx.DEFAULT,
  59. style=wx.NORMAL,
  60. weight=wx.BOLD))
  61. splash.SetTextPosition((150, 430))
  62. else:
  63. if globalvar.wxPythonPhoenix:
  64. import wx.adv as wxadv
  65. wxadv.SplashScreen(
  66. bitmap=introBmp,
  67. splashStyle=wxadv.SPLASH_CENTRE_ON_SCREEN | wxadv.SPLASH_TIMEOUT,
  68. milliseconds=2000,
  69. parent=None,
  70. id=wx.ID_ANY)
  71. else:
  72. wx.SplashScreen(
  73. bitmap=introBmp,
  74. splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  75. milliseconds=2000,
  76. parent=None,
  77. id=wx.ID_ANY)
  78. wx.Yield()
  79. # create and show main frame
  80. from lmgr.frame import GMFrame
  81. mainframe = GMFrame(parent=None, id=wx.ID_ANY,
  82. workspace=self.workspaceFile)
  83. mainframe.Show()
  84. self.SetTopWindow(mainframe)
  85. return True
  86. def printHelp():
  87. """ Print program help"""
  88. print("Usage:", file=sys.stderr)
  89. print(" python wxgui.py [options]", file=sys.stderr)
  90. print("%sOptions:" % os.linesep, file=sys.stderr)
  91. print(" -w\t--workspace file\tWorkspace file to load", file=sys.stderr)
  92. sys.exit(1)
  93. def process_opt(opts, args):
  94. """ Process command-line arguments"""
  95. workspaceFile = None
  96. for o, a in opts:
  97. if o in ("-h", "--help"):
  98. printHelp()
  99. elif o in ("-w", "--workspace"):
  100. if a != '':
  101. workspaceFile = str(a)
  102. else:
  103. workspaceFile = args.pop(0)
  104. return workspaceFile
  105. def cleanup():
  106. unregisterPid(os.getpid())
  107. def main(argv=None):
  108. if argv is None:
  109. argv = sys.argv
  110. try:
  111. try:
  112. opts, args = getopt.getopt(argv[1:], "hw:",
  113. ["help", "workspace"])
  114. except getopt.error as msg:
  115. raise Usage(msg)
  116. except Usage as err:
  117. print(err.msg, file=sys.stderr)
  118. print(sys.stderr, "for help use --help", file=sys.stderr)
  119. printHelp()
  120. workspaceFile = process_opt(opts, args)
  121. app = GMApp(workspaceFile)
  122. # suppress wxPython logs
  123. q = wx.LogNull()
  124. set_raise_on_error(True)
  125. # register GUI PID
  126. registerPid(os.getpid())
  127. app.MainLoop()
  128. if __name__ == "__main__":
  129. atexit.register(cleanup)
  130. sys.exit(main())