wxgui.py 5.1 KB

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