gis_set.py 35 KB

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