gis_set.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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-2012 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. ### i18N
  23. import gettext
  24. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  25. if __name__ == "__main__":
  26. sys.path.append(os.path.join(os.getenv('GISBASE'), 'etc', 'gui', 'wxpython'))
  27. from core import globalvar
  28. import wx
  29. import wx.lib.mixins.listctrl as listmix
  30. import wx.lib.scrolledpanel as scrolled
  31. from gui_core.ghelp import HelpFrame
  32. from core.gcmd import GMessage, GError, DecodeString, RunCommand
  33. from core.utils import GetListOfLocations, GetListOfMapsets
  34. sys.stderr = codecs.getwriter('utf8')(sys.stderr)
  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._readGisRC()
  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.lblocations = GListBox(parent = self.panel,
  123. id = wx.ID_ANY, size = (180, 200),
  124. choices = self.listOfLocations)
  125. self.lblocations.SetColumnWidth(0, 180)
  126. # TODO: sort; but keep PERMANENT on top of list
  127. # Mapsets
  128. self.lbmapsets = GListBox(parent = self.panel,
  129. id = wx.ID_ANY, size = (180, 200),
  130. choices = self.listOfMapsets)
  131. self.lbmapsets.SetColumnWidth(0, 180)
  132. # layout & properties
  133. self._set_properties()
  134. self._do_layout()
  135. # events
  136. self.bbrowse.Bind(wx.EVT_BUTTON, self.OnBrowse)
  137. self.bstart.Bind(wx.EVT_BUTTON, self.OnStart)
  138. self.bexit.Bind(wx.EVT_BUTTON, self.OnExit)
  139. self.bhelp.Bind(wx.EVT_BUTTON, self.OnHelp)
  140. self.bmapset.Bind(wx.EVT_BUTTON, self.OnCreateMapset)
  141. self.bwizard.Bind(wx.EVT_BUTTON, self.OnWizard)
  142. self.manageloc.Bind(wx.EVT_CHOICE, self.OnManageLoc)
  143. self.lblocations.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectLocation)
  144. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnSelectMapset)
  145. self.lbmapsets.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnStart)
  146. self.tgisdbase.Bind(wx.EVT_TEXT_ENTER, self.OnSetDatabase)
  147. self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
  148. def _set_properties(self):
  149. """!Set frame properties"""
  150. self.SetTitle(_("Welcome to GRASS GIS"))
  151. self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, "grass.ico"),
  152. wx.BITMAP_TYPE_ICO))
  153. self.lwelcome.SetForegroundColour(wx.Colour(35, 142, 35))
  154. self.lwelcome.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
  155. self.bstart.SetForegroundColour(wx.Colour(35, 142, 35))
  156. self.bstart.SetToolTipString(_("Enter GRASS session"))
  157. self.bstart.Enable(False)
  158. self.bmapset.Enable(False)
  159. self.manageloc.Enable(False)
  160. # set database
  161. if not self.gisdbase:
  162. # sets an initial path for gisdbase if nothing in GISRC
  163. if os.path.isdir(os.getenv("HOME")):
  164. self.gisdbase = os.getenv("HOME")
  165. else:
  166. self.gisdbase = os.getcwd()
  167. try:
  168. self.tgisdbase.SetValue(self.gisdbase)
  169. except UnicodeDecodeError:
  170. wx.MessageBox(parent = self, caption = _("Error"),
  171. message = _("Unable to set GRASS database. "
  172. "Check your locale settings."),
  173. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  174. self.OnSetDatabase(None)
  175. location = self.GetRCValue("LOCATION_NAME")
  176. if location == "<UNKNOWN>" or \
  177. not os.path.isdir(os.path.join(self.gisdbase, location)):
  178. location = None
  179. if location:
  180. # list of locations
  181. self.UpdateLocations(self.gisdbase)
  182. try:
  183. self.lblocations.SetSelection(self.listOfLocations.index(location),
  184. force = True)
  185. self.lblocations.EnsureVisible(self.listOfLocations.index(location))
  186. except ValueError:
  187. print >> sys.stderr, _("ERROR: Location <%s> not found") % location
  188. # list of mapsets
  189. self.UpdateMapsets(os.path.join(self.gisdbase, location))
  190. mapset = self.GetRCValue("MAPSET")
  191. if mapset:
  192. try:
  193. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset),
  194. force = True)
  195. self.lbmapsets.EnsureVisible(self.listOfMapsets.index(mapset))
  196. except ValueError:
  197. self.lbmapsets.Clear()
  198. print >> sys.stderr, _("ERROR: Mapset <%s> not found") % mapset
  199. def _do_layout(self):
  200. sizer = wx.BoxSizer(wx.VERTICAL)
  201. dbase_sizer = wx.BoxSizer(wx.HORIZONTAL)
  202. location_sizer = wx.BoxSizer(wx.HORIZONTAL)
  203. select_boxsizer = wx.StaticBoxSizer(self.select_box, wx.VERTICAL)
  204. select_sizer = wx.FlexGridSizer(rows = 2, cols = 2, vgap = 4, hgap = 4)
  205. select_sizer.AddGrowableRow(1)
  206. select_sizer.AddGrowableCol(0)
  207. select_sizer.AddGrowableCol(1)
  208. manage_sizer = wx.StaticBoxSizer(self.manage_box, wx.VERTICAL)
  209. btns_sizer = wx.BoxSizer(wx.HORIZONTAL)
  210. # gis data directory
  211. dbase_sizer.Add(item = self.ldbase, proportion = 0,
  212. flag = wx.ALIGN_CENTER_VERTICAL |
  213. wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  214. border = 3)
  215. dbase_sizer.Add(item = self.tgisdbase, proportion = 1,
  216. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  217. border = 3)
  218. dbase_sizer.Add(item = self.bbrowse, proportion = 0,
  219. flag = wx.ALIGN_CENTER_VERTICAL | wx.ALL,
  220. border = 3)
  221. # select sizer
  222. select_sizer.Add(item = self.llocation, proportion = 0,
  223. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  224. border = 3)
  225. select_sizer.Add(item = self.lmapset, proportion = 0,
  226. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  227. border = 3)
  228. select_sizer.Add(item = self.lblocations, proportion = 1,
  229. flag = wx.EXPAND)
  230. select_sizer.Add(item = self.lbmapsets, proportion = 1,
  231. flag = wx.EXPAND)
  232. select_boxsizer.Add(item = select_sizer, proportion = 1,
  233. flag = wx.EXPAND)
  234. # define new location and mapset
  235. manage_sizer.Add(item = self.ldefine, proportion = 0,
  236. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  237. border = 3)
  238. manage_sizer.Add(item = self.bwizard, proportion = 0,
  239. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
  240. border = 5)
  241. manage_sizer.Add(item = self.lcreate, proportion = 0,
  242. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  243. border = 3)
  244. manage_sizer.Add(item = self.bmapset, proportion = 0,
  245. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
  246. border = 5)
  247. manage_sizer.Add(item = self.lmanageloc, proportion = 0,
  248. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.ALL,
  249. border = 3)
  250. manage_sizer.Add(item = self.manageloc, proportion = 0,
  251. flag = wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM,
  252. border = 5)
  253. # location sizer
  254. location_sizer.Add(item = select_boxsizer, proportion = 1,
  255. flag = wx.LEFT | wx.RIGHT | wx.EXPAND,
  256. border = 3)
  257. location_sizer.Add(item = manage_sizer, proportion = 0,
  258. flag = wx.RIGHT | wx.EXPAND,
  259. border = 3)
  260. # buttons
  261. btns_sizer.Add(item = self.bstart, proportion = 0,
  262. flag = wx.ALIGN_CENTER_HORIZONTAL |
  263. wx.ALIGN_CENTER_VERTICAL |
  264. wx.ALL,
  265. border = 5)
  266. btns_sizer.Add(item = self.bexit, proportion = 0,
  267. flag = wx.ALIGN_CENTER_HORIZONTAL |
  268. wx.ALIGN_CENTER_VERTICAL |
  269. wx.ALL,
  270. border = 5)
  271. btns_sizer.Add(item = self.bhelp, proportion = 0,
  272. flag = wx.ALIGN_CENTER_HORIZONTAL |
  273. wx.ALIGN_CENTER_VERTICAL |
  274. wx.ALL,
  275. border = 5)
  276. # main sizer
  277. sizer.Add(item = self.hbitmap,
  278. proportion = 0,
  279. flag = wx.ALIGN_CENTER_VERTICAL |
  280. wx.ALIGN_CENTER_HORIZONTAL |
  281. wx.ALL,
  282. border = 3) # image
  283. sizer.Add(item = self.lwelcome, # welcome message
  284. proportion = 0,
  285. flag = wx.ALIGN_CENTER_VERTICAL |
  286. wx.ALIGN_CENTER_HORIZONTAL |
  287. wx.BOTTOM,
  288. border = 5)
  289. sizer.Add(item = self.ltitle, # title
  290. proportion = 0,
  291. flag = wx.ALIGN_CENTER_VERTICAL |
  292. wx.ALIGN_CENTER_HORIZONTAL)
  293. sizer.Add(item = dbase_sizer, proportion = 0,
  294. flag = wx.ALIGN_CENTER_HORIZONTAL |
  295. wx.RIGHT | wx.LEFT | wx.EXPAND,
  296. border = 20) # GISDBASE setting
  297. sizer.Add(item = location_sizer, proportion = 1,
  298. flag = wx.RIGHT | wx.LEFT | wx.EXPAND,
  299. border = 1)
  300. sizer.Add(item = btns_sizer, proportion = 0,
  301. flag = wx.ALIGN_CENTER_VERTICAL |
  302. wx.ALIGN_CENTER_HORIZONTAL |
  303. wx.RIGHT | wx.LEFT,
  304. border = 1)
  305. self.panel.SetAutoLayout(True)
  306. self.panel.SetSizer(sizer)
  307. sizer.Fit(self.panel)
  308. sizer.SetSizeHints(self)
  309. self.Layout()
  310. def _readGisRC(self):
  311. """!Read variables from $HOME/.grass7/rc file
  312. """
  313. grassrc = {}
  314. gisrc = os.getenv("GISRC")
  315. if gisrc and os.path.isfile(gisrc):
  316. try:
  317. rc = open(gisrc, "r")
  318. for line in rc.readlines():
  319. key, val = line.split(":", 1)
  320. grassrc[key.strip()] = DecodeString(val.strip())
  321. finally:
  322. rc.close()
  323. return grassrc
  324. def GetRCValue(self, value):
  325. """!Return GRASS variable (read from GISRC)
  326. """
  327. if self.grassrc.has_key(value):
  328. return self.grassrc[value]
  329. else:
  330. return None
  331. def OnWizard(self, event):
  332. """!Location wizard started"""
  333. from location_wizard.wizard import LocationWizard
  334. gWizard = LocationWizard(parent = self,
  335. grassdatabase = self.tgisdbase.GetValue())
  336. if gWizard.location != None:
  337. self.OnSetDatabase(event)
  338. self.UpdateMapsets(os.path.join(self.gisdbase, gWizard.location))
  339. self.lblocations.SetSelection(self.listOfLocations.index(gWizard.location))
  340. self.lbmapsets.SetSelection(0)
  341. def OnManageLoc(self, event):
  342. """!Location management choice control handler
  343. """
  344. sel = event.GetSelection()
  345. if sel == 0:
  346. self.RenameMapset()
  347. elif sel == 1:
  348. self.RenameLocation()
  349. elif sel == 2:
  350. self.DeleteMapset()
  351. elif sel == 3:
  352. self.DeleteLocation()
  353. event.Skip()
  354. def RenameMapset(self):
  355. """!Rename selected mapset
  356. """
  357. location = self.listOfLocations[self.lblocations.GetSelection()]
  358. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  359. if mapset == 'PERMANENT':
  360. GMessage(parent = self,
  361. message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  362. 'This mapset cannot be renamed.'))
  363. return
  364. dlg = wx.TextEntryDialog(parent = self,
  365. message = _('Current name: %s\n\nEnter new name:') % mapset,
  366. caption = _('Rename selected mapset'))
  367. if dlg.ShowModal() == wx.ID_OK:
  368. newmapset = dlg.GetValue()
  369. if newmapset == mapset:
  370. dlg.Destroy()
  371. return
  372. if newmapset in self.listOfMapsets:
  373. wx.MessageBox(parent = self,
  374. caption = _('Message'),
  375. message = _('Unable to rename mapset.\n\n'
  376. 'Mapset <%s> already exists in location.') % newmapset,
  377. style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  378. else:
  379. try:
  380. os.rename(os.path.join(self.gisdbase, location, mapset),
  381. os.path.join(self.gisdbase, location, newmapset))
  382. self.OnSelectLocation(None)
  383. self.lbmapsets.SetSelection(self.listOfMapsets.index(newmapset))
  384. except StandardError, e:
  385. wx.MessageBox(parent = self,
  386. caption = _('Error'),
  387. message = _('Unable to rename mapset.\n\n%s') % e,
  388. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  389. dlg.Destroy()
  390. def RenameLocation(self):
  391. """!Rename selected location
  392. """
  393. location = self.listOfLocations[self.lblocations.GetSelection()]
  394. dlg = wx.TextEntryDialog(parent = self,
  395. message = _('Current name: %s\n\nEnter new name:') % location,
  396. caption = _('Rename selected location'))
  397. if dlg.ShowModal() == wx.ID_OK:
  398. newlocation = dlg.GetValue()
  399. if newlocation == location:
  400. dlg.Destroy()
  401. return
  402. if newlocation in self.listOfLocations:
  403. wx.MessageBox(parent = self,
  404. caption = _('Message'),
  405. message = _('Unable to rename location.\n\n'
  406. 'Location <%s> already exists in GRASS database.') % newlocation,
  407. style = wx.OK | wx.ICON_INFORMATION | wx.CENTRE)
  408. else:
  409. try:
  410. os.rename(os.path.join(self.gisdbase, location),
  411. os.path.join(self.gisdbase, newlocation))
  412. self.UpdateLocations(self.gisdbase)
  413. self.lblocations.SetSelection(self.listOfLocations.index(newlocation))
  414. self.UpdateMapsets(newlocation)
  415. except StandardError, e:
  416. wx.MessageBox(parent = self,
  417. caption = _('Error'),
  418. message = _('Unable to rename location.\n\n%s') % e,
  419. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  420. dlg.Destroy()
  421. def DeleteMapset(self):
  422. """!Delete selected mapset
  423. """
  424. location = self.listOfLocations[self.lblocations.GetSelection()]
  425. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  426. if mapset == 'PERMANENT':
  427. GMessage(parent = self,
  428. message = _('Mapset <PERMANENT> is required for valid GRASS location.\n\n'
  429. 'This mapset cannot be deleted.'))
  430. return
  431. dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting mapset <%(mapset)s> "
  432. "from location <%(location)s>?\n\n"
  433. "ALL MAPS included in this mapset will be "
  434. "PERMANENTLY DELETED!") % {'mapset' : mapset,
  435. 'location' : location},
  436. caption = _("Delete selected mapset"),
  437. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  438. if dlg.ShowModal() == wx.ID_YES:
  439. try:
  440. shutil.rmtree(os.path.join(self.gisdbase, location, mapset))
  441. self.OnSelectLocation(None)
  442. self.lbmapsets.SetSelection(0)
  443. except:
  444. wx.MessageBox(message = _('Unable to delete mapset'))
  445. dlg.Destroy()
  446. def DeleteLocation(self):
  447. """
  448. Delete selected location
  449. """
  450. location = self.listOfLocations[self.lblocations.GetSelection()]
  451. dlg = wx.MessageDialog(parent = self, message = _("Do you want to continue with deleting "
  452. "location <%s>?\n\n"
  453. "ALL MAPS included in this location will be "
  454. "PERMANENTLY DELETED!") % (location),
  455. caption = _("Delete selected location"),
  456. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  457. if dlg.ShowModal() == wx.ID_YES:
  458. try:
  459. shutil.rmtree(os.path.join(self.gisdbase, location))
  460. self.UpdateLocations(self.gisdbase)
  461. self.lblocations.SetSelection(0)
  462. self.OnSelectLocation(None)
  463. self.lbmapsets.SetSelection(0)
  464. except:
  465. wx.MessageBox(message = _('Unable to delete location'))
  466. dlg.Destroy()
  467. def UpdateLocations(self, dbase):
  468. """!Update list of locations"""
  469. try:
  470. self.listOfLocations = GetListOfLocations(dbase)
  471. except UnicodeEncodeError:
  472. wx.MessageBox(parent = self, caption = _("Error"),
  473. message = _("Unable to set GRASS database. "
  474. "Check your locale settings."),
  475. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  476. self.lblocations.Clear()
  477. self.lblocations.InsertItems(self.listOfLocations, 0)
  478. if len(self.listOfLocations) > 0:
  479. self.lblocations.SetSelection(0)
  480. else:
  481. self.lblocations.SetSelection(wx.NOT_FOUND)
  482. return self.listOfLocations
  483. def UpdateMapsets(self, location):
  484. """!Update list of mapsets"""
  485. self.FormerMapsetSelection = wx.NOT_FOUND # for non-selectable item
  486. self.listOfMapsetsSelectable = list()
  487. self.listOfMapsets = GetListOfMapsets(self.gisdbase, location)
  488. self.lbmapsets.Clear()
  489. # disable mapset with denied permission
  490. locationName = os.path.basename(location)
  491. ret = RunCommand('g.mapset',
  492. read = True,
  493. flags = 'l',
  494. location = locationName,
  495. gisdbase = self.gisdbase)
  496. if ret:
  497. for line in ret.splitlines():
  498. self.listOfMapsetsSelectable += line.split(' ')
  499. else:
  500. RunCommand("g.gisenv",
  501. set = "GISDBASE=%s" % self.gisdbase)
  502. RunCommand("g.gisenv",
  503. set = "LOCATION_NAME=%s" % locationName)
  504. RunCommand("g.gisenv",
  505. set = "MAPSET=PERMANENT")
  506. # first run only
  507. self.listOfMapsetsSelectable = copy.copy(self.listOfMapsets)
  508. disabled = []
  509. idx = 0
  510. for mapset in self.listOfMapsets:
  511. if mapset not in self.listOfMapsetsSelectable or \
  512. os.path.isfile(os.path.join(self.gisdbase,
  513. locationName,
  514. mapset, ".gislock")):
  515. disabled.append(idx)
  516. idx += 1
  517. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled = disabled)
  518. return self.listOfMapsets
  519. def OnSelectLocation(self, event):
  520. """!Location selected"""
  521. if event:
  522. self.lblocations.SetSelection(event.GetIndex())
  523. if self.lblocations.GetSelection() != wx.NOT_FOUND:
  524. self.UpdateMapsets(os.path.join(self.gisdbase,
  525. self.listOfLocations[self.lblocations.GetSelection()]))
  526. else:
  527. self.listOfMapsets = []
  528. disabled = []
  529. idx = 0
  530. try:
  531. locationName = self.listOfLocations[self.lblocations.GetSelection()]
  532. except IndexError:
  533. locationName = ''
  534. for mapset in self.listOfMapsets:
  535. if mapset not in self.listOfMapsetsSelectable or \
  536. os.path.isfile(os.path.join(self.gisdbase,
  537. locationName,
  538. mapset, ".gislock")):
  539. disabled.append(idx)
  540. idx += 1
  541. self.lbmapsets.Clear()
  542. self.lbmapsets.InsertItems(self.listOfMapsets, 0, disabled = disabled)
  543. if len(self.listOfMapsets) > 0:
  544. self.lbmapsets.SetSelection(0)
  545. if locationName:
  546. # enable start button when location and mapset is selected
  547. self.bstart.Enable()
  548. self.bmapset.Enable()
  549. self.manageloc.Enable()
  550. else:
  551. self.lbmapsets.SetSelection(wx.NOT_FOUND)
  552. self.bstart.Enable(False)
  553. self.bmapset.Enable(False)
  554. self.manageloc.Enable(False)
  555. def OnSelectMapset(self, event):
  556. """!Mapset selected"""
  557. self.lbmapsets.SetSelection(event.GetIndex())
  558. if event.GetText() not in self.listOfMapsetsSelectable:
  559. self.lbmapsets.SetSelection(self.FormerMapsetSelection)
  560. else:
  561. self.FormerMapsetSelection = event.GetIndex()
  562. event.Skip()
  563. def OnSetDatabase(self, event):
  564. """!Database set"""
  565. self.gisdbase = self.tgisdbase.GetValue()
  566. self.UpdateLocations(self.gisdbase)
  567. self.OnSelectLocation(None)
  568. def OnBrowse(self, event):
  569. """'Browse' button clicked"""
  570. if not event:
  571. defaultPath = os.getenv('HOME')
  572. else:
  573. defaultPath = ""
  574. dlg = wx.DirDialog(parent = self, message = _("Choose GIS Data Directory"),
  575. defaultPath = defaultPath, style = wx.DD_DEFAULT_STYLE)
  576. if dlg.ShowModal() == wx.ID_OK:
  577. self.gisdbase = dlg.GetPath()
  578. self.tgisdbase.SetValue(self.gisdbase)
  579. self.OnSetDatabase(event)
  580. dlg.Destroy()
  581. def OnCreateMapset(self,event):
  582. """!Create new mapset"""
  583. self.gisdbase = self.tgisdbase.GetValue()
  584. location = self.listOfLocations[self.lblocations.GetSelection()]
  585. dlg = wx.TextEntryDialog(parent = self,
  586. message = _('Enter name for new mapset:'),
  587. caption = _('Create new mapset'))
  588. if dlg.ShowModal() == wx.ID_OK:
  589. mapset = dlg.GetValue()
  590. if mapset in self.listOfMapsets:
  591. GMessage(parent = self,
  592. message = _("Mapset <%s> already exists.") % mapset)
  593. return
  594. if mapset.lower() == 'ogr':
  595. dlg1 = wx.MessageDialog(parent = self,
  596. message = _("Mapset <%s> is reserved for direct "
  597. "read access to OGR layers. Please consider to use "
  598. "another name for your mapset.\n\n"
  599. "Are you really sure that you want to create this mapset?") % mapset,
  600. caption = _("Reserved mapset name"),
  601. style = wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
  602. ret = dlg1.ShowModal()
  603. dlg1.Destroy()
  604. if ret == wx.ID_NO:
  605. dlg.Destroy()
  606. return
  607. try:
  608. os.mkdir(os.path.join(self.gisdbase, location, mapset))
  609. # copy WIND file and its permissions from PERMANENT and set permissions to u+rw,go+r
  610. shutil.copy(os.path.join(self.gisdbase, location, 'PERMANENT', 'WIND'),
  611. os.path.join(self.gisdbase, location, mapset))
  612. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  613. self.OnSelectLocation(None)
  614. self.lbmapsets.SetSelection(self.listOfMapsets.index(mapset))
  615. except StandardError, e:
  616. GError(parent = self,
  617. message = _("Unable to create new mapset: %s") % e)
  618. return False
  619. dlg.Destroy()
  620. self.bstart.SetFocus()
  621. return True
  622. def OnStart(self, event):
  623. """'Start GRASS' button clicked"""
  624. dbase = self.tgisdbase.GetValue()
  625. location = self.listOfLocations[self.lblocations.GetSelection()]
  626. mapset = self.listOfMapsets[self.lbmapsets.GetSelection()]
  627. lockfile = os.path.join(dbase, location, mapset, '.gislock')
  628. if os.path.isfile(lockfile):
  629. dlg = wx.MessageDialog(parent = self,
  630. message = _("GRASS is already running in selected mapset <%(mapset)s>\n"
  631. "(file %(lock)s found).\n\n"
  632. "Concurrent use not allowed.\n\n"
  633. "Do you want to try to remove .gislock (note that you "
  634. "need permission for this operation) and continue?") %
  635. { 'mapset' : mapset, 'lock' : lockfile },
  636. caption = _("Lock file found"),
  637. style = wx.YES_NO | wx.NO_DEFAULT |
  638. wx.ICON_QUESTION | wx.CENTRE)
  639. ret = dlg.ShowModal()
  640. dlg.Destroy()
  641. if ret == wx.ID_YES:
  642. dlg1 = wx.MessageDialog(parent = self,
  643. message = _("ARE YOU REALLY SURE?\n\n"
  644. "If you really are running another GRASS session doing this "
  645. "could corrupt your data. Have another look in the processor "
  646. "manager just to be sure..."),
  647. caption = _("Lock file found"),
  648. style = wx.YES_NO | wx.NO_DEFAULT |
  649. wx.ICON_QUESTION | wx.CENTRE)
  650. ret = dlg1.ShowModal()
  651. dlg1.Destroy()
  652. if ret == wx.ID_YES:
  653. try:
  654. os.remove(lockfile)
  655. except IOError, e:
  656. GError(_("Unable to remove '%(lock)s'.\n\n"
  657. "Details: %(reason)s") % { 'lock' : lockfile, 'reason' : e})
  658. else:
  659. return
  660. else:
  661. return
  662. RunCommand("g.gisenv",
  663. set = "GISDBASE=%s" % dbase)
  664. RunCommand("g.gisenv",
  665. set = "LOCATION_NAME=%s" % location)
  666. RunCommand("g.gisenv",
  667. set = "MAPSET=%s" % mapset)
  668. self.Destroy()
  669. sys.exit(0)
  670. def OnExit(self, event):
  671. """'Exit' button clicked"""
  672. self.Destroy()
  673. sys.exit(2)
  674. def OnHelp(self, event):
  675. """'Help' button clicked"""
  676. # help text in lib/init/helptext.html
  677. file = os.path.join(self.gisbase, "docs", "html", "helptext.html")
  678. helpFrame = HelpFrame(parent = None, id = wx.ID_ANY,
  679. title = _("GRASS Quickstart"),
  680. size = (640, 480),
  681. file = file)
  682. helpFrame.Show(True)
  683. event.Skip()
  684. def OnCloseWindow(self, event):
  685. """!Close window event"""
  686. event.Skip()
  687. sys.exit(2)
  688. class GListBox(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
  689. """!Use wx.ListCtrl instead of wx.ListBox, different style for
  690. non-selectable items (e.g. mapsets with denied permission)"""
  691. def __init__(self, parent, id, size,
  692. choices, disabled = []):
  693. wx.ListCtrl.__init__(self, parent, id, size = size,
  694. style = wx.LC_REPORT | wx.LC_NO_HEADER | wx.LC_SINGLE_SEL |
  695. wx.BORDER_SUNKEN)
  696. listmix.ListCtrlAutoWidthMixin.__init__(self)
  697. self.InsertColumn(0, '')
  698. self.selected = wx.NOT_FOUND
  699. self._LoadData(choices, disabled)
  700. def _LoadData(self, choices, disabled = []):
  701. """!Load data into list
  702. @param choices list of item
  703. @param disabled list of indeces of non-selectable items
  704. """
  705. idx = 0
  706. for item in choices:
  707. index = self.InsertStringItem(sys.maxint, item)
  708. self.SetStringItem(index, 0, item)
  709. if idx in disabled:
  710. self.SetItemTextColour(idx, wx.Colour(150, 150, 150))
  711. idx += 1
  712. def Clear(self):
  713. self.DeleteAllItems()
  714. def InsertItems(self, choices, pos, disabled = []):
  715. self._LoadData(choices, disabled)
  716. def SetSelection(self, item, force = False):
  717. if item != wx.NOT_FOUND and \
  718. (platform.system() != 'Windows' or force):
  719. ### Windows -> FIXME
  720. self.SetItemState(item, wx.LIST_STATE_SELECTED, wx.LIST_STATE_SELECTED)
  721. self.selected = item
  722. def GetSelection(self):
  723. return self.selected
  724. class StartUp(wx.App):
  725. """!Start-up application"""
  726. def OnInit(self):
  727. wx.InitAllImageHandlers()
  728. StartUp = GRASSStartup()
  729. StartUp.CenterOnScreen()
  730. self.SetTopWindow(StartUp)
  731. StartUp.Show()
  732. if StartUp.GetRCValue("LOCATION_NAME") == "<UNKNOWN>":
  733. wx.MessageBox(parent = StartUp,
  734. caption = _('Starting GRASS for the first time'),
  735. message = _('GRASS needs a directory in which to store its data. '
  736. 'Create one now if you have not already done so. '
  737. 'A popular choice is "grassdata", located in '
  738. 'your home directory.'),
  739. style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
  740. StartUp.OnBrowse(None)
  741. return 1
  742. if __name__ == "__main__":
  743. if os.getenv("GISBASE") is None:
  744. sys.exit("Failed to start GUI, GRASS GIS is not running.")
  745. import gettext
  746. gettext.install('grasswxpy', os.path.join(os.getenv("GISBASE"), 'locale'), unicode = True)
  747. GRASSStartUp = StartUp(0)
  748. GRASSStartUp.MainLoop()