gis_set.py 36 KB

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