globalvar.py 6.5 KB

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