wxgui.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. @package wxgui
  3. @brief Main Python application for GRASS wxPython GUI
  4. Classes:
  5. - wxgui::GMApp
  6. - wxgui::GSplashScreen
  7. (C) 2006-2015 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. @author Michael Barton (Arizona State University)
  11. @author Jachym Cepicky (Mendel University of Agriculture)
  12. @author Martin Landa <landa.martin gmail.com>
  13. @author Vaclav Petras <wenzeslaus gmail.com> (menu customization)
  14. """
  15. import os
  16. import sys
  17. import getopt
  18. from core import globalvar
  19. from core.utils import _
  20. from grass.exceptions import Usage
  21. from grass.script.core import set_raise_on_error
  22. import wx
  23. try:
  24. import wx.lib.agw.advancedsplash as SC
  25. except ImportError:
  26. SC = None
  27. from lmgr.frame import GMFrame
  28. class GMApp(wx.App):
  29. def __init__(self, workspace = None):
  30. """ Main GUI class.
  31. :param workspace: path to the workspace file
  32. """
  33. self.workspaceFile = workspace
  34. # call parent class initializer
  35. wx.App.__init__(self, False)
  36. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  37. def OnInit(self):
  38. """ Initialize all available image handlers
  39. :return: True
  40. """
  41. if not globalvar.CheckWxVersion([2, 9]):
  42. wx.InitAllImageHandlers()
  43. splash = GSplashScreen(app=self, workspace=self.workspaceFile)
  44. splash.Show()
  45. return True
  46. class GSplashScreen(wx.SplashScreen):
  47. """Create a splash screen widget
  48. """
  49. def __init__(self, parent=None, app=None, workspace=None):
  50. self.workspaceFile = workspace
  51. self.parent = parent
  52. self.app = app
  53. bitmap = wx.Image(name=os.path.join(globalvar.IMGDIR, "splash_screen.png")).ConvertToBitmap()
  54. wx.SplashScreen.__init__(self, bitmap,
  55. wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  56. 1000, parent)
  57. self.Bind(wx.EVT_CLOSE, self.OnExit)
  58. wx.Yield()
  59. def OnExit(self, event):
  60. self.Hide()
  61. # create and show main frame
  62. frame = GMFrame(parent = None, id = wx.ID_ANY,
  63. workspace = self.workspaceFile)
  64. self.app.SetTopWindow(frame)
  65. frame.Show()
  66. event.Skip() # make sure the default handler runs too
  67. def printHelp():
  68. """ Print program help"""
  69. print >> sys.stderr, "Usage:"
  70. print >> sys.stderr, " python wxgui.py [options]"
  71. print >> sys.stderr, "%sOptions:" % os.linesep
  72. print >> sys.stderr, " -w\t--workspace file\tWorkspace file to load"
  73. sys.exit(1)
  74. def process_opt(opts, args):
  75. """ Process command-line arguments"""
  76. workspaceFile = None
  77. for o, a in opts:
  78. if o in ("-h", "--help"):
  79. printHelp()
  80. if o in ("-w", "--workspace"):
  81. if a != '':
  82. workspaceFile = str(a)
  83. else:
  84. workspaceFile = args.pop(0)
  85. return (workspaceFile,)
  86. def main(argv = None):
  87. if argv is None:
  88. argv = sys.argv
  89. try:
  90. try:
  91. opts, args = getopt.getopt(argv[1:], "hw:",
  92. ["help", "workspace"])
  93. except getopt.error as msg:
  94. raise Usage(msg)
  95. except Usage as err:
  96. print >> sys.stderr, err.msg
  97. print >> sys.stderr, "for help use --help"
  98. printHelp()
  99. workspaceFile = process_opt(opts, args)[0]
  100. app = GMApp(workspaceFile)
  101. # suppress wxPython logs
  102. q = wx.LogNull()
  103. set_raise_on_error(True)
  104. app.MainLoop()
  105. if __name__ == "__main__":
  106. sys.exit(main())