wxgui.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. from lmgr.frame import GMFrame
  24. class GMApp(wx.App):
  25. def __init__(self, workspace = None):
  26. """ Main GUI class.
  27. :param workspace: path to the workspace file
  28. """
  29. self.workspaceFile = workspace
  30. # call parent class initializer
  31. wx.App.__init__(self, False)
  32. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  33. def OnInit(self):
  34. """ Initialize all available image handlers
  35. :return: True
  36. """
  37. if not globalvar.CheckWxVersion([2, 9]):
  38. wx.InitAllImageHandlers()
  39. splash = GSplashScreen(app=self, workspace=self.workspaceFile)
  40. splash.Show()
  41. return True
  42. class GSplashScreen(wx.SplashScreen):
  43. """Create a splash screen widget
  44. """
  45. def __init__(self, parent=None, app=None, workspace=None):
  46. self.workspaceFile = workspace
  47. self.parent = parent
  48. self.app = app
  49. bitmap = wx.Image(name=os.path.join(globalvar.IMGDIR, "splash_screen.png")).ConvertToBitmap()
  50. wx.SplashScreen.__init__(self, bitmap=bitmap,
  51. splashStyle=wx.SPLASH_CENTRE_ON_SCREEN | wx.SPLASH_TIMEOUT,
  52. milliseconds=1000, parent=parent)
  53. self.Bind(wx.EVT_CLOSE, self.OnExit)
  54. wx.Yield()
  55. def OnExit(self, event):
  56. # create and show main frame
  57. frame = GMFrame(parent = None, id = wx.ID_ANY,
  58. workspace = self.workspaceFile)
  59. self.app.SetTopWindow(frame)
  60. self.Hide()
  61. frame.Show()
  62. event.Skip() # make sure the default handler runs too
  63. def printHelp():
  64. """ Print program help"""
  65. print >> sys.stderr, "Usage:"
  66. print >> sys.stderr, " python wxgui.py [options]"
  67. print >> sys.stderr, "%sOptions:" % os.linesep
  68. print >> sys.stderr, " -w\t--workspace file\tWorkspace file to load"
  69. sys.exit(1)
  70. def process_opt(opts, args):
  71. """ Process command-line arguments"""
  72. workspaceFile = None
  73. for o, a in opts:
  74. if o in ("-h", "--help"):
  75. printHelp()
  76. if o in ("-w", "--workspace"):
  77. if a != '':
  78. workspaceFile = str(a)
  79. else:
  80. workspaceFile = args.pop(0)
  81. return (workspaceFile,)
  82. def main(argv = None):
  83. if argv is None:
  84. argv = sys.argv
  85. try:
  86. try:
  87. opts, args = getopt.getopt(argv[1:], "hw:",
  88. ["help", "workspace"])
  89. except getopt.error as msg:
  90. raise Usage(msg)
  91. except Usage as err:
  92. print >> sys.stderr, err.msg
  93. print >> sys.stderr, "for help use --help"
  94. printHelp()
  95. workspaceFile = process_opt(opts, args)[0]
  96. app = GMApp(workspaceFile)
  97. # suppress wxPython logs
  98. q = wx.LogNull()
  99. set_raise_on_error(True)
  100. app.MainLoop()
  101. if __name__ == "__main__":
  102. sys.exit(main())