wxgui.py 4.0 KB

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