gis_set.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. """!
  2. @package gis_set.py
  3. GRASS start-up screen.
  4. Initialization module for wxPython GRASS GUI.
  5. Location/mapset management (selection, creation, etc.).
  6. Classes:
  7. - GRASSStartup
  8. - StartUp
  9. (C) 2006-2010 by the GRASS Development Team
  10. This program is free software under the GNU General Public License
  11. (>=v2). Read the file COPYING that comes with GRASS for details.
  12. @author Michael Barton and Jachym Cepicky (original author)
  13. @author Martin Landa <landa.martin gmail.com> (various updates)
  14. """
  15. import os
  16. import sys
  17. import glob
  18. import shutil
  19. import copy
  20. import platform
  21. ### i18N
  22. import gettext
  23. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  24. from gui_modules import globalvar
  25. if not os.getenv("GRASS_WXBUNDLED"):
  26. globalvar.CheckForWx()
  27. from gui_modules import help
  28. import wx
  29. import wx.html
  30. import wx.lib.rcsizer as rcs
  31. import wx.lib.filebrowsebutton as filebrowse
  32. import wx.lib.mixins.listctrl as listmix
  33. import wx.lib.scrolledpanel as scrolled
  34. class GRASSStartup(wx.Frame):
  35. """!GRASS start-up screen"""
  36. def __init__(self, parent=None, id=wx.ID_ANY, style=wx.DEFAULT_FRAME_STYLE):
  37. #
  38. # GRASS variables
  39. #
  40. self.gisbase = os.getenv("GISBASE")
  41. self.grassrc = self._read_grassrc()
  42. self.gisdbase = self.GetRCValue("GISDBASE")
  43. #
  44. # list of locations/mapsets
  45. #
  46. self.listOfLocations = []
  47. self.listOfMapsets = []
  48. self.listOfMapsetsSelectable = []
  49. wx.Frame.__init__(self, parent=parent, id=id, style=style)
  50. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  51. # self.panel = wx.Panel(parent=self, id=wx.ID_ANY)
  52. self.panel = scrolled.ScrolledPanel(parent = self, id = wx.ID_ANY)
  53. # i18N
  54. import gettext
  55. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  56. #
  57. # graphical elements
  58. #
  59. # image
  60. try:
  61. name = os.path.join(globalvar.ETCDIR, "gui", "images", "startup_banner.png")
  62. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY,
  63. wx.Bitmap(name=name,
  64. type=wx.BITMAP_TYPE_PNG))
  65. except:
  66. self.hbitmap = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(530,150))
  67. # labels
  68. ### crashes when LOCATION doesn't exist
  69. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  70. grassVersion = versionFile.readline().split(' ')[0].rstrip('\n')
  71. versionFile.close()
  72. self.select_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  73. label=" %s " % _("Choose project location and mapset"))
  74. self.manage_box = wx.StaticBox (parent=self.panel, id=wx.ID_ANY,
  75. label=" %s " % _("Manage"))
  76. self.lwelcome = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  77. label=_("Welcome to GRASS GIS %s\n"
  78. "The world's leading open source GIS") % grassVersion,
  79. style=wx.ALIGN_CENTRE)
  80. self.ltitle = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  81. label=_("Select an existing project location and mapset\n"
  82. "or define a new location"),
  83. style=wx.ALIGN_CENTRE)
  84. self.ldbase = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  85. label=_("GIS Data Directory:"))
  86. self.llocation = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  87. label=_("Project location\n(projection/coordinate system)"),
  88. style=wx.ALIGN_CENTRE)
  89. self.lmapset = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  90. label=_("Accessible mapsets\n(directories of GIS files)"),
  91. style=wx.ALIGN_CENTRE)
  92. self.lcreate = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  93. label=_("Create new mapset\nin selected location"),
  94. style=wx.ALIGN_CENTRE)
  95. self.ldefine = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  96. label=_("Define new location"),
  97. style=wx.ALIGN_CENTRE)
  98. self.lmanageloc = wx.StaticText(parent=self.panel, id=wx.ID_ANY,
  99. label=_("Rename/delete selected\nmapset or location"),
  100. style=wx.ALIGN_CENTRE)
  101. # buttons
  102. self.bstart = wx.Button(parent=self.panel, id=wx.ID_ANY,
  103. label=_("Start GRASS"))
  104. self.bstart.SetDefault()
  105. self.bexit = wx.Button(parent=self.panel, id=wx.ID_EXIT)
  106. self.bstart.SetMinSize((180, self.bexit.GetSize()[1]))
  107. self.bhelp = wx.Button(parent=self.panel, id=wx.ID_HELP)
  108. self.bbrowse = wx.Button(parent=self.panel, id=wx.ID_ANY,
  109. label=_("Browse"))
  110. self.bmapset = wx.Button(parent=self.panel, id=wx.ID_ANY,
  111. label=_("Create mapset"))
  112. self.bwizard = wx.Button(parent=self.panel, id=wx.ID_ANY,
  113. label=_("Location wizard"))
  114. self.manageloc = wx.Choice(parent=self.panel, id=wx.ID_ANY,
  115. choices=[_('Rename mapset'), _('Rename location'),
  116. _('Delete mapset'), _('Delete location')])
  117. self.manageloc.SetSelection(0)
  118. # textinputs
  119. self.tgisdbase = wx.TextCtrl(parent=self.panel, id=wx.ID_ANY, value="", size=(300, -1),
  120. style=wx.TE_PROCESS_ENTER)
  121. # Locations
  122. self.lpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  123. self.lblocations = GListBox(parent=self.lpanel,
  124. id=wx.ID_ANY, size=(180, 200),
  125. choices=self.listOfLocations)
  126. self.lblocations.SetColumnWidth(0, 180)
  127. # TODO: sort; but keep PERMANENT on top of list
  128. # Mapsets
  129. self.mpanel = wx.Panel(parent=self.panel, id=wx.ID_ANY)
  130. self.lbmapsets = GListBox(parent=self.mpanel,
  131. id=wx.ID_ANY, size=(180, 200),
  132. choices=self.listOfMapsets)
  133. self.lbmapsets.SetColumnWidth(0, 180)
  134. # layout & properties
  135. self._set_properties()
  136. self._do_layout()
  137. # events
  138. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  139. self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
  140. self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
  141. self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
  142. self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
  143. self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
  144. self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
  145. self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
  146. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
  147. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
  148. self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
  149. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  150. def _set_properties(self):
  151. """!Set frame properties"""
  152. self.SetTitle(_("Welcome to GRASS GIS"))
  153. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
  154. wx.BITMAP_TYPE_ICO))
  155. self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
  156. self.lwelcome.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  157. self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
  158. self.bstart.SetToolTipString(_("Enter GRASS session"))
  159. self.bstart.Enable(False)
  160. self.bmapset.Enable(False)
  161. self.manageloc.Enable(False)
  162. # set database
  163. if not self.gisdbase:
  164. # sets an initial path for gisdbase if nothing in GISRC
  165. if os.path.isdir(os.getenv("HOME")):
  166. self.gisdbase = os.getenv("HOME")
  167. else:
  168. self.gisdbase = os.getcwd()
  169. try:
  170. self.tgisdbase.SetValue(self.gisdbase)
  171. except UnicodeDecodeError:
  172. wx.MessageBox(parent = self, caption = _("Error"),
  173. message = _("Unable to set GRASS database. "
  174. "Check your locale settings."),
  175. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  176. self.OnSetDatabase(None)
  177. location = self.GetRCValue("LOCATION_NAME")
  178. if location == "<UNKNOWN>" or \
  179. not os.path.isdir(os.path.join(self.gisdbase, location)):
  180. location = None
  181. if location:
  182. # list of locations
  183. self.UpdateLocations(self.gisdbase)
  184. try:
  185. self.lblocations.SetSelection(self.listOfLocations.index(location),
  186. force=True)
  187. self.lblocations.EnsureVisible(self.listOfLocations.index(location))
  188. except ValueError:
  189. print >> sys.stderr, _("ERROR: Location <%s> not found") % \
  190. (location)
  191. # list of mapsets
  192. self.UpdateMapsets(os.path.join(self.gisdbase,location))
  193. mapset = self.GetRCValue("MAPSET")
  194. if mapset:
  195. try:
  196. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
  197. force=True)
  198. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  199. except ValueError:
  200. self.lbmapsets.Clear()
  201. print >> sys.stderr, _("ERROR: Mapset <%s> not found") % \
  202. (mapset)
  203. # self.bstart.Enable(True)
  204. def _do_layout(self):
  205. label_style = wx.ADJUST_MINSIZE | wx.ALIGN_CENTER_HORIZONTAL
  206. sizer = wx.BoxSizer(wx.VERTICAL)
  207. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  208. location_sizer = wx.FlexGridSizer(rows=1, cols=2, vgap=4, hgap=4)
  209. select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
  210. select_sizer = wx.FlexGridSizer(rows=2, cols=2, vgap=4, hgap=4)
  211. manage_boxsizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
  212. manage_sizer = wx.BoxSizer(wx.VERTICAL)
  213. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  214. # gis data directory
  215. dbase_sizer.Add(item=self.ldbase, proportion=0,
  216. flag=wx.ALIGN_CENTER_VERTICAL |
  217. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  218. border=3)
  219. dbase_sizer.Add(item=self.tgisdbase, proportion=0,
  220. flag=wx.ALIGN_CENTER_VERTICAL |
  221. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  222. border=3)
  223. dbase_sizer.Add(item=self.bbrowse, proportion=0,
  224. flag=wx.ALIGN_CENTER_VERTICAL |
  225. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  226. border=3)
  227. # select sizer
  228. select_sizer.Add(item=self.llocation, proportion=0,
  229. flag=label_style | wx.ALL,
  230. border=3)
  231. select_sizer.Add(item=self.lmapset, proportion=0,
  232. flag=label_style | wx.ALL,
  233. border=3)
  234. select_sizer.Add(item=self.lpanel, proportion=0,
  235. flag=wx.ADJUST_MINSIZE |
  236. wx.ALIGN_CENTER_VERTICAL |
  237. wx.ALIGN_CENTER_HORIZONTAL)
  238. select_sizer.Add(item=self.mpanel, proportion=0,
  239. flag=wx.ADJUST_MINSIZE |
  240. wx.ALIGN_CENTER_VERTICAL |
  241. wx.ALIGN_CENTER_HORIZONTAL)
  242. select_boxsizer.Add(item=select_sizer, proportion=0)
  243. # define new location and mapset
  244. manage_sizer.Add(item=self.ldefine, proportion=0,
  245. flag=label_style | wx.ALL,
  246. border=3)
  247. manage_sizer.Add(item=self.bwizard, proportion=0,
  248. flag=label_style | wx.BOTTOM,
  249. border=5)
  250. manage_sizer.Add(item=self.lcreate, proportion=0,
  251. flag=label_style | wx.ALL,
  252. border=3)
  253. manage_sizer.Add(item=self.bmapset, proportion=0,
  254. flag=label_style | wx.BOTTOM,
  255. border=5)
  256. manage_sizer.Add(item=self.lmanageloc, proportion=0,
  257. flag=label_style | wx.ALL,
  258. border=3)
  259. manage_sizer.Add(item=self.manageloc, proportion=0,
  260. flag=label_style | wx.BOTTOM,
  261. border=5)
  262. manage_boxsizer.Add(item=manage_sizer, proportion=0)
  263. # location sizer
  264. location_sizer.Add(item=select_boxsizer, proportion=0,
  265. flag=wx.ADJUST_MINSIZE |
  266. wx.ALIGN_CENTER_VERTICAL |
  267. wx.ALIGN_CENTER_HORIZONTAL |
  268. wx.RIGHT | wx.LEFT | wx.EXPAND,
  269. border=3) # GISDBASE setting
  270. location_sizer.Add(item=manage_boxsizer, proportion=0,
  271. flag=wx.ADJUST_MINSIZE |
  272. wx.ALIGN_TOP |
  273. wx.ALIGN_CENTER_HORIZONTAL |
  274. wx.RIGHT | wx.EXPAND,
  275. border=3)
  276. # buttons
  277. btns_sizer.Add(item=self.bstart, proportion=0,
  278. flag=wx.ALIGN_CENTER_HORIZONTAL |
  279. wx.ALIGN_CENTER_VERTICAL |
  280. wx.ALL,
  281. border=5)
  282. btns_sizer.Add(item=self.bexit, proportion=0,
  283. flag=wx.ALIGN_CENTER_HORIZONTAL |
  284. wx.ALIGN_CENTER_VERTICAL |
  285. wx.ALL,
  286. border=5)
  287. btns_sizer.Add(item=self.bhelp, proportion=0,
  288. flag=wx.ALIGN_CENTER_HORIZONTAL |
  289. wx.ALIGN_CENTER_VERTICAL |
  290. wx.ALL,
  291. border=5)
  292. # main sizer
  293. sizer.Add(item=self.hbitmap,
  294. proportion=0,
  295. flag=wx.ALIGN_CENTER_VERTICAL |
  296. wx.ALIGN_CENTER_HORIZONTAL |
  297. wx.ALL,
  298. border=3) # image
  299. sizer.Add(item=self.lwelcome, # welcome message
  300. proportion=0,
  301. flag=wx.ALIGN_CENTER_VERTICAL |
  302. wx.ALIGN_CENTER_HORIZONTAL |
  303. wx.BOTTOM,
  304. border=5)
  305. sizer.Add(item=self.ltitle, # title
  306. proportion=0,
  307. flag=wx.ALIGN_CENTER_VERTICAL |
  308. wx.ALIGN_CENTER_HORIZONTAL)
  309. sizer.Add(item=dbase_sizer, proportion=0,
  310. flag=wx.ALIGN_CENTER_HORIZONTAL |
  311. wx.RIGHT | wx.LEFT,
  312. border=1) # GISDBASE setting
  313. sizer.Add(item=location_sizer, proportion=1,
  314. flag=wx.ALIGN_CENTER_VERTICAL |
  315. wx.ALIGN_CENTER_HORIZONTAL |
  316. wx.RIGHT | wx.LEFT,
  317. border=1)
  318. sizer.Add(item=btns_sizer, proportion=0,
  319. flag=wx.ALIGN_CENTER_VERTICAL |
  320. wx.ALIGN_CENTER_HORIZONTAL |
  321. wx.RIGHT | wx.LEFT,
  322. border=1)
  323. self.panel.SetAutoLayout(True)
  324. self.panel.SetSizer(sizer)
  325. sizer.Fit(self.panel)
  326. sizer.SetSizeHints(self)
  327. self.Layout()
  328. def _read_grassrc(self):
  329. """!Read variables from $HOME/.grass7/rc file
  330. """
  331. grassrc = {}
  332. gisrc = os.getenv("GISRC")
  333. if gisrc and os.path.isfile(gisrc):
  334. try:
  335. rc = open(gisrc, "r")
  336. for line in rc.readlines():
  337. key, val = line.split(":", 1)
  338. grassrc[key.strip()] = val.strip()
  339. finally:
  340. rc.close()
  341. return grassrc
  342. def GetRCValue(self, value):
  343. "Return GRASS variable (read from GISRC)"""
  344. if self.grassrc.has_key(value):
  345. return self.grassrc[value]
  346. else:
  347. return None
  348. def OnWizard(self, event):
  349. """!Location wizard started"""
  350. from gui_modules import location_wizard
  351. gWizard = location_wizard.LocationWizard(parent = self,
  352. grassdatabase = self.tgisdbase.GetValue())
  353. if gWizard.location != None:
  354. self.OnSetDatabase(event)
  355. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  356. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  357. self.lbmapsets.SetSelection(0)
  358. def OnManageLoc(self, event):
  359. """
  360. Location management choice control handler
  361. """
  362. if event.GetString() == 'Rename mapset':
  363. self.RenameMapset()
  364. elif event.GetString() == 'Rename location':
  365. self.RenameLocation()
  366. elif event.GetString() == 'Delete mapset':
  367. self.DeleteMapset()
  368. elif event.GetString() == 'Delete location':
  369. self.DeleteLocation()
  370. def RenameMapset(self):
  371. """
  372. Rename selected mapset
  373. """
  374. location = self.listOfLocations[self.lblocations.GetSelection()]
  375. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  376. dlg = wx.TextEntryDialog(parent=self,
  377. message=_('Current name: %s\nEnter new name:') % mapset,
  378. caption=_('Rename selected mapset'))
  379. if dlg.ShowModal() == wx.ID_OK:
  380. newmapset = dlg.GetValue()
  381. if newmapset != mapset:
  382. try:
  383. os.rename(os.path.join(self.gisdbase, location, mapset),
  384. os.path.join(self.gisdbase, location, newmapset))
  385. self.OnSelectLocation(None)
  386. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  387. except:
  388. wx.MessageBox(message=_('Unable to rename mapset'))
  389. dlg.Destroy()
  390. def RenameLocation(self):
  391. """
  392. Rename selected location
  393. """
  394. location = self.listOfLocations[self.lblocations.GetSelection()]
  395. dlg = wx.TextEntryDialog(parent=self,
  396. message=_('Current name: %s\nEnter new name:') % location,
  397. caption=_('Rename selected location'))
  398. if dlg.ShowModal() == wx.ID_OK:
  399. newlocation = dlg.GetValue()
  400. if newlocation != location:
  401. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  402. try:
  403. os.rename(os.path.join(self.gisdbase, location),
  404. os.path.join(self.gisdbase, newlocation))
  405. self.UpdateLocations(self.gisdbase)
  406. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  407. self.UpdateMapsets(newlocation)
  408. except:
  409. wx.MessageBox(message=_('Unable to rename location'))
  410. dlg.Destroy()
  411. def DeleteMapset(self):
  412. """
  413. Delete selected mapset
  414. """
  415. location = self.listOfLocations[self.lblocations.GetSelection()]
  416. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  417. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting mapset <%(mapset)s> "
  418. "from location <%(location)s>?\n\n"
  419. "ALL MAPS included in this mapset will be "
  420. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  421. 'location' : location},
  422. caption=_("Delete selected mapset"),
  423. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  424. if dlg.ShowModal() == wx.ID_YES:
  425. try:
  426. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  427. self.OnSelectLocation(None)
  428. self.lbmapsets.SetSelection(0)
  429. except:
  430. wx.MessageBox(message=_('Unable to delete mapset'))
  431. dlg.Destroy()
  432. def DeleteLocation(self):
  433. """
  434. Delete selected location
  435. """
  436. location = self.listOfLocations[self.lblocations.GetSelection()]
  437. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting "
  438. "location <%s>?\n\n"
  439. "ALL MAPS included in this location will be "
  440. "PERMANENTLY DELETED!") % (location),
  441. caption=_("Delete selected location"),
  442. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  443. if dlg.ShowModal() == wx.ID_YES:
  444. try:
  445. shutil.rmtree(os.path.join(self.gisdbase, location))
  446. self.UpdateLocations(self.gisdbase)
  447. self.lblocations.SetSelection(0)
  448. self.OnSelectLocation(None)
  449. self.lbmapsets.SetSelection(0)
  450. except:
  451. wx.MessageBox(message=_('Unable to delete location'))
  452. dlg.Destroy()
  453. def UpdateLocations(self, dbase):
  454. """!Update list of locations"""
  455. try:
  456. self.listOfLocations = utils.GetListOfLocations(dbase)
  457. except UnicodeEncodeError:
  458. wx.MessageBox(parent = self, caption = _("Error"),
  459. message = _("Unable to set GRASS database. "
  460. "Check your locale settings."),
  461. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  462. self.lblocations.Clear()
  463. self.lblocations.InsertItems(self.listOfLocations, 0)
  464. if len(self.listOfLocations) > 0:
  465. self.lblocations.SetSelection(0)
  466. else:
  467. self.lblocations.SetSelection(wx.NOT_FOUND)
  468. return self.listOfLocations
  469. def UpdateMapsets(self, location):
  470. """!Update list of mapsets"""
  471. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  472. self.listOfMapsetsSelectable = list()
  473. self.listOfMapsets = utils.GetListOfMapsets(self.gisdbase, location)
  474. self.lbmapsets.Clear()
  475. # disable mapset with denied permission
  476. locationName = os.path.basename(location)
  477. try:
  478. ret = gcmd.RunCommand('g.mapset',
  479. read = True,
  480. flags = 'l',
  481. location = locationName,
  482. gisdbase = self.gisdbase)
  483. if not ret:
  484. raise gcmd.CmdError("")
  485. for line in ret.splitlines():
  486. self.listOfMapsetsSelectable += line.split(' ')
  487. except:
  488. gcmd.RunCommand("g.gisenv",
  489. set= "GISDBASE=%s" % self.gisdbase)
  490. gcmd.RunCommand("g.gisenv",
  491. set = "LOCATION_NAME=%s" % locationName)
  492. gcmd.RunCommand("g.gisenv",
  493. set = "MAPSET=PERMANENT")
  494. # first run only
  495. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  496. disabled = []
  497. idx = 0
  498. for mapset in self.listOfMapsets:
  499. if mapset not in self.listOfMapsetsSelectable or \
  500. os.path.isfile(os.path.join(self.gisdbase,
  501. locationName,
  502. mapset, ".gislock")):
  503. disabled.append(idx)
  504. idx += 1
  505. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  506. return self.listOfMapsets
  507. def OnSelectLocation(self, event):
  508. """!Location selected"""
  509. if event:
  510. self.lblocations.SetSelection(event.GetIndex())
  511. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  512. self.UpdateMapsets(os.path.join(self.gisdbase,
  513. self.listOfLocations[self.lblocations.GetSelection()]))
  514. else:
  515. self.listOfMapsets = []
  516. disabled = []
  517. idx = 0
  518. try:
  519. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  520. except IndexError:
  521. locationName = ''
  522. for mapset in self.listOfMapsets:
  523. if mapset not in self.listOfMapsetsSelectable or \
  524. os.path.isfile(os.path.join(self.gisdbase,
  525. locationName,
  526. mapset, ".gislock")):
  527. disabled.append(idx)
  528. idx += 1
  529. self.lbmapsets.Clear()
  530. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  531. if len(self.listOfMapsets) > 0:
  532. self.lbmapsets.SetSelection(0)
  533. if locationName:
  534. # enable start button when location and mapset is selected
  535. self.bstart.Enable()
  536. self.bmapset.Enable()
  537. self.manageloc.Enable()
  538. else:
  539. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  540. self.bstart.Enable(False)
  541. self.bmapset.Enable(False)
  542. self.manageloc.Enable(False)
  543. def OnSelectMapset(self, event):
  544. """!Mapset selected"""
  545. self.lbmapsets.SetSelection(event.GetIndex())
  546. if event.GetText() not in self.listOfMapsetsSelectable:
  547. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  548. else:
  549. self.FormerMapsetSelection = event.GetIndex()
  550. event.Skip()
  551. def OnSetDatabase(self, event):
  552. """!Database set"""
  553. self.gisdbase = self.tgisdbase.GetValue()
  554. self.UpdateLocations(self.gisdbase)
  555. self.OnSelectLocation(None)
  556. def OnBrowse(self, event):
  557. """'Browse' button clicked"""
  558. grassdata = None
  559. dlg = wx.DirDialog(self, _("Choose GIS Data Directory:"),
  560. style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  561. if dlg.ShowModal() == wx.ID_OK:
  562. self.gisdbase = dlg.GetPath()
  563. self.tgisdbase.SetValue(self.gisdbase)
  564. self.OnSetDatabase(event)
  565. dlg.Destroy()
  566. def OnCreateMapset(self,event):
  567. """!Create new mapset"""
  568. self.gisdbase = self.tgisdbase.GetValue()
  569. location = self.listOfLocations[self.lblocations.GetSelection()]
  570. dlg = wx.TextEntryDialog(parent=self,
  571. message=_('Enter name for new mapset:'),
  572. caption=_('Create new mapset'))
  573. if dlg.ShowModal() == wx.ID_OK:
  574. mapset = dlg.GetValue()
  575. try:
  576. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  577. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  578. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  579. os.path.join(self.gisdbase, location, mapset))
  580. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  581. self.OnSelectLocation(None)
  582. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  583. except StandardError, e:
  584. dlg = wx.MessageDialog(parent=self, message=_("Unable to create new mapset: %s") % e,
  585. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  586. dlg.ShowModal()
  587. dlg.Destroy()
  588. return False
  589. return True
  590. def OnStart(self, event):
  591. """'Start GRASS' button clicked"""
  592. gcmd.RunCommand("g.gisenv",
  593. set = "GISDBASE=%s" % \
  594. self.tgisdbase.GetValue())
  595. gcmd.RunCommand("g.gisenv",
  596. set = "LOCATION_NAME=%s" % \
  597. self.listOfLocations[self.lblocations.GetSelection()])
  598. gcmd.RunCommand("g.gisenv",
  599. set = "MAPSET=%s" % \
  600. self.listOfMapsets[self.lbmapsets.GetSelection()])
  601. self.Destroy()
  602. sys.exit(0)
  603. def OnExit(self, event):
  604. """'Exit' button clicked"""
  605. self.Destroy()
  606. sys.exit (2)
  607. def OnHelp(self, event):
  608. """'Help' button clicked"""
  609. # help text in lib/init/helptext.html
  610. file=os.path.join(self.gisbase, "docs", "html", "helptext.html")
  611. helpFrame = help.HelpWindow(parent=self, id=wx.ID_ANY,
  612. title=_("GRASS Quickstart"),
  613. size=(640, 480),
  614. file=file)
  615. helpFrame.Show(True)
  616. event.Skip()
  617. def OnCloseWindow(self, event):
  618. """!Close window event"""
  619. event.Skip()
  620. sys.exit(2)
  621. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  622. """!Use wx.ListCtrl instead of wx.ListBox, different style for
  623. non-selectable items (e.g. mapsets with denied permission)"""
  624. def __init__(self, parent, id, size,
  625. choices, disabled=[]):
  626. wx.ListCtrl.__init__(self, parent, id, size=size,
  627. style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  628. wx.BORDER_SUNKEN)
  629. listmix.ListCtrlAutoWidthMixin.__init__(self)
  630. self.InsertColumn(0, '')
  631. self.selected = wx.NOT_FOUND
  632. self.__LoadData(choices, disabled)
  633. def __LoadData(self, choices, disabled=[]):
  634. """
  635. Load data into list
  636. @param choices list of item
  637. @param disabled list of indeces of non-selectable items
  638. """
  639. idx = 0
  640. for item in choices:
  641. index = self.InsertStringItem(sys.maxint, item)
  642. self.SetStringItem(index, 0, item)
  643. if idx in disabled:
  644. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  645. idx += 1
  646. #self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  647. def Clear(self):
  648. self.DeleteAllItems()
  649. def InsertItems(self, choices, pos, disabled=[]):
  650. self.__LoadData(choices, disabled)
  651. def SetSelection(self, item, force = False):
  652. if item != wx.NOT_FOUND and \
  653. (platform.system() != 'Windows' or force):
  654. ### Windows -> FIXME
  655. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  656. self.selected = item
  657. def GetSelection(self):
  658. return self.selected
  659. class StartUp(wx.App):
  660. """!Start-up application"""
  661. def OnInit(self):
  662. wx.InitAllImageHandlers()
  663. StartUp = GRASSStartup()
  664. StartUp.CenterOnScreen()
  665. self.SetTopWindow(StartUp)
  666. StartUp.Show()
  667. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  668. wx.MessageBox(parent=StartUp,
  669. caption=_('Starting GRASS for the first time'),
  670. message=_('GRASS needs a directory in which to store its data. '
  671. 'Create one now if you have not already done so. '
  672. 'A popular choice is "grassdata", located in '
  673. 'your home directory.'),
  674. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  675. StartUp.OnBrowse(None)
  676. return 1
  677. if __name__ == "__main__":
  678. if os.getenv("GISBASE") is None:
  679. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  680. else:
  681. import gettext
  682. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  683. import gui_modules.gcmd as gcmd
  684. import gui_modules.utils as utils
  685. GRASSStartUp = StartUp(0)
  686. GRASSStartUp.MainLoop()