gis_set.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. import gui_modules.goutput
  28. from gui_modules.ghelp import HelpWindow
  29. import wx
  30. import wx.html
  31. import wx.lib.rcsizer as rcs
  32. import wx.lib.filebrowsebutton as filebrowse
  33. import wx.lib.mixins.listctrl as listmix
  34. import wx.lib.scrolledpanel as scrolled
  35. class GRASSStartup(wx.Frame):
  36. """!GRASS start-up screen"""
  37. def __init__(self, parent=None, id=wx.ID_ANY, style=wx.DEFAULT_FRAME_STYLE):
  38. #
  39. # GRASS variables
  40. #
  41. self.gisbase = os.getenv("GISBASE")
  42. self.grassrc = self._read_grassrc()
  43. self.gisdbase = self.GetRCValue("GISDBASE")
  44. #
  45. # list of locations/mapsets
  46. #
  47. self.listOfLocations = []
  48. self.listOfMapsets = []
  49. self.listOfMapsetsSelectable = []
  50. wx.Frame.__init__(self, parent=parent, id=id, style=style)
  51. self.locale = wx.Locale(language = wx.LANGUAGE_DEFAULT)
  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. (utils.UnicodeString(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. (utils.UnicodeString(mapset))
  203. def _do_layout(self):
  204. label_style = wx.ADJUST_MINSIZE | wx.ALIGN_CENTER_HORIZONTAL
  205. sizer = wx.BoxSizer(wx.VERTICAL)
  206. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  207. location_sizer = wx.FlexGridSizer(rows=1, cols=2, vgap=4, hgap=4)
  208. select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
  209. select_sizer = wx.FlexGridSizer(rows=2, cols=2, vgap=4, hgap=4)
  210. manage_boxsizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
  211. manage_sizer = wx.BoxSizer(wx.VERTICAL)
  212. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  213. # gis data directory
  214. dbase_sizer.Add(item=self.ldbase, proportion=0,
  215. flag=wx.ALIGN_CENTER_VERTICAL |
  216. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  217. border=3)
  218. dbase_sizer.Add(item=self.tgisdbase, proportion=0,
  219. flag=wx.ALIGN_CENTER_VERTICAL |
  220. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  221. border=3)
  222. dbase_sizer.Add(item=self.bbrowse, proportion=0,
  223. flag=wx.ALIGN_CENTER_VERTICAL |
  224. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  225. border=3)
  226. # select sizer
  227. select_sizer.Add(item=self.llocation, proportion=0,
  228. flag=label_style | wx.ALL,
  229. border=3)
  230. select_sizer.Add(item=self.lmapset, proportion=0,
  231. flag=label_style | wx.ALL,
  232. border=3)
  233. select_sizer.Add(item=self.lpanel, proportion=0,
  234. flag=wx.ADJUST_MINSIZE |
  235. wx.ALIGN_CENTER_VERTICAL |
  236. wx.ALIGN_CENTER_HORIZONTAL)
  237. select_sizer.Add(item=self.mpanel, proportion=0,
  238. flag=wx.ADJUST_MINSIZE |
  239. wx.ALIGN_CENTER_VERTICAL |
  240. wx.ALIGN_CENTER_HORIZONTAL)
  241. select_boxsizer.Add(item=select_sizer, proportion=0)
  242. # define new location and mapset
  243. manage_sizer.Add(item=self.ldefine, proportion=0,
  244. flag=label_style | wx.ALL,
  245. border=3)
  246. manage_sizer.Add(item=self.bwizard, proportion=0,
  247. flag=label_style | wx.BOTTOM,
  248. border=5)
  249. manage_sizer.Add(item=self.lcreate, proportion=0,
  250. flag=label_style | wx.ALL,
  251. border=3)
  252. manage_sizer.Add(item=self.bmapset, proportion=0,
  253. flag=label_style | wx.BOTTOM,
  254. border=5)
  255. manage_sizer.Add(item=self.lmanageloc, proportion=0,
  256. flag=label_style | wx.ALL,
  257. border=3)
  258. manage_sizer.Add(item=self.manageloc, proportion=0,
  259. flag=label_style | wx.BOTTOM,
  260. border=5)
  261. manage_boxsizer.Add(item=manage_sizer, proportion=0)
  262. # location sizer
  263. location_sizer.Add(item=select_boxsizer, proportion=0,
  264. flag=wx.ADJUST_MINSIZE |
  265. wx.ALIGN_CENTER_VERTICAL |
  266. wx.ALIGN_CENTER_HORIZONTAL |
  267. wx.RIGHT | wx.LEFT | wx.EXPAND,
  268. border=3) # GISDBASE setting
  269. location_sizer.Add(item=manage_boxsizer, proportion=0,
  270. flag=wx.ADJUST_MINSIZE |
  271. wx.ALIGN_TOP |
  272. wx.ALIGN_CENTER_HORIZONTAL |
  273. wx.RIGHT | wx.EXPAND,
  274. border=3)
  275. # buttons
  276. btns_sizer.Add(item=self.bstart, proportion=0,
  277. flag=wx.ALIGN_CENTER_HORIZONTAL |
  278. wx.ALIGN_CENTER_VERTICAL |
  279. wx.ALL,
  280. border=5)
  281. btns_sizer.Add(item=self.bexit, proportion=0,
  282. flag=wx.ALIGN_CENTER_HORIZONTAL |
  283. wx.ALIGN_CENTER_VERTICAL |
  284. wx.ALL,
  285. border=5)
  286. btns_sizer.Add(item=self.bhelp, proportion=0,
  287. flag=wx.ALIGN_CENTER_HORIZONTAL |
  288. wx.ALIGN_CENTER_VERTICAL |
  289. wx.ALL,
  290. border=5)
  291. # main sizer
  292. sizer.Add(item=self.hbitmap,
  293. proportion=0,
  294. flag=wx.ALIGN_CENTER_VERTICAL |
  295. wx.ALIGN_CENTER_HORIZONTAL |
  296. wx.ALL,
  297. border=3) # image
  298. sizer.Add(item=self.lwelcome, # welcome message
  299. proportion=0,
  300. flag=wx.ALIGN_CENTER_VERTICAL |
  301. wx.ALIGN_CENTER_HORIZONTAL |
  302. wx.BOTTOM,
  303. border=5)
  304. sizer.Add(item=self.ltitle, # title
  305. proportion=0,
  306. flag=wx.ALIGN_CENTER_VERTICAL |
  307. wx.ALIGN_CENTER_HORIZONTAL)
  308. sizer.Add(item=dbase_sizer, proportion=0,
  309. flag=wx.ALIGN_CENTER_HORIZONTAL |
  310. wx.RIGHT | wx.LEFT,
  311. border=1) # GISDBASE setting
  312. sizer.Add(item=location_sizer, proportion=1,
  313. flag=wx.ALIGN_CENTER_VERTICAL |
  314. wx.ALIGN_CENTER_HORIZONTAL |
  315. wx.RIGHT | wx.LEFT,
  316. border=1)
  317. sizer.Add(item=btns_sizer, proportion=0,
  318. flag=wx.ALIGN_CENTER_VERTICAL |
  319. wx.ALIGN_CENTER_HORIZONTAL |
  320. wx.RIGHT | wx.LEFT,
  321. border=1)
  322. self.panel.SetAutoLayout(True)
  323. self.panel.SetSizer(sizer)
  324. sizer.Fit(self.panel)
  325. sizer.SetSizeHints(self)
  326. self.Layout()
  327. def _read_grassrc(self):
  328. """!Read variables from $HOME/.grass7/rc file
  329. """
  330. grassrc = {}
  331. gisrc = os.getenv("GISRC")
  332. if gisrc and os.path.isfile(gisrc):
  333. try:
  334. rc = open(gisrc, "r")
  335. for line in rc.readlines():
  336. key, val = line.split(":", 1)
  337. grassrc[key.strip()] = val.strip()
  338. finally:
  339. rc.close()
  340. return grassrc
  341. def GetRCValue(self, value):
  342. "Return GRASS variable (read from GISRC)"""
  343. if self.grassrc.has_key(value):
  344. return self.grassrc[value]
  345. else:
  346. return None
  347. def OnWizard(self, event):
  348. """!Location wizard started"""
  349. from gui_modules import location_wizard
  350. gWizard = location_wizard.LocationWizard(parent = self,
  351. grassdatabase = self.tgisdbase.GetValue())
  352. if gWizard.location != None:
  353. self.OnSetDatabase(event)
  354. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  355. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  356. self.lbmapsets.SetSelection(0)
  357. def OnManageLoc(self, event):
  358. """!Location management choice control handler
  359. """
  360. sel = event.GetSelection()
  361. if sel == 0:
  362. self.RenameMapset()
  363. elif sel == 1:
  364. self.RenameLocation()
  365. elif sel == 2:
  366. self.DeleteMapset()
  367. elif sel == 3:
  368. self.DeleteLocation()
  369. event.Skip()
  370. def RenameMapset(self):
  371. """!Rename selected mapset
  372. """
  373. location = utils.UnicodeString(self.listOfLocations[self.lblocations.GetSelection()])
  374. mapset = utils.UnicodeString(self.listOfMapsets[self.lbmapsets.GetSelection()])
  375. dlg = wx.TextEntryDialog(parent=self,
  376. message=_('Current name: %s\n\nEnter new name:') % mapset,
  377. caption=_('Rename selected mapset'))
  378. if dlg.ShowModal() == wx.ID_OK:
  379. newmapset = dlg.GetValue()
  380. if newmapset == mapset:
  381. dlg.Destroy()
  382. return
  383. if newmapset in self.listOfMapsets:
  384. wx.MessageBox(parent = self,
  385. caption = _('Message'),
  386. message=_('Unable to rename mapset.\n\n'
  387. 'Mapset <%s> already exists in location.') % newmapset,
  388. style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  389. else:
  390. try:
  391. os.rename(os.path.join(self.gisdbase, location, mapset),
  392. os.path.join(self.gisdbase, location, newmapset))
  393. self.OnSelectLocation(None)
  394. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  395. except StandardError, e:
  396. wx.MessageBox(parent = self,
  397. caption = _('Error'),
  398. message=_('Unable to rename mapset.\n\n%s') % e,
  399. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  400. dlg.Destroy()
  401. def RenameLocation(self):
  402. """!Rename selected location
  403. """
  404. location = utils.UnicodeString(self.listOfLocations[self.lblocations.GetSelection()])
  405. dlg = wx.TextEntryDialog(parent=self,
  406. message=_('Current name: %s\n\nEnter new name:') % location,
  407. caption=_('Rename selected location'))
  408. if dlg.ShowModal() == wx.ID_OK:
  409. newlocation = dlg.GetValue()
  410. if newlocation == location:
  411. dlg.Destroy()
  412. return
  413. if newlocation in self.listOfLocations:
  414. wx.MessageBox(parent = self,
  415. caption = _('Message'),
  416. message=_('Unable to rename location.\n\n'
  417. 'Location <%s> already exists in GRASS database.') % newlocation,
  418. style=wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  419. else:
  420. try:
  421. os.rename(os.path.join(self.gisdbase, location),
  422. os.path.join(self.gisdbase, newlocation))
  423. self.UpdateLocations(self.gisdbase)
  424. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  425. self.UpdateMapsets(newlocation)
  426. except StandardError, e:
  427. wx.MessageBox(parent = self,
  428. caption = _('Error'),
  429. message=_('Unable to rename location.\n\n%s') % e,
  430. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  431. dlg.Destroy()
  432. def DeleteMapset(self):
  433. """
  434. Delete selected mapset
  435. """
  436. location = self.listOfLocations[self.lblocations.GetSelection()]
  437. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  438. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting mapset <%(mapset)s> "
  439. "from location <%(location)s>?\n\n"
  440. "ALL MAPS included in this mapset will be "
  441. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  442. 'location' : location},
  443. caption=_("Delete selected mapset"),
  444. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  445. if dlg.ShowModal() == wx.ID_YES:
  446. try:
  447. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  448. self.OnSelectLocation(None)
  449. self.lbmapsets.SetSelection(0)
  450. except:
  451. wx.MessageBox(message=_('Unable to delete mapset'))
  452. dlg.Destroy()
  453. def DeleteLocation(self):
  454. """
  455. Delete selected location
  456. """
  457. location = self.listOfLocations[self.lblocations.GetSelection()]
  458. dlg = wx.MessageDialog(parent=self, message=_("Do you want to continue with deleting "
  459. "location <%s>?\n\n"
  460. "ALL MAPS included in this location will be "
  461. "PERMANENTLY DELETED!") % (location),
  462. caption=_("Delete selected location"),
  463. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  464. if dlg.ShowModal() == wx.ID_YES:
  465. try:
  466. shutil.rmtree(os.path.join(self.gisdbase, location))
  467. self.UpdateLocations(self.gisdbase)
  468. self.lblocations.SetSelection(0)
  469. self.OnSelectLocation(None)
  470. self.lbmapsets.SetSelection(0)
  471. except:
  472. wx.MessageBox(message=_('Unable to delete location'))
  473. dlg.Destroy()
  474. def UpdateLocations(self, dbase):
  475. """!Update list of locations"""
  476. try:
  477. self.listOfLocations = utils.GetListOfLocations(dbase)
  478. except UnicodeEncodeError:
  479. wx.MessageBox(parent = self, caption = _("Error"),
  480. message = _("Unable to set GRASS database. "
  481. "Check your locale settings."),
  482. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  483. self.lblocations.Clear()
  484. self.lblocations.InsertItems(self.listOfLocations, 0)
  485. if len(self.listOfLocations) > 0:
  486. self.lblocations.SetSelection(0)
  487. else:
  488. self.lblocations.SetSelection(wx.NOT_FOUND)
  489. return self.listOfLocations
  490. def UpdateMapsets(self, location):
  491. """!Update list of mapsets"""
  492. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  493. self.listOfMapsetsSelectable = list()
  494. self.listOfMapsets = utils.GetListOfMapsets(self.gisdbase, location)
  495. self.lbmapsets.Clear()
  496. # disable mapset with denied permission
  497. locationName = os.path.basename(location)
  498. try:
  499. ret = gcmd.RunCommand('g.mapset',
  500. read = True,
  501. flags = 'l',
  502. location = locationName,
  503. gisdbase = self.gisdbase)
  504. if not ret:
  505. raise gcmd.CmdError("")
  506. for line in ret.splitlines():
  507. self.listOfMapsetsSelectable += line.split(' ')
  508. except:
  509. gcmd.RunCommand("g.gisenv",
  510. set= "GISDBASE=%s" % self.gisdbase)
  511. gcmd.RunCommand("g.gisenv",
  512. set = "LOCATION_NAME=%s" % locationName)
  513. gcmd.RunCommand("g.gisenv",
  514. set = "MAPSET=PERMANENT")
  515. # first run only
  516. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  517. disabled = []
  518. idx = 0
  519. for mapset in self.listOfMapsets:
  520. mapset = utils.UnicodeString(mapset)
  521. if mapset not in self.listOfMapsetsSelectable or \
  522. os.path.isfile(os.path.join(self.gisdbase,
  523. locationName,
  524. mapset, ".gislock")):
  525. disabled.append(idx)
  526. idx += 1
  527. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  528. return self.listOfMapsets
  529. def OnSelectLocation(self, event):
  530. """!Location selected"""
  531. if event:
  532. self.lblocations.SetSelection(event.GetIndex())
  533. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  534. self.UpdateMapsets(os.path.join(self.gisdbase,
  535. self.listOfLocations[self.lblocations.GetSelection()]))
  536. else:
  537. self.listOfMapsets = []
  538. disabled = []
  539. idx = 0
  540. try:
  541. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  542. except IndexError:
  543. locationName = ''
  544. for mapset in self.listOfMapsets:
  545. if mapset not in self.listOfMapsetsSelectable or \
  546. os.path.isfile(os.path.join(self.gisdbase,
  547. locationName,
  548. mapset, ".gislock")):
  549. disabled.append(idx)
  550. idx += 1
  551. self.lbmapsets.Clear()
  552. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled=disabled)
  553. if len(self.listOfMapsets) > 0:
  554. self.lbmapsets.SetSelection(0)
  555. if locationName:
  556. # enable start button when location and mapset is selected
  557. self.bstart.Enable()
  558. self.bmapset.Enable()
  559. self.manageloc.Enable()
  560. else:
  561. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  562. self.bstart.Enable(False)
  563. self.bmapset.Enable(False)
  564. self.manageloc.Enable(False)
  565. def OnSelectMapset(self, event):
  566. """!Mapset selected"""
  567. self.lbmapsets.SetSelection(event.GetIndex())
  568. if event.GetText() not in self.listOfMapsetsSelectable:
  569. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  570. else:
  571. self.FormerMapsetSelection = event.GetIndex()
  572. event.Skip()
  573. def OnSetDatabase(self, event):
  574. """!Database set"""
  575. self.gisdbase = self.tgisdbase.GetValue()
  576. self.UpdateLocations(self.gisdbase)
  577. self.OnSelectLocation(None)
  578. def OnBrowse(self, event):
  579. """'Browse' button clicked"""
  580. grassdata = None
  581. dlg = wx.DirDialog(self, _("Choose GIS Data Directory:"),
  582. style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
  583. if dlg.ShowModal() == wx.ID_OK:
  584. self.gisdbase = dlg.GetPath()
  585. self.tgisdbase.SetValue(self.gisdbase)
  586. self.OnSetDatabase(event)
  587. dlg.Destroy()
  588. def OnCreateMapset(self,event):
  589. """!Create new mapset"""
  590. self.gisdbase = self.tgisdbase.GetValue()
  591. location = self.listOfLocations[self.lblocations.GetSelection()]
  592. dlg = wx.TextEntryDialog(parent=self,
  593. message=_('Enter name for new mapset:'),
  594. caption=_('Create new mapset'))
  595. if dlg.ShowModal() == wx.ID_OK:
  596. mapset = dlg.GetValue()
  597. try:
  598. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  599. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  600. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  601. os.path.join(self.gisdbase, location, mapset))
  602. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  603. self.OnSelectLocation(None)
  604. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  605. except StandardError, e:
  606. dlg = wx.MessageDialog(parent=self, message=_("Unable to create new mapset: %s") % e,
  607. caption=_("Error"), style=wx.OK | wx.ICON_ERROR)
  608. dlg.ShowModal()
  609. dlg.Destroy()
  610. return False
  611. return True
  612. def OnStart(self, event):
  613. """'Start GRASS' button clicked"""
  614. gcmd.RunCommand("g.gisenv",
  615. set = "GISDBASE=%s" % \
  616. self.tgisdbase.GetValue())
  617. gcmd.RunCommand("g.gisenv",
  618. set = "LOCATION_NAME=%s" % \
  619. self.listOfLocations[self.lblocations.GetSelection()])
  620. gcmd.RunCommand("g.gisenv",
  621. set = "MAPSET=%s" % \
  622. self.listOfMapsets[self.lbmapsets.GetSelection()])
  623. self.Destroy()
  624. sys.exit(0)
  625. def OnExit(self, event):
  626. """'Exit' button clicked"""
  627. self.Destroy()
  628. sys.exit(2)
  629. def OnHelp(self, event):
  630. """'Help' button clicked"""
  631. # help text in lib/init/helptext.html
  632. file=os.path.join(self.gisbase, "docs", "html", "helptext.html")
  633. helpFrame = HelpWindow(parent=self, id=wx.ID_ANY,
  634. title=_("GRASS Quickstart"),
  635. size=(640, 480),
  636. file=file)
  637. helpFrame.Show(True)
  638. event.Skip()
  639. def OnCloseWindow(self, event):
  640. """!Close window event"""
  641. event.Skip()
  642. sys.exit(2)
  643. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  644. """!Use wx.ListCtrl instead of wx.ListBox, different style for
  645. non-selectable items (e.g. mapsets with denied permission)"""
  646. def __init__(self, parent, id, size,
  647. choices, disabled=[]):
  648. wx.ListCtrl.__init__(self, parent, id, size=size,
  649. style=wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  650. wx.BORDER_SUNKEN)
  651. listmix.ListCtrlAutoWidthMixin.__init__(self)
  652. self.InsertColumn(0, '')
  653. self.selected = wx.NOT_FOUND
  654. self.__LoadData(choices, disabled)
  655. def __LoadData(self, choices, disabled=[]):
  656. """
  657. Load data into list
  658. @param choices list of item
  659. @param disabled list of indeces of non-selectable items
  660. """
  661. idx = 0
  662. for item in choices:
  663. index = self.InsertStringItem(sys.maxint, item)
  664. self.SetStringItem(index, 0, item)
  665. if idx in disabled:
  666. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  667. idx += 1
  668. #self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  669. def Clear(self):
  670. self.DeleteAllItems()
  671. def InsertItems(self, choices, pos, disabled=[]):
  672. self.__LoadData(choices, disabled)
  673. def SetSelection(self, item, force = False):
  674. if item != wx.NOT_FOUND and \
  675. (platform.system() != 'Windows' or force):
  676. ### Windows -> FIXME
  677. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  678. self.selected = item
  679. def GetSelection(self):
  680. return self.selected
  681. class StartUp(wx.App):
  682. """!Start-up application"""
  683. def OnInit(self):
  684. wx.InitAllImageHandlers()
  685. StartUp = GRASSStartup()
  686. StartUp.CenterOnScreen()
  687. self.SetTopWindow(StartUp)
  688. StartUp.Show()
  689. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  690. wx.MessageBox(parent=StartUp,
  691. caption=_('Starting GRASS for the first time'),
  692. message=_('GRASS needs a directory in which to store its data. '
  693. 'Create one now if you have not already done so. '
  694. 'A popular choice is "grassdata", located in '
  695. 'your home directory.'),
  696. style=wx.OK | wx.ICON_ERROR | wx.CENTRE)
  697. StartUp.OnBrowse(None)
  698. return 1
  699. if __name__ == "__main__":
  700. if os.getenv("GISBASE") is None:
  701. print >> sys.stderr, "Failed to start GUI, GRASS GIS is not running."
  702. else:
  703. import gettext
  704. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  705. import gui_modules.gcmd as gcmd
  706. import gui_modules.utils as utils
  707. GRASSStartUp = StartUp(0)
  708. GRASSStartUp.MainLoop()