globalvar.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """!
  2. @package core.globalvar
  3. @brief Global variables used by wxGUI
  4. (C) 2007-2014 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. @author Martin Landa <landa.martin gmail.com>
  8. """
  9. import os
  10. import sys
  11. import locale
  12. if not os.getenv("GISBASE"):
  13. sys.exit("GRASS is not running. Exiting...")
  14. # path to python scripts
  15. ETCDIR = os.path.join(os.getenv("GISBASE"), "etc")
  16. GUIDIR = os.path.join(os.getenv("GISBASE"), "gui")
  17. WXGUIDIR = os.path.join(GUIDIR, "wxpython")
  18. ICONDIR = os.path.join(GUIDIR, "icons")
  19. IMGDIR = os.path.join(GUIDIR, "images")
  20. SYMBDIR = os.path.join(IMGDIR, "symbols")
  21. from core.debug import Debug
  22. # cannot import from the core.utils module to avoid cross dependencies
  23. try:
  24. # intended to be used also outside this module
  25. import gettext
  26. _ = gettext.translation('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale')).ugettext
  27. except IOError:
  28. # using no translation silently
  29. def null_gettext(string):
  30. return string
  31. _ = null_gettext
  32. from grass.script.core import get_commands
  33. def CheckWxVersion(version = [2, 8, 11, 0]):
  34. """!Check wx version"""
  35. ver = wx.version().split(' ')[0]
  36. if map(int, ver.split('.')) < version:
  37. return False
  38. return True
  39. def CheckForWx():
  40. """!Try to import wx module and check its version"""
  41. if 'wx' in sys.modules.keys():
  42. return
  43. minVersion = [2, 8, 10, 1]
  44. try:
  45. try:
  46. import wxversion
  47. except ImportError, e:
  48. raise ImportError(e)
  49. # wxversion.select(str(minVersion[0]) + '.' + str(minVersion[1]))
  50. wxversion.ensureMinimal(str(minVersion[0]) + '.' + str(minVersion[1]))
  51. import wx
  52. version = wx.version().split(' ')[0]
  53. if map(int, version.split('.')) < minVersion:
  54. raise ValueError('Your wxPython version is %s.%s.%s.%s' % tuple(version.split('.')))
  55. except ImportError, e:
  56. print >> sys.stderr, 'ERROR: wxGUI requires wxPython. %s' % str(e)
  57. sys.exit(1)
  58. except (ValueError, wxversion.VersionError), e:
  59. print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(minVersion) + \
  60. '%s.' % (str(e))
  61. sys.exit(1)
  62. except locale.Error, e:
  63. print >> sys.stderr, "Unable to set locale:", e
  64. os.environ['LC_ALL'] = ''
  65. if not os.getenv("GRASS_WXBUNDLED"):
  66. CheckForWx()
  67. import wx
  68. import wx.lib.flatnotebook as FN
  69. """
  70. Query layer (generated for example by selecting item in the Attribute Table Manager)
  71. Deleted automatically on re-render action
  72. """
  73. # temporal query layer (removed on re-render action)
  74. QUERYLAYER = 'qlayer'
  75. """!Style definition for FlatNotebook pages"""
  76. FNPageStyle = FN.FNB_VC8 | \
  77. FN.FNB_BACKGROUND_GRADIENT | \
  78. FN.FNB_NODRAG | \
  79. FN.FNB_TABS_BORDER_SIMPLE
  80. FNPageDStyle = FN.FNB_FANCY_TABS | \
  81. FN.FNB_BOTTOM | \
  82. FN.FNB_NO_NAV_BUTTONS | \
  83. FN.FNB_NO_X_BUTTON
  84. FNPageColor = wx.Colour(125,200,175)
  85. """!Dialog widget dimension"""
  86. DIALOG_SPIN_SIZE = (150, -1)
  87. DIALOG_COMBOBOX_SIZE = (300, -1)
  88. DIALOG_GSELECT_SIZE = (400, -1)
  89. DIALOG_TEXTCTRL_SIZE = (400, -1)
  90. DIALOG_LAYER_SIZE = (100, -1)
  91. DIALOG_COLOR_SIZE = (30, 30)
  92. MAP_WINDOW_SIZE = (800, 600)
  93. GM_WINDOW_MIN_SIZE = (525, 400)
  94. # small for ms window which wraps the menu
  95. # small for max os x which has the global menu
  96. # small for ubuntu when menuproxy is defined
  97. # not defined UBUNTU_MENUPROXY on linux means standard menu,
  98. # so the probably problem
  99. # UBUNTU_MENUPROXY= means ubuntu with disabled global menu [1]
  100. # use UBUNTU_MENUPROXY=0 to disbale global menu on ubuntu but in the same time
  101. # to get smaller lmgr
  102. # [1] https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationMenu#Troubleshooting
  103. if sys.platform in ('win32', 'darwin') or os.environ.get('UBUNTU_MENUPROXY'):
  104. GM_WINDOW_SIZE = (GM_WINDOW_MIN_SIZE[0], 600)
  105. else:
  106. GM_WINDOW_SIZE = (600, 600)
  107. if sys.platform == 'win32':
  108. BIN_EXT = '.exe'
  109. SCT_EXT = '.py'
  110. else:
  111. BIN_EXT = SCT_EXT = ''
  112. def UpdateGRASSAddOnCommands(eList = None):
  113. """!Update list of available GRASS AddOns commands to use when
  114. parsing string from the command line
  115. @param eList list of AddOns commands to remove
  116. """
  117. global grassCmd, grassScripts
  118. # scan addons (path)
  119. addonPath = os.getenv('GRASS_ADDON_PATH', '')
  120. addonBase = os.getenv('GRASS_ADDON_BASE')
  121. if addonBase:
  122. addonPath += os.pathsep + os.path.join(addonBase, 'bin') + os.pathsep + \
  123. os.path.join(addonBase, 'scripts')
  124. # remove commands first
  125. if eList:
  126. for ext in eList:
  127. if ext in grassCmd:
  128. grassCmd.remove(ext)
  129. Debug.msg(1, "Number of removed AddOn commands: %d", len(eList))
  130. nCmd = 0
  131. pathList = os.getenv('PATH', '').split(os.pathsep)
  132. for path in addonPath.split(os.pathsep):
  133. if not os.path.exists(path) or not os.path.isdir(path):
  134. continue
  135. # check if addon is in the path
  136. if pathList and path not in pathList:
  137. os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
  138. for fname in os.listdir(path):
  139. if fname in ['docs', 'modules.xml']:
  140. continue
  141. if grassScripts: # win32
  142. name, ext = os.path.splitext(fname)
  143. if name not in grassCmd:
  144. if ext not in [BIN_EXT, SCT_EXT]:
  145. continue
  146. if name not in grassCmd:
  147. grassCmd.add(name)
  148. Debug.msg(3, "AddOn commands: %s", name)
  149. nCmd += 1
  150. if ext == SCT_EXT and \
  151. ext in grassScripts.keys() and \
  152. name not in grassScripts[ext]:
  153. grassScripts[ext].append(name)
  154. else:
  155. if fname not in grassCmd:
  156. grassCmd.add(fname)
  157. Debug.msg(3, "AddOn commands: %s", fname)
  158. nCmd += 1
  159. Debug.msg(1, "Number of new AddOn commands: %d", nCmd)
  160. """@brief Collected GRASS-relared binaries/scripts"""
  161. grassCmd, grassScripts = get_commands()
  162. Debug.msg(1, "Number of GRASS commands: %d", len(grassCmd))
  163. UpdateGRASSAddOnCommands()
  164. """@Toolbar icon size"""
  165. toolbarSize = (24, 24)
  166. """@Is g.mlist available?"""
  167. if 'g.mlist' in grassCmd:
  168. have_mlist = True
  169. else:
  170. have_mlist = False
  171. """@Check version of wxPython, use agwStyle for 2.8.11+"""
  172. hasAgw = CheckWxVersion()