globalvar.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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):
  34. """Check wx version"""
  35. ver = wx.__version__
  36. if map(int, ver.split('.')) < version:
  37. return False
  38. return True
  39. def CheckForWx(forceVersion = os.getenv('GRASS_WXVERSION', None)):
  40. """Try to import wx module and check its version
  41. :param forceVersion: force wxPython version, eg. '2.8'
  42. """
  43. if 'wx' in sys.modules.keys():
  44. return
  45. minVersion = [2, 8, 10, 1]
  46. try:
  47. try:
  48. import wxversion
  49. except ImportError as e:
  50. raise ImportError(e)
  51. if forceVersion:
  52. wxversion.select(forceVersion)
  53. wxversion.ensureMinimal(str(minVersion[0]) + '.' + str(minVersion[1]))
  54. import wx
  55. version = wx.__version__
  56. if map(int, version.split('.')) < minVersion:
  57. raise ValueError('Your wxPython version is %s.%s.%s.%s' % tuple(version.split('.')))
  58. except ImportError as e:
  59. print >> sys.stderr, 'ERROR: wxGUI requires wxPython. %s' % str(e)
  60. sys.exit(1)
  61. except (ValueError, wxversion.VersionError) as e:
  62. print >> sys.stderr, 'ERROR: wxGUI requires wxPython >= %d.%d.%d.%d. ' % tuple(minVersion) + \
  63. '%s.' % (str(e))
  64. sys.exit(1)
  65. except locale.Error as e:
  66. print >> sys.stderr, "Unable to set locale:", e
  67. os.environ['LC_ALL'] = ''
  68. if not os.getenv("GRASS_WXBUNDLED"):
  69. CheckForWx()
  70. import wx
  71. import wx.lib.flatnotebook as FN
  72. """
  73. Query layer (generated for example by selecting item in the Attribute Table Manager)
  74. Deleted automatically on re-render action
  75. """
  76. # temporal query layer (removed on re-render action)
  77. QUERYLAYER = 'qlayer'
  78. """Style definition for FlatNotebook pages"""
  79. FNPageStyle = FN.FNB_VC8 | \
  80. FN.FNB_BACKGROUND_GRADIENT | \
  81. FN.FNB_NODRAG | \
  82. FN.FNB_TABS_BORDER_SIMPLE
  83. FNPageDStyle = FN.FNB_FANCY_TABS | \
  84. FN.FNB_BOTTOM | \
  85. FN.FNB_NO_NAV_BUTTONS | \
  86. FN.FNB_NO_X_BUTTON
  87. FNPageColor = wx.Colour(125,200,175)
  88. """Dialog widget dimension"""
  89. DIALOG_SPIN_SIZE = (150, -1)
  90. DIALOG_COMBOBOX_SIZE = (300, -1)
  91. DIALOG_GSELECT_SIZE = (400, -1)
  92. DIALOG_TEXTCTRL_SIZE = (400, -1)
  93. DIALOG_LAYER_SIZE = (100, -1)
  94. DIALOG_COLOR_SIZE = (30, 30)
  95. MAP_WINDOW_SIZE = (800, 600)
  96. GM_WINDOW_MIN_SIZE = (525, 400)
  97. # small for ms window which wraps the menu
  98. # small for max os x which has the global menu
  99. # small for ubuntu when menuproxy is defined
  100. # not defined UBUNTU_MENUPROXY on linux means standard menu,
  101. # so the probably problem
  102. # UBUNTU_MENUPROXY= means ubuntu with disabled global menu [1]
  103. # use UBUNTU_MENUPROXY=0 to disbale global menu on ubuntu but in the same time
  104. # to get smaller lmgr
  105. # [1] https://wiki.ubuntu.com/DesktopExperienceTeam/ApplicationMenu#Troubleshooting
  106. if sys.platform in ('win32', 'darwin') or os.environ.get('UBUNTU_MENUPROXY'):
  107. GM_WINDOW_SIZE = (GM_WINDOW_MIN_SIZE[0], 600)
  108. else:
  109. GM_WINDOW_SIZE = (600, 600)
  110. if sys.platform == 'win32':
  111. BIN_EXT = '.exe'
  112. SCT_EXT = '.bat'
  113. else:
  114. BIN_EXT = SCT_EXT = ''
  115. def UpdateGRASSAddOnCommands(eList = None):
  116. """Update list of available GRASS AddOns commands to use when
  117. parsing string from the command line
  118. :param eList: list of AddOns commands to remove
  119. """
  120. global grassCmd, grassScripts
  121. # scan addons (path)
  122. addonPath = os.getenv('GRASS_ADDON_PATH', '')
  123. addonBase = os.getenv('GRASS_ADDON_BASE')
  124. if addonBase:
  125. addonPath += os.pathsep + os.path.join(addonBase, 'bin')
  126. if sys.platform != 'win32':
  127. addonPath += os.pathsep + os.path.join(addonBase, 'scripts')
  128. # remove commands first
  129. if eList:
  130. for ext in eList:
  131. if ext in grassCmd:
  132. grassCmd.remove(ext)
  133. Debug.msg(1, "Number of removed AddOn commands: %d", len(eList))
  134. nCmd = 0
  135. pathList = os.getenv('PATH', '').split(os.pathsep)
  136. for path in addonPath.split(os.pathsep):
  137. if not os.path.exists(path) or not os.path.isdir(path):
  138. continue
  139. # check if addon is in the path
  140. if pathList and path not in pathList:
  141. os.environ['PATH'] = path + os.pathsep + os.environ['PATH']
  142. for fname in os.listdir(path):
  143. if fname in ['docs', 'modules.xml']:
  144. continue
  145. if grassScripts: # win32
  146. name, ext = os.path.splitext(fname)
  147. if name not in grassCmd:
  148. if ext not in [BIN_EXT, SCT_EXT]:
  149. continue
  150. if name not in grassCmd:
  151. grassCmd.add(name)
  152. Debug.msg(3, "AddOn commands: %s", name)
  153. nCmd += 1
  154. if ext == SCT_EXT and \
  155. ext in grassScripts.keys() and \
  156. name not in grassScripts[ext]:
  157. grassScripts[ext].append(name)
  158. else:
  159. if fname not in grassCmd:
  160. grassCmd.add(fname)
  161. Debug.msg(3, "AddOn commands: %s", fname)
  162. nCmd += 1
  163. Debug.msg(1, "Number of GRASS AddOn commands: %d", nCmd)
  164. """@brief Collected GRASS-relared binaries/scripts"""
  165. grassCmd, grassScripts = get_commands()
  166. Debug.msg(1, "Number of core GRASS commands: %d", len(grassCmd))
  167. UpdateGRASSAddOnCommands()
  168. """@Toolbar icon size"""
  169. toolbarSize = (24, 24)
  170. """@Check version of wxPython, use agwStyle for 2.8.11+"""
  171. hasAgw = CheckWxVersion([2, 8, 11, 0])
  172. wxPython3 = CheckWxVersion([3, 0, 0, 0])
  173. """@Add GUIDIR/scripts into path"""
  174. os.environ['PATH'] = os.path.join(GUIDIR, 'scripts') + os.pathsep + os.environ['PATH']