gis_set.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. """
  2. @package gis_set
  3. GRASS start-up screen.
  4. Initialization module for wxPython GRASS GUI.
  5. Location/mapset management (selection, creation, etc.).
  6. Classes:
  7. - gis_set::GRASSStartup
  8. - gis_set::GListBox
  9. - gis_set::StartUp
  10. (C) 2006-2014 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. @author Michael Barton and Jachym Cepicky (original author)
  14. @author Martin Landa <landa.martin gmail.com> (various updates)
  15. """
  16. import os
  17. import sys
  18. import shutil
  19. import copy
  20. import platform
  21. import codecs
  22. import getpass
  23. from core import globalvar
  24. from core.utils import _
  25. import wx
  26. import wx.lib.mixins.listctrl as listmix
  27. from grass.script import core as grass
  28. from core.gcmd import GMessage, GError, DecodeString, RunCommand
  29. from core.utils import GetListOfLocations, GetListOfMapsets
  30. from startup.utils import (
  31. get_lockfile_if_present, get_possible_database_path)
  32. from location_wizard.dialogs import RegionDef
  33. from gui_core.dialogs import TextEntryDialog
  34. from gui_core.widgets import GenericValidator, StaticWrapText
  35. from gui_core.wrap import Button
  36. from gui_core.wrap import ListCtrl
  37. sys.stderr = codecs.getwriter('utf8')(sys.stderr)
  38. class GRASSStartup(wx.Frame):
  39. exit_success = 0
  40. # 2 is file not found from python interpreter
  41. exit_user_requested = 5
  42. """GRASS start-up screen"""
  43. def __init__(self, parent=None, id=wx.ID_ANY,
  44. style=wx.DEFAULT_FRAME_STYLE):
  45. #
  46. # GRASS variables
  47. #
  48. self.gisbase = os.getenv("GISBASE")
  49. self.grassrc = self._readGisRC()
  50. self.gisdbase = self.GetRCValue("GISDBASE")
  51. #
  52. # list of locations/mapsets
  53. #
  54. self.listOfLocations = []
  55. self.listOfMapsets = []
  56. self.listOfMapsetsSelectable = []
  57. wx.Frame.__init__(self, parent=parent, id=id, style=style)
  58. self.locale = wx.Locale(language=wx.LANGUAGE_DEFAULT)
  59. # scroll panel was used here but not properly and is probably not need
  60. # as long as it is not high too much
  61. self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  62. # i18N
  63. #
  64. # graphical elements
  65. #
  66. # image
  67. try:
  68. if os.getenv('ISISROOT'):
  69. name = os.path.join(
  70. globalvar.GUIDIR,
  71. "images",
  72. "startup_banner_isis.png")
  73. else:
  74. name = os.path.join(
  75. globalvar.GUIDIR, "images", "startup_banner.png")
  76. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
  77. wx.Bitmap(name=name,
  78. type=wx.BITMAP_TYPE_PNG))
  79. except:
  80. self.hbitmap = wx.StaticBitmap(
  81. self.panel, wx.ID_ANY, wx.BitmapFromImage(
  82. wx.EmptyImage(530, 150)))
  83. # labels
  84. # crashes when LOCATION doesn't exist
  85. # get version & revision
  86. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  87. versionLine = versionFile.readline().rstrip('\n')
  88. versionFile.close()
  89. try:
  90. grassVersion, grassRevision = versionLine.split(' ', 1)
  91. if grassVersion.endswith('svn'):
  92. grassRevisionStr = ' (%s)' % grassRevision
  93. else:
  94. grassRevisionStr = ''
  95. except ValueError:
  96. grassVersion = versionLine
  97. grassRevisionStr = ''
  98. self.gisdbase_box = wx.StaticBox(
  99. parent=self.panel, id=wx.ID_ANY, label=" %s " %
  100. _("1. Select GRASS GIS database directory"))
  101. self.location_box = wx.StaticBox(
  102. parent=self.panel, id=wx.ID_ANY, label=" %s " %
  103. _("2. Select GRASS Location"))
  104. self.mapset_box = wx.StaticBox(
  105. parent=self.panel, id=wx.ID_ANY, label=" %s " %
  106. _("3. Select GRASS Mapset"))
  107. self.lmessage = wx.StaticText(parent=self.panel)
  108. # It is not clear if all wx versions supports color, so try-except.
  109. # The color itself may not be correct for all platforms/system settings
  110. # but in http://xoomer.virgilio.it/infinity77/wxPython/Widgets/wx.SystemSettings.html
  111. # there is no 'warning' color.
  112. try:
  113. self.lmessage.SetForegroundColour(wx.Colour(255, 0, 0))
  114. except AttributeError:
  115. pass
  116. self.gisdbase_panel = wx.Panel(parent=self.panel)
  117. self.location_panel = wx.Panel(parent=self.panel)
  118. self.mapset_panel = wx.Panel(parent=self.panel)
  119. self.ldbase = wx.StaticText(
  120. parent=self.gisdbase_panel, id=wx.ID_ANY,
  121. label=_("GRASS GIS database directory contains Locations."))
  122. self.llocation = StaticWrapText(
  123. parent=self.location_panel, id=wx.ID_ANY,
  124. label=_("All data in one Location is in the same "
  125. " coordinate reference system (projection)."
  126. " One Location can be one project."
  127. " Location contains Mapsets."),
  128. style=wx.ALIGN_LEFT)
  129. self.lmapset = StaticWrapText(
  130. parent=self.mapset_panel, id=wx.ID_ANY,
  131. label=_("Mapset contains GIS data related"
  132. " to one project, task within one project,"
  133. " subregion or user."),
  134. style=wx.ALIGN_LEFT)
  135. try:
  136. for label in [self.ldbase, self.llocation, self.lmapset]:
  137. label.SetForegroundColour(
  138. wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT))
  139. except AttributeError:
  140. # for explanation of try-except see above
  141. pass
  142. # buttons
  143. self.bstart = Button(parent=self.panel, id=wx.ID_ANY,
  144. label=_("Start &GRASS session"))
  145. self.bstart.SetDefault()
  146. self.bexit = Button(parent=self.panel, id=wx.ID_EXIT)
  147. self.bstart.SetMinSize((180, self.bexit.GetSize()[1]))
  148. self.bhelp = Button(parent=self.panel, id=wx.ID_HELP)
  149. self.bbrowse = Button(parent=self.gisdbase_panel, id=wx.ID_ANY,
  150. label=_("&Browse"))
  151. self.bmapset = Button(parent=self.mapset_panel, id=wx.ID_ANY,
  152. # GTC New mapset
  153. label=_("&New"))
  154. self.bmapset.SetToolTip(_("Create a new Mapset in selected Location"))
  155. self.bwizard = Button(parent=self.location_panel, id=wx.ID_ANY,
  156. # GTC New location
  157. label=_("N&ew"))
  158. self.bwizard.SetToolTip(
  159. _(
  160. "Create a new location using location wizard."
  161. " After location is created successfully,"
  162. " GRASS session is started."))
  163. self.rename_location_button = Button(parent=self.location_panel, id=wx.ID_ANY,
  164. # GTC Rename location
  165. label=_("Ren&ame"))
  166. self.rename_location_button.SetToolTip(_("Rename selected location"))
  167. self.delete_location_button = Button(parent=self.location_panel, id=wx.ID_ANY,
  168. # GTC Delete location
  169. label=_("De&lete"))
  170. self.delete_location_button.SetToolTip(_("Delete selected location"))
  171. self.download_location_button = Button(parent=self.location_panel, id=wx.ID_ANY,
  172. label=_("Do&wnload"))
  173. self.download_location_button.SetToolTip(_("Download sample location"))
  174. self.rename_mapset_button = Button(parent=self.mapset_panel, id=wx.ID_ANY,
  175. # GTC Rename mapset
  176. label=_("&Rename"))
  177. self.rename_mapset_button.SetToolTip(_("Rename selected mapset"))
  178. self.delete_mapset_button = Button(parent=self.mapset_panel, id=wx.ID_ANY,
  179. # GTC Delete mapset
  180. label=_("&Delete"))
  181. self.delete_mapset_button.SetToolTip(_("Delete selected mapset"))
  182. # textinputs
  183. self.tgisdbase = wx.TextCtrl(
  184. parent=self.gisdbase_panel, id=wx.ID_ANY, value="", size=(
  185. 300, -1), style=wx.TE_PROCESS_ENTER)
  186. # Locations
  187. self.lblocations = GListBox(parent=self.location_panel,
  188. id=wx.ID_ANY, size=(180, 200),
  189. choices=self.listOfLocations)
  190. self.lblocations.SetColumnWidth(0, 180)
  191. # TODO: sort; but keep PERMANENT on top of list
  192. # Mapsets
  193. self.lbmapsets = GListBox(parent=self.mapset_panel,
  194. id=wx.ID_ANY, size=(180, 200),
  195. choices=self.listOfMapsets)
  196. self.lbmapsets.SetColumnWidth(0, 180)
  197. # layout & properties, first do layout so everything is created
  198. self._do_layout()
  199. self._set_properties(grassVersion, grassRevisionStr)
  200. # events
  201. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  202. self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
  203. self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
  204. self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
  205. self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
  206. self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
  207. self.rename_location_button.Bind(wx.EVT_BUTTON, self.RenameLocation)
  208. self.delete_location_button.Bind(wx.EVT_BUTTON, self.DeleteLocation)
  209. self.download_location_button.Bind(wx.EVT_BUTTON, self.DownloadLocation)
  210. self.rename_mapset_button.Bind(wx.EVT_BUTTON, self.RenameMapset)
  211. self.delete_mapset_button.Bind(wx.EVT_BUTTON, self.DeleteMapset)
  212. self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
  213. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
  214. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
  215. self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
  216. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  217. def _set_properties(self, version, revision):
  218. """Set frame properties
  219. :param version: Version in the form of X.Y.Z
  220. :param revision: Version control revision with leading space
  221. *revision* should be an empty string in case of release and
  222. otherwise it needs a leading space to be separated from the rest
  223. of the title.
  224. """
  225. self.SetTitle(_("GRASS GIS %s Startup%s") % (version, revision))
  226. self.SetIcon(wx.Icon(os.path.join(globalvar.ICONDIR, "grass.ico"),
  227. wx.BITMAP_TYPE_ICO))
  228. self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
  229. self.bstart.SetToolTip(_("Enter GRASS session"))
  230. self.bstart.Enable(False)
  231. self.bmapset.Enable(False)
  232. # this all was originally a choice, perhaps just mapset needed
  233. self.rename_location_button.Enable(False)
  234. self.delete_location_button.Enable(False)
  235. self.rename_mapset_button.Enable(False)
  236. self.delete_mapset_button.Enable(False)
  237. # set database
  238. if not self.gisdbase:
  239. # sets an initial path for gisdbase if nothing in GISRC
  240. if os.path.isdir(os.getenv("HOME")):
  241. self.gisdbase = os.getenv("HOME")
  242. else:
  243. self.gisdbase = os.getcwd()
  244. try:
  245. self.tgisdbase.SetValue(self.gisdbase)
  246. except UnicodeDecodeError:
  247. wx.MessageBox(parent=self, caption=_("Error"),
  248. message=_("Unable to set GRASS database. "
  249. "Check your locale settings."),
  250. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  251. self.OnSetDatabase(None)
  252. location = self.GetRCValue("LOCATION_NAME")
  253. if location == "<UNKNOWN>":
  254. return
  255. if not os.path.isdir(os.path.join(self.gisdbase, location)):
  256. location = None
  257. # list of locations
  258. self.UpdateLocations(self.gisdbase)
  259. try:
  260. self.lblocations.SetSelection(self.listOfLocations.index(location),
  261. force=True)
  262. self.lblocations.EnsureVisible(
  263. self.listOfLocations.index(location))
  264. except ValueError:
  265. sys.stderr.write(
  266. _("ERROR: Location <%s> not found\n") %
  267. self.GetRCValue("LOCATION_NAME"))
  268. if len(self.listOfLocations) > 0:
  269. self.lblocations.SetSelection(0, force=True)
  270. self.lblocations.EnsureVisible(0)
  271. location = self.listOfLocations[0]
  272. else:
  273. return
  274. # list of mapsets
  275. self.UpdateMapsets(os.path.join(self.gisdbase, location))
  276. mapset = self.GetRCValue("MAPSET")
  277. if mapset:
  278. try:
  279. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
  280. force=True)
  281. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  282. except ValueError:
  283. sys.stderr.write(_("ERROR: Mapset <%s> not found\n") % mapset)
  284. self.lbmapsets.SetSelection(0, force=True)
  285. self.lbmapsets.EnsureVisible(0)
  286. def _do_layout(self):
  287. sizer = wx.BoxSizer(wx.VERTICAL)
  288. self.sizer = sizer # for the layout call after changing message
  289. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  290. location_mapset_sizer = wx.BoxSizer(wx.HORIZONTAL)
  291. gisdbase_panel_sizer = wx.BoxSizer(wx.VERTICAL)
  292. gisdbase_boxsizer = wx.StaticBoxSizer(self.gisdbase_box, wx.VERTICAL)
  293. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  294. self.gisdbase_panel.SetSizer(gisdbase_panel_sizer)
  295. # gis data directory
  296. gisdbase_boxsizer.Add(self.gisdbase_panel, proportion=1,
  297. flag=wx.EXPAND | wx.ALL,
  298. border=1)
  299. gisdbase_panel_sizer.Add(dbase_sizer, proportion=1,
  300. flag=wx.EXPAND | wx.ALL,
  301. border=1)
  302. gisdbase_panel_sizer.Add(self.ldbase, proportion=0,
  303. flag=wx.EXPAND | wx.ALL,
  304. border=1)
  305. dbase_sizer.Add(self.tgisdbase, proportion=1,
  306. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  307. border=1)
  308. dbase_sizer.Add(self.bbrowse, proportion=0,
  309. flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  310. border=1)
  311. gisdbase_panel_sizer.Fit(self.gisdbase_panel)
  312. # location and mapset lists
  313. def layout_list_box(box, panel, list_box, buttons, description):
  314. panel_sizer = wx.BoxSizer(wx.VERTICAL)
  315. main_sizer = wx.BoxSizer(wx.HORIZONTAL)
  316. box_sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
  317. buttons_sizer = wx.BoxSizer(wx.VERTICAL)
  318. panel.SetSizer(panel_sizer)
  319. panel_sizer.Fit(panel)
  320. main_sizer.Add(list_box, proportion=1,
  321. flag=wx.EXPAND | wx.ALL,
  322. border=1)
  323. main_sizer.Add(buttons_sizer, proportion=0,
  324. flag=wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  325. border=1)
  326. for button in buttons:
  327. buttons_sizer.Add(button, proportion=0,
  328. flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,
  329. border=3)
  330. box_sizer.Add(panel, proportion=1,
  331. flag=wx.EXPAND | wx.ALL,
  332. border=1)
  333. panel_sizer.Add(main_sizer, proportion=1,
  334. flag=wx.EXPAND | wx.ALL,
  335. border=1)
  336. panel_sizer.Add(description, proportion=0,
  337. flag=wx.EXPAND | wx.ALL,
  338. border=1)
  339. return box_sizer
  340. location_boxsizer = layout_list_box(
  341. box=self.location_box,
  342. panel=self.location_panel,
  343. list_box=self.lblocations,
  344. buttons=[self.bwizard, self.rename_location_button,
  345. self.delete_location_button,
  346. self.download_location_button],
  347. description=self.llocation)
  348. mapset_boxsizer = layout_list_box(
  349. box=self.mapset_box,
  350. panel=self.mapset_panel,
  351. list_box=self.lbmapsets,
  352. buttons=[self.bmapset, self.rename_mapset_button,
  353. self.delete_mapset_button],
  354. description=self.lmapset)
  355. # location and mapset sizer
  356. location_mapset_sizer.Add(location_boxsizer, proportion=1,
  357. flag=wx.LEFT | wx.RIGHT | wx.EXPAND,
  358. border=3)
  359. location_mapset_sizer.Add(mapset_boxsizer, proportion=1,
  360. flag=wx.RIGHT | wx.EXPAND,
  361. border=3)
  362. # buttons
  363. btns_sizer.Add(self.bstart, proportion=0,
  364. flag=wx.ALIGN_CENTER_HORIZONTAL |
  365. wx.ALIGN_CENTER_VERTICAL |
  366. wx.ALL,
  367. border=5)
  368. btns_sizer.Add(self.bexit, proportion=0,
  369. flag=wx.ALIGN_CENTER_HORIZONTAL |
  370. wx.ALIGN_CENTER_VERTICAL |
  371. wx.ALL,
  372. border=5)
  373. btns_sizer.Add(self.bhelp, proportion=0,
  374. flag=wx.ALIGN_CENTER_HORIZONTAL |
  375. wx.ALIGN_CENTER_VERTICAL |
  376. wx.ALL,
  377. border=5)
  378. # main sizer
  379. sizer.Add(self.hbitmap,
  380. proportion=0,
  381. flag=wx.ALIGN_CENTER_VERTICAL |
  382. wx.ALIGN_CENTER_HORIZONTAL |
  383. wx.ALL,
  384. border=3) # image
  385. sizer.Add(gisdbase_boxsizer, proportion=0,
  386. flag=wx.ALIGN_CENTER_HORIZONTAL |
  387. wx.RIGHT | wx.LEFT | wx.TOP | wx.EXPAND,
  388. border=3) # GISDBASE setting
  389. # warning/error message
  390. sizer.Add(self.lmessage,
  391. proportion=0,
  392. flag=wx.ALIGN_CENTER_VERTICAL |
  393. wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, border=5)
  394. sizer.Add(location_mapset_sizer, proportion=1,
  395. flag=wx.RIGHT | wx.LEFT | wx.EXPAND,
  396. border=1)
  397. sizer.Add(btns_sizer, proportion=0,
  398. flag=wx.ALIGN_CENTER_VERTICAL |
  399. wx.ALIGN_CENTER_HORIZONTAL |
  400. wx.RIGHT | wx.LEFT,
  401. border=3)
  402. self.panel.SetAutoLayout(True)
  403. self.panel.SetSizer(sizer)
  404. sizer.Fit(self.panel)
  405. sizer.SetSizeHints(self)
  406. self.Layout()
  407. def _readGisRC(self):
  408. """Read variables from $HOME/.grass7/rc file
  409. """
  410. grassrc = {}
  411. gisrc = os.getenv("GISRC")
  412. if gisrc and os.path.isfile(gisrc):
  413. try:
  414. rc = open(gisrc, "r")
  415. for line in rc.readlines():
  416. try:
  417. key, val = line.split(":", 1)
  418. except ValueError as e:
  419. sys.stderr.write(
  420. _('Invalid line in GISRC file (%s):%s\n' % (e, line)))
  421. grassrc[key.strip()] = DecodeString(val.strip())
  422. finally:
  423. rc.close()
  424. return grassrc
  425. def _showWarning(self, text):
  426. """Displays a warning, hint or info message to the user.
  427. This function can be used for all kinds of messages except for
  428. error messages.
  429. .. note::
  430. There is no cleaning procedure. You should call _hideMessage when
  431. you know that there is everything correct now.
  432. """
  433. self.lmessage.SetLabel(text)
  434. self.lmessage.Wrap(self.GetClientSize()[0])
  435. self.sizer.Layout()
  436. def _showError(self, text):
  437. """Displays a error message to the user.
  438. This function should be used only when something serious and unexpected
  439. happens, otherwise _showWarning should be used.
  440. .. note::
  441. There is no cleaning procedure. You should call _hideMessage when
  442. you know that there is everything correct now.
  443. """
  444. self.lmessage.SetLabel(_("Error: {text}").format(text=text))
  445. self.lmessage.Wrap(self.GetClientSize()[0])
  446. self.sizer.Layout()
  447. def _hideMessage(self):
  448. """Clears/hides the error message."""
  449. # we do no hide widget
  450. # because we do not want the dialog to change the size
  451. self.lmessage.SetLabel("")
  452. self.sizer.Layout()
  453. def GetRCValue(self, value):
  454. """Return GRASS variable (read from GISRC)
  455. """
  456. if value in self.grassrc:
  457. return self.grassrc[value]
  458. else:
  459. return None
  460. def SuggestDatabase(self):
  461. """Suggest (set) possible GRASS Database value"""
  462. # only if nothing is set (<UNKNOWN> comes from init script)
  463. if self.GetRCValue("LOCATION_NAME") != "<UNKNOWN>":
  464. return
  465. path = get_possible_database_path()
  466. if path:
  467. try:
  468. self.tgisdbase.SetValue(path)
  469. except UnicodeDecodeError:
  470. # restore previous state
  471. # wizard gives error in this case, we just ignore
  472. path = None
  473. self.tgisdbase.SetValue(self.gisdbase)
  474. # if we still have path
  475. if path:
  476. self.gisdbase = path
  477. self.OnSetDatabase(None)
  478. else:
  479. # nothing found
  480. # TODO: should it be warning, hint or message?
  481. self._showWarning(_(
  482. 'GRASS needs a directory (GRASS database) '
  483. 'in which to store its data. '
  484. 'Create one now if you have not already done so. '
  485. 'A popular choice is "grassdata", located in '
  486. 'your home directory. '
  487. 'Press Browse button to select the directory.'))
  488. def OnWizard(self, event):
  489. """Location wizard started"""
  490. from location_wizard.wizard import LocationWizard
  491. gWizard = LocationWizard(parent=self,
  492. grassdatabase=self.tgisdbase.GetValue())
  493. if gWizard.location is not None:
  494. self.tgisdbase.SetValue(gWizard.grassdatabase)
  495. self.OnSetDatabase(None)
  496. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  497. self.lblocations.SetSelection(
  498. self.listOfLocations.index(
  499. gWizard.location))
  500. self.lbmapsets.SetSelection(0)
  501. self.SetLocation(self.gisdbase, gWizard.location, 'PERMANENT')
  502. if gWizard.georeffile:
  503. message = _("Do you want to import <%(name)s> to the newly created location?") % {
  504. 'name': gWizard.georeffile}
  505. dlg = wx.MessageDialog(parent=self, message=message, caption=_(
  506. "Import data?"), style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION)
  507. dlg.CenterOnParent()
  508. if dlg.ShowModal() == wx.ID_YES:
  509. self.ImportFile(gWizard.georeffile)
  510. dlg.Destroy()
  511. if gWizard.default_region:
  512. defineRegion = RegionDef(self, location=gWizard.location)
  513. defineRegion.CenterOnParent()
  514. defineRegion.ShowModal()
  515. defineRegion.Destroy()
  516. if gWizard.user_mapset:
  517. dlg = TextEntryDialog(
  518. parent=self,
  519. message=_("New mapset:"),
  520. caption=_("Create new mapset"),
  521. defaultValue=self._getDefaultMapsetName(),
  522. validator=GenericValidator(
  523. grass.legal_name,
  524. self._nameValidationFailed
  525. ),
  526. style=wx.OK | wx.CANCEL | wx.HELP
  527. )
  528. help = dlg.FindWindowById(wx.ID_HELP)
  529. help.Bind(wx.EVT_BUTTON, self.OnHelp)
  530. if dlg.ShowModal() == wx.ID_OK:
  531. mapsetName = dlg.GetValue()
  532. self.CreateNewMapset(mapsetName)
  533. def ImportFile(self, filePath):
  534. """Tries to import file as vector or raster.
  535. If successfull sets default region from imported map.
  536. """
  537. RunCommand('db.connect', flags='c')
  538. mapName = os.path.splitext(os.path.basename(filePath))[0]
  539. vectors = RunCommand('v.in.ogr', input=filePath, flags='l',
  540. read=True)
  541. wx.BeginBusyCursor()
  542. wx.Yield()
  543. if vectors:
  544. # vector detected
  545. returncode, error = RunCommand(
  546. 'v.in.ogr', input=filePath, output=mapName, flags='e',
  547. getErrorMsg=True)
  548. else:
  549. returncode, error = RunCommand(
  550. 'r.in.gdal', input=filePath, output=mapName, flags='e',
  551. getErrorMsg=True)
  552. wx.EndBusyCursor()
  553. if returncode != 0:
  554. GError(
  555. parent=self,
  556. message=_(
  557. "Import of <%(name)s> failed.\n"
  558. "Reason: %(msg)s") % ({
  559. 'name': filePath,
  560. 'msg': error}))
  561. else:
  562. GMessage(
  563. message=_(
  564. "Data file <%(name)s> imported successfully. "
  565. "The location's default region was set from this imported map.") % {
  566. 'name': filePath},
  567. parent=self)
  568. # the event can be refactored out by using lambda in bind
  569. def RenameMapset(self, event):
  570. """Rename selected mapset
  571. """
  572. location = self.listOfLocations[self.lblocations.GetSelection()]
  573. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  574. if mapset == 'PERMANENT':
  575. GMessage(
  576. parent=self, message=_(
  577. 'Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  578. 'This mapset cannot be renamed.'))
  579. return
  580. dlg = TextEntryDialog(
  581. parent=self,
  582. message=_('Current name: %s\n\nEnter new name:') %
  583. mapset,
  584. caption=_('Rename selected mapset'),
  585. validator=GenericValidator(
  586. grass.legal_name,
  587. self._nameValidationFailed))
  588. if dlg.ShowModal() == wx.ID_OK:
  589. newmapset = dlg.GetValue()
  590. if newmapset == mapset:
  591. dlg.Destroy()
  592. return
  593. if newmapset in self.listOfMapsets:
  594. wx.MessageBox(
  595. parent=self, caption=_('Message'), message=_(
  596. 'Unable to rename mapset.\n\n'
  597. 'Mapset <%s> already exists in location.') %
  598. newmapset, style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  599. else:
  600. try:
  601. os.rename(os.path.join(self.gisdbase, location, mapset),
  602. os.path.join(self.gisdbase, location, newmapset))
  603. self.OnSelectLocation(None)
  604. self.lbmapsets.SetSelection(
  605. self.listOfMapsets.index(newmapset))
  606. except Exception as e:
  607. wx.MessageBox(
  608. parent=self,
  609. caption=_('Error'),
  610. message=_('Unable to rename mapset.\n\n%s') %
  611. e,
  612. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  613. dlg.Destroy()
  614. def RenameLocation(self, event):
  615. """Rename selected location
  616. """
  617. location = self.listOfLocations[self.lblocations.GetSelection()]
  618. dlg = TextEntryDialog(
  619. parent=self,
  620. message=_('Current name: %s\n\nEnter new name:') %
  621. location,
  622. caption=_('Rename selected location'),
  623. validator=GenericValidator(
  624. grass.legal_name,
  625. self._nameValidationFailed))
  626. if dlg.ShowModal() == wx.ID_OK:
  627. newlocation = dlg.GetValue()
  628. if newlocation == location:
  629. dlg.Destroy()
  630. return
  631. if newlocation in self.listOfLocations:
  632. wx.MessageBox(
  633. parent=self, caption=_('Message'), message=_(
  634. 'Unable to rename location.\n\n'
  635. 'Location <%s> already exists in GRASS database.') %
  636. newlocation, style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  637. else:
  638. try:
  639. os.rename(os.path.join(self.gisdbase, location),
  640. os.path.join(self.gisdbase, newlocation))
  641. self.UpdateLocations(self.gisdbase)
  642. self.lblocations.SetSelection(
  643. self.listOfLocations.index(newlocation))
  644. self.UpdateMapsets(newlocation)
  645. except Exception as e:
  646. wx.MessageBox(
  647. parent=self,
  648. caption=_('Error'),
  649. message=_('Unable to rename location.\n\n%s') %
  650. e,
  651. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  652. dlg.Destroy()
  653. def DeleteMapset(self, event):
  654. """Delete selected mapset
  655. """
  656. location = self.listOfLocations[self.lblocations.GetSelection()]
  657. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  658. if mapset == 'PERMANENT':
  659. GMessage(
  660. parent=self, message=_(
  661. 'Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  662. 'This mapset cannot be deleted.'))
  663. return
  664. dlg = wx.MessageDialog(
  665. parent=self,
  666. message=_(
  667. "Do you want to continue with deleting mapset <%(mapset)s> "
  668. "from location <%(location)s>?\n\n"
  669. "ALL MAPS included in this mapset will be "
  670. "PERMANENTLY DELETED!") %
  671. {'mapset': mapset, 'location': location},
  672. caption=_("Delete selected mapset"),
  673. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  674. if dlg.ShowModal() == wx.ID_YES:
  675. try:
  676. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  677. self.OnSelectLocation(None)
  678. self.lbmapsets.SetSelection(0)
  679. except:
  680. wx.MessageBox(message=_('Unable to delete mapset'))
  681. dlg.Destroy()
  682. def DeleteLocation(self, event):
  683. """
  684. Delete selected location
  685. """
  686. location = self.listOfLocations[self.lblocations.GetSelection()]
  687. dlg = wx.MessageDialog(
  688. parent=self,
  689. message=_(
  690. "Do you want to continue with deleting "
  691. "location <%s>?\n\n"
  692. "ALL MAPS included in this location will be "
  693. "PERMANENTLY DELETED!") %
  694. (location),
  695. caption=_("Delete selected location"),
  696. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  697. if dlg.ShowModal() == wx.ID_YES:
  698. try:
  699. shutil.rmtree(os.path.join(self.gisdbase, location))
  700. self.UpdateLocations(self.gisdbase)
  701. self.lblocations.SetSelection(0)
  702. self.OnSelectLocation(None)
  703. self.lbmapsets.SetSelection(0)
  704. except:
  705. wx.MessageBox(message=_('Unable to delete location'))
  706. dlg.Destroy()
  707. def DownloadLocation(self, event):
  708. """Download location online"""
  709. from startup.locdownload import LocationDownloadDialog
  710. loc_download = LocationDownloadDialog(parent=self, database=self.gisdbase)
  711. loc_download.ShowModal()
  712. location = loc_download.GetLocation()
  713. if location:
  714. # get the new location to the list
  715. self.UpdateLocations(self.gisdbase)
  716. # seems to be used in similar context
  717. self.UpdateMapsets(os.path.join(self.gisdbase, location))
  718. self.lblocations.SetSelection(
  719. self.listOfLocations.index(location))
  720. # wizard does this as well, not sure if needed
  721. self.SetLocation(self.gisdbase, location, 'PERMANENT')
  722. # seems to be used in similar context
  723. self.OnSelectLocation(None)
  724. loc_download.Destroy()
  725. def UpdateLocations(self, dbase):
  726. """Update list of locations"""
  727. try:
  728. self.listOfLocations = GetListOfLocations(dbase)
  729. except (UnicodeEncodeError, UnicodeDecodeError) as e:
  730. GError(parent=self,
  731. message=_("Unicode error detected. "
  732. "Check your locale settings. Details: {0}").format(e),
  733. showTraceback=False)
  734. self.lblocations.Clear()
  735. self.lblocations.InsertItems(self.listOfLocations, 0)
  736. if len(self.listOfLocations) > 0:
  737. self._hideMessage()
  738. self.lblocations.SetSelection(0)
  739. else:
  740. self.lblocations.SetSelection(wx.NOT_FOUND)
  741. self._showWarning(_("No GRASS Location found in '%s'."
  742. " Create a new Location or choose different"
  743. " GRASS database directory.")
  744. % self.gisdbase)
  745. return self.listOfLocations
  746. def UpdateMapsets(self, location):
  747. """Update list of mapsets"""
  748. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  749. self.listOfMapsetsSelectable = list()
  750. self.listOfMapsets = GetListOfMapsets(self.gisdbase, location)
  751. self.lbmapsets.Clear()
  752. # disable mapset with denied permission
  753. locationName = os.path.basename(location)
  754. ret = RunCommand('g.mapset',
  755. read=True,
  756. flags='l',
  757. location=locationName,
  758. gisdbase=self.gisdbase)
  759. if ret:
  760. for line in ret.splitlines():
  761. self.listOfMapsetsSelectable += line.split(' ')
  762. else:
  763. RunCommand("g.gisenv",
  764. set="GISDBASE=%s" % self.gisdbase)
  765. RunCommand("g.gisenv",
  766. set="LOCATION_NAME=%s" % locationName)
  767. RunCommand("g.gisenv",
  768. set="MAPSET=PERMANENT")
  769. # first run only
  770. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  771. disabled = []
  772. idx = 0
  773. for mapset in self.listOfMapsets:
  774. if mapset not in self.listOfMapsetsSelectable or \
  775. get_lockfile_if_present(self.gisdbase,
  776. locationName, mapset):
  777. disabled.append(idx)
  778. idx += 1
  779. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  780. return self.listOfMapsets
  781. def OnSelectLocation(self, event):
  782. """Location selected"""
  783. if event:
  784. self.lblocations.SetSelection(event.GetIndex())
  785. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  786. self.UpdateMapsets(
  787. os.path.join(
  788. self.gisdbase,
  789. self.listOfLocations[
  790. self.lblocations.GetSelection()]))
  791. else:
  792. self.listOfMapsets = []
  793. disabled = []
  794. idx = 0
  795. try:
  796. locationName = self.listOfLocations[
  797. self.lblocations.GetSelection()]
  798. except IndexError:
  799. locationName = ''
  800. for mapset in self.listOfMapsets:
  801. if mapset not in self.listOfMapsetsSelectable or \
  802. get_lockfile_if_present(self.gisdbase,
  803. locationName, mapset):
  804. disabled.append(idx)
  805. idx += 1
  806. self.lbmapsets.Clear()
  807. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  808. if len(self.listOfMapsets) > 0:
  809. self.lbmapsets.SetSelection(0)
  810. if locationName:
  811. # enable start button when location and mapset is selected
  812. self.bstart.Enable()
  813. self.bstart.SetFocus()
  814. self.bmapset.Enable()
  815. # replacing disabled choice, perhaps just mapset needed
  816. self.rename_location_button.Enable()
  817. self.delete_location_button.Enable()
  818. self.rename_mapset_button.Enable()
  819. self.delete_mapset_button.Enable()
  820. else:
  821. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  822. self.bstart.Enable(False)
  823. self.bmapset.Enable(False)
  824. # this all was originally a choice, perhaps just mapset needed
  825. self.rename_location_button.Enable(False)
  826. self.delete_location_button.Enable(False)
  827. self.rename_mapset_button.Enable(False)
  828. self.delete_mapset_button.Enable(False)
  829. def OnSelectMapset(self, event):
  830. """Mapset selected"""
  831. self.lbmapsets.SetSelection(event.GetIndex())
  832. if event.GetText() not in self.listOfMapsetsSelectable:
  833. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  834. else:
  835. self.FormerMapsetSelection = event.GetIndex()
  836. event.Skip()
  837. def OnSetDatabase(self, event):
  838. """Database set"""
  839. gisdbase = self.tgisdbase.GetValue()
  840. self._hideMessage()
  841. if not os.path.exists(gisdbase):
  842. self._showError(_("Path '%s' doesn't exist.") % gisdbase)
  843. return
  844. self.gisdbase = self.tgisdbase.GetValue()
  845. self.UpdateLocations(self.gisdbase)
  846. self.OnSelectLocation(None)
  847. def OnBrowse(self, event):
  848. """'Browse' button clicked"""
  849. if not event:
  850. defaultPath = os.getenv('HOME')
  851. else:
  852. defaultPath = ""
  853. dlg = wx.DirDialog(parent=self, message=_("Choose GIS Data Directory"),
  854. defaultPath=defaultPath, style=wx.DD_DEFAULT_STYLE)
  855. if dlg.ShowModal() == wx.ID_OK:
  856. self.gisdbase = dlg.GetPath()
  857. self.tgisdbase.SetValue(self.gisdbase)
  858. self.OnSetDatabase(event)
  859. dlg.Destroy()
  860. def OnCreateMapset(self, event):
  861. """Create new mapset"""
  862. dlg = TextEntryDialog(
  863. parent=self,
  864. message=_('Enter name for new mapset:'),
  865. caption=_('Create new mapset'),
  866. defaultValue=self._getDefaultMapsetName(),
  867. validator=GenericValidator(
  868. grass.legal_name,
  869. self._nameValidationFailed))
  870. if dlg.ShowModal() == wx.ID_OK:
  871. mapset = dlg.GetValue()
  872. return self.CreateNewMapset(mapset=mapset)
  873. else:
  874. return False
  875. def CreateNewMapset(self, mapset):
  876. if mapset in self.listOfMapsets:
  877. GMessage(parent=self,
  878. message=_("Mapset <%s> already exists.") % mapset)
  879. return False
  880. if mapset.lower() == 'ogr':
  881. dlg1 = wx.MessageDialog(
  882. parent=self,
  883. message=_(
  884. "Mapset <%s> is reserved for direct "
  885. "read access to OGR layers. Please consider to use "
  886. "another name for your mapset.\n\n"
  887. "Are you really sure that you want to create this mapset?") %
  888. mapset,
  889. caption=_("Reserved mapset name"),
  890. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  891. ret = dlg1.ShowModal()
  892. dlg1.Destroy()
  893. if ret == wx.ID_NO:
  894. dlg1.Destroy()
  895. return False
  896. try:
  897. self.gisdbase = self.tgisdbase.GetValue()
  898. location = self.listOfLocations[self.lblocations.GetSelection()]
  899. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  900. # copy WIND file and its permissions from PERMANENT and set
  901. # permissions to u+rw,go+r
  902. shutil.copy(
  903. os.path.join(
  904. self.gisdbase,
  905. location,
  906. 'PERMANENT',
  907. 'WIND'),
  908. os.path.join(
  909. self.gisdbase,
  910. location,
  911. mapset))
  912. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  913. self.OnSelectLocation(None)
  914. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  915. self.bstart.SetFocus()
  916. return True
  917. except Exception as e:
  918. GError(parent=self,
  919. message=_("Unable to create new mapset: %s") % e,
  920. showTraceback=False)
  921. return False
  922. def OnStart(self, event):
  923. """'Start GRASS' button clicked"""
  924. dbase = self.tgisdbase.GetValue()
  925. location = self.listOfLocations[self.lblocations.GetSelection()]
  926. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  927. lockfile = get_lockfile_if_present(dbase, location, mapset)
  928. if lockfile:
  929. dlg = wx.MessageDialog(
  930. parent=self,
  931. message=_(
  932. "GRASS is already running in selected mapset <%(mapset)s>\n"
  933. "(file %(lock)s found).\n\n"
  934. "Concurrent use not allowed.\n\n"
  935. "Do you want to try to remove .gislock (note that you "
  936. "need permission for this operation) and continue?") %
  937. {'mapset': mapset, 'lock': lockfile},
  938. caption=_("Lock file found"),
  939. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION | wx.CENTRE)
  940. ret = dlg.ShowModal()
  941. dlg.Destroy()
  942. if ret == wx.ID_YES:
  943. dlg1 = wx.MessageDialog(
  944. parent=self,
  945. message=_(
  946. "ARE YOU REALLY SURE?\n\n"
  947. "If you really are running another GRASS session doing this "
  948. "could corrupt your data. Have another look in the processor "
  949. "manager just to be sure..."),
  950. caption=_("Lock file found"),
  951. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION | wx.CENTRE)
  952. ret = dlg1.ShowModal()
  953. dlg1.Destroy()
  954. if ret == wx.ID_YES:
  955. try:
  956. os.remove(lockfile)
  957. except IOError as e:
  958. GError(_("Unable to remove '%(lock)s'.\n\n"
  959. "Details: %(reason)s") % {'lock': lockfile, 'reason': e})
  960. else:
  961. return
  962. else:
  963. return
  964. self.SetLocation(dbase, location, mapset)
  965. self.ExitSuccessfully()
  966. def SetLocation(self, dbase, location, mapset):
  967. RunCommand("g.gisenv",
  968. set="GISDBASE=%s" % dbase)
  969. RunCommand("g.gisenv",
  970. set="LOCATION_NAME=%s" % location)
  971. RunCommand("g.gisenv",
  972. set="MAPSET=%s" % mapset)
  973. def _getDefaultMapsetName(self):
  974. """Returns default name for mapset."""
  975. try:
  976. defaultName = getpass.getuser()
  977. # raise error if not ascii (not valid mapset name)
  978. defaultName.encode('ascii')
  979. except: # whatever might go wrong
  980. defaultName = 'user'
  981. return defaultName
  982. def ExitSuccessfully(self):
  983. self.Destroy()
  984. sys.exit(self.exit_success)
  985. def OnExit(self, event):
  986. """'Exit' button clicked"""
  987. self.Destroy()
  988. sys.exit(self.exit_user_requested)
  989. def OnHelp(self, event):
  990. """'Help' button clicked"""
  991. # help text in lib/init/helptext.html
  992. RunCommand('g.manual', entry='helptext')
  993. def OnCloseWindow(self, event):
  994. """Close window event"""
  995. event.Skip()
  996. sys.exit(self.exit_user_requested)
  997. def _nameValidationFailed(self, ctrl):
  998. message = _(
  999. "Name <%(name)s> is not a valid name for location or mapset. "
  1000. "Please use only ASCII characters excluding %(chars)s "
  1001. "and space.") % {
  1002. 'name': ctrl.GetValue(),
  1003. 'chars': '/"\'@,=*~'}
  1004. GError(parent=self, message=message, caption=_("Invalid name"))
  1005. class GListBox(ListCtrl, listmix.ListCtrlAutoWidthMixin):
  1006. """Use wx.ListCtrl instead of wx.ListBox, different style for
  1007. non-selectable items (e.g. mapsets with denied permission)"""
  1008. def __init__(self, parent, id, size,
  1009. choices, disabled=[]):
  1010. ListCtrl.__init__(
  1011. self, parent, id, size=size, style=wx.LC_REPORT | wx.LC_NO_HEADER |
  1012. wx.LC_SINGLE_SEL | wx.BORDER_SUNKEN)
  1013. listmix.ListCtrlAutoWidthMixin.__init__(self)
  1014. self.InsertColumn(0, '')
  1015. self.selected = wx.NOT_FOUND
  1016. self._LoadData(choices, disabled)
  1017. def _LoadData(self, choices, disabled=[]):
  1018. """Load data into list
  1019. :param choices: list of item
  1020. :param disabled: list of indices of non-selectable items
  1021. """
  1022. idx = 0
  1023. count = self.GetItemCount()
  1024. for item in choices:
  1025. index = self.InsertStringItem(count + idx, item)
  1026. self.SetStringItem(index, 0, item)
  1027. if idx in disabled:
  1028. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  1029. idx += 1
  1030. def Clear(self):
  1031. self.DeleteAllItems()
  1032. def InsertItems(self, choices, pos, disabled=[]):
  1033. self._LoadData(choices, disabled)
  1034. def SetSelection(self, item, force=False):
  1035. if item != wx.NOT_FOUND and \
  1036. (platform.system() != 'Windows' or force):
  1037. # Windows -> FIXME
  1038. self.SetItemState(
  1039. item,
  1040. wx.LIST_STATE_SELECTED,
  1041. wx.LIST_STATE_SELECTED)
  1042. self.selected = item
  1043. def GetSelection(self):
  1044. return self.selected
  1045. class StartUp(wx.App):
  1046. """Start-up application"""
  1047. def OnInit(self):
  1048. if not globalvar.CheckWxVersion([2, 9]):
  1049. wx.InitAllImageHandlers()
  1050. StartUp = GRASSStartup()
  1051. StartUp.CenterOnScreen()
  1052. self.SetTopWindow(StartUp)
  1053. StartUp.Show()
  1054. StartUp.SuggestDatabase()
  1055. return 1
  1056. if __name__ == "__main__":
  1057. if os.getenv("GISBASE") is None:
  1058. sys.exit("Failed to start GUI, GRASS GIS is not running.")
  1059. GRASSStartUp = StartUp(0)
  1060. GRASSStartUp.MainLoop()