gis_set.py 37 KB

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